Merge spring-batch-infrastructure-tests in spring-batch-infrastructure

This commit is contained in:
Mahmoud Ben Hassine
2020-12-04 14:13:33 +01:00
parent afa013f09b
commit bf612650ea
86 changed files with 118 additions and 1004 deletions

View File

@@ -342,6 +342,12 @@ project('spring-batch-infrastructure') {
testCompile "javax.xml.bind:jaxb-api:$jaxbApiVersion"
testCompile "org.springframework.kafka:spring-kafka-test:$springKafkaVersion"
testCompile "org.assertj:assertj-core:$assertjVersion"
testCompile "org.xmlunit:xmlunit-core:$xmlunitVersion"
testCompile "org.xmlunit:xmlunit-matchers:$xmlunitVersion"
testCompile "com.thoughtworks.xstream:xstream:$xstreamVersion"
testCompile "org.apache.activemq:activemq-broker:$activemqVersion"
testCompile "org.apache.activemq:activemq-kahadb-store:$activemqVersion"
testCompile "org.springframework.data:spring-data-jpa:$springDataJpaVersion"
testRuntime "com.sun.mail:javax.mail:$javaMailVersion"
testRuntime "org.codehaus.groovy:groovy-jsr223:$groovyVersion"
@@ -449,62 +455,6 @@ project('spring-batch-core-tests') {
}
}
project('spring-batch-infrastructure-tests') {
description = 'Spring Batch Infrastructure Tests'
project.tasks.findByPath("artifactoryPublish")?.enabled = false
dependencies {
compile project(":spring-batch-infrastructure")
compile "javax.jms:javax.jms-api:$jmsVersion"
compile "org.apache.commons:commons-dbcp2:$commonsDdbcpVersion"
compile "org.springframework:spring-tx:$springVersion"
compile "org.springframework:spring-aop:$springVersion"
testCompile "org.hsqldb:hsqldb:$hsqldbVersion"
testCompile "commons-io:commons-io:$commonsIoVersion"
testCompile "org.apache.derby:derby:$derbyVersion"
testCompile "org.apache.activemq:activemq-broker:$activemqVersion"
testCompile "org.apache.activemq:activemq-kahadb-store:$activemqVersion"
testCompile "junit:junit:${junitVersion}"
testCompile "org.hamcrest:hamcrest-library:$hamcrestVersion"
testCompile "org.apache.geronimo.specs:geronimo-j2ee-management_1.1_spec:1.0.1"
testCompile "org.xmlunit:xmlunit-core:$xmlunitVersion"
testCompile "org.xmlunit:xmlunit-matchers:$xmlunitVersion"
testCompile "org.apache.logging.log4j:log4j-api:$log4jVersion"
testCompile "org.apache.logging.log4j:log4j-core:$log4jVersion"
testCompile "com.thoughtworks.xstream:xstream:$xstreamVersion"
testCompile("com.fasterxml.woodstox:woodstox-core:$woodstoxVersion") {
exclude group: 'stax', module: 'stax-api'
}
testCompile "org.apache.commons:commons-lang3:$commonsLangVersion"
testCompile("org.springframework:spring-oxm:$springVersion") {
exclude group: 'commons-lang', module: 'commons-lang'
}
testCompile "org.springframework.data:spring-data-jpa:$springDataJpaVersion"
testCompile "org.springframework:spring-jdbc:$springVersion"
testCompile "org.springframework:spring-test:$springVersion"
testCompile "org.mockito:mockito-core:$mockitoVersion"
testCompile "javax.xml.bind:jaxb-api:$jaxbApiVersion"
compile("org.hibernate:hibernate-core:$hibernateVersion") { dep ->
optional dep
exclude group: 'org.jboss.spec.javax.transaction', module: 'jboss-transaction-api_1.1_spec'
}
compile("org.hibernate:hibernate-entitymanager:$hibernateVersion") { dep ->
optional dep
exclude group: 'org.jboss.spec.javax.transaction', module: 'jboss-transaction-api_1.1_spec'
}
optional "javax.transaction:javax.transaction-api:1.2"
optional "org.springframework:spring-orm:$springVersion"
optional "org.springframework:spring-jms:$springVersion"
runtime "mysql:mysql-connector-java:$mysqlVersion"
runtime "org.postgresql:postgresql:$postgresqlVersion"
}
test {
enabled = project.hasProperty('alltests')
}
}
//Domain for batch job testing
project('spring-batch-test') {
description = 'Spring Batch Test'

View File

@@ -1,43 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beansProjectDescription>
<version>1</version>
<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-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>
<name><![CDATA[synch]]></name>
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/test/resources/data-source-context.xml</config>
</configs>
</configSet>
<configSet>
<name><![CDATA[batch]]></name>
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/test/resources/org/springframework/batch/jms/jms-context.xml</config>
<config>src/test/resources/data-source-context.xml</config>
</configs>
</configSet>
<configSet>
<name><![CDATA[asynch]]></name>
<allowBeanDefinitionOverriding>true</allowBeanDefinitionOverriding>
<incomplete>false</incomplete>
<configs>
<config>src/test/resources/data-source-context.xml</config>
</configs>
</configSet>
</configSets>
</beansProjectDescription>

View File

@@ -1,190 +0,0 @@
/*
* Copyright 2012 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
*
* https://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.springframework.batch.item.database;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.JdbcTestUtils;
/**
* @author Dave Syer
* @author David Thexton
* @author Michael Minella
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "JdbcPagingItemReaderCommonTests-context.xml")
public class JdbcPagingItemReaderAsyncTests {
/**
* The page size
*/
private static final int PAGE_SIZE = 10;
/**
* The number of items to read
*/
private static final int ITEM_COUNT = 1000;
/**
* The number of threads to create
*/
private static final int THREAD_COUNT = 10;
private static Log logger = LogFactory.getLog(JdbcPagingItemReaderAsyncTests.class);
@Autowired
private DataSource dataSource;
private int maxId;
@Before
public void init() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
Integer tempMaxId = jdbcTemplate.queryForObject("SELECT MAX(ID) from T_FOOS", Integer.class);
maxId = tempMaxId != null? tempMaxId : 0;
for (int i = ITEM_COUNT; i > maxId; i--) {
jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i);
}
assertEquals(ITEM_COUNT, JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"));
}
@After
public void destroy() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("DELETE from T_FOOS where ID>?", maxId);
}
@Test
public void testAsyncReader() throws Throwable {
List<Throwable> throwables = new ArrayList<>();
int max = 10;
for (int i = 0; i < max; i++) {
try {
doTest();
}
catch (Throwable e) {
throwables.add(e);
}
}
if (!throwables.isEmpty()) {
throw new IllegalStateException(String.format("Failed %d out of %d", throwables.size(), max), throwables
.get(0));
}
}
/**
* @throws Exception
* @throws InterruptedException
* @throws ExecutionException
*/
private void doTest() throws Exception, InterruptedException, ExecutionException {
final ItemReader<Foo> reader = getItemReader();
CompletionService<List<Foo>> completionService = new ExecutorCompletionService<>(Executors
.newFixedThreadPool(THREAD_COUNT));
for (int i = 0; i < THREAD_COUNT; i++) {
completionService.submit(new Callable<List<Foo>>() {
@Override
public List<Foo> call() throws Exception {
List<Foo> list = new ArrayList<>();
Foo next = null;
do {
next = reader.read();
Thread.sleep(10L);
logger.debug("Reading item: " + next);
if (next != null) {
list.add(next);
}
} while (next != null);
return list;
}
});
}
int count = 0;
Set<Foo> results = new HashSet<>();
for (int i = 0; i < THREAD_COUNT; i++) {
List<Foo> items = completionService.take().get();
count += items.size();
logger.debug("Finished items count: " + items.size());
logger.debug("Finished items: " + items);
assertNotNull(items);
results.addAll(items);
}
assertEquals(ITEM_COUNT, count);
assertEquals(ITEM_COUNT, results.size());
}
protected ItemReader<Foo> getItemReader() throws Exception {
JdbcPagingItemReader<Foo> reader = new JdbcPagingItemReader<>();
reader.setDataSource(dataSource);
SqlPagingQueryProviderFactoryBean factory = new SqlPagingQueryProviderFactoryBean();
factory.setDataSource(dataSource);
factory.setSelectClause("select ID, NAME, VALUE");
factory.setFromClause("from T_FOOS");
Map<String, Order> sortKeys = new LinkedHashMap<>();
sortKeys.put("VALUE", Order.ASCENDING);
factory.setSortKeys(sortKeys);
reader.setQueryProvider(factory.getObject());
reader.setRowMapper(new RowMapper<Foo>() {
@Override
public Foo mapRow(ResultSet rs, int i) throws SQLException {
Foo foo = new Foo();
foo.setId(rs.getInt(1));
foo.setName(rs.getString(2));
foo.setValue(rs.getInt(3));
return foo;
}
});
reader.setPageSize(PAGE_SIZE);
reader.afterPropertiesSet();
reader.setSaveState(false);
return reader;
}
}

