Refactor infrastructure-tests to use more placeholders for initialization.

This commit is contained in:
dsyer
2010-03-29 10:18:55 +00:00
parent 4c0c964a45
commit 762c11c80b
13 changed files with 219 additions and 146 deletions

View File

@@ -0,0 +1,4 @@
#Mon Mar 29 10:41:12 BST 2010
eclipse.preferences.version=1
org.springframework.ide.eclipse.beans.core.ignoreMissingNamespaceHandler=false
org.springframework.ide.eclipse.beans.core.loadNamespaceHandlerFromClasspath=false

View File

@@ -1,14 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<pluginVersion><![CDATA[2.0.5.v200805010412]]></pluginVersion>
<pluginVersion><![CDATA[2.3.1.201002090815-CI-R3692-B638]]></pluginVersion>
<configSuffixes>
<configSuffix><![CDATA[xml]]></configSuffix>
</configSuffixes>
<enableImports><![CDATA[false]]></enableImports>
<configs>
<config>src/test/resources/org/springframework/batch/jms/jms-context.xml</config>
<config>src/test/resources/data-source.xml</config>
<config>src/test/resources/data-source-context.xml</config>
<config>src/test/resources/org/springframework/batch/item/database/data-source-context.xml</config>
<config>src/test/resources/org/springframework/batch/item/database/JdbcPagingItemReaderCommonTests-context.xml</config>
<config>src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml</config>
</configs>
<configSets>
<configSet>
@@ -16,7 +19,7 @@
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/test/resources/data-source.xml</config>
<config>src/test/resources/data-source-context.xml</config>
</configs>
</configSet>
<configSet>
@@ -24,8 +27,8 @@
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/test/resources/data-source.xml</config>
<config>src/test/resources/org/springframework/batch/jms/jms-context.xml</config>
<config>src/test/resources/data-source-context.xml</config>
</configs>
</configSet>
<configSet>
@@ -33,7 +36,7 @@
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/test/resources/data-source.xml</config>
<config>src/test/resources/data-source-context.xml</config>
</configs>
</configSet>
</configSets>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-batch-infrastructure-tests</artifactId>
<name>Infrastructure Tests</name>
@@ -156,6 +157,10 @@
<artifactId>ibatis-sqlmap</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>

View File

