1. opensymphony
  2. propertyset

Commits

hani  committed 6a30861
  • Participants
  • Parent commits 6417f99
  • Branches master

Comments (0)

Files changed (2)

File src/test/com/opensymphony/module/propertyset/DatabaseHelper.java

View file
 
 import junit.framework.Assert;
 
-import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
 import net.sf.hibernate.cfg.Configuration;
+import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 
 import java.io.*;
 
+import java.net.URL;
+
 import java.sql.Connection;
 import java.sql.Statement;
-import java.net.URL;
 
 import javax.naming.InitialContext;
 
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
-  /**
-   * Create the database by loading a URL pointing at a SQL script.
-   */
-  public static void createDatabase(URL url)
-  {
-    Assert.assertNotNull("Database url is null", url);
-
-    try
-    {
-      String sql = getDatabaseCreationScript(url);
-      createDatabase(sql);
-    }
-    catch(IOException e)
-    {
-      log.error(e.getMessage(), e);
+    /**
+     * Create the database by loading a URL pointing at a SQL script.
+     */
+    public static void createDatabase(URL url) {
+        Assert.assertNotNull("Database url is null", url);
+
+        try {
+            String sql = getDatabaseCreationScript(url);
+            createDatabase(sql);
+        } catch (IOException e) {
+            log.error(e.getMessage(), e);
+        }
     }
-  }
-
-  /**
-   * Create a new database and initialize it with the specified sql script.
-   * @param sql the sql to execute
-   */
-  public static void createDatabase(String sql)
-  {
-    Connection connection;
-    Statement statement = null;
-    String sqlLine = null;
-
-    try
-    {
-      InitialContext context = new InitialContext();
-      DataSource ds = (DataSource)context.lookup("jdbc/CreateDS");
-      connection = ds.getConnection();
-      statement = connection.createStatement();
-
-      String[] sqls = StringUtils.split(sql, ";");
-
-      for(int i = 0; i < sqls.length; i++)
-      {
-        sqlLine = StringUtils.stripToEmpty(sqls[i]);
-        sqlLine = StringUtils.replace(sqlLine, "\r", "");
-        sqlLine = StringUtils.replace(sqlLine, "\n", "");
-
-        //String s = sqls[i];
-        if((sqlLine.length() > 0) && (sqlLine.charAt(0) != '#'))
-        {
-          try
-          {
-            statement.executeQuery(sqlLine);
-          }
-          catch(MSQLException e)
-          {
-            if(sqlLine.toLowerCase().indexOf("drop") == -1)
-            {
-              log.error("Error executing " + sqlLine, e);
+
+    /**
+     * Create a new database and initialize it with the specified sql script.
+     * @param sql the sql to execute
+     */
+    public static void createDatabase(String sql) {
+        Connection connection;
+        Statement statement = null;
+        String sqlLine = null;
+
+        try {
+            InitialContext context = new InitialContext();
+            DataSource ds = (DataSource) context.lookup("jdbc/CreateDS");
+            connection = ds.getConnection();
+            statement = connection.createStatement();
+
+            String[] sqls = StringUtils.split(sql, ";");
+
+            for (int i = 0; i < sqls.length; i++) {
+                sqlLine = StringUtils.stripToEmpty(sqls[i]);
+                sqlLine = StringUtils.replace(sqlLine, "\r", "");
+                sqlLine = StringUtils.replace(sqlLine, "\n", "");
+
+                //String s = sqls[i];
+                if ((sqlLine.length() > 0) && (sqlLine.charAt(0) != '#')) {
+                    try {
+                        statement.executeQuery(sqlLine);
+                    } catch (MSQLException e) {
+                        if (sqlLine.toLowerCase().indexOf("drop") == -1) {
+                            log.error("Error executing " + sqlLine, e);
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("Database creation error.  sqlLine:" + sqlLine, e);
+        } finally {
+            if (statement != null) {
+                try {
+                    statement.close();
+                } catch (Exception ex) {
+                    //not catch
+                }
             }
-          }
         }
-      }
     }
-    catch(Exception e)
-    {
-      log.error("Database creation error.  sqlLine:" + sqlLine, e);
+
+    /**
+     * Use the default Hibernate *.hbm.xml files.  These build the primary keys
+     * based on an identity or sequence, whatever is native to the database.
+     * @throws Exception
+     */
+    public static Configuration createHibernateConfiguration() throws Exception {
+        Configuration configuration = new Configuration();
+
+        //cfg.addClass(HibernateHistoryStep.class);
+        URL propertySet = DatabaseHelper.class.getResource("/com/opensymphony/module/propertyset/hibernate/PropertySetItemImpl.hbm.xml");
+        Assert.assertTrue(propertySet != null);
+        configuration.addURL(propertySet);
+
+        new SchemaExport(configuration).create(false, true);
+
+        return configuration;
     }
-    finally
-    {
-      if(statement != null)
-      {
-        try
-        {
-          statement.close();
-        }
-        catch(Exception ex)
-        {
-          //not catch
+
+    private static String getDatabaseCreationScript(URL url) throws IOException {
+        InputStreamReader reader = new InputStreamReader(url.openStream());
+        StringBuffer sb = new StringBuffer(100);
+        int c = 0;
+
+        while (c > -1) {
+            c = reader.read();
+
+            if (c > -1) {
+                sb.append((char) c);
+            }
         }
-      }
-    }
-  }
-
-  /**
-   * Use the default Hibernate *.hbm.xml files.  These build the primary keys
-   * based on an identity or sequence, whatever is native to the database.
-   * @throws Exception
-   */
-  public static Configuration createHibernateConfiguration() throws Exception
-  {
-    Configuration configuration = new Configuration();
-
-    //cfg.addClass(HibernateHistoryStep.class);
-    URL propertySet = DatabaseHelper.class.getResource("/com/opensymphony/module/propertyset/hibernate/PropertySetItemImpl.hbm.xml");
-    Assert.assertTrue(propertySet != null);
-    configuration.addURL(propertySet);
-
-    new SchemaExport(configuration).create(false, true);
-
-    return configuration;
-  }
-
-  private static String getDatabaseCreationScript(URL url) throws IOException
-  {
-    InputStreamReader reader = new InputStreamReader(url.openStream());
-    StringBuffer sb = new StringBuffer(100);
-    int c = 0;
-
-    while(c > -1)
-    {
-      c = reader.read();
-
-      if(c > -1)
-      {
-        sb.append((char)c);
-      }
-    }
 
-    return sb.toString();
-  }
+        return sb.toString();
+    }
 }

File src/test/com/opensymphony/module/propertyset/hibernate/HibernatePropertySetTest.java

View file
 
 import com.opensymphony.module.propertyset.BasePropertySetTest;
 import com.opensymphony.module.propertyset.DatabaseHelper;
+
 import net.sf.hibernate.cfg.Configuration;
 
 
     public void setUp() throws Exception {
         //DatabaseHelper.exportSchemaForJDBC();
         DatabaseHelper.createDatabase("");
+
         Configuration config = DatabaseHelper.createHibernateConfiguration();
         DefaultHibernateConfigurationProvider configurationProvider = new DefaultHibernateConfigurationProvider();
         configurationProvider.setConfiguration(config);