KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > util > TokenizerTest


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  
25 /*
26  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/util/TokenizerTest.java,v 1.3 2005/12/25 03:46:00 tcfujii Exp $
27  * $Revision: 1.3 $
28  * $Date: 2005/12/25 03:46:00 $
29  */

30  
31 package com.sun.cli.util;
32
33 import java.util.Iterator JavaDoc;
34
35 import junit.framework.TestCase;
36
37
38
39 public class TokenizerTest extends TestCase
40 {
41         public
42     TokenizerTest( String JavaDoc name )
43     {
44         super( name );
45     }
46     
47     public final static String JavaDoc WHITE_SPACE = " \t";
48     public final static char ESCAPE_CHAR = '\\';
49     public final static String JavaDoc ESCAPABLE_CHARS = "" + ESCAPE_CHAR + WHITE_SPACE + '\"';
50     
51         Tokenizer
52     create( String JavaDoc input, boolean multipleDelimsCountAsOne )
53     {
54         return( new TokenizerImpl( input,
55             WHITE_SPACE, multipleDelimsCountAsOne, ESCAPE_CHAR, ESCAPABLE_CHARS) );
56     }
57     
58         Tokenizer
59     create( String JavaDoc 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 JavaDoc e )
79         {
80         }
81     }
82     
83     
84         public void
85     testLeadingDelim()
86     {
87         final String JavaDoc input = WHITE_SPACE.charAt( 0 ) + "hello";
88         final String JavaDoc [] tokens = create( input, false ).getTokens();
89         
90         assertEquals( 2, tokens.length );
91     }
92     
93     
94         public void
95     testSingleDelimOnly()
96     {
97         final String JavaDoc input = "" + WHITE_SPACE.charAt( 0 );
98         final String JavaDoc [] tokens = create( input, false ).getTokens();
99         
100         assertEquals( 2, tokens.length );
101     }
102     
103         public void
104     testSingleToken()
105     {
106         final String JavaDoc input = "hello";
107         final String JavaDoc [] 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 JavaDoc input = "hello there 1 2 3 4 5";
117         final Iterator JavaDoc iter = create( input ).iterator();
118         
119         int count = 0;
120         String JavaDoc 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 JavaDoc input1 = "hello there 1 2 3 4 5";
134         final String JavaDoc input2 = "hello\tthere 1\t2 3 4\t5";
135         final Iterator JavaDoc iter1 = create( input1 ).iterator();
136         final Iterator JavaDoc 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 JavaDoc TEST = "test" + BACKSLASH + "n" + BACKSLASH + "r";
152         final String JavaDoc [] 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 JavaDoc TEST = "test" + BACKSLASH + "xyz";
162         final String JavaDoc [] 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         // create a String which each escapable character is represented
172
final StringBuffer JavaDoc b = new StringBuffer JavaDoc();
173         for( int i = 0; i < ESCAPABLE_CHARS.length(); ++i )
174         {
175             b.append( "\\" + ESCAPABLE_CHARS.charAt( i ) );
176         }
177         final String JavaDoc [] 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 JavaDoc input = "hello" + WHITE_SPACE.charAt( 0 );
188         final String JavaDoc [] tokens = create( input, false ).getTokens();
189         
190         assertEquals( 2, tokens.length );
191     }
192     
193         public void
194     testTrailingDelimiter2()
195     {
196         final String JavaDoc input = "hello" + WHITE_SPACE.charAt( 0 ) +
197                             "\"there\"" + WHITE_SPACE.charAt( 0 );
198         final String JavaDoc [] tokens = create( input, false ).getTokens();
199         
200         assertEquals( 3, tokens.length );
201     }
202     
203         public void
204     testMultipleDelimsWithNoData()
205     {
206         final String JavaDoc input = "" + WHITE_SPACE.charAt( 0 );
207         final String JavaDoc [] tokens = create( input, false ).getTokens();
208         
209         assertEquals( input.length() + 1, tokens.length );
210     }
211     
212         public void
213     testMultipleDelimsAsOne()
214     {
215         final String JavaDoc input = "hello" + WHITE_SPACE + "there" + WHITE_SPACE;
216         final String JavaDoc [] tokens = create( input, true ).getTokens();
217         
218         assertEquals( 3, tokens.length );
219     }
220     
221         public void
222     testQuotedString()
223     {
224         final String JavaDoc input = "\"hello there\" \"another\" \"3\" \"words\" \"\"";
225         final String JavaDoc [] 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     //-------------------------------------------------------------------------
235

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