From 762c11c80b8f4be9667bf554a4fce52097f1de13 Mon Sep 17 00:00:00 2001 From: dsyer Date: Mon, 29 Mar 2010 10:18:55 +0000 Subject: [PATCH] Refactor infrastructure-tests to use more placeholders for initialization. --- ...ringframework.ide.eclipse.beans.core.prefs | 4 + .../.springBeans | 13 ++- spring-batch-infrastructure-tests/pom.xml | 7 +- .../batch/config/DatasourceTests.java | 12 +++ .../batch/config/MessagingTests.java | 11 +- .../datasource/DataSourceInitializer.java | 101 ++++++++---------- .../src/test/resources/batch-derby.properties | 15 +++ .../src/test/resources/batch-hsql.properties | 17 +++ .../test/resources/data-source-context.xml | 67 ++++++++++++ .../src/test/resources/data-source.xml | 39 ------- ...dbcPagingItemReaderCommonTests-context.xml | 26 ++--- ...JpaPagingItemReaderCommonTests-context.xml | 22 ++-- .../springframework/batch/jms/jms-context.xml | 31 +++--- 13 files changed, 219 insertions(+), 146 deletions(-) create mode 100644 spring-batch-infrastructure-tests/.settings/org.springframework.ide.eclipse.beans.core.prefs create mode 100644 spring-batch-infrastructure-tests/src/test/resources/batch-derby.properties create mode 100644 spring-batch-infrastructure-tests/src/test/resources/batch-hsql.properties create mode 100644 spring-batch-infrastructure-tests/src/test/resources/data-source-context.xml delete mode 100644 spring-batch-infrastructure-tests/src/test/resources/data-source.xml diff --git a/spring-batch-infrastructure-tests/.settings/org.springframework.ide.eclipse.beans.core.prefs b/spring-batch-infrastructure-tests/.settings/org.springframework.ide.eclipse.beans.core.prefs new file mode 100644 index 000000000..481b7a265 --- /dev/null +++ b/spring-batch-infrastructure-tests/.settings/org.springframework.ide.eclipse.beans.core.prefs @@ -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 diff --git a/spring-batch-infrastructure-tests/.springBeans b/spring-batch-infrastructure-tests/.springBeans index d32d899ee..a0683edd7 100644 --- a/spring-batch-infrastructure-tests/.springBeans +++ b/spring-batch-infrastructure-tests/.springBeans @@ -1,14 +1,17 @@ 1 - + src/test/resources/org/springframework/batch/jms/jms-context.xml - src/test/resources/data-source.xml + src/test/resources/data-source-context.xml + src/test/resources/org/springframework/batch/item/database/data-source-context.xml + src/test/resources/org/springframework/batch/item/database/JdbcPagingItemReaderCommonTests-context.xml + src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml @@ -16,7 +19,7 @@ true false - src/test/resources/data-source.xml + src/test/resources/data-source-context.xml @@ -24,8 +27,8 @@ true false - src/test/resources/data-source.xml src/test/resources/org/springframework/batch/jms/jms-context.xml + src/test/resources/data-source-context.xml @@ -33,7 +36,7 @@ true false - src/test/resources/data-source.xml + src/test/resources/data-source-context.xml diff --git a/spring-batch-infrastructure-tests/pom.xml b/spring-batch-infrastructure-tests/pom.xml index 7aae079e7..a600f8e1b 100644 --- a/spring-batch-infrastructure-tests/pom.xml +++ b/spring-batch-infrastructure-tests/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 spring-batch-infrastructure-tests Infrastructure Tests @@ -156,6 +157,10 @@ ibatis-sqlmap true + + commons-dbcp + commons-dbcp + javax.persistence persistence-api diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/DatasourceTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/DatasourceTests.java index 4bf12f528..0af5d61b4 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/DatasourceTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/DatasourceTests.java @@ -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 { diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/MessagingTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/MessagingTests.java index bbe80c7cd..4d4d75b21 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/MessagingTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/config/MessagingTests.java @@ -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") diff --git a/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java b/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java index aa90be47b..40fa615d5 100644 --- a/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java +++ b/spring-batch-infrastructure-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java @@ -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

- * - * 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 - * hsql.server project. Then you can right click in Eclipse and - * Run As -> 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 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); - } - } - } - } - } diff --git a/spring-batch-infrastructure-tests/src/test/resources/batch-derby.properties b/spring-batch-infrastructure-tests/src/test/resources/batch-derby.properties new file mode 100644 index 000000000..9b9b6c74f --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/resources/batch-derby.properties @@ -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 \ No newline at end of file diff --git a/spring-batch-infrastructure-tests/src/test/resources/batch-hsql.properties b/spring-batch-infrastructure-tests/src/test/resources/batch-hsql.properties new file mode 100644 index 000000000..105b10f94 --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/resources/batch-hsql.properties @@ -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 diff --git a/spring-batch-infrastructure-tests/src/test/resources/data-source-context.xml b/spring-batch-infrastructure-tests/src/test/resources/data-source-context.xml new file mode 100644 index 000000000..070737a2c --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/resources/data-source-context.xml @@ -0,0 +1,67 @@ + + + + + + + + + + ${batch.business.schema.script} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-infrastructure-tests/src/test/resources/data-source.xml b/spring-batch-infrastructure-tests/src/test/resources/data-source.xml deleted file mode 100644 index 5ce1be588..000000000 --- a/spring-batch-infrastructure-tests/src/test/resources/data-source.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - vm://localhost - - - - - - - - - \ No newline at end of file diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JdbcPagingItemReaderCommonTests-context.xml b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JdbcPagingItemReaderCommonTests-context.xml index eee73841b..8ec4b50cd 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JdbcPagingItemReaderCommonTests-context.xml +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JdbcPagingItemReaderCommonTests-context.xml @@ -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"> - + + + + - - - - - - - - - - - - - - + + + + classpath:org/springframework/batch/item/database/init-foo-schema-hsqldb.sql + + \ No newline at end of file diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml index 500260735..9b4129cff 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/JpaPagingItemReaderCommonTests-context.xml @@ -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"> - + + + + - - - - - + + + + classpath:org/springframework/batch/item/database/init-foo-schema-hsqldb.sql + + @@ -27,10 +31,4 @@ - - - - - \ No newline at end of file diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml index 4442df11f..5df8128a8 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/jms/jms-context.xml @@ -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"> - - - - - - - - - - - + @@ -100,4 +89,22 @@ + + + + + + vm://localhost + + + + + + + + \ No newline at end of file