View Javadoc
1   package org.codehaus.plexus.util.cli;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  
21  public class EnhancedStringTokenizerTest
22      extends TestCase
23  {
24      /**
25       * @param testName
26       */
27      public EnhancedStringTokenizerTest( final String testName )
28      {
29          super( testName );
30      }
31  
32      /*
33       * @see TestCase#setUp()
34       */
35      public void setUp()
36          throws Exception
37      {
38          super.setUp();
39      }
40  
41      public void test1()
42      {
43          EnhancedStringTokenizer est = new EnhancedStringTokenizer( "this is a test string" );
44          StringBuilder sb = new StringBuilder();
45          while ( est.hasMoreTokens() )
46          {
47              sb.append( est.nextToken() );
48              sb.append( " " );
49          }
50          assertEquals( "this is a test string ", sb.toString() );
51      }
52  
53      public void test2()
54      {
55          EnhancedStringTokenizer est = new EnhancedStringTokenizer( "1,,,3,,4", "," );
56          assertEquals( "Token 1", "1", est.nextToken() );
57          assertEquals( "Token 2", "", est.nextToken() );
58          assertEquals( "Token 3", "", est.nextToken() );
59          assertEquals( "Token 4", "3", est.nextToken() );
60          assertEquals( "Token 5", "", est.nextToken() );
61          assertEquals( "Token 6", "4", est.nextToken() );
62      }
63  
64      public void test3()
65      {
66          EnhancedStringTokenizer est = new EnhancedStringTokenizer( "1,,,3,,4", ",", true );
67          assertEquals( "Token 1", "1", est.nextToken() );
68          assertEquals( "Token 2", ",", est.nextToken() );
69          assertEquals( "Token 3", "", est.nextToken() );
70          assertEquals( "Token 4", ",", est.nextToken() );
71          assertEquals( "Token 5", "", est.nextToken() );
72          assertEquals( "Token 6", ",", est.nextToken() );
73          assertEquals( "Token 7", "3", est.nextToken() );
74          assertEquals( "Token 8", ",", est.nextToken() );
75          assertEquals( "Token 9", "", est.nextToken() );
76          assertEquals( "Token 10", ",", est.nextToken() );
77          assertEquals( "Token 11", "4", est.nextToken() );
78      }
79  
80      public void testMultipleDelim()
81      {
82          EnhancedStringTokenizer est = new EnhancedStringTokenizer( "1 2|3|4", " |", true );
83          assertEquals( "Token 1", "1", est.nextToken() );
84          assertEquals( "Token 2", " ", est.nextToken() );
85          assertEquals( "Token 3", "2", est.nextToken() );
86          assertEquals( "Token 4", "|", est.nextToken() );
87          assertEquals( "Token 5", "3", est.nextToken() );
88          assertEquals( "Token 6", "|", est.nextToken() );
89          assertEquals( "Token 7", "4", est.nextToken() );
90          assertEquals( "est.hasMoreTokens()", false, est.hasMoreTokens() );
91      }
92  
93      public void testEmptyString()
94      {
95          EnhancedStringTokenizer est = new EnhancedStringTokenizer( "" );
96          assertEquals( "est.hasMoreTokens()", false, est.hasMoreTokens() );
97          try
98          {
99              est.nextToken();
100             fail();
101         }
102         catch ( Exception e )
103         {
104         }
105     }
106 
107     public void testSimpleString()
108     {
109         EnhancedStringTokenizer est = new EnhancedStringTokenizer( "a " );
110         assertEquals( "Token 1", "a", est.nextToken() );
111         assertEquals( "Token 2", "", est.nextToken() );
112         assertEquals( "est.hasMoreTokens()", false, est.hasMoreTokens() );
113     }
114 }