RESOLVED - issue BATCH-745: strong typing in AggregateItemReader

Added AggregateItem<T>
This commit is contained in:
dsyer
2008-07-28 15:42:35 +00:00
parent 04c9e16b1b
commit 4b6f350a47
12 changed files with 385 additions and 75 deletions

View File

@@ -18,32 +18,19 @@ package org.springframework.batch.sample.domain.trade.internal;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.support.AggregateItemReader;
import org.springframework.batch.sample.domain.trade.Trade;
@SuppressWarnings("unchecked")
/**
* TODO type safety
*/
public class TradeFieldSetMapper implements FieldSetMapper {
public class TradeFieldSetMapper implements FieldSetMapper<Trade> {
public static final int ISIN_COLUMN = 0;
public static final int QUANTITY_COLUMN = 1;
public static final int PRICE_COLUMN = 2;
public static final int CUSTOMER_COLUMN = 3;
public Object mapLine(FieldSet fieldSet, int lineNum) {
public Trade mapLine(FieldSet fieldSet, int lineNum) {
if ("BEGIN".equals(fieldSet.readString(0))) {
return AggregateItemReader.BEGIN_RECORD;
}
if ("END".equals(fieldSet.readString(0))) {
return AggregateItemReader.END_RECORD;
}
Trade trade = new Trade();
trade.setIsin(fieldSet.readString(0));
trade.setQuantity(fieldSet.readLong(1));

View File

@@ -41,10 +41,6 @@ public class ExceptionThrowingItemReaderProxy<T> extends DelegatingItemReader<T>
this.throwExceptionOnRecordNumber = throwExceptionOnRecordNumber;
}
public int getThrowExceptionOnRecordNumber() {
return throwExceptionOnRecordNumber;
}
public T read() throws Exception {
counter++;

View File

@@ -37,7 +37,11 @@
<property name="resource"
value="classpath:data/multilineJob/input/20070122.teststream.multilineStep.txt" />
<property name="lineTokenizer" ref="fixedFileDescriptor" />
<property name="fieldSetMapper" ref="tradeLineMapper" />
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.support.AggregateItemFieldSetMapper">
<property name="delegate" ref="tradeLineMapper" />
</bean>
</property>
<!-- <property name="validator" ref="fixedValidator" /> -->
</bean>

View File

@@ -60,9 +60,9 @@
class="org.springmodules.validation.valang.ValangValidator">
<property name="valang">
<value>
<![CDATA[
{ isin : length(?) < 13 : 'ISIN too long' : 'isin_length' : 12}
]]>
<![CDATA[
{ isin : length(?) < 13 : 'ISIN too long' : 'isin_length' : 12}
]]>
</value>
</property>
</bean>

View File

@@ -52,7 +52,6 @@ public class RestartFunctionalTests extends AbstractBatchLauncherTests {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/*
* (non-Javadoc)
* @see org.springframework.test.AbstractSingleSpringContextTests#onTearDown()
@@ -76,7 +75,7 @@ public class RestartFunctionalTests extends AbstractBatchLauncherTests {
int before = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
try {
runJob();
runJobForRestartTest();
fail("First run of the job is expected to fail.");
}
catch (UnexpectedJobExecutionException expected) {
@@ -89,7 +88,7 @@ public class RestartFunctionalTests extends AbstractBatchLauncherTests {
// assert based on commit interval = 2
assertEquals(before + 2, medium);
runJob();
runJobForRestartTest();
int after = jdbcTemplate.queryForInt("SELECT COUNT(*) FROM TRADE");
@@ -97,9 +96,11 @@ public class RestartFunctionalTests extends AbstractBatchLauncherTests {
}
// load the application context and launch the job
private void runJob() throws Exception {
private void runJobForRestartTest() throws Exception {
// The second time we run the job it needs to be a new instance so we
// need to make the parameters unique...
launcher.run(getJob(), new DefaultJobParametersConverter().getJobParameters(PropertiesConverter
.stringToProperties("restart=true")));
.stringToProperties("force.new.job.parameters=true")));
}
}

View File

@@ -1,16 +1,11 @@
package org.springframework.batch.sample.domain.trade.internal;
import static org.junit.Assert.assertEquals;
import java.math.BigDecimal;
import org.junit.Test;
import org.springframework.batch.item.file.mapping.DefaultFieldSet;
import org.springframework.batch.item.file.mapping.FieldSet;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.support.AggregateItemReader;
import org.springframework.batch.sample.domain.trade.Trade;
import org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper;
import org.springframework.batch.sample.support.AbstractFieldSetMapperTests;
public class TradeFieldSetMapperTests extends AbstractFieldSetMapperTests {
@@ -48,16 +43,4 @@ public class TradeFieldSetMapperTests extends AbstractFieldSetMapperTests {
return mapper;
}
@Test
public void testBeginRecord() throws Exception {
assertEquals(AggregateItemReader.BEGIN_RECORD, fieldSetMapper().mapLine(
new DefaultFieldSet(new String[] { "BEGIN" }), -1));
}
@Test
public void testEndRecord() throws Exception {
assertEquals(AggregateItemReader.END_RECORD, fieldSetMapper().mapLine(
new DefaultFieldSet(new String[] { "END" }), -1));
}
}