View File

@@ -1,165 +0,0 @@
/*
* Copyright 2010-2014 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
*
* https://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.springframework.batch.item.database;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.JdbcTestUtils;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "JpaPagingItemReaderCommonTests-context.xml")
public class JpaPagingItemReaderAsyncTests {
/**
* The number of items to read
*/
private static final int ITEM_COUNT = 1000;
/**
* The number of threads to create
*/
private static final int THREAD_COUNT = 10;
private static final int PAGE_SIZE = 10;
private static Log logger = LogFactory.getLog(JpaPagingItemReaderAsyncTests.class);
@Autowired
private DataSource dataSource;
@Autowired
private EntityManagerFactory entityManagerFactory;
private int maxId;
@Before
public void init() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
maxId = jdbcTemplate.queryForObject("SELECT MAX(ID) from T_FOOS", Integer.class);
for (int i = ITEM_COUNT; i > maxId; i--) {
jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i);
}
assertEquals(ITEM_COUNT, JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"));
}
@After
public void destroy() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("DELETE from T_FOOS where ID>?", maxId);
}
@Test
public void testAsyncReader() throws Throwable {
List<Throwable> throwables = new ArrayList<>();
int max = 10;
for (int i = 0; i < max; i++) {
try {
doTest();
}
catch (Throwable e) {
throwables.add(e);
}
}
if (!throwables.isEmpty()) {
throw new IllegalStateException(String.format("Failed %d out of %d", throwables.size(), max), throwables
.get(0));
}
}
/**
* @throws Exception
* @throws InterruptedException
* @throws ExecutionException
*/
private void doTest() throws Exception, InterruptedException, ExecutionException {
final JpaPagingItemReader<Foo> reader = getItemReader();
CompletionService<List<Foo>> completionService = new ExecutorCompletionService<>(Executors
.newFixedThreadPool(THREAD_COUNT));
for (int i = 0; i < THREAD_COUNT; i++) {
completionService.submit(new Callable<List<Foo>>() {
@Override
public List<Foo> call() throws Exception {
List<Foo> list = new ArrayList<>();
Foo next = null;
do {
next = reader.read();
Thread.sleep(10L);
logger.debug("Reading item: " + next);
if (next != null) {
list.add(next);
}
} while (next != null);
return list;
}
});
}
int count = 0;
Set<Foo> results = new HashSet<>();
for (int i = 0; i < THREAD_COUNT; i++) {
List<Foo> items = completionService.take().get();
count += items.size();
logger.debug("Finished items count: " + items.size());
logger.debug("Finished items: " + items);
assertNotNull(items);
results.addAll(items);
}
assertEquals(ITEM_COUNT, count);
assertEquals(ITEM_COUNT, results.size());
reader.close();
}
private JpaPagingItemReader<Foo> getItemReader() throws Exception {
String jpqlQuery = "select f from Foo f";
JpaPagingItemReader<Foo> reader = new JpaPagingItemReader<>();
reader.setQueryString(jpqlQuery);
reader.setEntityManagerFactory(entityManagerFactory);
reader.setPageSize(PAGE_SIZE);
reader.afterPropertiesSet();
reader.setSaveState(false);
reader.open(new ExecutionContext());
return reader;
}
}

