Move unit base
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 31 May 2008 20:14:34 +0000 (20:14 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 31 May 2008 20:14:34 +0000 (20:14 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1171 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/DbModel.java [new file with mode: 0644]
org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java [new file with mode: 0644]
org.argeo.slc.hibernate/src/test/java/org/argeo/slc/hibernate/unit/DbModel.java [deleted file]
org.argeo.slc.hibernate/src/test/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java [deleted file]

diff --git a/org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/DbModel.java b/org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/DbModel.java
new file mode 100644 (file)
index 0000000..03146f0
--- /dev/null
@@ -0,0 +1,47 @@
+package org.argeo.slc.hibernate.unit;\r
+\r
+import java.sql.Connection;\r
+import java.util.List;\r
+import java.util.Properties;\r
+\r
+import org.hibernate.cfg.Configuration;\r
+import org.hibernate.cfg.Environment;\r
+import org.hibernate.tool.hbm2ddl.SchemaExport;\r
+\r
+/**\r
+ * Creates a relational data model from Hibernate mapping files. The benefit of\r
+ * this class is to be able to use Hibernate to have test data which are\r
+ * independent from the type of database used.\r
+ */\r
+public class DbModel {\r
+       private String dialect;\r
+       private List<String> mappings;\r
+\r
+       /** Sets the Hibernate dialect to use. */\r
+       public void setDialect(String dialect) {\r
+               this.dialect = dialect;\r
+       }\r
+\r
+       /** Sets the list of mappings to consider. */\r
+       public void setMappings(List<String> mappings) {\r
+               this.mappings = mappings;\r
+       }\r
+\r
+       /**\r
+        * Creates an Hibernate schema export tool, in order to create the\r
+        * underlying datamodel.\r
+        */\r
+       public SchemaExport createSchemaExport(Connection connection) {\r
+               Configuration configuration = new Configuration();\r
+               Properties properties = new Properties();\r
+               properties.setProperty(Environment.DIALECT, dialect);\r
+               properties.setProperty(Environment.HBM2DDL_AUTO, "create");\r
+               configuration.setProperties(properties);\r
+\r
+               for (String mapping : mappings) {\r
+                       configuration.addResource(mapping.trim());\r
+               }\r
+\r
+               return new SchemaExport(configuration, connection);\r
+       }\r
+}\r
diff --git a/org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java b/org.argeo.slc.hibernate/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java
new file mode 100644 (file)
index 0000000..7fbfd86
--- /dev/null
@@ -0,0 +1,167 @@
+package org.argeo.slc.hibernate.unit;\r
+\r
+import java.io.InputStream;\r
+import java.sql.SQLException;\r
+\r
+import javax.sql.DataSource;\r
+\r
+import org.apache.commons.io.IOUtils;\r
+import org.argeo.slc.core.SlcException;\r
+import org.argeo.slc.unit.AbstractSpringTestCase;\r
+import org.dbunit.DataSourceDatabaseTester;\r
+import org.dbunit.DatabaseUnitException;\r
+import org.dbunit.IDatabaseTester;\r
+import org.dbunit.database.IDatabaseConnection;\r
+import org.dbunit.dataset.IDataSet;\r
+import org.dbunit.dataset.ReplacementDataSet;\r
+import org.dbunit.dataset.xml.FlatXmlDataSet;\r
+import org.dbunit.operation.DatabaseOperation;\r
+import org.hibernate.tool.hbm2ddl.SchemaExport;\r
+import org.springframework.core.io.ClassPathResource;\r
+import org.springframework.core.io.Resource;\r
+\r
+/**\r
+ * Helper to make db vendor independent tests using DbUnit data sets. Based on\r
+ * {@link DbModel}.\r
+ */\r
+public abstract class IndependentDbTestCase extends AbstractSpringTestCase {\r
+       private IDatabaseTester databaseTester;\r
+\r
+       /** Creates the DDL of the data model and loads the data. */\r
+       @Override\r
+       protected void setUp() throws Exception {\r
+               super.setUp();\r
+\r
+               databaseTester = new DataSourceDatabaseTester(getDataSource());\r
+               databaseTester.setSetUpOperation(new DatabaseOperation() {\r
+\r
+                       @Override\r
+                       public void execute(IDatabaseConnection connection, IDataSet dataSet)\r
+                                       throws DatabaseUnitException, SQLException {\r
+                               DbModel dbModel = getDbModel();\r
+                               SchemaExport schemaExport = dbModel\r
+                                               .createSchemaExport(connection.getConnection());\r
+                               schemaExport.create(true, true);\r
+\r
+                               DatabaseOperation.INSERT.execute(connection, dataSet);\r
+                       }\r
+\r
+               });\r
+               databaseTester.setDataSet(createDataSet());\r
+               databaseTester.onSetup();\r
+       }\r
+\r
+       @Override\r
+       protected void tearDown() throws Exception {\r
+               if (databaseTester != null) {\r
+                       databaseTester.onTearDown();\r
+               }\r
+               super.tearDown();\r
+       }\r
+\r
+       /**\r
+        * The data source to use. The default implementation returns a bean named\r
+        * {@link #getDataSourceBeanName}\r
+        */\r
+       protected DataSource getDataSource() {\r
+               return (DataSource) getContext().getBean(getDataSourceBeanName());\r
+       }\r
+\r
+       /**\r
+        * The name of the data source bean to use. The default implementation\r
+        * returns <i>dataSource</i>.\r
+        */\r
+       protected String getDataSourceBeanName() {\r
+               return "slcDefault.dataSource.inMemoryTestHSQL";\r
+       }\r
+\r
+       /**\r
+        * Creates the data set to use. The default implementation creates a\r
+        * <code>FlatXmlDataSet</code> load from the resource defined in\r
+        * {@link #getDataSetResource()}\r
+        */\r
+       protected IDataSet createDataSet() {\r
+               InputStream in = null;\r
+               try {\r
+                       in = getDataSetResource().getInputStream();\r
+                       String[] replaceStrings = getReplacementStrings();\r
+                       IDataSet dataSet;\r
+                       if (replaceStrings.length == 0) {\r
+                               dataSet = new FlatXmlDataSet(in);\r
+                       } else {\r
+                               dataSet = new ReplacementDataSet(new FlatXmlDataSet(in));\r
+                               for (String str : replaceStrings) {\r
+                                       replace((ReplacementDataSet) dataSet, str);\r
+                               }\r
+                       }\r
+                       return dataSet;\r
+               } catch (Exception e) {\r
+                       throw new SlcException("Cannot create data set", e);\r
+               } finally {\r
+                       IOUtils.closeQuietly(in);\r
+               }\r
+       }\r
+\r
+       /**\r
+        * To be overridden. Return an empty array by default.\r
+        * \r
+        * @return the array of strings to replace in the dataset\r
+        */\r
+       protected String[] getReplacementStrings() {\r
+               return new String[0];\r
+       }\r
+\r
+       /**\r
+        * Set the object replacing the given string. To be overridden. Does nothing\r
+        * by default.\r
+        */\r
+       protected void replace(ReplacementDataSet dataSet, String str)\r
+                       throws Exception {\r
+\r
+       }\r
+\r
+       /**\r
+        * Replace the given string by the content of the resource with the same\r
+        * name in the same package, as a byte array.\r
+        */\r
+       protected void replaceByRessource(ReplacementDataSet dataSet, String str)\r
+                       throws Exception {\r
+               Resource zipResource = new ClassPathResource(inPackage(str));\r
+\r
+               dataSet.addReplacementObject(str, IOUtils.toByteArray(zipResource\r
+                               .getInputStream()));\r
+       }\r
+\r
+       /**\r
+        * The resource of the data set to load. The default implementation loads a\r
+        * <code>ClassPathResource</code> located at\r
+        * {@link #getDataSetResourceLocation()}.\r
+        */\r
+       protected Resource getDataSetResource() {\r
+               return new ClassPathResource(getDataSetResourceLocation());\r
+       }\r
+\r
+       /**\r
+        * The location of the data set to load. The default implementation loads\r
+        * <i>dataSet.xml</i> found in the same package as the test.\r
+        */\r
+       protected String getDataSetResourceLocation() {\r
+               return inPackage("dataSet.xml");\r
+       }\r
+\r
+       /**\r
+        * The DB model to us to create the DDL of the testes database. The default\r
+        * implementation loads a bean named after {@link #getDbModelBeanName()}.\r
+        */\r
+       protected DbModel getDbModel() {\r
+               return (DbModel) getContext().getBean(getDbModelBeanName());\r
+       }\r
+\r
+       /**\r
+        * The name of the bean to load. The default implementation returns\r
+        * <i>dbModel</i>.\r
+        */\r
+       protected String getDbModelBeanName() {\r
+               return "dbModel";\r
+       }\r
+}\r
diff --git a/org.argeo.slc.hibernate/src/test/java/org/argeo/slc/hibernate/unit/DbModel.java b/org.argeo.slc.hibernate/src/test/java/org/argeo/slc/hibernate/unit/DbModel.java
deleted file mode 100644 (file)
index 03146f0..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-package org.argeo.slc.hibernate.unit;\r
-\r
-import java.sql.Connection;\r
-import java.util.List;\r
-import java.util.Properties;\r
-\r
-import org.hibernate.cfg.Configuration;\r
-import org.hibernate.cfg.Environment;\r
-import org.hibernate.tool.hbm2ddl.SchemaExport;\r
-\r
-/**\r
- * Creates a relational data model from Hibernate mapping files. The benefit of\r
- * this class is to be able to use Hibernate to have test data which are\r
- * independent from the type of database used.\r
- */\r
-public class DbModel {\r
-       private String dialect;\r
-       private List<String> mappings;\r
-\r
-       /** Sets the Hibernate dialect to use. */\r
-       public void setDialect(String dialect) {\r
-               this.dialect = dialect;\r
-       }\r
-\r
-       /** Sets the list of mappings to consider. */\r
-       public void setMappings(List<String> mappings) {\r
-               this.mappings = mappings;\r
-       }\r
-\r
-       /**\r
-        * Creates an Hibernate schema export tool, in order to create the\r
-        * underlying datamodel.\r
-        */\r
-       public SchemaExport createSchemaExport(Connection connection) {\r
-               Configuration configuration = new Configuration();\r
-               Properties properties = new Properties();\r
-               properties.setProperty(Environment.DIALECT, dialect);\r
-               properties.setProperty(Environment.HBM2DDL_AUTO, "create");\r
-               configuration.setProperties(properties);\r
-\r
-               for (String mapping : mappings) {\r
-                       configuration.addResource(mapping.trim());\r
-               }\r
-\r
-               return new SchemaExport(configuration, connection);\r
-       }\r
-}\r
diff --git a/org.argeo.slc.hibernate/src/test/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java b/org.argeo.slc.hibernate/src/test/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java
deleted file mode 100644 (file)
index 7fbfd86..0000000
+++ /dev/null
@@ -1,167 +0,0 @@
-package org.argeo.slc.hibernate.unit;\r
-\r
-import java.io.InputStream;\r
-import java.sql.SQLException;\r
-\r
-import javax.sql.DataSource;\r
-\r
-import org.apache.commons.io.IOUtils;\r
-import org.argeo.slc.core.SlcException;\r
-import org.argeo.slc.unit.AbstractSpringTestCase;\r
-import org.dbunit.DataSourceDatabaseTester;\r
-import org.dbunit.DatabaseUnitException;\r
-import org.dbunit.IDatabaseTester;\r
-import org.dbunit.database.IDatabaseConnection;\r
-import org.dbunit.dataset.IDataSet;\r
-import org.dbunit.dataset.ReplacementDataSet;\r
-import org.dbunit.dataset.xml.FlatXmlDataSet;\r
-import org.dbunit.operation.DatabaseOperation;\r
-import org.hibernate.tool.hbm2ddl.SchemaExport;\r
-import org.springframework.core.io.ClassPathResource;\r
-import org.springframework.core.io.Resource;\r
-\r
-/**\r
- * Helper to make db vendor independent tests using DbUnit data sets. Based on\r
- * {@link DbModel}.\r
- */\r
-public abstract class IndependentDbTestCase extends AbstractSpringTestCase {\r
-       private IDatabaseTester databaseTester;\r
-\r
-       /** Creates the DDL of the data model and loads the data. */\r
-       @Override\r
-       protected void setUp() throws Exception {\r
-               super.setUp();\r
-\r
-               databaseTester = new DataSourceDatabaseTester(getDataSource());\r
-               databaseTester.setSetUpOperation(new DatabaseOperation() {\r
-\r
-                       @Override\r
-                       public void execute(IDatabaseConnection connection, IDataSet dataSet)\r
-                                       throws DatabaseUnitException, SQLException {\r
-                               DbModel dbModel = getDbModel();\r
-                               SchemaExport schemaExport = dbModel\r
-                                               .createSchemaExport(connection.getConnection());\r
-                               schemaExport.create(true, true);\r
-\r
-                               DatabaseOperation.INSERT.execute(connection, dataSet);\r
-                       }\r
-\r
-               });\r
-               databaseTester.setDataSet(createDataSet());\r
-               databaseTester.onSetup();\r
-       }\r
-\r
-       @Override\r
-       protected void tearDown() throws Exception {\r
-               if (databaseTester != null) {\r
-                       databaseTester.onTearDown();\r
-               }\r
-               super.tearDown();\r
-       }\r
-\r
-       /**\r
-        * The data source to use. The default implementation returns a bean named\r
-        * {@link #getDataSourceBeanName}\r
-        */\r
-       protected DataSource getDataSource() {\r
-               return (DataSource) getContext().getBean(getDataSourceBeanName());\r
-       }\r
-\r
-       /**\r
-        * The name of the data source bean to use. The default implementation\r
-        * returns <i>dataSource</i>.\r
-        */\r
-       protected String getDataSourceBeanName() {\r
-               return "slcDefault.dataSource.inMemoryTestHSQL";\r
-       }\r
-\r
-       /**\r
-        * Creates the data set to use. The default implementation creates a\r
-        * <code>FlatXmlDataSet</code> load from the resource defined in\r
-        * {@link #getDataSetResource()}\r
-        */\r
-       protected IDataSet createDataSet() {\r
-               InputStream in = null;\r
-               try {\r
-                       in = getDataSetResource().getInputStream();\r
-                       String[] replaceStrings = getReplacementStrings();\r
-                       IDataSet dataSet;\r
-                       if (replaceStrings.length == 0) {\r
-                               dataSet = new FlatXmlDataSet(in);\r
-                       } else {\r
-                               dataSet = new ReplacementDataSet(new FlatXmlDataSet(in));\r
-                               for (String str : replaceStrings) {\r
-                                       replace((ReplacementDataSet) dataSet, str);\r
-                               }\r
-                       }\r
-                       return dataSet;\r
-               } catch (Exception e) {\r
-                       throw new SlcException("Cannot create data set", e);\r
-               } finally {\r
-                       IOUtils.closeQuietly(in);\r
-               }\r
-       }\r
-\r
-       /**\r
-        * To be overridden. Return an empty array by default.\r
-        * \r
-        * @return the array of strings to replace in the dataset\r
-        */\r
-       protected String[] getReplacementStrings() {\r
-               return new String[0];\r
-       }\r
-\r
-       /**\r
-        * Set the object replacing the given string. To be overridden. Does nothing\r
-        * by default.\r
-        */\r
-       protected void replace(ReplacementDataSet dataSet, String str)\r
-                       throws Exception {\r
-\r
-       }\r
-\r
-       /**\r
-        * Replace the given string by the content of the resource with the same\r
-        * name in the same package, as a byte array.\r
-        */\r
-       protected void replaceByRessource(ReplacementDataSet dataSet, String str)\r
-                       throws Exception {\r
-               Resource zipResource = new ClassPathResource(inPackage(str));\r
-\r
-               dataSet.addReplacementObject(str, IOUtils.toByteArray(zipResource\r
-                               .getInputStream()));\r
-       }\r
-\r
-       /**\r
-        * The resource of the data set to load. The default implementation loads a\r
-        * <code>ClassPathResource</code> located at\r
-        * {@link #getDataSetResourceLocation()}.\r
-        */\r
-       protected Resource getDataSetResource() {\r
-               return new ClassPathResource(getDataSetResourceLocation());\r
-       }\r
-\r
-       /**\r
-        * The location of the data set to load. The default implementation loads\r
-        * <i>dataSet.xml</i> found in the same package as the test.\r
-        */\r
-       protected String getDataSetResourceLocation() {\r
-               return inPackage("dataSet.xml");\r
-       }\r
-\r
-       /**\r
-        * The DB model to us to create the DDL of the testes database. The default\r
-        * implementation loads a bean named after {@link #getDbModelBeanName()}.\r
-        */\r
-       protected DbModel getDbModel() {\r
-               return (DbModel) getContext().getBean(getDbModelBeanName());\r
-       }\r
-\r
-       /**\r
-        * The name of the bean to load. The default implementation returns\r
-        * <i>dbModel</i>.\r
-        */\r
-       protected String getDbModelBeanName() {\r
-               return "dbModel";\r
-       }\r
-}\r