1 23 24 28 29 32 33 package com.sun.enterprise.admin.util; 34 35 import java.util.StringTokenizer ; 36 import junit.framework.*; 37 import junit.framework.Assert; 38 39 42 public class QuotedStringTokenizerTest extends TestCase 43 { 44 public void testCreate() 45 { 46 new QuotedStringTokenizer(""); 47 new QuotedStringTokenizer("a"); 48 try { 49 new QuotedStringTokenizer(null); 50 Assert.assertTrue(false); 51 } catch (IllegalArgumentException iae) {} 52 } 53 54 void _countTokens(String s, int expCount) 55 { 56 QuotedStringTokenizer tok = new QuotedStringTokenizer(s, null); 57 Assert.assertEquals(expCount, tok.countTokens()); 58 } 59 60 public void testCountTokens() 61 { 62 _countTokens("", 0); 63 _countTokens(" ", 0); 64 _countTokens(" ", 0); 65 _countTokens("a", 1); 66 _countTokens(" a", 1); 67 _countTokens(" a ", 1); 68 _countTokens(" a ", 1); 69 _countTokens(" ab ", 1); 70 _countTokens(" a b ", 2); 71 } 72 73 public void testCountTokensWithQuotes() 74 { 75 _countTokens("\"a\"", 1); 76 _countTokens("\"a b\"", 1); 77 _countTokens(" \"a\" ", 1); 78 _countTokens("\"", 1); 79 _countTokens("\"\"", 1); 80 _countTokens("\" \"", 1); 81 _countTokens("\"a\"b", 1); 82 _countTokens("\"a\" b", 2); 83 _countTokens("\"a\" \"b\"", 2); 84 _countTokens("\"a\"\"b\"", 1); 85 _countTokens("\" ", 1); 86 } 87 88 public void testNextToken1() 89 { 90 QuotedStringTokenizer tok = new QuotedStringTokenizer("a"); 91 Assert.assertTrue(tok.hasMoreTokens()); 92 Assert.assertEquals("a", tok.nextToken()); 93 Assert.assertFalse(tok.hasMoreTokens()); 94 try { 95 tok.nextToken(); 96 Assert.assertTrue(false); 97 } catch (java.util.NoSuchElementException nsee) {} 98 } 99 100 public void testNextToken2() 101 { 102 QuotedStringTokenizer tok = new QuotedStringTokenizer(""); 103 Assert.assertFalse(tok.hasMoreTokens()); 104 try { 105 tok.nextToken(); 106 Assert.assertTrue(false); 107 } catch (java.util.NoSuchElementException nsee) {} 108 } 109 110 public void testNextToken3() 111 { 112 QuotedStringTokenizer tok = new QuotedStringTokenizer("a b c d e f"); 113 Assert.assertTrue(tok.countTokens() == 6); 114 tok.nextToken(); 115 Assert.assertEquals("b", tok.nextToken()); 116 Assert.assertTrue(tok.hasMoreTokens()); 117 while (tok.hasMoreTokens()) 118 tok.nextToken(); 119 try { 120 tok.nextToken(); 121 Assert.assertTrue(false); 122 } catch (java.util.NoSuchElementException nsee) {} 123 } 124 125 public void testNextToken4() 126 { 127 QuotedStringTokenizer tok = new QuotedStringTokenizer("abcdef"); 128 Assert.assertTrue(tok.countTokens() == 1); 129 Assert.assertEquals("abcdef", tok.nextToken()); 130 tok = new QuotedStringTokenizer(" abcdef g "); 131 Assert.assertTrue(tok.countTokens() == 2); 132 Assert.assertEquals("abcdef", tok.nextToken()); 133 Assert.assertEquals("g", tok.nextToken()); 134 } 135 136 public void testNextToken5() 137 { 138 QuotedStringTokenizer tok = new QuotedStringTokenizer("abc \t d \t\t ef"); 139 Assert.assertEquals(3, tok.countTokens()); 140 Assert.assertEquals("abc", tok.nextToken()); 141 Assert.assertEquals("d", tok.nextToken()); 142 Assert.assertEquals("ef", tok.nextToken()); 143 } 144 145 146 public void testNextTokenWithQuotes() 147 { 148 QuotedStringTokenizer mtok = new QuotedStringTokenizer("\"\""); 149 Assert.assertEquals("\"\"", mtok.nextToken()); 150 Assert.assertFalse(mtok.hasMoreTokens()); 151 152 mtok = new QuotedStringTokenizer("\""); 153 Assert.assertEquals("\"", mtok.nextToken()); 154 Assert.assertFalse(mtok.hasMoreTokens()); 155 156 mtok = new QuotedStringTokenizer("\" "); 158 Assert.assertEquals("\" ", mtok.nextToken()); 159 Assert.assertFalse(mtok.hasMoreTokens()); 160 161 mtok = new QuotedStringTokenizer(" \" "); 163 Assert.assertEquals("\" ", mtok.nextToken()); 164 Assert.assertFalse(mtok.hasMoreTokens()); 165 166 mtok = new QuotedStringTokenizer("\t\"\t"); 167 Assert.assertEquals("\"\t", mtok.nextToken()); 168 Assert.assertFalse(mtok.hasMoreTokens()); 169 170 mtok = new QuotedStringTokenizer("\"a\""); 171 Assert.assertEquals("\"a\"", mtok.nextToken()); 172 Assert.assertFalse(mtok.hasMoreTokens()); 173 174 mtok = new QuotedStringTokenizer(" \"a\" "); 175 Assert.assertEquals("\"a\"", mtok.nextToken()); 176 Assert.assertFalse(mtok.hasMoreTokens()); 177 178 mtok = new QuotedStringTokenizer("\"a"); 179 Assert.assertEquals("\"a", mtok.nextToken()); 180 Assert.assertFalse(mtok.hasMoreTokens()); 181 182 mtok = new QuotedStringTokenizer("\"a\" \"b\""); 183 Assert.assertEquals("\"a\"", mtok.nextToken()); 184 Assert.assertEquals("\"b\"", mtok.nextToken()); 185 186 mtok = new QuotedStringTokenizer("\"a\"b"); 187 Assert.assertEquals("\"a\"b", mtok.nextToken()); 188 189 mtok = new QuotedStringTokenizer("\"a "); 191 Assert.assertEquals("\"a ", mtok.nextToken()); 192 193 mtok = new QuotedStringTokenizer("\"a b\""); 194 Assert.assertEquals(1, mtok.countTokens()); 195 Assert.assertEquals("\"a b\"", mtok.nextToken()); 196 } 197 198 public void testWithQuoteAsDelimiter() 199 { 200 QuotedStringTokenizer mtok = new QuotedStringTokenizer("\"\"", "\" "); 201 Assert.assertEquals(0, mtok.countTokens()); 202 203 mtok = new QuotedStringTokenizer("\"", "\" "); 204 Assert.assertEquals(0, mtok.countTokens()); 205 206 mtok = new QuotedStringTokenizer("\" ", "\" "); 207 Assert.assertEquals(0, mtok.countTokens()); 208 209 mtok = new QuotedStringTokenizer(" \" ", "\" "); 210 Assert.assertEquals(0, mtok.countTokens()); 211 212 mtok = new QuotedStringTokenizer("\t\"\t", "\" "); 213 Assert.assertEquals(2, mtok.countTokens()); 214 215 mtok = new QuotedStringTokenizer("\"a\"", "\" "); 216 Assert.assertEquals("a", mtok.nextToken()); 217 Assert.assertFalse(mtok.hasMoreTokens()); 218 219 mtok = new QuotedStringTokenizer(" \"a\" ", "\" "); 220 Assert.assertEquals("a", mtok.nextToken()); 221 Assert.assertFalse(mtok.hasMoreTokens()); 222 223 mtok = new QuotedStringTokenizer("\"a", "\" "); 224 Assert.assertEquals("a", mtok.nextToken()); 225 Assert.assertFalse(mtok.hasMoreTokens()); 226 227 mtok = new QuotedStringTokenizer("\"a\" \"b\"", "\" "); 228 Assert.assertEquals("a", mtok.nextToken()); 229 Assert.assertEquals("b", mtok.nextToken()); 230 231 mtok = new QuotedStringTokenizer("\"a\"b", "\" "); 232 Assert.assertEquals("a", mtok.nextToken()); 233 Assert.assertEquals("b", mtok.nextToken()); 234 235 mtok = new QuotedStringTokenizer("ab", "\" "); 236 Assert.assertEquals("ab", mtok.nextToken()); 237 } 238 239 public void testIsDelimiter() 240 { 241 QuotedStringTokenizer tok = new QuotedStringTokenizer(""); 242 Assert.assertTrue(tok.isDelimiter('\t')); 243 } 244 245 public void testQuotesInQuotes() 246 { 247 QuotedStringTokenizer tok = new QuotedStringTokenizer("\"abc\"def g"); 248 Assert.assertEquals("\"abc\"def", tok.nextToken()); 249 Assert.assertEquals("g", tok.nextToken()); 250 251 tok = new QuotedStringTokenizer("\"abc\"def\""); 252 Assert.assertEquals("\"abc\"def\"", tok.nextToken()); 253 254 tok = new QuotedStringTokenizer(" \"abc\"def\" "); 255 Assert.assertEquals("\"abc\"def\" ", tok.nextToken()); 256 } 257 258 public void testJavaStringTokenizer() 259 { 260 StringTokenizer tok = new StringTokenizer ("\" ", " "); 261 Assert.assertEquals("\"", tok.nextToken()); 262 tok = new StringTokenizer ("a", "\""); 263 Assert.assertEquals("a", tok.nextToken()); 264 tok = new StringTokenizer ("\"\"", "\" "); 265 Assert.assertEquals(0, tok.countTokens()); 266 tok = new StringTokenizer ("\t\"\t", "\" "); 267 Assert.assertEquals(2, tok.countTokens()); 268 } 269 270 protected void setUp() { 271 } 272 273 protected void tearDown() { 274 } 275 276 public static junit.framework.Test suite() 277 { 278 TestSuite suite = new TestSuite(QuotedStringTokenizerTest.class); 279 return suite; 280 } 281 282 public static void main(String args[]) throws Exception 283 { 284 junit.textui.TestRunner.run(suite()); 285 } 287 } 288 | Popular Tags |