From 04ef2e4533e909122a560a5cb6499fa62bac82ec Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Fri, 13 Feb 2015 09:33:41 +0000 Subject: [PATCH] SLC Unit git-svn-id: https://svn.argeo.org/slc/trunk@7839 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- org.argeo.slc.unit/pom.xml | 18 +- .../unit/AbstractOsgiRuntimeTestCase.java | 2 +- .../slc/unit/AbstractSpringTestCase.java | 0 .../src}/org/argeo/slc/unit/UnitUtils.java | 0 .../AbstractExecutionFlowTestcase.java | 0 .../ExecutionFlowDescriptorTestUtils.java | 0 .../src}/org/argeo/slc/unit/package.html | 0 pom.xml | 1 + .../slc/hibernate/unit/DbModelHibernate.java | 69 ------- .../hibernate/unit/IndependentDbTestCase.java | 174 ------------------ .../java/org/argeo/slc/unit/UnitXmlUtils.java | 54 ------ .../argeo/slc/unit/db/DbUnitDeployment.java | 95 ---------- .../slc/unit/db/DbUnitDeploymentData.java | 45 ----- 13 files changed, 19 insertions(+), 439 deletions(-) rename {runtime/org.argeo.slc.unit/src/main/java => org.argeo.slc.unit/src}/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java (99%) rename {runtime/org.argeo.slc.unit/src/main/java => org.argeo.slc.unit/src}/org/argeo/slc/unit/AbstractSpringTestCase.java (100%) rename {runtime/org.argeo.slc.unit/src/main/java => org.argeo.slc.unit/src}/org/argeo/slc/unit/UnitUtils.java (100%) rename {runtime/org.argeo.slc.unit/src/main/java => org.argeo.slc.unit/src}/org/argeo/slc/unit/execution/AbstractExecutionFlowTestcase.java (100%) rename {runtime/org.argeo.slc.unit/src/main/java => org.argeo.slc.unit/src}/org/argeo/slc/unit/execution/ExecutionFlowDescriptorTestUtils.java (100%) rename {runtime/org.argeo.slc.unit/src/main/java => org.argeo.slc.unit/src}/org/argeo/slc/unit/package.html (100%) delete mode 100644 runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/DbModelHibernate.java delete mode 100644 runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java delete mode 100644 runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/UnitXmlUtils.java delete mode 100644 runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeployment.java delete mode 100644 runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeploymentData.java diff --git a/org.argeo.slc.unit/pom.xml b/org.argeo.slc.unit/pom.xml index 580d58cd1..8c1ceca6a 100644 --- a/org.argeo.slc.unit/pom.xml +++ b/org.argeo.slc.unit/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 org.argeo.slc @@ -8,4 +9,19 @@ org.argeo.slc.unit SLC Unit Testing Support + + + + org.argeo.commons + org.argeo.osgi.boot + ${version.argeo-commons} + + + + + org.argeo.slc + org.argeo.slc.core + 2.1.1-SNAPSHOT + + \ No newline at end of file diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java b/org.argeo.slc.unit/src/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java similarity index 99% rename from runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java rename to org.argeo.slc.unit/src/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java index d17a90ee4..b5bd8aa62 100644 --- a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java +++ b/org.argeo.slc.unit/src/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java @@ -32,7 +32,7 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.InvalidSyntaxException; import org.osgi.framework.ServiceReference; import org.springframework.context.ApplicationContext; -import org.springframework.osgi.util.OsgiStringUtils; +import org.eclipse.gemini.blueprint.util.OsgiStringUtils; @SuppressWarnings("restriction") public abstract class AbstractOsgiRuntimeTestCase extends TestCase { diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java b/org.argeo.slc.unit/src/org/argeo/slc/unit/AbstractSpringTestCase.java similarity index 100% rename from runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/AbstractSpringTestCase.java rename to org.argeo.slc.unit/src/org/argeo/slc/unit/AbstractSpringTestCase.java diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/UnitUtils.java b/org.argeo.slc.unit/src/org/argeo/slc/unit/UnitUtils.java similarity index 100% rename from runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/UnitUtils.java rename to org.argeo.slc.unit/src/org/argeo/slc/unit/UnitUtils.java diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/execution/AbstractExecutionFlowTestcase.java b/org.argeo.slc.unit/src/org/argeo/slc/unit/execution/AbstractExecutionFlowTestcase.java similarity index 100% rename from runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/execution/AbstractExecutionFlowTestcase.java rename to org.argeo.slc.unit/src/org/argeo/slc/unit/execution/AbstractExecutionFlowTestcase.java diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/execution/ExecutionFlowDescriptorTestUtils.java b/org.argeo.slc.unit/src/org/argeo/slc/unit/execution/ExecutionFlowDescriptorTestUtils.java similarity index 100% rename from runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/execution/ExecutionFlowDescriptorTestUtils.java rename to org.argeo.slc.unit/src/org/argeo/slc/unit/execution/ExecutionFlowDescriptorTestUtils.java diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/package.html b/org.argeo.slc.unit/src/org/argeo/slc/unit/package.html similarity index 100% rename from runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/package.html rename to org.argeo.slc.unit/src/org/argeo/slc/unit/package.html diff --git a/pom.xml b/pom.xml index e474716d1..91d640de5 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ org.argeo.slc.api org.argeo.slc.core + org.argeo.slc.unit diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/DbModelHibernate.java b/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/DbModelHibernate.java deleted file mode 100644 index fc572a170..000000000 --- a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/DbModelHibernate.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.hibernate.unit; - -import java.sql.Connection; -import java.util.List; -import java.util.Properties; - -import org.argeo.slc.support.deploy.db.DbModel; -import org.hibernate.cfg.Configuration; -import org.hibernate.cfg.Environment; -import org.hibernate.tool.hbm2ddl.SchemaExport; - -/** - * Creates a relational data model from Hibernate mapping files. The benefit of - * this class is to be able to use Hibernate to have test data which are - * independent from the type of database used. - */ -public class DbModelHibernate implements DbModel { - private String dialect; - private List mappings; - - /** Sets the Hibernate dialect to use. */ - public void setDialect(String dialect) { - this.dialect = dialect; - } - - /** Sets the list of mappings to consider. */ - public void setMappings(List mappings) { - this.mappings = mappings; - } - - /** - * Creates an Hibernate schema export tool, in order to create the - * underlying datamodel. - */ - protected SchemaExport createSchemaExport(Connection connection) { - Configuration configuration = new Configuration(); - Properties properties = new Properties(); - properties.setProperty(Environment.DIALECT, dialect); - properties.setProperty(Environment.HBM2DDL_AUTO, "create"); - configuration.setProperties(properties); - - for (String mapping : mappings) { - configuration.addResource(mapping.trim()); - } - - return new SchemaExport(configuration, connection); - } - - public void createSchema(Connection connection) { - SchemaExport schemaExport = createSchemaExport(connection); - schemaExport.create(true, true); - } - -} diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java b/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java deleted file mode 100644 index c1bdb1ffd..000000000 --- a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/hibernate/unit/IndependentDbTestCase.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.hibernate.unit; - -import java.io.InputStream; -import java.sql.SQLException; - -import javax.sql.DataSource; - -import org.apache.commons.io.IOUtils; -import org.argeo.slc.SlcException; -import org.argeo.slc.unit.AbstractSpringTestCase; -import org.dbunit.DataSourceDatabaseTester; -import org.dbunit.DatabaseUnitException; -import org.dbunit.IDatabaseTester; -import org.dbunit.database.IDatabaseConnection; -import org.dbunit.dataset.IDataSet; -import org.dbunit.dataset.ReplacementDataSet; -import org.dbunit.dataset.xml.FlatXmlDataSet; -import org.dbunit.operation.DatabaseOperation; -import org.hibernate.tool.hbm2ddl.SchemaExport; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; - -/** - * Helper to make db vendor independent tests using DbUnit data sets. Based on - * {@link DbModelHibernate}. - */ -public abstract class IndependentDbTestCase extends AbstractSpringTestCase { - private IDatabaseTester databaseTester; - - /** Creates the DDL of the data model and loads the data. */ - @Override - protected void setUp() throws Exception { - super.setUp(); - - databaseTester = new DataSourceDatabaseTester(getDataSource()); - databaseTester.setSetUpOperation(new DatabaseOperation() { - - @Override - public void execute(IDatabaseConnection connection, IDataSet dataSet) - throws DatabaseUnitException, SQLException { - DbModelHibernate dbModel = getDbModel(); - SchemaExport schemaExport = dbModel - .createSchemaExport(connection.getConnection()); - schemaExport.create(true, true); - - DatabaseOperation.INSERT.execute(connection, dataSet); - } - - }); - databaseTester.setDataSet(createDataSet()); - databaseTester.onSetup(); - } - - @Override - protected void tearDown() throws Exception { - if (databaseTester != null) { - databaseTester.onTearDown(); - } - super.tearDown(); - } - - /** - * The data source to use. The default implementation returns a bean named - * {@link #getDataSourceBeanName} - */ - protected DataSource getDataSource() { - return getBean(DataSource.class); - } - - /** - * Creates the data set to use. The default implementation creates a - * FlatXmlDataSet load from the resource defined in - * {@link #getDataSetResource()} - */ - protected IDataSet createDataSet() { - InputStream in = null; - try { - in = getDataSetResource().getInputStream(); - String[] replaceStrings = getReplacementStrings(); - IDataSet dataSet; - if (replaceStrings.length == 0) { - dataSet = new FlatXmlDataSet(in); - } else { - dataSet = new ReplacementDataSet(new FlatXmlDataSet(in)); - for (String str : replaceStrings) { - replace((ReplacementDataSet) dataSet, str); - } - } - return dataSet; - } catch (Exception e) { - throw new SlcException("Cannot create data set", e); - } finally { - IOUtils.closeQuietly(in); - } - } - - /** - * To be overridden. Return an empty array by default. - * - * @return the array of strings to replace in the dataset - */ - protected String[] getReplacementStrings() { - return new String[0]; - } - - /** - * Set the object replacing the given string. To be overridden. Does nothing - * by default. - */ - protected void replace(ReplacementDataSet dataSet, String str) - throws Exception { - - } - - /** - * Replace the given string by the content of the resource with the same - * name in the same package, as a byte array. - */ - protected void replaceByRessource(ReplacementDataSet dataSet, String str) - throws Exception { - Resource zipResource = new ClassPathResource(inPackage(str)); - - dataSet.addReplacementObject(str, IOUtils.toByteArray(zipResource - .getInputStream())); - } - - /** - * The resource of the data set to load. The default implementation loads a - * ClassPathResource located at - * {@link #getDataSetResourceLocation()}. - */ - protected Resource getDataSetResource() { - return new ClassPathResource(getDataSetResourceLocation()); - } - - /** - * The location of the data set to load. The default implementation loads - * dataSet.xml found in the same package as the test. - */ - protected String getDataSetResourceLocation() { - return inPackage("dataSet.xml"); - } - - /** - * The DB model to us to create the DDL of the testes database. The default - * implementation loads a bean named after {@link #getDbModelBeanName()}. - */ - protected DbModelHibernate getDbModel() { - return (DbModelHibernate) getContext().getBean(getDbModelBeanName()); - } - - /** - * The name of the bean to load. The default implementation returns - * dbModel. - */ - protected String getDbModelBeanName() { - return "dbModel"; - } -} diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/UnitXmlUtils.java b/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/UnitXmlUtils.java deleted file mode 100644 index 3371405bc..000000000 --- a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/UnitXmlUtils.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.unit; - -import java.io.IOException; - -import javax.xml.transform.Source; - -import junit.framework.TestCase; - -import org.springframework.xml.validation.XmlValidator; -import org.springframework.xml.xsd.XsdSchema; -import org.xml.sax.SAXParseException; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -public abstract class UnitXmlUtils { - private final static Log log = LogFactory.getLog(UnitXmlUtils.class); - - public static void assertXsdSchemaValidation(XsdSchema schema, Source source) - throws IOException { - XmlValidator validator = schema.createValidator(); - assertXmlValidation(validator, source); - } - - public static void assertXmlValidation(XmlValidator validator, Source source) - throws IOException { - SAXParseException[] exceptions = validator.validate(source); - if (exceptions.length != 0) { - for (SAXParseException ex : exceptions) { - log.error(ex.getMessage()); - } - TestCase.fail("Could not validate"); - } - } - - private UnitXmlUtils() { - - } -} diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeployment.java b/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeployment.java deleted file mode 100644 index 25ecb0648..000000000 --- a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeployment.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.unit.db; - -import java.sql.SQLException; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.argeo.slc.SlcException; -import org.argeo.slc.UnsupportedException; -import org.argeo.slc.build.Distribution; -import org.argeo.slc.deploy.DeployedSystem; -import org.argeo.slc.deploy.Deployment; -import org.argeo.slc.deploy.DeploymentData; -import org.argeo.slc.deploy.TargetData; -import org.argeo.slc.support.deploy.db.DbModel; -import org.argeo.slc.support.deploy.db.JdbcAware; -import org.dbunit.DataSourceDatabaseTester; -import org.dbunit.DatabaseUnitException; -import org.dbunit.IDatabaseTester; -import org.dbunit.database.IDatabaseConnection; -import org.dbunit.dataset.IDataSet; -import org.dbunit.operation.DatabaseOperation; - -public class DbUnitDeployment implements Deployment { - private static Log log = LogFactory.getLog(DbUnitDeployment.class); - - private JdbcAware jdbcAware; - private DbUnitDeploymentData deploymentData; - private DbModel dbModel; - - public void run() { - try { - IDatabaseTester databaseTester = new DataSourceDatabaseTester( - jdbcAware.getDataSource()); - databaseTester.setSetUpOperation(new DatabaseOperation() { - - @Override - public void execute(IDatabaseConnection connection, - IDataSet dataSet) throws DatabaseUnitException, - SQLException { - if (dbModel != null) { - dbModel.createSchema(connection.getConnection()); - DatabaseOperation.INSERT.execute(connection, dataSet); - } else { - DatabaseOperation.UPDATE.execute(connection, dataSet); - } - } - - }); - databaseTester.setDataSet(deploymentData.createDataSet()); - databaseTester.onSetup(); - databaseTester.onTearDown(); - - log.info("Database deployed."); - } catch (Exception e) { - throw new SlcException("Could not initialize the database", e); - } - } - - public DeployedSystem getDeployedSystem() { - throw new UnsupportedOperationException(); - } - - public void setDbModel(DbModel dbModel) { - this.dbModel = dbModel; - } - - public void setDeploymentData(DeploymentData deploymentData) { - this.deploymentData = (DbUnitDeploymentData) deploymentData; - } - - public void setTargetData(TargetData targetData) { - this.jdbcAware = (JdbcAware) targetData; - - } - - public void setDistribution(Distribution distribution) { - throw new UnsupportedException("Method not supported"); - } - -} diff --git a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeploymentData.java b/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeploymentData.java deleted file mode 100644 index 46835c6f6..000000000 --- a/runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/unit/db/DbUnitDeploymentData.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.argeo.slc.unit.db; - -import java.io.InputStream; - -import org.argeo.slc.SlcException; -import org.argeo.slc.deploy.DeploymentData; -import org.dbunit.dataset.IDataSet; -import org.dbunit.dataset.xml.FlatXmlDataSet; -import org.springframework.core.io.Resource; - -public class DbUnitDeploymentData implements DeploymentData { - private Resource dataSetLocation; - - public IDataSet createDataSet() { - try { - InputStream in = dataSetLocation.getInputStream(); - IDataSet dataSet = new FlatXmlDataSet(in); - in.close(); - return dataSet; - } catch (Exception e) { - throw new SlcException("Cannot create data set", e); - } - - } - - public void setDataSetLocation(Resource dataSetLocation) { - this.dataSetLocation = dataSetLocation; - } - -} -- 2.39.2