ItEr28S13MontaxeProbasUnidadeItEr19S05: Integrated DBUnit with Spring TestContext.
This commit is contained in:
parent
ca6b1ea55e
commit
b80ae13ab8
9 changed files with 273 additions and 1 deletions
|
|
@ -72,5 +72,17 @@
|
|||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time-hibernate</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dbunit</groupId>
|
||||
<artifactId>dbunit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package org.navalplanner.business.common.test.dbunit;
|
||||
|
||||
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
|
||||
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
|
||||
import static org.navalplanner.business.test.BusinessGlobalNames.DBUNIT_CONFIG_TEST_FILE;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
|
||||
DBUnitTestExecutionListener.class })
|
||||
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
|
||||
BUSINESS_SPRING_CONFIG_TEST_FILE })
|
||||
@DBUnitConfiguration(locations = DBUNIT_CONFIG_TEST_FILE)
|
||||
@Transactional
|
||||
public abstract class AbstractDBUnitTest {
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package org.navalplanner.business.common.test.dbunit;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The <code>DBUnitConfiguration</code> annotation defines class-level metadata used
|
||||
* to provide DBUnit configuration data to an instance of {@link DBUnitTestExecutionListener}.
|
||||
* </p>
|
||||
*
|
||||
* @author Bob McCune
|
||||
* @version 1.0, 4/1/2008
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Inherited
|
||||
@Documented
|
||||
public @interface DBUnitConfiguration {
|
||||
|
||||
/**
|
||||
* The DBUnit data set configuration file locations.
|
||||
*
|
||||
* @return the location of the DBUnit data set configuration files
|
||||
*/
|
||||
String[] locations() default {};
|
||||
|
||||
/**
|
||||
* The DBUnit DataSet type of the configuration files. If not specified the default will be assumed to be
|
||||
* {@link org.dbunit.dataset.xml.XmlDataSet}.
|
||||
*
|
||||
* @return the data set type of the configuration files
|
||||
*/
|
||||
Class type() default org.dbunit.dataset.xml.XmlDataSet.class;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
package org.navalplanner.business.common.test.dbunit;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.sql.Connection;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.dbunit.database.DatabaseConfig;
|
||||
import org.dbunit.database.DatabaseConnection;
|
||||
import org.dbunit.database.IDatabaseConnection;
|
||||
import org.dbunit.dataset.IDataSet;
|
||||
import org.dbunit.operation.DatabaseOperation;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.datasource.DataSourceUtils;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
||||
|
||||
/**
|
||||
* A Spring @{TestExecutionListener} used to integrate the functionality of
|
||||
* DBUnit.
|
||||
*
|
||||
* @author Bob McCune
|
||||
* @version 1.0
|
||||
*/
|
||||
public class DBUnitTestExecutionListener extends
|
||||
TransactionalTestExecutionListener {
|
||||
|
||||
private static final Log logger = LogFactory
|
||||
.getLog(DBUnitTestExecutionListener.class);
|
||||
|
||||
private static final String DEFAULT_DATASOURCE_NAME = "dataSource";
|
||||
private static final String TABLE_TYPES[] = { "TABLE", "ALIAS" };
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
|
||||
super.beforeTestMethod(testContext);
|
||||
|
||||
DataSource dataSource = getDataSource(testContext);
|
||||
Connection conn = DataSourceUtils.getConnection(dataSource);
|
||||
IDatabaseConnection dbUnitConn = getDBUnitConnection(conn);
|
||||
try {
|
||||
IDataSet dataSets[] = getDataSets(testContext);
|
||||
for (IDataSet dataSet : dataSets) {
|
||||
DatabaseOperation.CLEAN_INSERT.execute(dbUnitConn, dataSet);
|
||||
logger.info("Performed CLEAN_INSERT of IDataSet.");
|
||||
}
|
||||
} finally {
|
||||
DataSourceUtils.releaseConnection(conn, dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
private DataSource getDataSource(TestContext context) throws Exception {
|
||||
DataSource dataSource;
|
||||
Map beans = context.getApplicationContext().getBeansOfType(
|
||||
DataSource.class);
|
||||
if (beans.size() > 1) {
|
||||
dataSource = (DataSource) beans.get(DEFAULT_DATASOURCE_NAME);
|
||||
if (dataSource == null) {
|
||||
throw new NoSuchBeanDefinitionException(
|
||||
"Unable to locate default data source.");
|
||||
}
|
||||
} else {
|
||||
dataSource = (DataSource) beans.values().iterator().next();
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private IDatabaseConnection getDBUnitConnection(Connection c)
|
||||
throws Exception {
|
||||
IDatabaseConnection conn = new DatabaseConnection(c);
|
||||
DatabaseConfig config = conn.getConfig();
|
||||
config.setFeature("http://www.dbunit.org/features/qualifiedTableNames",
|
||||
true);
|
||||
config.setProperty("http://www.dbunit.org/properties/tableType",
|
||||
TABLE_TYPES);
|
||||
return conn;
|
||||
}
|
||||
|
||||
private IDataSet[] getDataSets(TestContext context) throws Exception {
|
||||
String dataFiles[] = getDataLocations(context);
|
||||
IDataSet dataSets[] = new IDataSet[dataFiles.length];
|
||||
for (int i = 0; i < dataFiles.length; i++) {
|
||||
Resource resource = new ClassPathResource(dataFiles[i]);
|
||||
Class clazz = getDataSetType(context);
|
||||
Constructor con = clazz.getConstructor(InputStream.class);
|
||||
dataSets[i] = (IDataSet) con.newInstance(resource.getInputStream());
|
||||
}
|
||||
return dataSets;
|
||||
}
|
||||
|
||||
protected Class getDataSetType(TestContext context) {
|
||||
Class<?> testClass = context.getTestClass();
|
||||
DBUnitConfiguration config = testClass
|
||||
.getAnnotation(DBUnitConfiguration.class);
|
||||
return config.type();
|
||||
}
|
||||
|
||||
private String[] getDataLocations(TestContext context) {
|
||||
Class<?> testClass = context.getTestClass();
|
||||
DBUnitConfiguration config = testClass
|
||||
.getAnnotation(DBUnitConfiguration.class);
|
||||
if (config == null) {
|
||||
throw new IllegalStateException("Test class '" + testClass
|
||||
+ " has is missing @DBUnitConfiguration annotation.");
|
||||
}
|
||||
if (config.locations().length == 0) {
|
||||
throw new IllegalStateException(
|
||||
"@DBUnitConfiguration annotation doesn't specify any DBUnit configuration locations.");
|
||||
}
|
||||
return config.locations();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package org.navalplanner.business.common.test.dbunit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.advance.daos.IAdvanceTypeDAO;
|
||||
import org.navalplanner.business.advance.entities.AdvanceType;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class ExampleDBUnitTest extends AbstractDBUnitTest {
|
||||
|
||||
@Autowired
|
||||
private IAdvanceTypeDAO advanceDAO;
|
||||
|
||||
@Test
|
||||
public void percentageInsertedInDB() {
|
||||
AdvanceType advance = advanceDAO.findByName("percentage");
|
||||
assertEquals("percentage",advance.getUnitName());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -31,6 +31,7 @@ public class BusinessGlobalNames {
|
|||
public final static String BUSINESS_SPRING_CONFIG_TEST_FILE =
|
||||
"classpath:/navalplanner-business-spring-config-test.xml";
|
||||
|
||||
public final static String DBUNIT_CONFIG_TEST_FILE =
|
||||
"/dbunit-data.xml";
|
||||
private BusinessGlobalNames () {}
|
||||
|
||||
}
|
||||
|
|
|
|||
37
navalplanner-business/src/test/resources/dbunit-data.xml
Normal file
37
navalplanner-business/src/test/resources/dbunit-data.xml
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<dataset>
|
||||
|
||||
<!-- ==================================================================== -->
|
||||
<!-- This file defines the test data used by the various DAOs in the -->
|
||||
<!-- FormPub project DO NOT ALTER THIS FILE as the unit tests depend on -->
|
||||
<!-- these values to be present in the database. -->
|
||||
<!-- ==================================================================== -->
|
||||
|
||||
<table name="public.advancetype">
|
||||
|
||||
<column>id</column>
|
||||
<column>version</column>
|
||||
<column>unitname</column>
|
||||
<column>defaultmaxvalue</column>
|
||||
<column>updatable</column>
|
||||
<column>unitprecision</column>
|
||||
<column>active</column>
|
||||
<column>percentage</column>
|
||||
|
||||
<row>
|
||||
<value>1</value>
|
||||
<value>1</value>
|
||||
<value>percentage</value>
|
||||
<value>100</value>
|
||||
<value>true</value>
|
||||
<value>0.01</value>
|
||||
<value>true</value>
|
||||
<value>true</value>
|
||||
</row>
|
||||
|
||||
</table>
|
||||
|
||||
</dataset>
|
||||
|
||||
|
||||
10
navalplanner-business/src/test/resources/log4j.properties
Normal file
10
navalplanner-business/src/test/resources/log4j.properties
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# For JBoss: Avoid to setup Log4J outside $JBOSS_HOME/server/default/deploy/log4j.xml!
|
||||
# For all other servers: Comment out the Log4J listener in web.xml to activate Log4J.
|
||||
log4j.rootLogger=INFO, stdout
|
||||
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
|
||||
|
||||
log4j.logger.org.springframework=WARN
|
||||
log4j.logger.org.hibernate=WARN
|
||||
15
pom.xml
15
pom.xml
|
|
@ -361,6 +361,21 @@
|
|||
<artifactId>navalplanner-business</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.dbunit</groupId>
|
||||
<artifactId>dbunit</artifactId>
|
||||
<version>2.4.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>1.2.14</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.5.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue