KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > CLITokenizerTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 /**
27    Note that this test requires resources for testing. These resources
28    are construct4ed from the two files P1 & P2 located in the current
29    directory. If these file names are changed then the corresponding
30    names in this submodules build.xml file should be changed also
31 */

32 import junit.framework.*;
33 import junit.textui.TestRunner;
34
35 /**
36  *
37  * @author jane.young@sun.com
38  * @version $Revision: 1.4 $
39  */

40
41 /**
42    Execute these tests using gmake (and Ant) by:
43    cd <framework>
44    gmake ANT_TARGETS=CLITokenizerTest
45 */

46
47 public class CLITokenizerTest extends TestCase {
48
49   // test simple token with ":" as the delimiter
50
public void testSimpleTokens() throws Exception JavaDoc{
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   // test tokens with quotes and ":" as the delimiter
63
public void testTokensWithQuotes() throws Exception JavaDoc{
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   // test tokens with escape characters and ":" as the delimiter
78
public void testTokensWithEscapeChar() throws Exception JavaDoc{
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   // test tokens with escape characters and quotes and ":" as the delimiter
93
public void testTokensWithEscapeCharAndQuotes() throws Exception JavaDoc{
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   // test tokens with escape characters and quotes and ":" as the delimiter
110
public void testRecursiveTokensWithEscapeCharAndQuotes() throws Exception JavaDoc{
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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   // test token with " " (spaces as the delimiter
164
public void testTokensWithSpace() throws Exception JavaDoc{
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   // test token with " " (spaces as the delimiter)
185
public void testTokensWithSpace2() throws Exception JavaDoc{
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 JavaDoc {
198       try {
199           CLITokenizer cliTokenizer = new CLITokenizer("abc=def:\"123=4:56\":xyz\"", ":");
200       }
201       catch (Exception JavaDoc e) {
202           assertEquals("CommandException caught", "com.sun.enterprise.cli.framework.CommandException", e.getClass().getName());
203       }
204   }
205     
206
207   public CLITokenizerTest(String JavaDoc 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 JavaDoc args[]) throws Exception JavaDoc {
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