View File

@@ -1,110 +0,0 @@
/*
* Copyright 2010-2014 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
*
* https://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.springframework.batch.item.sample;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Simple domain object for testing purposes.
*/
@Entity
@Table(name = "T_FOOS")
public class Foo {
public static final String FAILURE_MESSAGE = "Foo Failure!";
public static final String UGLY_FAILURE_MESSAGE = "Ugly Foo Failure!";
@Id
private int id;
private String name;
private int value;
public Foo(){}
public Foo(int id, String name, int value) {
this.id = id;
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Foo[id=" +id +",name=" + name + ",value=" + value + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + value;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Foo other = (Foo) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
if (value != other.value)
return false;
return true;
}
public void fail() throws Exception {
throw new Exception(FAILURE_MESSAGE);
}
public void failUgly() throws Throwable {
throw new Throwable(UGLY_FAILURE_MESSAGE);
}
}

View File

@@ -1,167 +0,0 @@
/*
* 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
*
* https://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.dao.DataAccessException;
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 boolean initialized = false;
public void setInitialize(boolean initialize) {
this.initialize = initialize;
}
@Override
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 + "]");
}
}
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(dataSource, "DataSource is required");
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;
final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String[] scripts;
try {
String[] list = StringUtils.delimitedListToStringArray(stripComments(IOUtils.readLines(scriptResource
.getInputStream(), "UTF-8")), ";");
scripts = list;
}
catch (IOException e) {
throw new BeanInitializationException("Cannot load script from [" + scriptResource + "]", e);
}
for (int i = 0; i < scripts.length; i++) {
final String script = scripts[i].trim();
TransactionTemplate transactionTemplate = new TransactionTemplate(new DataSourceTransactionManager(
dataSource));
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
if (StringUtils.hasText(script)) {
try {
jdbcTemplate.execute(script);
}
catch (DataAccessException e) {
if (!script.toUpperCase().startsWith("DROP")) {
throw e;
}
}
}
return null;
}
});
}
}
private String stripComments(List<String> list) {
StringBuilder buffer = new StringBuilder();
for (String line : list) {
if (!line.startsWith("//") && !line.startsWith("--")) {
buffer.append(line).append("\n");
}
}
return buffer.toString();
}
public Class<DataSource> 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;
}
}

View File

@@ -1,13 +0,0 @@
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence https://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="bar" transaction-type="RESOURCE_LOCAL">
<class>org.springframework.batch.item.sample.Foo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>

View File

@@ -1,15 +0,0 @@
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.logging.log4j.core.appender.ConsoleAppender
log4j.appender.stdout.layout=org.apache.logging.log4j.core.layout.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.jdbc.datasource=INFO
# log4j.category.org.springframework.jms=DEBUG
# log4j.category.org.springframework.batch=DEBUG
#log4j.category.org.springframework.batch.support=INFO
# log4j.category.org.springframework.retry=DEBUG
# log4j.category.org.springframework.batch.item.database=DEBUG

View File

@@ -1,18 +0,0 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.springframework.batch.item.sample.Foo" table="T_FOOS">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="name" />
<property name="value" />
</class>
<query name="allFoos">
from Foo
</query>
</hibernate-mapping>

View File

@@ -1,20 +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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util-3.1.xsd">
<import resource="classpath:/data-source-context.xml"/>
<!-- Override the default: -->
<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.schema.script}</value>
</list>
</property>
</bean>
</beans>

View File

@@ -1,33 +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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<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="initialize" value="${batch.data.source.init}"/>
<property name="initScripts">
<list>
<value>${batch.schema.script}</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="bar"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
</bean>
</property>
</bean>
</beans>

View File

@@ -1,22 +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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean 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="incrementerParent" class="org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer"
abstract="true">
<property name="dataSource" ref="dataSource" />
<property name="columnName" value="ID" />
</bean>
</beans>

View File

@@ -1,25 +0,0 @@
DROP TABLE T_FOOS;
DROP TABLE T_WRITE_FOOS;
CREATE TABLE T_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
CODE VARCHAR(10),
VALUE INTEGER
);
ALTER TABLE T_FOOS ADD PRIMARY KEY (ID);
INSERT INTO t_foos (id, name, value) VALUES (1, 'bar2', 2);
INSERT INTO t_foos (id, name, value) VALUES (2, 'bar4', 4);
INSERT INTO t_foos (id, name, value) VALUES (3, 'bar1', 1);
INSERT INTO t_foos (id, name, value) VALUES (4, 'bar5', 5);
INSERT INTO t_foos (id, name, value) VALUES (5, 'bar3', 3);
CREATE TABLE T_WRITE_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
VALUE INTEGER
);
ALTER TABLE T_WRITE_FOOS ADD PRIMARY KEY (ID);

View File

@@ -1,25 +0,0 @@
DROP TABLE T_FOOS if exists;
DROP TABLE T_WRITE_FOOS if exists;
CREATE TABLE T_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
CODE VARCHAR(10),
VALUE INTEGER
);
ALTER TABLE T_FOOS ADD PRIMARY KEY (ID);
INSERT INTO t_foos (id, name, value) VALUES (1, 'bar2', 2);
INSERT INTO t_foos (id, name, value) VALUES (2, 'bar4', 4);
INSERT INTO t_foos (id, name, value) VALUES (3, 'bar1', 1);
INSERT INTO t_foos (id, name, value) VALUES (4, 'bar5', 5);
INSERT INTO t_foos (id, name, value) VALUES (5, 'bar3', 3);
CREATE TABLE T_WRITE_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
VALUE INTEGER
);
ALTER TABLE T_WRITE_FOOS ADD PRIMARY KEY (ID);

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.batch.item.database.support;
package org.springframework.batch.item.database;
import static org.junit.Assert.assertEquals;
@@ -38,7 +38,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "../JpaPagingItemReaderCommonTests-context.xml" })
@ContextConfiguration(locations={"JpaPagingItemReaderCommonTests-context.xml"})
public class JpaNativeQueryProviderIntegrationTests {
@Autowired

View File

@@ -32,7 +32,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* @author Mahmoud Ben Hassine
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"JpaPagingItemReaderParameterTests-context.xml"})
@ContextConfiguration(locations={"JpaPagingItemReaderCommonTests-context.xml"})
public class JpaPagingItemReaderNamedQueryIntegrationTests extends AbstractPagingItemReaderParameterTests {
@Autowired

View File

@@ -18,20 +18,31 @@ package org.springframework.batch.item.database;
import java.util.Collections;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.junit.runner.RunWith;
import org.springframework.batch.item.database.orm.JpaNativeQueryProvider;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.PlatformTransactionManager;
/**
* @author Anatoly Polinsky
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"JpaPagingItemReaderParameterTests-context.xml"})
@ContextConfiguration(classes = JpaPagingItemReaderNativeQueryIntegrationTests.JpaConfiguration.class)
public class JpaPagingItemReaderNativeQueryIntegrationTests extends AbstractPagingItemReaderParameterTests {
@Autowired
@@ -59,4 +70,39 @@ public class JpaPagingItemReaderNativeQueryIntegrationTests extends AbstractPagi
return reader;
}
@Configuration
public static class JpaConfiguration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("org/springframework/batch/item/database/init-foo-schema-hsqldb.sql")
.build();
}
@Bean
public PersistenceUnitManager persistenceUnitManager() {
DefaultPersistenceUnitManager persistenceUnitManager = new DefaultPersistenceUnitManager();
persistenceUnitManager.setDefaultDataSource(dataSource());
persistenceUnitManager.afterPropertiesSet();
return persistenceUnitManager;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean.setPersistenceUnitManager(persistenceUnitManager());
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.afterPropertiesSet();
return factoryBean.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
return new JpaTransactionManager(entityManagerFactory());
}
}
}

View File

@@ -26,7 +26,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@ContextConfiguration(locations = "JpaPagingItemReaderCommonTests-context.xml")
public class JpaPagingItemReaderParameterTests extends AbstractPagingItemReaderParameterTests {
@Autowired

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
@@ -78,22 +79,22 @@ public abstract class AbstractStaxEventReaderItemReaderTests {
assertEquals(3, results.size());
Trade trade1 = results.get(0);
assertEquals("XYZ0001", trade1.getIsin());
assertEquals(5, trade1.getQuantity());
assertEquals(new BigDecimal("11.39"), trade1.getPrice());
assertEquals("Customer1", trade1.getCustomer());
Assert.assertEquals("XYZ0001", trade1.getIsin());
Assert.assertEquals(5, trade1.getQuantity());
Assert.assertEquals(new BigDecimal("11.39"), trade1.getPrice());
Assert.assertEquals("Customer1", trade1.getCustomer());
Trade trade2 = results.get(1);
assertEquals("XYZ0002", trade2.getIsin());
assertEquals(2, trade2.getQuantity());
assertEquals(new BigDecimal("72.99"), trade2.getPrice());
assertEquals("Customer2", trade2.getCustomer());
Assert.assertEquals("XYZ0002", trade2.getIsin());
Assert.assertEquals(2, trade2.getQuantity());
Assert.assertEquals(new BigDecimal("72.99"), trade2.getPrice());
Assert.assertEquals("Customer2", trade2.getCustomer());
Trade trade3 = results.get(2);
assertEquals("XYZ0003", trade3.getIsin());
assertEquals(9, trade3.getQuantity());
assertEquals(new BigDecimal("99.99"), trade3.getPrice());
assertEquals("Customer3", trade3.getCustomer());
Assert.assertEquals("XYZ0003", trade3.getIsin());
Assert.assertEquals(9, trade3.getQuantity());
Assert.assertEquals(new BigDecimal("99.99"), trade3.getPrice());
Assert.assertEquals("Customer3", trade3.getCustomer());
}
@After

View File

@@ -25,6 +25,7 @@ import java.util.List;
import javax.xml.transform.stream.StreamSource;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
@@ -57,10 +58,10 @@ public class Jaxb2NamespaceUnmarshallingTests {
public void testUnmarshal() throws Exception {
QualifiedTrade trade = (QualifiedTrade) getUnmarshaller().unmarshal(
new StreamSource(new StringReader(TRADE_XML)));
assertEquals("XYZ0001", trade.getIsin());
assertEquals(5, trade.getQuantity());
assertEquals(new BigDecimal("11.39"), trade.getPrice());
assertEquals("Customer1", trade.getCustomer());
Assert.assertEquals("XYZ0001", trade.getIsin());
Assert.assertEquals(5, trade.getQuantity());
Assert.assertEquals(new BigDecimal("11.39"), trade.getPrice());
Assert.assertEquals("Customer1", trade.getCustomer());
}
@Test
@@ -91,22 +92,22 @@ public class Jaxb2NamespaceUnmarshallingTests {
assertEquals(3, results.size());
QualifiedTrade trade1 = results.get(0);
assertEquals("XYZ0001", trade1.getIsin());
assertEquals(5, trade1.getQuantity());
assertEquals(new BigDecimal("11.39"), trade1.getPrice());
assertEquals("Customer1", trade1.getCustomer());
Assert.assertEquals("XYZ0001", trade1.getIsin());
Assert.assertEquals(5, trade1.getQuantity());
Assert.assertEquals(new BigDecimal("11.39"), trade1.getPrice());
Assert.assertEquals("Customer1", trade1.getCustomer());
QualifiedTrade trade2 = results.get(1);
assertEquals("XYZ0002", trade2.getIsin());
assertEquals(2, trade2.getQuantity());
assertEquals(new BigDecimal("72.99"), trade2.getPrice());
assertEquals("Customer2", trade2.getCustomer());
Assert.assertEquals("XYZ0002", trade2.getIsin());
Assert.assertEquals(2, trade2.getQuantity());
Assert.assertEquals(new BigDecimal("72.99"), trade2.getPrice());
Assert.assertEquals("Customer2", trade2.getCustomer());
QualifiedTrade trade3 = results.get(2);
assertEquals("XYZ0003", trade3.getIsin());
assertEquals(9, trade3.getQuantity());
assertEquals(new BigDecimal("99.99"), trade3.getPrice());
assertEquals("Customer3", trade3.getCustomer());
Assert.assertEquals("XYZ0003", trade3.getIsin());
Assert.assertEquals(9, trade3.getQuantity());
Assert.assertEquals(new BigDecimal("99.99"), trade3.getPrice());
Assert.assertEquals("Customer3", trade3.getCustomer());
}
@After

View File

@@ -169,7 +169,7 @@ public class SynchronousTests implements ApplicationContextAware {
@Transactional
@Test
public void testPartialRollback() throws Exception {
public void JpaNativeQueryProviderIntegrationTeststestPartialRollback() throws Exception {
// The JmsTemplate is used elsewhere outside a transaction, so
// we need to use one here that is transaction aware.

View File

@@ -1,14 +1,14 @@
# Placeholders batch.*
# for Oracle:
batch.jdbc.driver=oracle.jdbc.OracleDriver
batch.jdbc.url=jdbc:oracle:thin:@oracle:1521:xe
batch.jdbc.user=spring
batch.jdbc.password=spring
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.schema.script=classpath:org/springframework/batch/item/database/init-foo-schema-oracle.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.OracleSequenceMaxValueIncrementer
batch.database.incrementer.parent=sequenceIncrementerParent
batch.verify.cursor.position=true
# Placeholders batch.*
# for Oracle:
batch.jdbc.driver=oracle.jdbc.OracleDriver
batch.jdbc.url=jdbc:oracle:thin:@oracle:1521:xe
batch.jdbc.user=spring
batch.jdbc.password=spring
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.schema.script=classpath:org/springframework/batch/item/database/init-foo-schema-oracle.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.OracleSequenceMaxValueIncrementer
batch.database.incrementer.parent=sequenceIncrementerParent
batch.verify.cursor.position=true

View File

@@ -1,14 +1,14 @@
# Placeholders batch.*
# for Oracle:
batch.jdbc.driver=org.postgresql.Driver
batch.jdbc.url=jdbc:postgresql://localhost:9432/test
batch.jdbc.user=test
batch.jdbc.password=test
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.schema.script=classpath:org/springframework/batch/item/database/init-foo-schema-postgres.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.PostgresSequenceMaxValueIncrementer
batch.database.incrementer.parent=sequenceIncrementerParent
batch.verify.cursor.position=true
# Placeholders batch.*
# for Oracle:
batch.jdbc.driver=org.postgresql.Driver
batch.jdbc.url=jdbc:postgresql://localhost:9432/test
batch.jdbc.user=test
batch.jdbc.password=test
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.schema.script=classpath:org/springframework/batch/item/database/init-foo-schema-postgres.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.PostgresSequenceMaxValueIncrementer
batch.database.incrementer.parent=sequenceIncrementerParent
batch.verify.cursor.position=true

View File

@@ -5,7 +5,6 @@
<!-- 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>

View File

@@ -15,7 +15,6 @@
<query name="allFoos">
from Foo
</query>
<query name="foosStartingFrom2">
from Foo where value >= 2
</query>

View File

@@ -17,7 +17,7 @@
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="xincrementerParent" class="org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer"
<bean id="incrementerParent" class="org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer"
abstract="true">
<property name="dataSource" ref="dataSource" />
<property name="columnName" value="ID" />

View File

@@ -1,36 +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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean 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.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="bar"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false"/>
</bean>
</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

@@ -14,7 +14,6 @@
<!-- Initialise the database -->
<bean id="dataSourceInitializer" class="test.jdbc.datasource.DataSourceInitializer">
<property name="dataSource" ref="dataSource"/>
<property name="initialize" value="true"/>
<property name="initScripts">
<list>
<value>classpath:org/springframework/batch/item/database/init-books-schema.sql</value>

View File

@@ -4,6 +4,7 @@ DROP TABLE T_WRITE_FOOS if exists;
CREATE TABLE T_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
CODE VARCHAR(10),
VALUE INTEGER
);