1 23 24 25 30 31 package com.sun.cli.util; 32 33 import java.util.Iterator ; 34 35 import junit.framework.TestCase; 36 37 38 39 public class TokenizerTest extends TestCase 40 { 41 public 42 TokenizerTest( String name ) 43 { 44 super( name ); 45 } 46 47 public final static String WHITE_SPACE = " \t"; 48 public final static char ESCAPE_CHAR = '\\'; 49 public final static String ESCAPABLE_CHARS = "" + ESCAPE_CHAR + WHITE_SPACE + '\"'; 50 51 Tokenizer 52 create( String input, boolean multipleDelimsCountAsOne ) 53 { 54 return( new TokenizerImpl( input, 55 WHITE_SPACE, multipleDelimsCountAsOne, ESCAPE_CHAR, ESCAPABLE_CHARS) ); 56 } 57 58 Tokenizer 59 create( String input ) 60 { 61 return( new TokenizerImpl( input, 62 WHITE_SPACE, false, ESCAPE_CHAR, ESCAPABLE_CHARS) ); 63 } 64 65 66 public void 67 testEmpty() 68 { 69 final Tokenizer tk = create( "" ); 70 71 assertEquals( "expecting no tokens", 0, tk.getTokens().length ); 72 73 try 74 { 75 tk.iterator().next(); 76 fail( "expecting exception trying to get token" ); 77 } 78 catch( Exception e ) 79 { 80 } 81 } 82 83 84 public void 85 testLeadingDelim() 86 { 87 final String input = WHITE_SPACE.charAt( 0 ) + "hello"; 88 final String [] tokens = create( input, false ).getTokens(); 89 90 assertEquals( 2, tokens.length ); 91 } 92 93 94 public void 95 testSingleDelimOnly() 96 { 97 final String input = "" + WHITE_SPACE.charAt( 0 ); 98 final String [] tokens = create( input, false ).getTokens(); 99 100 assertEquals( 2, tokens.length ); 101 } 102 103 public void 104 testSingleToken() 105 { 106 final String input = "hello"; 107 final String [] tokens = create( input, false ).getTokens(); 108 109 assertEquals( "expecting 1 token", 1, tokens.length ); 110 assertEquals( "expecting " + input, input, tokens[ 0 ] ); 111 } 112 113 public void 114 testMultipleTokens() 115 { 116 final String input = "hello there 1 2 3 4 5"; 117 final Iterator iter = create( input ).iterator(); 118 119 int count = 0; 120 String temp = ""; 121 while ( iter.hasNext() ) 122 { 123 ++count; 124 temp = temp + " " + iter.next(); 125 } 126 assertEquals( "expecting 7 tokens", 7, count ); 127 assertEquals( "expecting match", input, temp.substring( 1, temp.length() )); 128 } 129 130 public void 131 testWhiteSpaceEquality() 132 { 133 final String input1 = "hello there 1 2 3 4 5"; 134 final String input2 = "hello\tthere 1\t2 3 4\t5"; 135 final Iterator iter1 = create( input1 ).iterator(); 136 final Iterator iter2 = create( input2 ).iterator(); 137 138 while ( iter1.hasNext() ) 139 { 140 assertEquals( "expecting equal results from different white space", 141 iter1.next(), iter2.next() ); 142 } 143 } 144 145 private final static char BACKSLASH = '\\'; 146 147 148 public void 149 testEscapedNewlineCR() 150 { 151 final String TEST = "test" + BACKSLASH + "n" + BACKSLASH + "r"; 152 final String [] tokens = create( TEST ).getTokens(); 153 154 assertEquals( 1, tokens.length ); 155 assertEquals( "test\n\r", tokens[ 0 ] ); 156 } 157 158 public void 159 testUnescapableChar() 160 { 161 final String TEST = "test" + BACKSLASH + "xyz"; 162 final String [] tokens = create( TEST ).getTokens(); 163 164 assertEquals( 1, tokens.length ); 165 assertEquals( "test\\xyz", tokens[ 0 ] ); 166 } 167 168 public void 169 testEscaping() 170 { 171 final StringBuffer b = new StringBuffer (); 173 for( int i = 0; i < ESCAPABLE_CHARS.length(); ++i ) 174 { 175 b.append( "\\" + ESCAPABLE_CHARS.charAt( i ) ); 176 } 177 final String [] tokens = create( b.toString() ).getTokens(); 178 179 assertEquals( "expecting 1 token", 1, tokens.length ); 180 assertEquals( "expecting match", ESCAPABLE_CHARS, tokens[ 0 ] ); 181 } 182 183 184 public void 185 testTrailingDelimiter1() 186 { 187 final String input = "hello" + WHITE_SPACE.charAt( 0 ); 188 final String [] tokens = create( input, false ).getTokens(); 189 190 assertEquals( 2, tokens.length ); 191 } 192 193 public void 194 testTrailingDelimiter2() 195 { 196 final String input = "hello" + WHITE_SPACE.charAt( 0 ) + 197 "\"there\"" + WHITE_SPACE.charAt( 0 ); 198 final String [] tokens = create( input, false ).getTokens(); 199 200 assertEquals( 3, tokens.length ); 201 } 202 203 public void 204 testMultipleDelimsWithNoData() 205 { 206 final String input = "" + WHITE_SPACE.charAt( 0 ); 207 final String [] tokens = create( input, false ).getTokens(); 208 209 assertEquals( input.length() + 1, tokens.length ); 210 } 211 212 public void 213 testMultipleDelimsAsOne() 214 { 215 final String input = "hello" + WHITE_SPACE + "there" + WHITE_SPACE; 216 final String [] tokens = create( input, true ).getTokens(); 217 218 assertEquals( 3, tokens.length ); 219 } 220 221 public void 222 testQuotedString() 223 { 224 final String input = "\"hello there\" \"another\" \"3\" \"words\" \"\""; 225 final String [] tokens = create( input ).getTokens(); 226 227 assertEquals( 5, tokens.length ); 228 assertEquals( "hello there", tokens[ 0 ] ); 229 assertEquals( "another", tokens[ 1 ] ); 230 assertEquals( "3", tokens[ 2 ] ); 231 assertEquals( "words", tokens[ 3 ] ); 232 assertEquals( "", tokens[ 4 ] ); 233 } 234 236 237 238 protected void 239 setUp() 240 { 241 } 242 243 protected void 244 tearDown() 245 { 246 } 247 248 }; 249 250 251 252 | Popular Tags |