IN PROGRESS - BATCH-863: introduce LineMapper interface to encapsulate string-to-item mapping

ResourceLineReader now passes the line to LineMapper
This commit is contained in:
robokaso
2008-10-06 12:18:57 +00:00
parent 97a80463ed
commit 3ab9807775
8 changed files with 173 additions and 156 deletions

View File

@@ -1,169 +1,50 @@
package org.springframework.batch.item.file;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.CommonItemStreamItemReaderTests;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.file.mapping.LineMapper;
import org.springframework.batch.item.sample.Foo;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource;
/**
* Tests for {@link ResourceLineReader}.
*/
public class ResourceLineReaderTests {
public class ResourceLineReaderTests extends CommonItemStreamItemReaderTests{
private ResourceLineReader tested;
private static final Resource resource = new ByteArrayResource("line1\nline2\nline3\nline4\nline5".getBytes());
private ExecutionContext executionContext = new ExecutionContext();
private static ResourceLineReader getItemReader() {
ResourceLineReader result = new ResourceLineReader();
result.setResource(resource);
return result;
}
@Before
public void setUp() throws Exception {
tested = getItemReader();
tested.open(executionContext);
}
/**
* Regular scenario - read the input and eventually return null.
*/
@Test
public void testRead() throws Exception {
assertEquals("line1", tested.read());
assertEquals("line2", tested.read());
assertEquals("line3", tested.read());
assertEquals("line4", tested.read());
assertEquals("line5", tested.read());
assertNull(tested.read());
}
/**
* No input should be handled gracefully - null is returned on first
* read.
*/
@Test
public void testNoInput() throws Exception {
tested = getItemReader();
tested.setResource(new DescriptiveResource("doesn't exist") {
@Override
public boolean exists() {
return false;
}
});
tested.open(executionContext);
assertNull(tested.read());
}
/**
* Restart scenario - read items, update execution context, create new
* reader and restore from restart data - the new input source should
* continue where the old one finished.
*/
@Test
public void testRestart() throws Exception {
tested.update(executionContext);
assertEquals("line1", tested.read());
assertEquals("line2", tested.read());
tested.update(executionContext);
// create new input source
tested = getItemReader();
tested.open(executionContext);
assertEquals("line3", tested.read());
}
/**
* Restart scenario - read items, rollback to last marked position, update
* execution context, create new reader and restore from restart data - the
* new input source should continue where the old one finished.
*/
@Test
public void testResetAndRestart() throws Exception {
tested.update(executionContext);
assertEquals("line1", tested.read());
assertEquals("line2", tested.read());
tested.update(executionContext);
assertEquals("line3", tested.read());
// create new input source
tested = getItemReader();
tested.open(executionContext);
assertEquals("line3", tested.read());
}
@Test
public void testReopen() throws Exception {
tested.update(executionContext);
assertEquals("line1", tested.read());
assertEquals("line2", tested.read());
tested.update(executionContext);
tested.close(executionContext);
tested.open(executionContext);
assertEquals("line3", tested.read());
}
private static final String FOOS = "1 \n 2 \n 3 \n 4 \n 5 \n";
@Test
public void testRestartWithCustomRecordSeparatorPolicy() throws Exception {
tested.setRecordSeparatorPolicy(new RecordSeparatorPolicy() {
// 1 record = 2 lines
boolean pair = true;
public boolean isEndOfRecord(String line) {
pair = !pair;
return pair;
}
public String postProcess(String record) {
return record;
}
public String preProcess(String record) {
return record;
@Override
protected ItemReader<Foo> getItemReader() throws Exception {
ResourceLineReader<Foo> tested = new ResourceLineReader<Foo>();
Resource resource = new ByteArrayResource(FOOS.getBytes());
tested.setResource(resource);
tested.setLineMapper(new LineMapper<Foo>() {
public Foo mapLine(String line, int lineNumber) {
Foo foo = new Foo();
foo.setValue(Integer.valueOf(line.trim()));
return foo;
}
});
tested.open(executionContext);
assertEquals("line1line2", tested.read());
tested.update(executionContext);
tested.close(executionContext);
tested.open(executionContext);
tested.setSaveState(true);
tested.afterPropertiesSet();
return tested;
}
@Override
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
ResourceLineReader<Foo> reader = (ResourceLineReader<Foo>) tested;
reader.close(new ExecutionContext());
assertEquals("line3line4", tested.read());
reader.setResource(new ByteArrayResource("".getBytes()));
reader.afterPropertiesSet();
reader.open(new ExecutionContext());
}
}

View File

@@ -0,0 +1,39 @@
package org.springframework.batch.item.file.mapping;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.batch.item.file.transform.LineTokenizer;
/**
* Tests for {@link DefaultLineMapper}.
*/
public class DefaultLineMapperTests {
private DefaultLineMapper<String> tested = new DefaultLineMapper<String>();
@Test
public void testMapping() throws Exception {
final String line = "TEST";
final FieldSet fs = new DefaultFieldSet(new String[]{"token1", "token2"});
final String item = "ITEM";
LineTokenizer tokenizer = createStrictMock(LineTokenizer.class);
expect(tokenizer.process(line)).andReturn(fs);
replay(tokenizer);
@SuppressWarnings("unchecked")
FieldSetMapper<String> fsMapper = createStrictMock(FieldSetMapper.class);
expect(fsMapper.process(fs)).andReturn(item);
replay(fsMapper);
tested.setTokenizer(tokenizer);
tested.setFieldSetMapper(fsMapper);
assertSame(item, tested.mapLine(line, 1));
verify(tokenizer);
verify(fsMapper);
}
}

View File

@@ -0,0 +1,18 @@
package org.springframework.batch.item.file.mapping;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Tests for {@link PassThroughLineMapper}.
*/
public class PassThroughLineMapperTests {
private PassThroughLineMapper tested = new PassThroughLineMapper();
@Test
public void testMapLine() throws Exception {
assertSame("line", tested.mapLine("line", 1));
}
}