1 23 24 package com.sun.enterprise.cli.framework; 25 26 32 import junit.framework.*; 33 import junit.textui.TestRunner; 34 35 40 41 46 47 public class CLITokenizerTest extends TestCase { 48 49 public void testSimpleTokens() throws Exception { 51 52 CLITokenizer cliTokenizer = new CLITokenizer("name1=value1:name2=value2:name3=value3", ":"); 53 54 assertEquals("number of tokens is 3", 3, cliTokenizer.countTokens()); 55 assertEquals("first token is name1=value1", "name1=value1", cliTokenizer.nextToken()); 56 assertEquals("second token is name2=value2", "name2=value2", cliTokenizer.nextToken()); 57 assertEquals("hasMoreToken should be true, since there is still one more token", true, cliTokenizer.hasMoreTokens()); 58 assertEquals("third token is name3=value3", "name3=value3", cliTokenizer.nextToken()); 59 assertEquals("hasMoreToken should be false, since there are no more tokens", false, cliTokenizer.hasMoreTokens()); 60 } 61 62 public void testTokensWithQuotes() throws Exception { 64 65 CLITokenizer cliTokenizer = new CLITokenizer("\"name1:123\"=value1:name2=\"value2:xyz\":\"name3=:value3\":name4=value4", ":"); 66 67 assertEquals("number of tokens is 4", 4, cliTokenizer.countTokens()); 68 assertEquals("first token is name1:123=value1", "name1:123=value1", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 69 assertEquals("second token is name2=\"value2:xyz\"", "name2=\"value2:xyz\"", cliTokenizer.nextToken()); 70 assertEquals("hasMoreToken should be true, since there is still two more token", true, cliTokenizer.hasMoreTokens()); 71 assertEquals("third token is name3=:value3", "name3=:value3", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 72 assertEquals("forth token is name4=value4", "name4=value4", cliTokenizer.nextToken()); 73 assertEquals("hasMoreToken should be false, since there are no more tokens", false, cliTokenizer.hasMoreTokens()); 74 } 75 76 77 public void testTokensWithEscapeChar() throws Exception { 79 80 final CLITokenizer cliTokenizer = new CLITokenizer("name1\\:123=value1:name2=value2\\:xyz:name3=\\:value3:name4=value4", ":"); 81 82 assertEquals("number of tokens is 4", 4, cliTokenizer.countTokens()); 83 assertEquals("first token is name1:123=value1", "name1:123=value1", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 84 assertEquals("second token is name2=\"value2:xyz\"", "name2=value2\\:xyz", cliTokenizer.nextToken()); 85 assertEquals("hasMoreToken should be true, since there is still two more token", true, cliTokenizer.hasMoreTokens()); 86 assertEquals("third token is name3=:value3", "name3=:value3", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 87 assertEquals("forth token is name4=value4", "name4=value4", cliTokenizer.nextToken()); 88 assertEquals("hasMoreToken should be false, since there are no more tokens", false, cliTokenizer.hasMoreTokens()); 89 } 90 91 92 public void testTokensWithEscapeCharAndQuotes() throws Exception { 94 95 final CLITokenizer cliTokenizer = new CLITokenizer("name1=value1:name2=abc\\:def:name3=\"abc:def\":name4\\=123=value4:\"name5=123\"=value5:name6=\"abc\\:def\":name7=value7", ":"); 96 97 assertEquals("number of tokens is 7", 7, cliTokenizer.countTokens()); 98 assertEquals("first token is name1=value1", "name1=value1", cliTokenizer.nextToken()); 99 assertEquals("second token is name2=abc:def", "name2=abc:def", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 100 assertEquals("hasMoreToken should be true, since there is still four more tokens", true, cliTokenizer.hasMoreTokens()); 101 assertEquals("third token is name3=abc:def", "name3=abc:def", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 102 assertEquals("forth token is name4\\=123=value4", "name4\\=123=value4", cliTokenizer.nextToken()); 103 assertEquals("fifth token is \"name5=123\"=value5", "\"name5=123\"=value5", cliTokenizer.nextToken()); 104 assertEquals("sixth token is name6=abc\\:def", "name6=abc\\:def", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 105 assertEquals("seventh token is name7=value7", "name7=value7", cliTokenizer.nextToken()); 106 assertEquals("hasMoreToken should be false, since there are no more tokens", false, cliTokenizer.hasMoreTokens()); 107 } 108 109 public void testRecursiveTokensWithEscapeCharAndQuotes() throws Exception { 111 112 final CLITokenizer cliTokenizer = new CLITokenizer("name1=value1:name2=abc\\:def:name3=\"abc:def\":name4\\=123=value4:\"name5=123\"=value5:name6=\"abc\\:def\":name7=value7", ":"); 113 114 assertEquals("number of tokens is 7", 7, cliTokenizer.countTokens()); 115 116 final String firstToken = cliTokenizer.nextToken(); 117 final CLITokenizer cliTokenizer1 = new CLITokenizer(firstToken, "="); 118 assertEquals("first token is name1=value1", "name1=value1", firstToken); 119 assertEquals("name of name1=value1", "name1", cliTokenizer1.nextToken()); 120 assertEquals("value of name1=value1", "value1", cliTokenizer1.nextToken()); 121 122 final String secondToken = cliTokenizer.nextToken(); 123 final CLITokenizer cliTokenizer2 = new CLITokenizer(secondToken, "="); 124 assertEquals("second token is name2=abc\\:def", "name2=abc\\:def", secondToken); 125 assertEquals("name of name2=abc:def", "name2", cliTokenizer2.nextTokenWithoutEscapeAndQuoteChars()); 126 assertEquals("value of name2=abc:def", "abc:def", cliTokenizer2.nextTokenWithoutEscapeAndQuoteChars()); 127 assertEquals("hasMoreToken should be true, since there is still four more tokens", true, cliTokenizer.hasMoreTokens()); 128 129 final String thirdToken = cliTokenizer.nextToken(); 130 final CLITokenizer cliTokenizer3 = new CLITokenizer(thirdToken, "="); 131 assertEquals("third token is name3=\"abc:def\"", "name3=\"abc:def\"", thirdToken); 132 assertEquals("name of name3=abc:def", "name3", cliTokenizer3.nextTokenWithoutEscapeAndQuoteChars()); 133 assertEquals("value of name3=abc:def", "abc:def", cliTokenizer3.nextTokenWithoutEscapeAndQuoteChars()); 134 135 final String fourthToken = cliTokenizer.nextToken(); 136 final CLITokenizer cliTokenizer4 = new CLITokenizer(fourthToken, "="); 137 assertEquals("forth token is name4\\=123=value4", "name4\\=123=value4", fourthToken); 138 assertEquals("name of name4\\=123=value4", "name4=123", cliTokenizer4.nextTokenWithoutEscapeAndQuoteChars()); 139 assertEquals("value of name4\\=123=value4", "value4", cliTokenizer4.nextTokenWithoutEscapeAndQuoteChars()); 140 141 final String fifthToken = cliTokenizer.nextToken(); 142 final CLITokenizer cliTokenizer5 = new CLITokenizer(fifthToken, "="); 143 assertEquals("fifth token is \"name5=123\"=value5", "\"name5=123\"=value5", fifthToken); 144 assertEquals("name of \"name5=123\"=value5", "name5=123", cliTokenizer5.nextTokenWithoutEscapeAndQuoteChars()); 145 assertEquals("value of \"name5=123\"=value5", "value5", cliTokenizer5.nextTokenWithoutEscapeAndQuoteChars()); 146 147 final String sixthToken = cliTokenizer.nextToken(); 148 final CLITokenizer cliTokenizer6 = new CLITokenizer(sixthToken, "="); 149 assertEquals("sixth token is name6=\"abc\\:def\"", "name6=\"abc\\:def\"", sixthToken); 150 assertEquals("name of name6=\"abc\\:def\"", "name6", cliTokenizer6.nextTokenWithoutEscapeAndQuoteChars()); 151 assertEquals("value of name6=\"abc\\:def\"", "abc\\:def", cliTokenizer6.nextTokenWithoutEscapeAndQuoteChars()); 152 153 154 final String seventhToken = cliTokenizer.nextToken(); 155 final CLITokenizer cliTokenizer7 = new CLITokenizer(seventhToken, "="); 156 assertEquals("seventh token is name7=value7", "name7=value7", seventhToken); 157 assertEquals("name of name7=value7", "name7", cliTokenizer7.nextTokenWithoutEscapeAndQuoteChars()); 158 assertEquals("value of name7=value7", "value7", cliTokenizer7.nextTokenWithoutEscapeAndQuoteChars()); 159 160 assertEquals("hasMoreToken should be false, since there are no more tokens", false, cliTokenizer.hasMoreTokens()); 161 } 162 163 public void testTokensWithSpace() throws Exception { 165 166 CLITokenizer cliTokenizer = new CLITokenizer(" There is a \"right time\" and a \"right way\" to do everything ", " "); 167 168 assertEquals("number of tokens is 10", 10, cliTokenizer.countTokens()); 169 assertEquals("first token is There", "There", cliTokenizer.nextToken()); 170 assertEquals("second token is is", "is", cliTokenizer.nextToken()); 171 assertEquals("hasMoreToken should be true, since there are still more tokens", true, cliTokenizer.hasMoreTokens()); 172 assertEquals("third token is a", "a", cliTokenizer.nextToken()); 173 assertEquals("fourth token is \"right time\"", "\"right time\"", cliTokenizer.nextToken()); 174 assertEquals("fifth token is and", "and", cliTokenizer.nextToken()); 175 assertEquals("sixth token is a", "a", cliTokenizer.nextToken()); 176 assertEquals("seventh token is \"right way\"", "\"right way\"", cliTokenizer.nextToken()); 177 assertEquals("eighth token is to", "to", cliTokenizer.nextToken()); 178 assertEquals("nineth token is do", "do", cliTokenizer.nextToken()); 179 assertEquals("tenth token is everything", "everything", cliTokenizer.nextToken()); 180 assertEquals("hasMoreToken should be false, since there are no more tokens", false, cliTokenizer.hasMoreTokens()); 181 } 182 183 184 public void testTokensWithSpace2() throws Exception { 186 187 CLITokenizer cliTokenizer = new CLITokenizer("-Dx=\\\" abc\\\":-D\\\"xyz=a bc\\\":-Dy=\\\"1 2 3\\\":-Dz=1\\\"2 3\\\":-Da=bc", ":"); 188 189 assertEquals("number of tokens is 5", 5, cliTokenizer.countTokens()); 190 assertEquals("first token is -Dx=\" abc\"", "-Dx=\" abc\"", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 191 assertEquals("second token is -D\"xyz=a bc\"", "-D\"xyz=a bc\"", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 192 assertEquals("third token is -Dy=\"1 2 3\"", "-Dy=\"1 2 3\"", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 193 assertEquals("forth token is -Dz=1\"2 3\"", "-Dz=1\"2 3\"", cliTokenizer.nextTokenWithoutEscapeAndQuoteChars()); 194 assertEquals("fifth token is -Da=bc", "-Da=bc", cliTokenizer.nextToken()); 195 } 196 197 public void testIncompletQuote() throws Exception { 198 try { 199 CLITokenizer cliTokenizer = new CLITokenizer("abc=def:\"123=4:56\":xyz\"", ":"); 200 } 201 catch (Exception e) { 202 assertEquals("CommandException caught", "com.sun.enterprise.cli.framework.CommandException", e.getClass().getName()); 203 } 204 } 205 206 207 public CLITokenizerTest(String name){ 208 super(name); 209 } 210 211 212 protected void setUp() { 213 } 214 215 216 217 protected void tearDown() { 218 } 219 220 private void nyi(){ 221 fail("Not Yet Implemented"); 222 } 223 224 public static Test suite(){ 225 TestSuite suite = new TestSuite(CLITokenizerTest.class); 226 return suite; 227 } 228 229 public static void main(String args[]) throws Exception { 230 final TestRunner runner= new TestRunner(); 231 final TestResult result = runner.doRun(CLITokenizerTest.suite(), false); 232 System.exit(result.errorCount() + result.failureCount()); 233 } 234 } 235 236 | Popular Tags |