KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > support > ArgParserTest


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

29  
30 package com.sun.cli.jmx.support;
31
32 import junit.framework.TestCase;
33
34 import com.sun.cli.util.stringifier.*;
35
36 import com.sun.cli.jmx.support.ParseResult;
37 import com.sun.cli.jmx.support.ParseResultStringifier;
38
39 public final class ArgParserTest extends TestCase
40 {
41         void
42     dm( Object JavaDoc o )
43     {
44         System.out.println( o.toString() );
45     }
46     
47         public
48     ArgParserTest()
49     {
50     }
51     
52         public void
53     testCreate()
54     {
55         new ArgParserImpl();
56     }
57     
58         public void
59     testEmpty()
60         throws ArgParserException
61     {
62         final ArgParserImpl ap = new ArgParserImpl( );
63         
64         final ParseResult [] results = ap.Parse( "", false );
65         
66         assertEquals( 0, results.length );
67     }
68     
69         ParseResult
70     stringWithCast( String JavaDoc str )
71     {
72         return( new ParseResult( ParseResult.LITERAL_STRING, str, "String" ) );
73     }
74     
75         ParseResult
76     stringWithoutCast( String JavaDoc str )
77     {
78         return( new ParseResult( ParseResult.OTHER, str ) );
79     }
80         ParseResult
81     otherWithCast( String JavaDoc str, String JavaDoc cast )
82     {
83         return( new ParseResult( ParseResult.OTHER, str, cast ) );
84     }
85         ParseResult
86     otherWithoutCast( String JavaDoc str )
87     {
88         return( new ParseResult( ParseResult.OTHER, str) );
89     }
90
91         String JavaDoc
92     quoteString( String JavaDoc s )
93     {
94         return( '\"' + s + '\"' );
95     }
96
97     
98         public void
99     testSingleNonNamedStringWithoutCast()
100         throws ArgParserException
101     {
102         final ParseResult [] results = new ArgParserImpl().Parse( "hello", false );
103         
104         assertEquals( 1, results.length );
105         assertEquals( stringWithoutCast( "hello" ), results[ 0 ] );
106     }
107     
108     
109         public void
110     testSingleNonNamedStringWithCast()
111         throws ArgParserException
112     {
113         final ParseResult [] results = new ArgParserImpl().Parse( "(String)hello", false );
114         
115         assertEquals( 1, results.length );
116         assertEquals( stringWithCast( "hello" ), results[ 0 ] );
117     }
118     
119         public void
120     testEmptyStringWithCast()
121         throws ArgParserException
122     {
123         final ParseResult [] results = new ArgParserImpl().Parse( "(String)", false );
124         
125         assertEquals( 1, results.length );
126         assertEquals( stringWithCast( "" ), results[ 0 ] );
127     }
128     
129         public void
130     testMultipleStringsWithCast()
131         throws ArgParserException
132     {
133         final String JavaDoc input = "(String)s1,\"s2\",(String)s3";
134         final ParseResult [] results = new ArgParserImpl().Parse( input, false );
135         
136         assertEquals( 3, results.length );
137         final ParseResult [] expected =
138         {
139             stringWithCast( "s1" ),
140             stringWithCast( "s2" ),
141             stringWithCast( "s3" ),
142         };
143         assertEquals( expected[ 0 ], results[ 0 ] );
144         assertEquals( expected[ 1 ], results[ 1 ] );
145         assertEquals( expected[ 2 ], results[ 2 ] );
146     }
147     
148     final static String JavaDoc TAB_STR = "\\t";
149     final static String JavaDoc NEWLINE_STR = "\\n";
150     final static String JavaDoc RETURN_STR = "\\r";
151     final static String JavaDoc ESCAPE_CHAR_STR = "\\\\";
152     
153         public void
154     testEscapeCharsInUnquotedString()
155         throws ArgParserException
156     {
157         final String JavaDoc input = TAB_STR + NEWLINE_STR + RETURN_STR + ESCAPE_CHAR_STR;
158         final ParseResult [] results = new ArgParserImpl().Parse( input, false );
159         
160         assertEquals( 1, results.length );
161         assertEquals( stringWithoutCast( "\t\n\r\\" ), results[ 0 ] );
162     }
163     
164         public void
165     testEscapeCharsInQuotedString()
166         throws ArgParserException
167     {
168         final String JavaDoc input = TAB_STR + NEWLINE_STR + RETURN_STR + ESCAPE_CHAR_STR;
169         final ParseResult [] results = new ArgParserImpl().Parse( quoteString( input ), false );
170         
171         assertEquals( 1, results.length );
172         assertEquals( stringWithCast( "\t\n\r\\" ), results[ 0 ] );
173     }
174     
175         public void
176     testEmptyArray()
177         throws ArgParserException
178     {
179         final String JavaDoc input = "{}";
180         final ParseResult [] results = new ArgParserImpl().Parse( input, false );
181         
182         assertEquals( 1, results.length );
183         final ParseResult expected =
184                 new ParseResult( ParseResult.ARRAY, new ParseResult [ 0 ] );
185         assertEquals( expected, results[ 0 ] );
186     }
187     
188         public void
189     testMultiArray()
190         throws ArgParserException
191     {
192         final String JavaDoc input = "{\"hello\",1,(String)there}";
193         final ParseResult [] results = new ArgParserImpl().Parse( input, false );
194         
195         assertEquals( 1, results.length );
196         
197         final ParseResult [] expectedArrayElements =
198         {
199             stringWithCast( "hello" ),
200             otherWithoutCast( "1" ),
201             stringWithCast( "there" ),
202         };
203         final ParseResult expected = new ParseResult( ParseResult.ARRAY, expectedArrayElements );
204         
205         assertEquals( 1, results.length );
206         assertEquals( expected, results[ 0 ] );
207     }
208
209         private void
210     testExpr( boolean namedArgs, String JavaDoc expr )
211         throws Exception JavaDoc
212     {
213         boolean success = false;
214         
215         final ParseResult [] results = new ArgParserImpl().Parse( expr, namedArgs );
216     }
217     
218         public void
219     testNonNamedSuccessCases( )
220         throws Exception JavaDoc
221     {
222         // test valid cases
223
boolean success;
224         
225         final String JavaDoc [] expressions = new String JavaDoc []
226         {
227             "1",
228             "999.0003777",
229             "\"\"", // empty String
230
")", // a valid String
231
"}", // a valid String
232
"=", // a valid String
233
"(boolean)true",
234             "(Boolean)false",
235             "hello",
236             "(String)hello",
237             "\\(String)",
238             "\",,,,,,,,,\"",
239             "\"hello\"",
240             "1,2,3,4,5,6,\"hello\\,,,,,goodbye\",hello\\,goodbye",
241             "{}",
242             "{,,,,}",
243             "{1}",
244             "{1,2}",
245             "{1,2,3,4,5,6,hello}",
246             "{1,2,3,4,5,6,{hello,world}}",
247             "(Object){(int)1,(long)2,(Integer)3,4,5,6,(String){(String)\"hello\",world}}"
248         };
249         final int numExprs = expressions.length;
250         
251         final String JavaDoc testName = "argName";
252         
253         String JavaDoc togetherList = "";
254         String JavaDoc togetherNamed = "";
255         for( int i = 0; i < numExprs; ++i )
256         {
257             final String JavaDoc testString = expressions[ i ];
258             
259             try
260             {
261                 testExpr( false, testString );
262             }
263             catch( Exception JavaDoc e )
264             {
265                 fail( "ERROR: case failed: " + expressions[ i ] );
266             }
267             
268             togetherList = togetherList + testString + ",";
269         }
270         
271         testExpr( false, togetherList );
272         testExpr( false, "{" + togetherList + "}" );
273     }
274     
275     
276         public void
277     testNamedSuccessCases( )
278         throws Exception JavaDoc
279     {
280         // test valid cases
281
boolean success;
282         
283         final String JavaDoc [] expressions = new String JavaDoc []
284         {
285             "test=hello\\,world",
286             "hello=1,world=there",
287             "arg1=1,arg2=2,arg3=3,argString=(String)hello\\,world",
288             "count=1",
289             "name=server1,cluster=cluster1,type=instance",
290             "empty1=,empty2=,empty3="
291         };
292         final int numExprs = expressions.length;
293         
294         String JavaDoc togetherList = "";
295         for( int i = 0; i < numExprs; ++i )
296         {
297             final String JavaDoc testString = expressions[ i ];
298             
299             try
300             {
301                 testExpr( true, testString );
302             }
303             catch( Exception JavaDoc e )
304             {
305                 fail( "ERROR: case failed: " + expressions[ i ] + "\n" + e.toString() );
306             }
307             
308             togetherList = togetherList + testString + ",";
309         }
310         
311         testExpr( false, togetherList );
312     }
313     
314         public void
315     TestFailureCases()
316         throws Exception JavaDoc
317     {
318         boolean success;
319         
320         final String JavaDoc [] failureExpressions = new String JavaDoc []
321         {
322             "{",
323             "(",
324             "()",
325             "(a-b-c)",
326             "(.abc)",
327             "({",
328             "(int)",
329             "\"\"\"",
330             "\"",
331         };
332         final int numFailureExprs = failureExpressions.length;
333         
334         String JavaDoc together = "{";
335         for( int i = 0; i < numFailureExprs; ++i )
336         {
337             try
338             {
339                 testExpr( false, failureExpressions[ i ] );
340             }
341             catch( Exception JavaDoc e )
342             {
343                 fail( "ERROR: case failed: " + failureExpressions[ i ] + "\n" + e.toString() );
344             }
345             
346             together = together + failureExpressions[ i ] + ",";
347         }
348         together = together + "}";
349         
350         try
351         {
352             testExpr( false, together );
353         }
354         catch( Exception JavaDoc e )
355         {
356             fail( "ERROR: case failed: " + together + "\n" + e.toString() );
357         }
358     }
359 }
360
361
Popular Tags