KickJava   Java API By Example, From Geeks To Geeks.

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


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: OptionGroupTest.java,v 1.1 2002/04/23 16:08:02 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 /**
18  * @author John Keyes (john at integralsource.com)
19  * @version $Revision: 1.1 $
20  */

21 public class OptionGroupTest extends TestCase
22 {
23
24     private Options _options = null;
25     private CommandLineParser parser = new PosixParser();
26
27
28     public static Test suite()
29     {
30         return new TestSuite ( OptionGroupTest.class );
31     }
32
33     public OptionGroupTest( String JavaDoc name )
34     {
35         super( name );
36     }
37
38     public void setUp()
39     {
40         Option file = new Option( "f", "file", false, "file to process" );
41         Option dir = new Option( "d", "directory", false, "directory to process" );
42         OptionGroup group = new OptionGroup();
43         group.addOption( file );
44         group.addOption( dir );
45         _options = new Options().addOptionGroup( group );
46
47         Option section = new Option( "s", "section", false, "section to process" );
48         Option chapter = new Option( "c", "chapter", false, "chapter to process" );
49         OptionGroup group2 = new OptionGroup();
50         group2.addOption( section );
51         group2.addOption( chapter );
52
53         _options.addOptionGroup( group2 );
54         _options.addOption( "r", "revision", false, "revision number" );
55     }
56
57     public void tearDown()
58     {
59     }
60
61     public void testSingleOptionFromGroup()
62     {
63         String JavaDoc[] args = new String JavaDoc[] { "-f" };
64
65         try
66         {
67             CommandLine cl = parser.parse( _options, args);
68
69             assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
70             assertTrue( "Confirm -f is set", cl.hasOption("f") );
71             assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
72             assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
73             assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
74             assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
75         }
76         catch (ParseException e)
77         {
78             fail( e.toString() );
79         }
80     }
81
82     public void testSingleOption()
83     {
84         String JavaDoc[] args = new String JavaDoc[] { "-r" };
85
86         try
87         {
88             CommandLine cl = parser.parse( _options, args);
89
90             assertTrue( "Confirm -r is set", cl.hasOption("r") );
91             assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") );
92             assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
93             assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
94             assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
95             assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
96         }
97         catch (ParseException e)
98         {
99             fail( e.toString() );
100         }
101     }
102
103     public void testTwoValidOptions()
104     {
105         String JavaDoc[] args = new String JavaDoc[] { "-r", "-f" };
106
107         try
108         {
109             CommandLine cl = parser.parse( _options, args);
110
111             assertTrue( "Confirm -r is set", cl.hasOption("r") );
112             assertTrue( "Confirm -f is set", cl.hasOption("f") );
113             assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
114             assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
115             assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
116             assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
117         }
118         catch (ParseException e)
119         {
120             fail( e.toString() );
121         }
122     }
123
124     public void testSingleLongOption()
125     {
126         String JavaDoc[] args = new String JavaDoc[] { "--file" };
127
128         try
129         {
130             CommandLine cl = parser.parse( _options, args);
131
132             assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
133             assertTrue( "Confirm -f is set", cl.hasOption("f") );
134             assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
135             assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
136             assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
137             assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
138         }
139         catch (ParseException e)
140         {
141             fail( e.toString() );
142         }
143     }
144
145     public void testTwoValidLongOptions()
146     {
147         String JavaDoc[] args = new String JavaDoc[] { "--revision", "--file" };
148
149         try
150         {
151             CommandLine cl = parser.parse( _options, args);
152
153             assertTrue( "Confirm -r is set", cl.hasOption("r") );
154             assertTrue( "Confirm -f is set", cl.hasOption("f") );
155             assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
156             assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
157             assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
158             assertTrue( "Confirm no extra args", cl.getArgList().size() == 0);
159         }
160         catch (ParseException e)
161         {
162             fail( e.toString() );
163         }
164     }
165
166     public void testNoOptionsExtraArgs()
167     {
168         String JavaDoc[] args = new String JavaDoc[] { "arg1", "arg2" };
169
170         try
171         {
172             CommandLine cl = parser.parse( _options, args);
173
174             assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
175             assertTrue( "Confirm -f is NOT set", !cl.hasOption("f") );
176             assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
177             assertTrue( "Confirm -s is NOT set", !cl.hasOption("s") );
178             assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
179             assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2);
180         }
181         catch (ParseException e)
182         {
183             fail( e.toString() );
184         }
185     }
186
187     public void testTwoOptionsFromGroup()
188     {
189         String JavaDoc[] args = new String JavaDoc[] { "-f", "-d" };
190
191         try
192         {
193             CommandLine cl = parser.parse( _options, args);
194             fail( "two arguments from group not allowed" );
195         }
196         catch (ParseException e)
197         {
198             if( !( e instanceof AlreadySelectedException ) )
199             {
200                 fail( "incorrect exception caught:" + e.getMessage() );
201             }
202         }
203     }
204
205     public void testTwoLongOptionsFromGroup()
206     {
207         String JavaDoc[] args = new String JavaDoc[] { "--file", "--directory" };
208
209         try
210         {
211             CommandLine cl = parser.parse( _options, args);
212             fail( "two arguments from group not allowed" );
213         }
214         catch (ParseException e)
215         {
216             if( !( e instanceof AlreadySelectedException ) )
217             {
218                 fail( "incorrect exception caught:" + e.getMessage() );
219             }
220         }
221     }
222
223     public void testTwoOptionsFromDifferentGroup()
224     {
225         String JavaDoc[] args = new String JavaDoc[] { "-f", "-s" };
226
227         try
228         {
229             CommandLine cl = parser.parse( _options, args);
230             assertTrue( "Confirm -r is NOT set", !cl.hasOption("r") );
231             assertTrue( "Confirm -f is set", cl.hasOption("f") );
232             assertTrue( "Confirm -d is NOT set", !cl.hasOption("d") );
233             assertTrue( "Confirm -s is set", cl.hasOption("s") );
234             assertTrue( "Confirm -c is NOT set", !cl.hasOption("c") );
235             assertTrue( "Confirm NO extra args", cl.getArgList().size() == 0);
236         }
237         catch (ParseException e)
238         {
239             fail( e.toString() );
240         }
241     }
242
243
244 }
245
Popular Tags