KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > ValueTest


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  *
8  * $Id: ValueTest.java,v 1.1 2001/12/19 18:16:25 jstrachan Exp $
9  */

10
11 package org.apache.commons.cli;
12
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 public class ValueTest extends TestCase
18 {
19
20     public static Test suite() {
21         return new TestSuite(ValueTest.class);
22     }
23
24     private CommandLine _cl = null;
25     private CommandLine _clOptional = null;
26     private Options opts = new Options();
27
28     public ValueTest(String JavaDoc name)
29     {
30         super(name);
31     }
32
33     public void setUp()
34     {
35         opts.addOption("a",
36                        false,
37                        "toggle -a");
38
39         opts.addOption("b",
40                        true,
41                        "set -b");
42
43         opts.addOption("c",
44                        "c",
45                        false,
46                        "toggle -c");
47
48         opts.addOption("d",
49                        "d",
50                        true,
51                        "set -d");
52
53         opts.addOption( OptionBuilder.hasOptionalArg()
54                         .create( 'e') );
55
56         opts.addOption( OptionBuilder.hasOptionalArg()
57                         .withLongOpt( "fish" )
58                         .create( ) );
59
60         opts.addOption( OptionBuilder.hasOptionalArgs()
61                         .withLongOpt( "gravy" )
62                         .create( ) );
63
64         opts.addOption( OptionBuilder.hasOptionalArgs( 2 )
65                         .withLongOpt( "hide" )
66                         .create( ) );
67
68         opts.addOption( OptionBuilder.hasOptionalArgs( 2 )
69                         .create( 'i' ) );
70
71         opts.addOption( OptionBuilder.hasOptionalArgs( )
72                         .create( 'j' ) );
73
74         String JavaDoc[] args = new String JavaDoc[] { "-a",
75             "-b", "foo",
76             "--c",
77             "--d", "bar"
78         };
79
80         try
81         {
82             CommandLineParser parser = new PosixParser();
83             _cl = parser.parse(opts,args);
84         }
85         catch (ParseException e)
86         {
87             fail("Cannot setUp() CommandLine: " + e.toString());
88         }
89     }
90
91     public void tearDown()
92     {
93
94     }
95
96     public void testShortNoArg()
97     {
98         assertTrue( _cl.hasOption("a") );
99         assertNull( _cl.getOptionValue("a") );
100     }
101
102     public void testShortWithArg()
103     {
104         assertTrue( _cl.hasOption("b") );
105         assertNotNull( _cl.getOptionValue("b") );
106         assertEquals( _cl.getOptionValue("b"), "foo");
107     }
108
109     public void testLongNoArg()
110     {
111         assertTrue( _cl.hasOption("c") );
112         assertNull( _cl.getOptionValue("c") );
113     }
114
115     public void testLongWithArg()
116     {
117         assertTrue( _cl.hasOption("d") );
118         assertNotNull( _cl.getOptionValue("d") );
119         assertEquals( _cl.getOptionValue("d"), "bar");
120     }
121
122     public void testShortOptionalArgNoValue()
123     {
124         String JavaDoc[] args = new String JavaDoc[] { "-e"
125         };
126         try
127         {
128             CommandLineParser parser = new PosixParser();
129             CommandLine cmd = parser.parse(opts,args);
130             assertTrue( cmd.hasOption("e") );
131             assertNull( cmd.getOptionValue("e") );
132         }
133         catch (ParseException e)
134         {
135             fail("Cannot setUp() CommandLine: " + e.toString());
136         }
137     }
138
139     public void testShortOptionalArgValue()
140     {
141         String JavaDoc[] args = new String JavaDoc[] { "-e", "everything"
142         };
143         try
144         {
145             CommandLineParser parser = new PosixParser();
146             CommandLine cmd = parser.parse(opts,args);
147             assertTrue( cmd.hasOption("e") );
148             assertEquals( "everything", cmd.getOptionValue("e") );
149         }
150         catch (ParseException e)
151         {
152             fail("Cannot setUp() CommandLine: " + e.toString());
153         }
154     }
155
156     public void testLongOptionalNoValue()
157     {
158         String JavaDoc[] args = new String JavaDoc[] { "--fish"
159         };
160         try
161         {
162             CommandLineParser parser = new PosixParser();
163             CommandLine cmd = parser.parse(opts,args);
164             assertTrue( cmd.hasOption("fish") );
165             assertNull( cmd.getOptionValue("fish") );
166         }
167         catch (ParseException e)
168         {
169             fail("Cannot setUp() CommandLine: " + e.toString());
170         }
171     }
172
173     public void testLongOptionalArgValue()
174     {
175         String JavaDoc[] args = new String JavaDoc[] { "--fish", "face"
176         };
177         try
178         {
179             CommandLineParser parser = new PosixParser();
180             CommandLine cmd = parser.parse(opts,args);
181             assertTrue( cmd.hasOption("fish") );
182             assertEquals( "face", cmd.getOptionValue("fish") );
183         }
184         catch (ParseException e)
185         {
186             fail("Cannot setUp() CommandLine: " + e.toString());
187         }
188     }
189
190     public void testShortOptionalArgValues()
191     {
192         String JavaDoc[] args = new String JavaDoc[] { "-j", "ink", "idea"
193         };
194         try
195         {
196             CommandLineParser parser = new PosixParser();
197             CommandLine cmd = parser.parse(opts,args);
198             assertTrue( cmd.hasOption("j") );
199             assertEquals( "ink", cmd.getOptionValue("j") );
200             assertEquals( "ink", cmd.getOptionValues("j")[0] );
201             assertEquals( "idea", cmd.getOptionValues("j")[1] );
202             assertEquals( cmd.getArgs().length, 0 );
203         }
204         catch (ParseException e)
205         {
206             fail("Cannot setUp() CommandLine: " + e.toString());
207         }
208     }
209
210     public void testLongOptionalArgValues()
211     {
212         String JavaDoc[] args = new String JavaDoc[] { "--gravy", "gold", "garden"
213         };
214         try
215         {
216             CommandLineParser parser = new PosixParser();
217             CommandLine cmd = parser.parse(opts,args);
218             assertTrue( cmd.hasOption("gravy") );
219             assertEquals( "gold", cmd.getOptionValue("gravy") );
220             assertEquals( "gold", cmd.getOptionValues("gravy")[0] );
221             assertEquals( "garden", cmd.getOptionValues("gravy")[1] );
222             assertEquals( cmd.getArgs().length, 0 );
223         }
224         catch (ParseException e)
225         {
226             fail("Cannot setUp() CommandLine: " + e.toString());
227         }
228     }
229
230     public void testShortOptionalNArgValues()
231     {
232         String JavaDoc[] args = new String JavaDoc[] { "-i", "ink", "idea", "isotope", "ice"
233         };
234         try
235         {
236             CommandLineParser parser = new PosixParser();
237             CommandLine cmd = parser.parse(opts,args);
238             assertTrue( cmd.hasOption("i") );
239             assertEquals( "ink", cmd.getOptionValue("i") );
240             assertEquals( "ink", cmd.getOptionValues("i")[0] );
241             assertEquals( "idea", cmd.getOptionValues("i")[1] );
242             assertEquals( cmd.getArgs().length, 2 );
243             assertEquals( "isotope", cmd.getArgs()[0] );
244             assertEquals( "ice", cmd.getArgs()[1] );
245         }
246         catch (ParseException e)
247         {
248             fail("Cannot setUp() CommandLine: " + e.toString());
249         }
250     }
251
252     public void testLongOptionalNArgValues()
253     {
254         String JavaDoc[] args = new String JavaDoc[] { "--hide", "house", "hair", "head"
255         };
256         try
257         {
258             CommandLineParser parser = new PosixParser();
259             CommandLine cmd = parser.parse(opts,args);
260             assertTrue( cmd.hasOption("hide") );
261             assertEquals( "house", cmd.getOptionValue("hide") );
262             assertEquals( "house", cmd.getOptionValues("hide")[0] );
263             assertEquals( "hair", cmd.getOptionValues("hide")[1] );
264             assertEquals( cmd.getArgs().length, 1 );
265             assertEquals( "head", cmd.getArgs()[0] );
266         }
267         catch (ParseException e)
268         {
269             fail("Cannot setUp() CommandLine: " + e.toString());
270         }
271     }
272 }
273
Popular Tags