diff --git a/pom.xml b/pom.xml index 17401c9cc..0a22e3c74 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ spring-batch-samples archetypes spring-batch-infrastructure-tests + spring-batch-core-tests spring-batch-integration @@ -289,6 +290,7 @@ samples spring-batch-infrastructure-tests + spring-batch-core-tests spring-batch-samples archetypes diff --git a/spring-batch-core-tests/.classpath b/spring-batch-core-tests/.classpath new file mode 100644 index 000000000..96489ff1c --- /dev/null +++ b/spring-batch-core-tests/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/spring-batch-core-tests/.project b/spring-batch-core-tests/.project new file mode 100644 index 000000000..90fe2d20d --- /dev/null +++ b/spring-batch-core-tests/.project @@ -0,0 +1,32 @@ + + + spring-batch-core-tests + Integration tests for the Spring Batch Infrastructure + + + spring-batch-infrastructure + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.springframework.ide.eclipse.core.springbuilder + + + + + + org.springframework.ide.eclipse.core.springnature + org.eclipse.jdt.core.javanature + org.maven.ide.eclipse.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/spring-batch-core-tests/.settings/org.eclipse.jdt.core.prefs b/spring-batch-core-tests/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..ab7ca06ee --- /dev/null +++ b/spring-batch-core-tests/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,82 @@ +#Wed Jul 16 12:18:28 CEST 2008 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.doc.comment.support=enabled +org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.autoboxing=ignore +org.eclipse.jdt.core.compiler.problem.deprecation=warning +org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled +org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled +org.eclipse.jdt.core.compiler.problem.discouragedReference=warning +org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore +org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled +org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore +org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning +org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning +org.eclipse.jdt.core.compiler.problem.forbiddenReference=error +org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning +org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning +org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore +org.eclipse.jdt.core.compiler.problem.invalidJavadoc=error +org.eclipse.jdt.core.compiler.problem.invalidJavadocTags=enabled +org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsDeprecatedRef=enabled +org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsNotVisibleRef=enabled +org.eclipse.jdt.core.compiler.problem.invalidJavadocTagsVisibility=default +org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore +org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning +org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingJavadocComments=ignore +org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsOverriding=disabled +org.eclipse.jdt.core.compiler.problem.missingJavadocCommentsVisibility=public +org.eclipse.jdt.core.compiler.problem.missingJavadocTags=ignore +org.eclipse.jdt.core.compiler.problem.missingJavadocTagsOverriding=disabled +org.eclipse.jdt.core.compiler.problem.missingJavadocTagsVisibility=public +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore +org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning +org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning +org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore +org.eclipse.jdt.core.compiler.problem.nullReference=warning +org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning +org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore +org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore +org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore +org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning +org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore +org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore +org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled +org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning +org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning +org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning +org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore +org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore +org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeUncheckedExceptions=disabled +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedImport=warning +org.eclipse.jdt.core.compiler.problem.unusedLabel=warning +org.eclipse.jdt.core.compiler.problem.unusedLocal=warning +org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled +org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning +org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning +org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/spring-batch-core-tests/.settings/org.eclipse.mylyn.tasks.ui.prefs b/spring-batch-core-tests/.settings/org.eclipse.mylyn.tasks.ui.prefs new file mode 100644 index 000000000..a0153a294 --- /dev/null +++ b/spring-batch-core-tests/.settings/org.eclipse.mylyn.tasks.ui.prefs @@ -0,0 +1,4 @@ +#Thu Aug 23 07:01:43 BST 2007 +eclipse.preferences.version=1 +project.repository.kind=jira +project.repository.url=http\://opensource.atlassian.com/projects/spring diff --git a/spring-batch-core-tests/.settings/org.maven.ide.eclipse.prefs b/spring-batch-core-tests/.settings/org.maven.ide.eclipse.prefs new file mode 100644 index 000000000..b49152cf9 --- /dev/null +++ b/spring-batch-core-tests/.settings/org.maven.ide.eclipse.prefs @@ -0,0 +1,9 @@ +#Sat Jun 20 14:28:54 BST 2009 +activeProfiles= +eclipse.preferences.version=1 +fullBuildGoals=process-test-resources +includeModules=false +resolveWorkspaceProjects=true +resourceFilterGoals=process-resources resources\:testResources +skipCompilerPlugin=true +version=1 diff --git a/spring-batch-core-tests/.settings/org.springframework.ide.eclipse.beans.core.prefs b/spring-batch-core-tests/.settings/org.springframework.ide.eclipse.beans.core.prefs new file mode 100644 index 000000000..ce40fadc6 --- /dev/null +++ b/spring-batch-core-tests/.settings/org.springframework.ide.eclipse.beans.core.prefs @@ -0,0 +1,23 @@ +#Tue Sep 22 11:04:01 BST 2009 +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/aop= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/batch= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/beans= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/context= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/faces= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/jdbc= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/jee= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/jms= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/lang= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/osgi= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/osgi-compendium= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/oxm= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/p= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/security= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/task= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/tx= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/util= +//org.springframework.ide.eclipse.beans.core.default.version.http\://www.springframework.org/schema/webflow-config= +eclipse.preferences.version=1 +org.springframework.ide.eclipse.beans.core.default.version.check.classpath=true +org.springframework.ide.eclipse.beans.core.enable.namespace.versions=false +org.springframework.ide.eclipse.beans.core.ignoreMissingNamespaceHandler=false diff --git a/spring-batch-core-tests/.springBeans b/spring-batch-core-tests/.springBeans new file mode 100644 index 000000000..f3f83d52b --- /dev/null +++ b/spring-batch-core-tests/.springBeans @@ -0,0 +1,237 @@ + + + 1 + + + + + + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + + true + false + + + + + diff --git a/spring-batch-core-tests/pom.xml b/spring-batch-core-tests/pom.xml new file mode 100644 index 000000000..7f154fe1d --- /dev/null +++ b/spring-batch-core-tests/pom.xml @@ -0,0 +1,134 @@ + + + 4.0.0 + spring-batch-core-tests + Core Tests + Integration tests for the Spring Batch Core + + org.springframework.batch + spring-batch-parent + 2.1.0.CI-SNAPSHOT + ../spring-batch-parent + + + + tiger + + 1.5 + + + + stax + stax + + + + + default + + true + + + + + org.apache.maven.plugins + maven-surefire-plugin + + true + + + + + + + test + + + + org.apache.maven.plugins + maven-surefire-plugin + + false + + + + + + + + + org.springframework.batch + spring-batch-core + 2.1.0.CI-SNAPSHOT + + + hsqldb + hsqldb + + + mysql + mysql-connector-java + 5.1.6 + runtime + + + commons-io + commons-io + test + + + commons-lang + commons-lang + test + + + org.apache.derby + derby + + + junit + junit + + + org.easymock + easymock + + + log4j + log4j + test + + + org.springframework + spring-jdbc + test + + + org.springframework + spring-test + test + + + org.springframework + spring-tx + + + org.springframework + spring-aop + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + junit:junit + + + + + + diff --git a/spring-batch-core-tests/src/main/resources/batch-derby.properties b/spring-batch-core-tests/src/main/resources/batch-derby.properties new file mode 100644 index 000000000..0f2a0eb60 --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/batch-derby.properties @@ -0,0 +1,18 @@ +# 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.schema= +batch.jndi.name= +batch.naming.factory.initial= +batch.naming.provider.url= +batch.schema.script=schema-derby.sql +batch.business.schema.script=business-schema-derby.sql +batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.DerbyMaxValueIncrementer +batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler + +# Bean Properties for override +# when not using sequences: +incrementerParent.columnName=ID diff --git a/spring-batch-core-tests/src/main/resources/batch-hsql.properties b/spring-batch-core-tests/src/main/resources/batch-hsql.properties new file mode 100644 index 000000000..d68b8457e --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/batch-hsql.properties @@ -0,0 +1,22 @@ +# 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.schema= +batch.jndi.name= +batch.naming.factory.initial= +batch.naming.provider.url= +batch.schema.script=schema-hsqldb.sql +batch.drop.script=schema-drop-hsqldb.sql +batch.business.schema.script=business-schema-hsqldb.sql +batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer +batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler + +# Bean Properties for override +# when not using sequences: +incrementerParent.columnName=ID diff --git a/spring-batch-core-tests/src/main/resources/batch-mysql.properties b/spring-batch-core-tests/src/main/resources/batch-mysql.properties new file mode 100644 index 000000000..f481d74a7 --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/batch-mysql.properties @@ -0,0 +1,19 @@ +# Placeholders batch.* +# for MySQL: +batch.jdbc.driver=com.mysql.jdbc.Driver +batch.jdbc.url=jdbc:mysql://localhost/test +batch.jdbc.user=root +batch.jdbc.password=root +batch.schema= +batch.jndi.name= +batch.naming.factory.initial= +batch.naming.provider.url= +batch.schema.script=schema-mysql.sql +batch.drop.script=schema-drop-mysql.sql +batch.business.schema.script=business-schema-mysql.sql +batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer +batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler + +# Bean Properties for override +# when not using sequences: +incrementerParent.columnName=ID diff --git a/spring-batch-core-tests/src/main/resources/log4j.properties b/spring-batch-core-tests/src/main/resources/log4j.properties new file mode 100644 index 000000000..f8c8855eb --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/log4j.properties @@ -0,0 +1,12 @@ +log4j.rootCategory=INFO, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{1}:%L - %m%n + +log4j.category.org.apache.activemq=ERROR +# log4j.category.org.springframework=DEBUG +log4j.category.org.springframework.jdbc=DEBUG +log4j.category.org.springframework.jms=DEBUG +log4j.category.org.springframework.batch=DEBUG +log4j.category.org.springframework.retry=DEBUG diff --git a/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/data-source-context.xml b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/data-source-context.xml new file mode 100644 index 000000000..438ec23c0 --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/data-source-context.xml @@ -0,0 +1,65 @@ + + + + + + + + + + ${batch.drop.script} + ${batch.schema.script} + ${batch.business.schema.script} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/env-context.xml b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/env-context.xml new file mode 100644 index 000000000..e18ed2c68 --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/env-context.xml @@ -0,0 +1,39 @@ + + + + + + + + + + ${batch.schema.script} + ${batch.business.schema.script} + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/execution-context.xml b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/execution-context.xml new file mode 100644 index 000000000..4d46593bb --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/execution-context.xml @@ -0,0 +1,103 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/init.sql b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/init.sql new file mode 100644 index 000000000..105bd81af --- /dev/null +++ b/spring-batch-core-tests/src/main/resources/org/springframework/batch/core/test/init.sql @@ -0,0 +1,6 @@ +DROP TABLE T_FOOS; +create table T_FOOS ( + id integer not null primary key, + name varchar(80), + foo_date timestamp +); diff --git a/spring-batch-core-tests/src/site/apt/index.apt b/spring-batch-core-tests/src/site/apt/index.apt new file mode 100644 index 000000000..45d636ad3 --- /dev/null +++ b/spring-batch-core-tests/src/site/apt/index.apt @@ -0,0 +1,15 @@ + ------ + Spring Batch Integration Tests + ------ + Dave Syer + ------ + March 2008 + +Overview of the Spring Batch Integration Tests + + This module contains integration tests for the Spring Batch Infrastructure module. In the tests we exercise some extended and mostly realistic scenarios using the infrastructure components. + + * Message-pipeline tests. There are a bunch of tests using JMS and RDBMS in the same transaction, exercising various failure and retry scenarios. The basic premise is that throughput in such systems is greatly increased by widening the transactio nboundaries to process more than one message in each transaction. If there is a failure we may want to retry the operation in the next transaction. + + * OXM integration tests. There are also some tests of the XML reader and writers in Spring Batch with more extensive configuration than in the unit tests. + diff --git a/spring-batch-core-tests/src/site/site.xml b/spring-batch-core-tests/src/site/site.xml new file mode 100644 index 000000000..29c288783 --- /dev/null +++ b/spring-batch-core-tests/src/site/site.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/spring-batch-core-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java b/spring-batch-core-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java new file mode 100644 index 000000000..c58538cfe --- /dev/null +++ b/spring-batch-core-tests/src/test/java/test/jdbc/datasource/DataSourceInitializer.java @@ -0,0 +1,155 @@ +/* + * Copyright 2006-2007 the original author or authors. + * + * 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 test.jdbc.datasource; + +import java.io.IOException; +import java.util.List; +import java.util.Arrays; + +import javax.sql.DataSource; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +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.core.io.Resource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.datasource.DataSourceTransactionManager; +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.StringUtils; + +public class DataSourceInitializer implements InitializingBean, DisposableBean { + + private Resource[] initScripts; + + private Resource destroyScript; + + private DataSource dataSource; + + private boolean initialize = false; + + private Log logger = LogFactory.getLog(getClass()); + + private static boolean initialized = false; + + 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); + 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); + } + } + initialized = true; + } + } + + private void doExecuteScript(final Resource scriptResource) { + if (scriptResource == null || !scriptResource.exists()) + return; + TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(dataSource)); + transactionTemplate.execute(new TransactionCallback() { + + @SuppressWarnings("unchecked") + public Object doInTransaction(TransactionStatus status) { + JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); + String[] scripts; + try { + scripts = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource + .getInputStream())), ";"); + } + catch (IOException e) { + throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e); + } + for (int i = 0; i < scripts.length; i++) { + String script = scripts[i].trim(); + if (StringUtils.hasText(script)) { + jdbcTemplate.execute(scripts[i]); + } + } + return null; + } + + }); + + } + + private String stripComments(List list) { + StringBuffer buffer = new StringBuffer(); + for (String line : list) { + if (!line.startsWith("//") && !line.startsWith("--")) { + buffer.append(line + "\n"); + } + } + return buffer.toString(); + } + + public Class getObjectType() { + return DataSource.class; + } + + public void setInitScripts(Resource[] initScripts) { + this.initScripts = initScripts; + } + + public void setDestroyScript(Resource destroyScript) { + this.destroyScript = destroyScript; + } + + public void setDataSource(DataSource dataSource) { + this.dataSource = dataSource; + } + +} diff --git a/spring-batch-core-tests/src/test/java/test/jdbc/datasource/DerbyDataSourceFactoryBean.java b/spring-batch-core-tests/src/test/java/test/jdbc/datasource/DerbyDataSourceFactoryBean.java new file mode 100644 index 000000000..2a16eb389 --- /dev/null +++ b/spring-batch-core-tests/src/test/java/test/jdbc/datasource/DerbyDataSourceFactoryBean.java @@ -0,0 +1,35 @@ +package test.jdbc.datasource; + +import java.io.File; + +import javax.sql.DataSource; + +import org.apache.derby.jdbc.EmbeddedDataSource; +import org.springframework.beans.factory.config.AbstractFactoryBean; + +public class DerbyDataSourceFactoryBean extends AbstractFactoryBean { + + private String dataDirectory = "derby-home"; + + public void setDataDirectory(String dataDirectory) { + this.dataDirectory = dataDirectory; + } + + protected Object createInstance() throws Exception { + File directory = new File(dataDirectory); + System.setProperty("derby.system.home", directory.getCanonicalPath()); + System.setProperty("derby.storage.fileSyncTransactionLog", "true"); + System.setProperty("derby.storage.pageCacheSize", "100"); + + final EmbeddedDataSource ds = new EmbeddedDataSource(); + ds.setDatabaseName("derbydb"); + ds.setCreateDatabase("create"); + + return ds; + } + + public Class getObjectType() { + return DataSource.class; + } + +}