@@ -24,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.junit.runner.RunWith;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.sql.DataSource;
@@ -38,6 +40,16 @@ public class DatasourceTests {
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@BeforeClass
public static void init() {
System.setProperty("batch.business.schema.script", "classpath:/org/springframework/batch/jms/init.sql");
}
@AfterClass
public static void cleanup() {
System.clearProperty("batch.business.schema.script");
}
@Transactional @Test
public void testTemplate() throws Exception {

View File

@@ -16,18 +16,19 @@
package org.springframework.batch.config;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/batch/jms/jms-context.xml")

View File

@@ -18,6 +18,7 @@ package test.jdbc.datasource;
import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import javax.sql.DataSource;
@@ -27,7 +28,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -36,56 +36,60 @@ import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* Wrapper for a {@link DataSource} that can run scripts on start up and shut
* down. Us as a bean definition <br/><br/>
*
* Run this class to initialize a database in a running server process.
* Make sure the server is running first by launching the "hsql-server" from the
* <code>hsql.server</code> project. Then you can right click in Eclipse and
* Run As -&gt; Java Application. Do the same any time you want to wipe the
* database and start again.
*
* @author Dave Syer
*
*/
public class DataSourceInitializer implements InitializingBean, DisposableBean {
private static final Log logger = LogFactory.getLog(DataSourceInitializer.class);
private Resource[] initScripts;
private Resource[] destroyScripts;
private Resource destroyScript;
private DataSource dataSource;
private boolean ignoreFailedDrop = true;
private boolean initialize = false;
private Log logger = LogFactory.getLog(getClass());
private boolean initialized = false;
/**
* Main method as convenient entry point.
*
* @param args
*/
public static void main(String... args) {
new ClassPathXmlApplicationContext(ClassUtils.addResourcePathToPackagePath(DataSourceInitializer.class,
DataSourceInitializer.class.getSimpleName() + "-context.xml"));
public void setInitialize(boolean initialize) {
this.initialize = initialize;
}
public void destroy() throws Exception {
if (!initialized) {
return;
}
try {
if (destroyScript!=null) {
doExecuteScript(destroyScript);
initialized = false;
}
}
catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.warn("Could not execute destroy script [" + destroyScript + "]", e);
}
else {
logger.warn("Could not execute destroy script [" + destroyScript + "]");
}
}
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(dataSource);
initialize();
}
private void initialize() {
if (!initialized) {
logger.info("Initializing with scripts: "+Arrays.asList(initScripts));
if (!initialized && initialize) {
try {
doExecuteScript(destroyScript);
}
catch (Exception e) {
logger.debug("Could not execute destroy script [" + destroyScript + "]", e);
}
if (initScripts != null) {
for (int i = 0; i < initScripts.length; i++) {
Resource initScript = initScripts[i];
logger.info("Executing init script: "+initScript);
doExecuteScript(initScript);
}
}
@@ -114,13 +118,9 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
String script = scripts[i].trim();
if (StringUtils.hasText(script)) {
try {
jdbcTemplate.execute(script);
}
catch (DataAccessException e) {
if (ignoreFailedDrop && script.toLowerCase().startsWith("drop")) {
logger.debug("DROP script failed (ignoring): " + script);
}
else {
jdbcTemplate.execute(scripts[i]);
} catch (DataAccessException e) {
if (!script.toUpperCase().startsWith("DROP")) {
throw e;
}
}
@@ -143,31 +143,20 @@ public class DataSourceInitializer implements InitializingBean, DisposableBean {
return buffer.toString();
}
public Class<DataSource> getObjectType() {
return DataSource.class;
}
public void setInitScripts(Resource[] initScripts) {
this.initScripts = initScripts;
}
public void setDestroyScripts(Resource[] destroyScripts) {
this.destroyScripts = destroyScripts;
public void setDestroyScript(Resource destroyScript) {
this.destroyScript = destroyScript;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setIgnoreFailedDrop(boolean ignoreFailedDrop) {
this.ignoreFailedDrop = ignoreFailedDrop;
}
public void destroy() throws Exception {
if (initialized) {
if (destroyScripts != null) {
for (int i = 0; i < destroyScripts.length; i++) {
Resource destroyScript = destroyScripts[i];
doExecuteScript(destroyScript);
}
}
}
}
}

View File

@@ -0,0 +1,15 @@
# Placeholders batch.*
# for Derby:
batch.jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver
batch.jdbc.url=jdbc:derby:derby-home/test;create=true
batch.jdbc.user=sa
batch.jdbc.password=
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.drop.script=classpath:/org/springframework/batch/core/schema-drop-derby.sql
batch.schema.script=classpath:/org/springframework/batch/core/schema-derby.sql
batch.business.schema.script=classpath:/org/springframework/batch/jms/init.sql
batch.data.source.init=true
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.DerbyMaxValueIncrementer
batch.database.incrementer.parent=columnIncrementerParent
batch.verify.cursor.position=false

View File

@@ -0,0 +1,17 @@
# Placeholders batch.*
# for HSQLDB:
batch.jdbc.driver=org.hsqldb.jdbcDriver
batch.jdbc.url=jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true
# use this one for a separate server process so you can inspect the results
# (or add it to system properties with -D to override at run time).
# batch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples
batch.jdbc.user=sa
batch.jdbc.password=
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.schema.script=classpath:/org/springframework/batch/core/schema-hsqldb.sql
batch.business.schema.script=classpath:/org/springframework/batch/jms/init.sql
batch.data.source.init=true
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer
batch.database.incrementer.parent=columnIncrementerParent
batch.verify.cursor.position=true

View File

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Initialise the database before every test case: -->
<bean id="dataSourceInitializer" class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource"/>
<property name="initialize" value="${batch.data.source.init}"/>
<property name="initScripts">
<list>
<value>${batch.business.schema.script}</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${batch.jdbc.driver}" />
<property name="url" value="${batch.jdbc.url}" />
<property name="username" value="${batch.jdbc.user}" />
<property name="password" value="${batch.jdbc.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Set up or detect a System property called "ENVIRONMENT" used to construct a properties file on the classpath. The default is "hsql". -->
<bean id="environment"
class="org.springframework.batch.support.SystemPropertyInitializer">
<property name="defaultValue" value="hsql"/>
<property name="keyName" value="ENVIRONMENT"/>
</bean>
<!-- Use this to set additional properties on beans at run time -->
<bean id="overrideProperties" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"
depends-on="environment">
<property name="location" value="classpath:batch-${ENVIRONMENT}.properties" />
<!-- Allow system properties (-D) to override those from file -->
<property name="localOverride" value="true" />
<property name="properties">
<bean class="java.lang.System" factory-method="getProperties" />
</property>
<property name="ignoreInvalidKeys" value="true" />
<property name="order" value="2" />
</bean>
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="environment">
<property name="location" value="classpath:batch-${ENVIRONMENT}.properties" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="order" value="1" />
</bean>
<bean id="sequenceIncrementerParent" class="${batch.database.incrementer.class}" abstract="true">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="columnIncrementerParent" class="${batch.database.incrementer.class}" abstract="true" parent="sequenceIncrementerParent">
<property name="columnName" value="ID" />
</bean>
<bean id="incrementerParent" parent="${batch.database.incrementer.parent}">
<property name="incrementerName" value="DUMMY"/>
</bean>
</beans>

View File

@@ -1,39 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSourceInitializer" class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource" />
<property name="initScripts" value="org/springframework/batch/jms/init.sql" />
<property name="destroyScripts" value="org/springframework/batch/jms/destroy.sql" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:testdb" />
</bean>
<bean id="derby" class="test.jdbc.datasource.DerbyDataSourceFactoryBean" lazy-init="true" destroy-method="destroy"
autowire-candidate="false">
<property name="dataDirectory" value="derby-home" />
</bean>
<bean id="brokerService" class="org.apache.activemq.broker.BrokerService" init-method="start"
destroy-method="stop">
<property name="brokerName" value="broker" />
<property name="useJmx" value="false"/>
<property name="transportConnectorURIs">
<list>
<value>vm://localhost</value>
</list>
</property>
<property name="persistenceAdapter">
<bean class="org.apache.activemq.store.memory.MemoryPersistenceAdapter"/>
<!-- bean class="org.apache.activemq.store.jdbc.JDBCPersistenceAdapter">
<property name="dataSource" ref="dataSource"/>
<property name="createTablesOnStartup" value="true" />
</bean-->
</property>
</bean>
</beans>

View File

@@ -4,23 +4,17 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean class="test.jdbc.datasource.DataSourceInitializer">
<import resource="classpath:/data-source-context.xml"/>
<!-- Initialise the database before every test case: -->
<bean id="dataSourceInitializer" class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource"/>
<property name="initScripts" value="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:testdb" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="xincrementerParent" class="org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer"
abstract="true">
<property name="dataSource" ref="dataSource" />
<property name="columnName" value="ID" />
<property name="initialize" value="${batch.data.source.init}"/>
<property name="initScripts">
<list>
<value>classpath:org/springframework/batch/item/database/init-foo-schema-hsqldb.sql</value>
</list>
</property>
</bean>
</beans>

View File

@@ -4,13 +4,17 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean class="test.jdbc.datasource.DataSourceInitializer">
<import resource="classpath:/data-source-context.xml"/>
<!-- Initialise the database before every test case: -->
<bean id="dataSourceInitializer" class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource"/>
<property name="initScripts" value="org/springframework/batch/item/database/init-foo-schema-hsqldb.sql" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:testdb" />
<property name="initialize" value="${batch.data.source.init}"/>
<property name="initScripts">
<list>
<value>classpath:org/springframework/batch/item/database/init-foo-schema-hsqldb.sql</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
@@ -27,10 +31,4 @@
</property>
</bean>
<bean id="incrementerParent" class="org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer"
abstract="true">
<property name="dataSource" ref="dataSource" />
<property name="columnName" value="ID" />
</bean>
</beans>

View File

@@ -7,18 +7,7 @@
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<import resource="classpath:/data-source.xml" />
<!-- Transaction manager for a datasource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Transaction manager for jms -->
<bean id="jmsTransactionManager" autowire-candidate="false"
class="org.springframework.jms.connection.JmsTransactionManager">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<import resource="classpath:/data-source-context.xml" />
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
@@ -100,4 +89,22 @@
</property>
</bean>
<bean id="brokerService" class="org.apache.activemq.broker.BrokerService" init-method="start"
destroy-method="stop">
<property name="brokerName" value="broker" />
<property name="useJmx" value="false"/>
<property name="transportConnectorURIs">
<list>
<value>vm://localhost</value>
</list>
</property>
<property name="persistenceAdapter">
<bean class="org.apache.activemq.store.memory.MemoryPersistenceAdapter"/>
<!-- bean class="org.apache.activemq.store.jdbc.JDBCPersistenceAdapter">
<property name="dataSource" ref="dataSource"/>
<property name="createTablesOnStartup" value="true" />
</bean-->
</property>
</bean>
</beans>