BATCH-1033: Created PrefixMatchingCompositeLineMapper and reorganized PrefixMatchingCompositeLineTokenizer so that its lookup functionality could be reused. Also updated the multiRecordType sample by removing PrefixMatchingCompositeFieldSetMapper in favor of the new class.
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* 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.file.mapping;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.file.transform.DefaultFieldSet;
|
||||
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
||||
import org.springframework.batch.item.file.transform.FieldSet;
|
||||
import org.springframework.batch.item.file.transform.LineTokenizer;
|
||||
import org.springframework.batch.item.file.transform.Name;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
*/
|
||||
public class PrefixMatchingCompositeLineMapperTests {
|
||||
|
||||
private PrefixMatchingCompositeLineMapper<Name> mapper = new PrefixMatchingCompositeLineMapper<Name>();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_NoMappers() throws Exception {
|
||||
mapper.setTokenizers(Collections.singletonMap("", (LineTokenizer) new DelimitedLineTokenizer()));
|
||||
Map<String, FieldSetMapper<Name>> fieldSetMappers = Collections.emptyMap();
|
||||
mapper.setFieldSetMappers(fieldSetMappers);
|
||||
mapper.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_NullLine() throws Exception {
|
||||
Map<String, LineTokenizer> tokenizers = new HashMap<String, LineTokenizer>();
|
||||
tokenizers.put(null, new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return new DefaultFieldSet(new String[] { "a", "b" });
|
||||
}
|
||||
});
|
||||
tokenizers.put("bar", new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return new DefaultFieldSet(new String[] { "c", "d" });
|
||||
}
|
||||
});
|
||||
mapper.setTokenizers(tokenizers);
|
||||
|
||||
Map<String, FieldSetMapper<Name>> fieldSetMappers = new HashMap<String, FieldSetMapper<Name>>();
|
||||
fieldSetMappers.put(null, new FieldSetMapper<Name>() {
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(0), fs.readString(1), 0);
|
||||
}
|
||||
});
|
||||
fieldSetMappers.put("bar", new FieldSetMapper<Name>() {
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(1), fs.readString(0), 0);
|
||||
}
|
||||
});
|
||||
mapper.setFieldSetMappers(fieldSetMappers);
|
||||
|
||||
Name name = mapper.mapLine(null, 1);
|
||||
assertEquals(new Name("a", "b", 0), name);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_KeyFound() throws Exception {
|
||||
Map<String, LineTokenizer> tokenizers = new HashMap<String, LineTokenizer>();
|
||||
tokenizers.put("foo", new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return new DefaultFieldSet(new String[] { "a", "b" });
|
||||
}
|
||||
});
|
||||
tokenizers.put("bar", new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return new DefaultFieldSet(new String[] { "c", "d" });
|
||||
}
|
||||
});
|
||||
mapper.setTokenizers(tokenizers);
|
||||
|
||||
Map<String, FieldSetMapper<Name>> fieldSetMappers = new HashMap<String, FieldSetMapper<Name>>();
|
||||
fieldSetMappers.put("foo", new FieldSetMapper<Name>() {
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(0), fs.readString(1), 0);
|
||||
}
|
||||
});
|
||||
fieldSetMappers.put("bar", new FieldSetMapper<Name>() {
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(1), fs.readString(0), 0);
|
||||
}
|
||||
});
|
||||
mapper.setFieldSetMappers(fieldSetMappers);
|
||||
|
||||
Name name = mapper.mapLine("bar", 1);
|
||||
assertEquals(new Name("d", "c", 0), name);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void test_MapperKeyNotFound() throws Exception {
|
||||
Map<String, LineTokenizer> tokenizers = new HashMap<String, LineTokenizer>();
|
||||
tokenizers.put("foo", new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return new DefaultFieldSet(new String[] { "a", "b" });
|
||||
}
|
||||
});
|
||||
tokenizers.put("bar", new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return new DefaultFieldSet(new String[] { "c", "d" });
|
||||
}
|
||||
});
|
||||
mapper.setTokenizers(tokenizers);
|
||||
|
||||
Map<String, FieldSetMapper<Name>> fieldSetMappers = new HashMap<String, FieldSetMapper<Name>>();
|
||||
fieldSetMappers.put("foo", new FieldSetMapper<Name>() {
|
||||
public Name mapFieldSet(FieldSet fs) {
|
||||
return new Name(fs.readString(0), fs.readString(1), 0);
|
||||
}
|
||||
});
|
||||
mapper.setFieldSetMappers(fieldSetMappers);
|
||||
|
||||
Name name = mapper.mapLine("bar", 1);
|
||||
assertEquals(new Name("d", "c", 0), name);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.springframework.batch.item.file.transform;
|
||||
|
||||
import org.apache.commons.lang.builder.EqualsBuilder;
|
||||
|
||||
public class Name {
|
||||
private String first;
|
||||
private String last;
|
||||
@@ -38,4 +40,8 @@ public class Name {
|
||||
public void setBorn(int born) {
|
||||
this.born = born;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
return EqualsBuilder.reflectionEquals(this, o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,33 +16,52 @@
|
||||
|
||||
package org.springframework.batch.item.file.transform;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
*/
|
||||
public class PrefixMatchingCompositeLineTokenizerTests {
|
||||
|
||||
public class PrefixMatchingCompositeLineTokenizerTests extends TestCase {
|
||||
private PrefixMatchingCompositeLineTokenizer tokenizer = new PrefixMatchingCompositeLineTokenizer();
|
||||
|
||||
PrefixMatchingCompositeLineTokenizer tokenizer = new PrefixMatchingCompositeLineTokenizer();
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNoTokenizers() throws Exception {
|
||||
try {
|
||||
tokenizer.tokenize("a line");
|
||||
fail("Expected IllegalStateException");
|
||||
} catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
tokenizer.afterPropertiesSet();
|
||||
tokenizer.tokenize("a line");
|
||||
}
|
||||
|
||||
public void testNullLine() throws Exception {
|
||||
tokenizer.setTokenizers(Collections.singletonMap("foo", (LineTokenizer) new DelimitedLineTokenizer()));
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testNullLineNoKey() throws Exception {
|
||||
tokenizer.setTokenizers(Collections.singletonMap("foo", (LineTokenizer) new DelimitedLineTokenizer()));
|
||||
tokenizer.afterPropertiesSet();
|
||||
FieldSet fields = tokenizer.tokenize(null);
|
||||
assertEquals(0, fields.getFieldCount());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNullLineWithKey() throws Exception {
|
||||
Map<String, LineTokenizer> map = new HashMap<String, LineTokenizer>();
|
||||
map.put(null, new DelimitedLineTokenizer());
|
||||
map.put("foo", new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
tokenizer.setTokenizers(map);
|
||||
tokenizer.afterPropertiesSet();
|
||||
FieldSet fields = tokenizer.tokenize(null);
|
||||
assertEquals(0, fields.getFieldCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyKeyMatchesAnyLine() throws Exception {
|
||||
Map<String, LineTokenizer> map = new HashMap<String, LineTokenizer>();
|
||||
map.put("", new DelimitedLineTokenizer());
|
||||
@@ -51,13 +70,15 @@ public class PrefixMatchingCompositeLineTokenizerTests extends TestCase {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
tokenizer.setTokenizers(map);
|
||||
tokenizer.setTokenizers(map);
|
||||
tokenizer.afterPropertiesSet();
|
||||
FieldSet fields = tokenizer.tokenize("abc");
|
||||
assertEquals(1, fields.getFieldCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyKeyDoesNotMatchWhenAlternativeAvailable() throws Exception {
|
||||
|
||||
|
||||
Map<String, LineTokenizer> map = new LinkedHashMap<String, LineTokenizer>();
|
||||
map.put("", new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
@@ -65,27 +86,27 @@ public class PrefixMatchingCompositeLineTokenizerTests extends TestCase {
|
||||
}
|
||||
});
|
||||
map.put("foo", new DelimitedLineTokenizer());
|
||||
tokenizer.setTokenizers(map);
|
||||
tokenizer.setTokenizers(map);
|
||||
tokenizer.afterPropertiesSet();
|
||||
FieldSet fields = tokenizer.tokenize("foo,bar");
|
||||
assertEquals("bar", fields.readString(1));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testNoMatch() throws Exception {
|
||||
tokenizer.setTokenizers(Collections.singletonMap("foo", (LineTokenizer) new DelimitedLineTokenizer()));
|
||||
try {
|
||||
tokenizer.tokenize("nomatch");
|
||||
fail("Expected IllegalStateException");
|
||||
} catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
tokenizer.setTokenizers(Collections.singletonMap("foo", (LineTokenizer) new DelimitedLineTokenizer()));
|
||||
tokenizer.afterPropertiesSet();
|
||||
tokenizer.tokenize("nomatch");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMatchWithPrefix() throws Exception {
|
||||
tokenizer.setTokenizers(Collections.singletonMap("foo", (LineTokenizer) new LineTokenizer() {
|
||||
public FieldSet tokenize(String line) {
|
||||
return new DefaultFieldSet(new String[] {line});
|
||||
return new DefaultFieldSet(new String[] { line });
|
||||
}
|
||||
}));
|
||||
tokenizer.afterPropertiesSet();
|
||||
FieldSet fields = tokenizer.tokenize("foo bar");
|
||||
assertEquals(1, fields.getFieldCount());
|
||||
assertEquals("foo bar", fields.readString(0));
|
||||
|
||||
Reference in New Issue
Block a user