Better unit test coverage for StrTokenizer.java.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@226712 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2005-08-01 01:14:57 +00:00
parent e47b5d8e75
commit 2b6e554ccf
2 changed files with 168 additions and 172 deletions

View File

@ -223,6 +223,15 @@ public class StrTokenizer implements ListIterator, Cloneable {
//-----------------------------------------------------------------------
/**
* Returns a clone of <code>CSV_TOKENIZER_PROTOTYPE</code>.
*
* @return a clone of <code>CSV_TOKENIZER_PROTOTYPE</code>.
*/
private static StrTokenizer getCSVClone() {
return (StrTokenizer) CSV_TOKENIZER_PROTOTYPE.clone();
}
/**
* Gets a new tokenizer instance which parses Comma Seperated Value strings
* initializing it with the given input. The default for CSV processing
@ -233,7 +242,7 @@ public class StrTokenizer implements ListIterator, Cloneable {
* @return a new tokenizer instance which parses Comma Seperated Value strings
*/
public static StrTokenizer getCSVInstance() {
return (StrTokenizer)CSV_TOKENIZER_PROTOTYPE.clone();
return getCSVClone();
}
/**
@ -246,7 +255,7 @@ public class StrTokenizer implements ListIterator, Cloneable {
* @return a new tokenizer instance which parses Comma Seperated Value strings
*/
public static StrTokenizer getCSVInstance(String input) {
StrTokenizer tok = (StrTokenizer)(CSV_TOKENIZER_PROTOTYPE.clone());
StrTokenizer tok = getCSVClone();
tok.reset(input);
return tok;
}
@ -261,11 +270,21 @@ public class StrTokenizer implements ListIterator, Cloneable {
* @return a new tokenizer instance which parses Comma Seperated Value strings
*/
public static StrTokenizer getCSVInstance(char[] input) {
StrTokenizer tok = (StrTokenizer)(CSV_TOKENIZER_PROTOTYPE.clone());
StrTokenizer tok = getCSVClone();
tok.reset(input);
return tok;
}
/**
* Returns a clone of <code>TSV_TOKENIZER_PROTOTYPE</code>.
*
* @return a clone of <code>TSV_TOKENIZER_PROTOTYPE</code>.
*/
private static StrTokenizer getTSVClone() {
return (StrTokenizer) TSV_TOKENIZER_PROTOTYPE.clone();
}
/**
* Gets a new tokenizer instance which parses Tab Seperated Value strings.
* The default for CSV processing will be trim whitespace from both ends
@ -275,7 +294,7 @@ public class StrTokenizer implements ListIterator, Cloneable {
* @return a new tokenizer instance which parses Tab Seperated Value strings.
*/
public static StrTokenizer getTSVInstance() {
return (StrTokenizer)TSV_TOKENIZER_PROTOTYPE.clone();
return getTSVClone();
}
/**
@ -286,7 +305,7 @@ public class StrTokenizer implements ListIterator, Cloneable {
* @return a new tokenizer instance which parses Tab Seperated Value strings.
*/
public static StrTokenizer getTSVInstance(String input) {
StrTokenizer tok = (StrTokenizer)(TSV_TOKENIZER_PROTOTYPE.clone());
StrTokenizer tok = getTSVClone();
tok.reset(input);
return tok;
}
@ -299,7 +318,7 @@ public class StrTokenizer implements ListIterator, Cloneable {
* @return a new tokenizer instance which parses Tab Seperated Value strings.
*/
public static StrTokenizer getTSVInstance(char[] input) {
StrTokenizer tok = (StrTokenizer)(TSV_TOKENIZER_PROTOTYPE.clone());
StrTokenizer tok = getTSVClone();
tok.reset(input);
return tok;
}

View File

@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang.text;
import junit.framework.Test;
@ -24,17 +25,17 @@ import org.apache.commons.lang.ObjectUtils;
/**
* Unit test for Tokenizer.
*
*
* @author Matthew Inger
*/
public class StrTokenizerTest extends TestCase {
/**
* JUnit constructor.
* @param name
*/
public StrTokenizerTest(String name) {
super(name);
private static final String CSV_SIMPLE_FIXTURE = "A,b,c";
private static final String TSV_SIMPLE_FIXTURE = "A\tb\tc";
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
@ -43,12 +44,21 @@ public class StrTokenizerTest extends TestCase {
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
/**
* JUnit constructor.
*
* @param name
*/
public StrTokenizerTest(String name) {
super(name);
}
//-----------------------------------------------------------------------
private void checkClone(StrTokenizer tokenizer) {
assertFalse(StrTokenizer.getCSVInstance() == tokenizer);
assertFalse(StrTokenizer.getTSVInstance() == tokenizer);
}
// -----------------------------------------------------------------------
public void test1() {
String input = "a;b;c;\"d;\"\"e\";f; ; ;";
@ -57,31 +67,18 @@ public class StrTokenizerTest extends TestCase {
tok.setQuoteChar('"');
tok.setIgnoredMatcher(StrTokenizer.TRIM_MATCHER);
tok.setIgnoreEmptyTokens(false);
String tokens [] = tok.getAllTokens();
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"b",
"c",
"d;\"e",
"f",
"",
"",
"",
};
String expected[] = new String[]{"a", "b", "c", "d;\"e", "f", "", "", "",};
assertTrue(tokens.length == expected.length);
for (int i = 0; i < expected.length; i++) {
assertTrue("token[" + i + "] was '" + tokens[i]
+ "' but was expected to be '" + expected[i]
+ "'",
assertTrue("token[" + i + "] was '" + tokens[i] + "' but was expected to be '" + expected[i] + "'",
ObjectUtils.equals(expected[i], tokens[i]));
}
}
public void test2() {
String input = "a;b;c ;\"d;\"\"e\";f; ; ;";
@ -90,31 +87,18 @@ public class StrTokenizerTest extends TestCase {
tok.setQuoteChar('"');
tok.setIgnoredMatcher(StrTokenizer.NONE_MATCHER);
tok.setIgnoreEmptyTokens(false);
String tokens [] = tok.getAllTokens();
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"b",
"c ",
"d;\"e",
"f",
" ",
" ",
"",
};
String expected[] = new String[]{"a", "b", "c ", "d;\"e", "f", " ", " ", "",};
assertTrue(tokens.length == expected.length);
for (int i = 0; i < expected.length; i++) {
assertTrue("token[" + i + "] was '" + tokens[i]
+ "' but was expected to be '" + expected[i]
+ "'",
assertTrue("token[" + i + "] was '" + tokens[i] + "' but was expected to be '" + expected[i] + "'",
ObjectUtils.equals(expected[i], tokens[i]));
}
}
public void test3() {
String input = "a;b; c;\"d;\"\"e\";f; ; ;";
@ -123,31 +107,18 @@ public class StrTokenizerTest extends TestCase {
tok.setQuoteChar('"');
tok.setIgnoredMatcher(StrTokenizer.NONE_MATCHER);
tok.setIgnoreEmptyTokens(false);
String tokens [] = tok.getAllTokens();
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"b",
" c",
"d;\"e",
"f",
" ",
" ",
"",
};
String expected[] = new String[]{"a", "b", " c", "d;\"e", "f", " ", " ", "",};
assertTrue(tokens.length == expected.length);
for (int i = 0; i < expected.length; i++) {
assertTrue("token[" + i + "] was '" + tokens[i]
+ "' but was expected to be '" + expected[i]
+ "'",
assertTrue("token[" + i + "] was '" + tokens[i] + "' but was expected to be '" + expected[i] + "'",
ObjectUtils.equals(expected[i], tokens[i]));
}
}
public void test4() {
String input = "a;b; c;\"d;\"\"e\";f; ; ;";
@ -156,28 +127,18 @@ public class StrTokenizerTest extends TestCase {
tok.setQuoteChar('"');
tok.setIgnoredMatcher(StrTokenizer.TRIM_MATCHER);
tok.setIgnoreEmptyTokens(true);
String tokens [] = tok.getAllTokens();
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"b",
"c",
"d;\"e",
"f",
};
String expected[] = new String[]{"a", "b", "c", "d;\"e", "f",};
assertTrue(tokens.length == expected.length);
for (int i = 0; i < expected.length; i++) {
assertTrue("token[" + i + "] was '" + tokens[i]
+ "' but was expected to be '" + expected[i]
+ "'",
assertTrue("token[" + i + "] was '" + tokens[i] + "' but was expected to be '" + expected[i] + "'",
ObjectUtils.equals(expected[i], tokens[i]));
}
}
public void test5() {
String input = "a;b; c;\"d;\"\"e\";f; ; ;";
@ -187,31 +148,18 @@ public class StrTokenizerTest extends TestCase {
tok.setIgnoredMatcher(StrTokenizer.TRIM_MATCHER);
tok.setIgnoreEmptyTokens(false);
tok.setEmptyTokenAsNull(true);
String tokens [] = tok.getAllTokens();
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"b",
"c",
"d;\"e",
"f",
null,
null,
null,
};
String expected[] = new String[]{"a", "b", "c", "d;\"e", "f", null, null, null,};
assertTrue(tokens.length == expected.length);
for (int i = 0; i < expected.length; i++) {
assertTrue("token[" + i + "] was '" + tokens[i]
+ "' but was expected to be '" + expected[i]
+ "'",
assertTrue("token[" + i + "] was '" + tokens[i] + "' but was expected to be '" + expected[i] + "'",
ObjectUtils.equals(expected[i], tokens[i]));
}
}
public void test6() {
String input = "a;b; c;\"d;\"\"e\";f; ; ;";
@ -220,20 +168,10 @@ public class StrTokenizerTest extends TestCase {
tok.setQuoteChar('"');
tok.setIgnoredMatcher(StrTokenizer.TRIM_MATCHER);
tok.setIgnoreEmptyTokens(false);
// tok.setTreatingEmptyAsNull(true);
String tokens [] = tok.getAllTokens();
// tok.setTreatingEmptyAsNull(true);
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"b",
" c",
"d;\"e",
"f",
null,
null,
null,
};
String expected[] = new String[]{"a", "b", " c", "d;\"e", "f", null, null, null,};
int nextCount = 0;
while (tok.hasNext()) {
@ -249,17 +187,14 @@ public class StrTokenizerTest extends TestCase {
assertTrue(tokens.length == expected.length);
assertTrue("could not cycle through entire token list"
+ " using the 'hasNext' and 'next' methods",
assertTrue("could not cycle through entire token list" + " using the 'hasNext' and 'next' methods",
nextCount == expected.length);
assertTrue("could not cycle through entire token list"
+ " using the 'hasPrevious' and 'previous' methods",
assertTrue("could not cycle through entire token list" + " using the 'hasPrevious' and 'previous' methods",
prevCount == expected.length);
}
public void test7() {
String input = "a b c \"d e\" f ";
@ -268,31 +203,18 @@ public class StrTokenizerTest extends TestCase {
tok.setQuoteMatcher(StrTokenizer.DOUBLE_QUOTE_MATCHER);
tok.setIgnoredMatcher(StrTokenizer.NONE_MATCHER);
tok.setIgnoreEmptyTokens(false);
String tokens [] = tok.getAllTokens();
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"",
"",
"b",
"c",
"d e",
"f",
"",
};
String expected[] = new String[]{"a", "", "", "b", "c", "d e", "f", "",};
assertTrue(tokens.length == expected.length);
for (int i = 0; i < expected.length; i++) {
assertTrue("token[" + i + "] was '" + tokens[i]
+ "' but was expected to be '" + expected[i]
+ "'",
assertTrue("token[" + i + "] was '" + tokens[i] + "' but was expected to be '" + expected[i] + "'",
ObjectUtils.equals(expected[i], tokens[i]));
}
}
public void test8() {
String input = "a b c \"d e\" f ";
@ -301,22 +223,13 @@ public class StrTokenizerTest extends TestCase {
tok.setQuoteMatcher(StrTokenizer.DOUBLE_QUOTE_MATCHER);
tok.setIgnoredMatcher(StrTokenizer.NONE_MATCHER);
tok.setIgnoreEmptyTokens(true);
String tokens [] = tok.getAllTokens();
String tokens[] = tok.getAllTokens();
String expected[] = new String[]
{
"a",
"b",
"c",
"d e",
"f",
};
String expected[] = new String[]{"a", "b", "c", "d e", "f",};
assertTrue(tokens.length == expected.length);
for (int i = 0; i < expected.length; i++) {
assertTrue("token[" + i + "] was '" + tokens[i]
+ "' but was expected to be '" + expected[i]
+ "'",
assertTrue("token[" + i + "] was '" + tokens[i] + "' but was expected to be '" + expected[i] + "'",
ObjectUtils.equals(expected[i], tokens[i]));
}
@ -329,7 +242,7 @@ public class StrTokenizerTest extends TestCase {
assertEquals("b", tok.next());
assertEquals("c", tok.next());
}
public void testBasic2() {
String input = "a \nb\fc";
StrTokenizer tok = new StrTokenizer(input);
@ -337,7 +250,7 @@ public class StrTokenizerTest extends TestCase {
assertEquals("b", tok.next());
assertEquals("c", tok.next());
}
public void testBasic3() {
String input = "a \nb\u0001\fc";
StrTokenizer tok = new StrTokenizer(input);
@ -345,7 +258,7 @@ public class StrTokenizerTest extends TestCase {
assertEquals("b\u0001", tok.next());
assertEquals("c", tok.next());
}
public void testBasic4() {
String input = "a \"b\" c";
StrTokenizer tok = new StrTokenizer(input);
@ -353,15 +266,7 @@ public class StrTokenizerTest extends TestCase {
assertEquals("\"b\"", tok.next());
assertEquals("c", tok.next());
}
public void testBasicQuoted1() {
String input = "a \"b\" c";
StrTokenizer tok = new StrTokenizer(input, ' ', '"');
assertEquals("a", tok.next());
assertEquals("b", tok.next());
assertEquals("c", tok.next());
}
public void testBasicDelim1() {
String input = "a:b:c";
StrTokenizer tok = new StrTokenizer(input, ':');
@ -369,13 +274,13 @@ public class StrTokenizerTest extends TestCase {
assertEquals("b", tok.next());
assertEquals("c", tok.next());
}
public void testBasicDelim2() {
String input = "a:b:c";
StrTokenizer tok = new StrTokenizer(input, ',');
assertEquals("a:b:c", tok.next());
}
public void testBasicEmpty1() {
String input = "a b c";
StrTokenizer tok = new StrTokenizer(input);
@ -385,7 +290,7 @@ public class StrTokenizerTest extends TestCase {
assertEquals("b", tok.next());
assertEquals("c", tok.next());
}
public void testBasicEmpty2() {
String input = "a b c";
StrTokenizer tok = new StrTokenizer(input);
@ -396,16 +301,67 @@ public class StrTokenizerTest extends TestCase {
assertEquals("b", tok.next());
assertEquals("c", tok.next());
}
public void testBasicQuoted1() {
String input = "a \"b\" c";
StrTokenizer tok = new StrTokenizer(input, ' ', '"');
assertEquals("a", tok.next());
assertEquals("b", tok.next());
assertEquals("c", tok.next());
}
public void testCSV(String data) {
this.testXSVAbc(StrTokenizer.getCSVInstance(data));
this.testXSVAbc(StrTokenizer.getCSVInstance(data.toCharArray()));
}
public void testCSVEmpty() {
this.testEmpty(StrTokenizer.getCSVInstance());
this.testEmpty(StrTokenizer.getCSVInstance(""));
}
public void testCSVSimple() {
this.testCSV(CSV_SIMPLE_FIXTURE);
}
public void testCSVSimpleNeedsTrim() {
this.testCSV(" " + CSV_SIMPLE_FIXTURE);
this.testCSV(" \n\t " + CSV_SIMPLE_FIXTURE);
this.testCSV(" \n " + CSV_SIMPLE_FIXTURE + "\n\n\r");
}
void testEmpty(StrTokenizer tokenizer) {
this.checkClone(tokenizer);
assertEquals(false, tokenizer.hasNext());
assertEquals(false, tokenizer.hasPrevious());
assertEquals(null, tokenizer.next());
assertEquals(null, tokenizer.nextToken());
assertEquals(0, tokenizer.size());
}
public void testGetContent() {
String input = "a b c \"d e\" f ";
StrTokenizer tok = new StrTokenizer(input);
assertSame(input, tok.getContent());
tok = new StrTokenizer(input.toCharArray());
assertEquals(input, tok.getContent());
}
public void testMatcher() {
assertEquals(1, StrTokenizer.SPACE_MATCHER.isMatch(new char[]{' '}, 1, 0));
assertEquals(0, StrTokenizer.SPACE_MATCHER.isMatch(new char[]{'\n'}, 1, 0));
assertEquals(0, StrTokenizer.SPACE_MATCHER.isMatch(new char[]{'\u0001'}, 1, 0));
assertEquals(1, StrTokenizer.TRIM_MATCHER.isMatch(new char[]{' '}, 1, 0));
assertEquals(1, StrTokenizer.TRIM_MATCHER.isMatch(new char[]{'\n'}, 1, 0));
assertEquals(1, StrTokenizer.TRIM_MATCHER.isMatch(new char[]{'\u0001'}, 1, 0));
assertEquals(1, StrTokenizer.SPLIT_MATCHER.isMatch(new char[]{' '}, 1, 0));
assertEquals(1, StrTokenizer.SPLIT_MATCHER.isMatch(new char[]{'\n'}, 1, 0));
assertEquals(0, StrTokenizer.SPLIT_MATCHER.isMatch(new char[]{'\u0001'}, 1, 0));
}
public void testReset() {
String input = "a b c";
StrTokenizer tok = new StrTokenizer(input);
@ -423,19 +379,40 @@ public class StrTokenizerTest extends TestCase {
assertEquals("f", tok.next());
assertEquals("g", tok.next());
}
public void testMatcher() {
assertEquals(1, StrTokenizer.SPACE_MATCHER.isMatch(new char[] {' '}, 1, 0));
assertEquals(0, StrTokenizer.SPACE_MATCHER.isMatch(new char[] {'\n'}, 1, 0));
assertEquals(0, StrTokenizer.SPACE_MATCHER.isMatch(new char[] {'\u0001'}, 1, 0));
assertEquals(1, StrTokenizer.TRIM_MATCHER.isMatch(new char[] {' '}, 1, 0));
assertEquals(1, StrTokenizer.TRIM_MATCHER.isMatch(new char[] {'\n'}, 1, 0));
assertEquals(1, StrTokenizer.TRIM_MATCHER.isMatch(new char[] {'\u0001'}, 1, 0));
assertEquals(1, StrTokenizer.SPLIT_MATCHER.isMatch(new char[] {' '}, 1, 0));
assertEquals(1, StrTokenizer.SPLIT_MATCHER.isMatch(new char[] {'\n'}, 1, 0));
assertEquals(0, StrTokenizer.SPLIT_MATCHER.isMatch(new char[] {'\u0001'}, 1, 0));
public void testTSV() {
this.testXSVAbc(StrTokenizer.getTSVInstance(TSV_SIMPLE_FIXTURE));
this.testXSVAbc(StrTokenizer.getTSVInstance(TSV_SIMPLE_FIXTURE.toCharArray()));
}
public void testTSVEmpty() {
this.testEmpty(StrTokenizer.getCSVInstance());
this.testEmpty(StrTokenizer.getCSVInstance(""));
}
void testXSVAbc(StrTokenizer tokenizer) {
this.checkClone(tokenizer);
assertEquals(-1, tokenizer.previousIndex());
assertEquals(0, tokenizer.nextIndex());
assertEquals(null, tokenizer.previousToken());
assertEquals("A", tokenizer.nextToken());
assertEquals(1, tokenizer.nextIndex());
assertEquals("b", tokenizer.nextToken());
assertEquals(2, tokenizer.nextIndex());
assertEquals("c", tokenizer.nextToken());
assertEquals(3, tokenizer.nextIndex());
assertEquals(null, tokenizer.nextToken());
assertEquals(3, tokenizer.nextIndex());
assertEquals("c", tokenizer.previousToken());
assertEquals(2, tokenizer.nextIndex());
assertEquals("b", tokenizer.previousToken());
assertEquals(1, tokenizer.nextIndex());
assertEquals("A", tokenizer.previousToken());
assertEquals(0, tokenizer.nextIndex());
assertEquals(null, tokenizer.previousToken());
assertEquals(0, tokenizer.nextIndex());
assertEquals(-1, tokenizer.previousIndex());
assertEquals(3, tokenizer.size());
}
}