BATCH-1033: Moved matchPrefix method to PatternMatcher class. Also modified matchPrefix to ensure that the most specific prefix wins. Changed Prefix*LineMapper to have Prefix*LineTokenizer as a property.

This commit is contained in:
dhgarrette
2009-01-28 19:09:00 +00:00
parent c49b374f2d
commit 02151edd2b
6 changed files with 207 additions and 136 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.batch.item.file.transform.Name;
/**
* @author Dan Garrette
* @since 2.0
*/
public class PrefixMatchingCompositeLineMapperTests {
@@ -44,38 +45,6 @@ public class PrefixMatchingCompositeLineMapperTests {
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>();

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import org.junit.Test;
/**
* @author Ben Hale
* @author Dan Garrette
*/
public class PrefixMatchingCompositeLineTokenizerTests {
@@ -38,54 +39,6 @@ public class PrefixMatchingCompositeLineTokenizerTests {
tokenizer.tokenize("a line");
}
@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 LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "a" });
}
});
map.put("foo", new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "b" });
}
});
tokenizer.setTokenizers(map);
tokenizer.afterPropertiesSet();
FieldSet fields = tokenizer.tokenize(null);
assertEquals(1, fields.getFieldCount());
assertEquals("a", fields.readString(0));
}
@Test
public void testNullKey() throws Exception {
Map<String, LineTokenizer> map = new HashMap<String, LineTokenizer>();
map.put(null, new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "a" });
}
});
map.put("foo", new LineTokenizer() {
public FieldSet tokenize(String line) {
return new DefaultFieldSet(new String[] { "b" });
}
});
tokenizer.setTokenizers(map);
tokenizer.afterPropertiesSet();
FieldSet fields = tokenizer.tokenize("foo");
assertEquals(1, fields.getFieldCount());
assertEquals("b", fields.readString(0));
}
@Test
public void testEmptyKeyMatchesAnyLine() throws Exception {
Map<String, LineTokenizer> map = new HashMap<String, LineTokenizer>();

View File

@@ -0,0 +1,112 @@
/*
* 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.support;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
/**
* @author Dan Garrette
* @since 2.0
*/
public class PatternMatcherTests {
private static Map<String, Integer> map = new HashMap<String, Integer>();
static {
map.put("an", 3);
map.put("a", 2);
map.put("big", 4);
}
private static Map<String, Integer> defaultMap = new HashMap<String, Integer>();
static {
defaultMap.put("an", 3);
defaultMap.put("a", 2);
defaultMap.put("big", 4);
defaultMap.put("", 1);
}
@Test
public void testMatch_noWildcard_yes() {
assertTrue(PatternMatcher.match("abc", "abc"));
}
@Test
public void testMatch_noWildcard_no() {
assertFalse(PatternMatcher.match("abc", "ab"));
}
@Test
public void testMatch_qMark_yes() {
assertTrue(PatternMatcher.match("a?c", "abc"));
}
@Test
public void testMatch_qMark_no() {
assertFalse(PatternMatcher.match("a?c", "ab"));
}
@Test
public void testMatch_star_yes() {
assertTrue(PatternMatcher.match("a*c", "abdegc"));
}
@Test
public void testMatch_star_no() {
assertFalse(PatternMatcher.match("a*c", "abdeg"));
}
@Test
public void testMatchPrefix_subsumed() {
assertEquals(2, PatternMatcher.matchPrefix("apple", map).intValue());
}
@Test
public void testMatchPrefix_subsuming() {
assertEquals(3, PatternMatcher.matchPrefix("animal", map).intValue());
}
@Test
public void testMatchPrefix_unrelated() {
assertEquals(4, PatternMatcher.matchPrefix("biggest", map).intValue());
}
@Test(expected = IllegalStateException.class)
public void testMatchPrefix_noMatch() {
PatternMatcher.matchPrefix("bat", map);
}
@Test
public void testMatchPrefix_defaultValue_unrelated() {
assertEquals(4, PatternMatcher.matchPrefix("biggest", defaultMap).intValue());
}
@Test
public void testMatchPrefix_defaultValue_emptyString() {
assertEquals(1, PatternMatcher.matchPrefix("", defaultMap).intValue());
}
@Test
public void testMatchPrefix_defaultValue_noMatch() {
assertEquals(1, PatternMatcher.matchPrefix("bat", defaultMap).intValue());
}
}