diff --git a/src/java/org/apache/commons/lang/text/StrTokenizer.java b/src/java/org/apache/commons/lang/text/StrTokenizer.java index 9b5db3261..9f5620624 100644 --- a/src/java/org/apache/commons/lang/text/StrTokenizer.java +++ b/src/java/org/apache/commons/lang/text/StrTokenizer.java @@ -1068,13 +1068,17 @@ public class StrTokenizer implements ListIterator, Cloneable { * @return the string content being parsed */ public String getContent() { + if (chars == null) { + return null; + } return new String(chars); } //----------------------------------------------------------------------- /** - * Creates a new instance of this Tokenizer. The new instance is reset so that it will be at the start of the token - * list. If a {@link CloneNotSupportedException} is caught, return null. + * Creates a new instance of this Tokenizer. The new instance is reset so + * that it will be at the start of the token list. + * If a {@link CloneNotSupportedException} is caught, return null. * * @return a new instance of this Tokenizer which has been reset. */ @@ -1087,8 +1091,8 @@ public class StrTokenizer implements ListIterator, Cloneable { } /** - * Creates a new instance of this Tokenizer. The new instance is reset so that it will be at the start of the token - * list. + * Creates a new instance of this Tokenizer. The new instance is reset so that + * it will be at the start of the token list. * * @return a new instance of this Tokenizer which has been reset. * @throws CloneNotSupportedException if there is a problem cloning @@ -1102,4 +1106,18 @@ public class StrTokenizer implements ListIterator, Cloneable { return cloned; } + //----------------------------------------------------------------------- + /** + * Gets the String content that the tokenizer is parsing. + * + * @return the string content being parsed + */ + public String toString() { + if (tokens == null) { + return "StrTokenizer[not tokenized yet]"; + } else { + return "StrTokenizer" + getTokenList(); + } + } + } diff --git a/src/test/org/apache/commons/lang/text/StrTokenizerTest.java b/src/test/org/apache/commons/lang/text/StrTokenizerTest.java index 3ab8d3d82..046b480e1 100644 --- a/src/test/org/apache/commons/lang/text/StrTokenizerTest.java +++ b/src/test/org/apache/commons/lang/text/StrTokenizerTest.java @@ -536,6 +536,9 @@ public class StrTokenizerTest extends TestCase { tok = new StrTokenizer(input.toCharArray()); assertEquals(input, tok.getContent()); + + tok = new StrTokenizer(); + assertEquals(null, tok.getContent()); } //----------------------------------------------------------------------- @@ -830,4 +833,12 @@ public class StrTokenizerTest extends TestCase { assertEquals("a", tkn.next()); } + //----------------------------------------------------------------------- + public void testToString() { + StrTokenizer tkn = new StrTokenizer("a b c d e"); + assertEquals("StrTokenizer[not tokenized yet]", tkn.toString()); + tkn.next(); + assertEquals("StrTokenizer[a, b, c, d, e]", tkn.toString()); + } + }