BATCH-700:LineTokenizer implementations are now consistent in their handling of incorrect data.

This commit is contained in:
lucasward
2008-07-04 03:35:28 +00:00
parent e748dad82e
commit 3757b239d0
7 changed files with 254 additions and 53 deletions

View File

@@ -68,19 +68,24 @@ public class DelimitedLineTokenizerTests extends TestCase {
tokenizer.setNames(new String[] {"A", "B"});
try {
tokenizer.tokenize("a,b,c");
fail("Expected IllegalArgumentException");
fail("Expected IncorrectTokenCountException");
}
catch (IllegalArgumentException e) {
// expected
catch (IncorrectTokenCountException e) {
assertEquals(2, e.getExpectedCount());
assertEquals(3, e.getActualCount());
}
}
public void testTooManyNames() {
tokenizer.setNames(new String[] {"A", "B", "C", "D"});
FieldSet line = tokenizer.tokenize("a,b,c");
assertEquals(4, line.getFieldCount());
assertEquals("c", line.readString("C"));
assertEquals(null, line.readString("D"));
try{
tokenizer.tokenize("a,b,c");
}
catch(IncorrectTokenCountException e){
assertEquals(4, e.getExpectedCount());
assertEquals(3, e.getActualCount());
}
}
public void testDelimitedLineTokenizerChar() {
@@ -146,6 +151,18 @@ public class DelimitedLineTokenizerTests extends TestCase {
FieldSet line = tokenizer.tokenize("");
assertEquals(0, line.getFieldCount());
}
public void testEmptyLineWithNames(){
tokenizer.setNames(new String[]{"A", "B"});
try{
tokenizer.tokenize("");
}
catch(IncorrectTokenCountException ex){
assertEquals(2, ex.getExpectedCount());
assertEquals(0, ex.getActualCount());
}
}
public void testWhitespaceLine() throws Exception {
FieldSet line = tokenizer.tokenize(" ");

View File

@@ -32,72 +32,79 @@ public class FixedLengthTokenizerTests extends TestCase {
*/
public void testTokenizeEmptyString() {
tokenizer.setColumns(new Range[] {new Range(1,5),new Range(6,10),new Range(11,15)});
FieldSet tokens = tokenizer.tokenize("");
assertEquals(0, tokens.getFieldCount());
try{
tokenizer.tokenize("");
}
catch(IncorrectLineLengthException ex){
assertEquals(15, ex.getExpectedLength());
assertEquals(0, ex.getActualLength());
}
}
public void testEmptyStringWithNoRanges(){
tokenizer.setColumns(new Range[]{});
tokenizer.tokenize("");
}
public void testTokenizeSmallerStringThanRanges() {
tokenizer.setColumns(new Range[] {new Range(1,5),new Range(6,10),new Range(11,15)});
FieldSet tokens = tokenizer.tokenize("12345");
assertEquals(3, tokens.getFieldCount());
assertEquals("12345", tokens.readString(0));
assertEquals("", tokens.readString(1));
assertEquals("", tokens.readString(2));
try{
tokenizer.tokenize("12345");
}
catch(IncorrectLineLengthException ex){
assertEquals(15, ex.getExpectedLength());
assertEquals(5, ex.getActualLength());
}
}
public void testTokenizeNullString() {
tokenizer.setColumns(new Range[] {new Range(1,5),new Range(6,10),new Range(11,15)});
FieldSet tokens = tokenizer.tokenize(null);
assertEquals(0, tokens.getFieldCount());
try{
tokenizer.tokenize(null);
}
catch(IncorrectLineLengthException ex){}
}
public void testTokenizeRegularUse() {
tokenizer.setColumns(new Range[] {new Range(1,2),new Range(3,7),new Range(8,12)});
// test shorter line as defined by record descriptor
line = "H1";
line = "H11234512345";
FieldSet tokens = tokenizer.tokenize(line);
assertEquals(3, tokens.getFieldCount());
assertEquals("H1", tokens.readString(0));
assertEquals("", tokens.readString(1));
assertEquals("", tokens.readString(2));
assertEquals("12345", tokens.readString(1));
assertEquals("12345", tokens.readString(2));
}
public void testNormalLength() throws Exception {
tokenizer.setColumns(new Range[] {new Range(1,10),new Range(11,25),new Range(26,30)});
// test shorter line as defined by record descriptor
line = "H1";
FieldSet tokens = tokenizer.tokenize(line);
// test normal length
line = "H1 12345678 12345";
tokens = tokenizer.tokenize(line);
FieldSet tokens = tokenizer.tokenize(line);
assertEquals(3, tokens.getFieldCount());
assertEquals(line.substring(0, 10).trim(), tokens.readString(0));
assertEquals(line.substring(10, 25).trim(), tokens.readString(1));
assertEquals(line.substring(25).trim(), tokens.readString(2));
}
public void testLongerLinesRestIgnored() throws Exception {
public void testLongerLines() throws Exception {
tokenizer.setColumns(new Range[] {new Range(1,10),new Range(11,25),new Range(26,30)});
// test shorter line as defined by record descriptor
line = "H1";
FieldSet tokens = tokenizer.tokenize(line);
// test longer lines => rest will be ignored
line = "H1 12345678 1234567890";
tokens = tokenizer.tokenize(line);
assertEquals(3, tokens.getFieldCount());
assertEquals(line.substring(0, 10).trim(), tokens.readString(0));
assertEquals(line.substring(10, 25).trim(), tokens.readString(1));
assertEquals(line.substring(25, 30).trim(), tokens.readString(2));
try{
tokenizer.tokenize(line);
}
catch(IncorrectLineLengthException ex){
assertEquals(30, ex.getExpectedLength());
assertEquals(35, ex.getActualLength());
}
}
public void testNonAdjacentRangesUnsorted() throws Exception {
tokenizer.setColumns(new Range[] {new Range(14,28), new Range(34,38), new Range(1,10)});
// test shorter line as defined by record descriptor
line = "H1";
FieldSet tokens = tokenizer.tokenize(line);
// test normal length
line = "H1 +++12345678 +++++12345+++";
tokens = tokenizer.tokenize(line);
line = "H1 +++12345678 +++++12345";
FieldSet tokens = tokenizer.tokenize(line);
assertEquals(3, tokens.getFieldCount());
assertEquals(line.substring(0, 10).trim(), tokens.readString(2));
assertEquals(line.substring(13, 28).trim(), tokens.readString(0));
@@ -106,12 +113,9 @@ public class FixedLengthTokenizerTests extends TestCase {
public void testAnotherTypeOfRecord() throws Exception {
tokenizer.setColumns(new Range[] {new Range(1,5),new Range(6,15),new Range(16,25),new Range(26,27)});
// test shorter line as defined by record descriptor
line = "H1";
FieldSet tokens = tokenizer.tokenize(line);
// test another type of record
line = "H2 123456 12345 12";
tokens = tokenizer.tokenize(line);
FieldSet tokens = tokenizer.tokenize(line);
assertEquals(4, tokens.getFieldCount());
assertEquals(line.substring(0, 5).trim(), tokens.readString(0));
assertEquals(line.substring(5, 15).trim(), tokens.readString(1));
@@ -121,14 +125,15 @@ public class FixedLengthTokenizerTests extends TestCase {
public void testTokenizerInvalidSetup() {
tokenizer.setNames(new String[] {"a", "b"});
tokenizer.setColumns(new Range[] {new Range(1,5),new Range(6,15),new Range(16,25),new Range(26,27)});
tokenizer.setColumns(new Range[] {new Range(1,5)});
try {
tokenizer.tokenize("Test tokenize");
tokenizer.tokenize("12345");
fail("Exception was expected: too few names provided");
}
catch (Exception e) {
assertTrue(true);
catch (IncorrectTokenCountException e) {
assertEquals(2, e.getExpectedCount());
assertEquals(1, e.getActualCount());
}
}