REOPENED - BATCH-745: strong typing in AggregateItemReader
moved AggregateItemReader to samples
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
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.process(new DefaultFieldSet(new String[] { "BEGIN" })).isHeader());
|
||||
assertFalse(mapper.process(new DefaultFieldSet(new String[] { "BEGIN" })).isFooter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBeginRecord() throws Exception {
|
||||
mapper.setBegin("FOO");
|
||||
assertTrue(mapper.process(new DefaultFieldSet(new String[] { "FOO" })).isHeader());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultEndRecord() throws Exception {
|
||||
assertFalse(mapper.process(new DefaultFieldSet(new String[] { "END" })).isHeader());
|
||||
assertTrue(mapper.process(new DefaultFieldSet(new String[] { "END" })).isFooter());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetEndRecord() throws Exception {
|
||||
mapper.setEnd("FOO");
|
||||
assertTrue(mapper.process(new DefaultFieldSet(new String[] { "FOO" })).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 process(FieldSet fs) {
|
||||
return "foo";
|
||||
}
|
||||
});
|
||||
assertEquals("foo", mapper.process(new DefaultFieldSet(new String[] { "FOO" })).getItem());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
package org.springframework.batch.item.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
|
||||
public class AggregateItemReaderTests {
|
||||
|
||||
private ItemReader<AggregateItem<String>> input;
|
||||
|
||||
private AggregateItemReader<String> provider;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// create mock for input
|
||||
input = new ItemReader<AggregateItem<String>>() {
|
||||
|
||||
private int count = 0;
|
||||
|
||||
public AggregateItem<String> read() {
|
||||
switch (count++) {
|
||||
case 0:
|
||||
return AggregateItem.getHeader();
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
return new AggregateItem<String>("line");
|
||||
case 4:
|
||||
return AggregateItem.getFooter();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
// create provider
|
||||
provider = new AggregateItemReader<String>();
|
||||
provider.setItemReader(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNext() throws Exception {
|
||||
// read object
|
||||
Object result = provider.read();
|
||||
|
||||
// it should be collection of 3 strings "line"
|
||||
assertTrue(result instanceof Collection);
|
||||
Collection<?> lines = (Collection<?>) result;
|
||||
assertEquals(3, lines.size());
|
||||
|
||||
for (Object line : lines) {
|
||||
assertEquals("line", line);
|
||||
}
|
||||
|
||||
// read object again - it should return null
|
||||
assertNull(provider.read());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,68 +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
|
||||
*
|
||||
* 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