RESOLVED - issue BATCH-1420: Late Binding only happens first time when using inner bean definition with collection property

This commit is contained in:
dsyer
2009-10-16 19:15:17 +00:00
parent db1351f7ab
commit 232d739dbd
16 changed files with 585 additions and 30 deletions

View File

@@ -0,0 +1,118 @@
/*
* 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 org.springframework.batch.sample;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.sample.domain.trade.CustomerCredit;
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
import org.springframework.batch.test.AbstractJobTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class PartitionFileJobFunctionalTests extends AbstractJobTests {
@Autowired
@Qualifier("inputTestReader")
private ItemReader<CustomerCredit> inputReader;
@Autowired
@Qualifier("outputTestReader")
private ItemReader<CustomerCredit> outputReader;
/**
* Check the resulting credits correspond to inputs increased by fixed
* amount.
*/
@Test
public void testUpdateCredit() throws Exception {
assertTrue("Define a prototype bean called 'outputTestReader' to check the output", getApplicationContext()
.containsBeanDefinition("outputTestReader"));
open(inputReader);
List<CustomerCredit> inputs = new ArrayList<CustomerCredit>(getCredits(inputReader));
close(inputReader);
JobExecution jobExecution = this.launchJob();
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
open(outputReader);
List<CustomerCredit> outputs = new ArrayList<CustomerCredit>(getCredits(outputReader));
close(outputReader);
assertEquals(inputs.size(), outputs.size());
int itemCount = inputs.size();
assertTrue(itemCount > 0);
inputs.iterator();
for (int i = 0; i < itemCount; i++) {
assertEquals(inputs.get(i).getCredit().add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT).intValue(),
outputs.get(i).getCredit().intValue());
}
}
/**
* Read all credits using the provided reader.
*/
private Set<CustomerCredit> getCredits(ItemReader<CustomerCredit> reader) throws Exception {
CustomerCredit credit;
Set<CustomerCredit> result = new LinkedHashSet<CustomerCredit>();
while ((credit = reader.read()) != null) {
result.add(credit);
}
return result;
}
/**
* Open the reader if applicable.
*/
private void open(ItemReader<?> reader) {
if (reader instanceof ItemStream) {
((ItemStream) reader).open(new ExecutionContext());
}
}
/**
* Close the reader if applicable.
*/
private void close(ItemReader<?> reader) {
if (reader instanceof ItemStream) {
((ItemStream) reader).close();
}
}
}

View File

@@ -20,7 +20,9 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -39,12 +41,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration()
public class PartitionJobFunctionalTests extends AbstractJobTests {
public class PartitionJdbcJobFunctionalTests extends AbstractJobTests {
@Autowired
@Qualifier("inputTestReader")
private ItemReader<CustomerCredit> inputReader;
@Autowired
@Qualifier("outputTestReader")
private ItemReader<CustomerCredit> outputReader;
/**
* Check the resulting credits correspond to inputs increased by fixed
* amount.
@@ -56,23 +62,21 @@ public class PartitionJobFunctionalTests extends AbstractJobTests {
.containsBeanDefinition("outputTestReader"));
open(inputReader);
List<CustomerCredit> inputs = getCredits(inputReader);
List<CustomerCredit> inputs = new ArrayList<CustomerCredit>(getCredits(inputReader));
close(inputReader);
JobExecution jobExecution = this.launchJob();
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
@SuppressWarnings("unchecked")
ItemReader<CustomerCredit> outputReader = (ItemReader<CustomerCredit>) getApplicationContext().getBean(
"outputTestReader");
open(outputReader);
List<CustomerCredit> outputs = getCredits(outputReader);
List<CustomerCredit> outputs = new ArrayList<CustomerCredit>(getCredits(outputReader));
close(outputReader);
assertEquals(inputs.size(), outputs.size());
int itemCount = inputs.size();
assertTrue(itemCount > 0);
inputs.iterator();
for (int i = 0; i < itemCount; i++) {
assertEquals(inputs.get(i).getCredit().add(CustomerCreditIncreaseProcessor.FIXED_AMOUNT).intValue(),
outputs.get(i).getCredit().intValue());
@@ -83,9 +87,9 @@ public class PartitionJobFunctionalTests extends AbstractJobTests {
/**
* Read all credits using the provided reader.
*/
private List<CustomerCredit> getCredits(ItemReader<CustomerCredit> reader) throws Exception {
private Set<CustomerCredit> getCredits(ItemReader<CustomerCredit> reader) throws Exception {
CustomerCredit credit;
List<CustomerCredit> result = new ArrayList<CustomerCredit>();
Set<CustomerCredit> result = new LinkedHashSet<CustomerCredit>();
while ((credit = reader.read()) != null) {
result.add(credit);
}

View File

@@ -0,0 +1,38 @@
package org.springframework.batch.sample.common;
import static org.junit.Assert.assertEquals;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ColumnRangePartitionerTests {
private DataSource dataSource;
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
private ColumnRangePartitioner partitioner = new ColumnRangePartitioner();
@Test
public void testPartition() {
partitioner.setDataSource(dataSource);
partitioner.setTable("CUSTOMER");
partitioner.setColumn("ID");
Map<String, ExecutionContext> partition = partitioner.partition(2);
assertEquals(2, partition.size());
}
}

View File

@@ -0,0 +1,44 @@
package org.springframework.batch.sample.common;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
public class OutputFileListenerTests {
private OutputFileListener listener = new OutputFileListener();
private StepExecution stepExecution = new StepExecution("foo", new JobExecution(0L), 1L);
@Test
public void testCreateOutputNameFromInput() {
listener.createOutputNameFromInput(stepExecution);
assertEquals("{outputFile=file:./target/output/foo.csv}", stepExecution.getExecutionContext().toString());
}
@Test
public void testSetPath() {
listener.setPath("spam/");
listener.createOutputNameFromInput(stepExecution);
assertEquals("{outputFile=spam/foo.csv}", stepExecution.getExecutionContext().toString());
}
@Test
public void testSetOutputKeyName() {
listener.setPath("");
listener.setOutputKeyName("spam");
listener.createOutputNameFromInput(stepExecution);
assertEquals("{spam=foo.csv}", stepExecution.getExecutionContext().toString());
}
@Test
public void testSetInputKeyName() {
listener.setPath("");
listener.setInputKeyName("spam");
stepExecution.getExecutionContext().putString("spam", "bar");
listener.createOutputNameFromInput(stepExecution);
assertEquals("{outputFile=bar.csv, spam=bar}", stepExecution.getExecutionContext().toString());
}
}

View File

@@ -5,6 +5,6 @@
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="classpath:/simple-job-launcher-context.xml" />
<import resource="classpath:/jobs/partitionJob.xml" />
<import resource="classpath:/jobs/partitionFileJob.xml" />
</beans>

View File

@@ -0,0 +1,10 @@
<?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">
<import resource="classpath:/simple-job-launcher-context.xml" />
<import resource="classpath:/jobs/partitionJdbcJob.xml" />
</beans>

View File

@@ -0,0 +1,10 @@
<?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 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.0.xsd">
<import resource="classpath:data-source-context.xml"/>
</beans>