RESOLVED - issue BATCH-745: strong typing in AggregateItemReader
Added AggregateItem<T>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package org.springframework.batch.item.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
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;
|
||||
|
||||
public class AggregateItemFieldSetMapperTests {
|
||||
|
||||
private AggregateItemFieldSetMapper<String> mapper = new AggregateItemFieldSetMapper<String>();
|
||||
|
||||
@Test
|
||||
public void testDefaultBeginRecord() throws Exception {
|
||||
assertTrue(mapper.mapLine(new DefaultFieldSet(new String[] { "BEGIN" }), -1).isHeader());
|
||||
assertFalse(mapper.mapLine(new DefaultFieldSet(new String[] { "BEGIN" }), -1).isFooter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBeginRecord() throws Exception {
|
||||
mapper.setBegin("FOO");
|
||||
assertTrue(mapper.mapLine(new DefaultFieldSet(new String[] { "FOO" }), -1).isHeader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultEndRecord() throws Exception {
|
||||
assertFalse(mapper.mapLine(new DefaultFieldSet(new String[] { "END" }), -1).isHeader());
|
||||
assertTrue(mapper.mapLine(new DefaultFieldSet(new String[] { "END" }), -1).isFooter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetEndRecord() throws Exception {
|
||||
mapper.setEnd("FOO");
|
||||
assertTrue(mapper.mapLine(new DefaultFieldSet(new String[] { "FOO" }), -1).isFooter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMandatoryProperties() throws Exception {
|
||||
try {
|
||||
mapper.afterPropertiesSet();
|
||||
fail("Expected IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelegate() throws Exception {
|
||||
mapper.setDelegate(new FieldSetMapper<String>() {
|
||||
public String mapLine(FieldSet fs, int lineNum) {
|
||||
return "foo";
|
||||
}
|
||||
});
|
||||
assertEquals("foo", mapper.mapLine(new DefaultFieldSet(new String[] { "FOO" }), -1).getItem());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -12,27 +12,27 @@ import org.springframework.batch.item.ItemReader;
|
||||
|
||||
public class AggregateItemReaderTests {
|
||||
|
||||
private ItemReader<Object> input;
|
||||
private ItemReader<AggregateItem<String>> input;
|
||||
|
||||
private AggregateItemReader provider;
|
||||
private AggregateItemReader<String> provider;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// create mock for input
|
||||
input = new AbstractItemReader<Object>() {
|
||||
input = new AbstractItemReader<AggregateItem<String>>() {
|
||||
|
||||
private int count = 0;
|
||||
|
||||
public Object read() {
|
||||
public AggregateItem<String> read() {
|
||||
switch (count++) {
|
||||
case 0:
|
||||
return AggregateItemReader.BEGIN_RECORD;
|
||||
return AggregateItem.getHeader();
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
return "line";
|
||||
return new AggregateItem<String>("line");
|
||||
case 4:
|
||||
return AggregateItemReader.END_RECORD;
|
||||
return AggregateItem.getFooter();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -40,7 +40,7 @@ public class AggregateItemReaderTests {
|
||||
|
||||
};
|
||||
// create provider
|
||||
provider = new AggregateItemReader();
|
||||
provider = new AggregateItemReader<String>();
|
||||
provider.setItemReader(input);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.item.support;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class AggregateItemTests {
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.item.support.AggregateItem#getFooter()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetFooter() {
|
||||
assertTrue(AggregateItem.getFooter().isFooter());
|
||||
assertFalse(AggregateItem.getFooter().isHeader());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.item.support.AggregateItem#getHeader()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetHeader() {
|
||||
assertTrue(AggregateItem.getHeader().isHeader());
|
||||
assertFalse(AggregateItem.getHeader().isFooter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeginRecordHasNoItem() throws Exception {
|
||||
try {
|
||||
AggregateItem.getHeader().getItem();
|
||||
fail("Expected IllegalStateException");
|
||||
} catch(IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEndRecordHasNoItem() throws Exception {
|
||||
try {
|
||||
AggregateItem.getFooter().getItem();
|
||||
fail("Expected IllegalStateException");
|
||||
} catch(IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user