KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.HashMap JavaDoc;
37
38 /**
39  *
40  * @author jane.young@sun.com
41  * @version $Revision: 1.4 $
42  */

43
44 /**
45    Execute these tests using gmake (and Ant) by:
46    cd <framework>
47    gmake ANT_TARGETS=CommandLineParserTest
48 */

49
50 public class CommandLineParserTest extends TestCase {
51     public void testInsertOperands() throws Exception JavaDoc {
52         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "-t=false", "operand"};
53         CommandLineParser clp = new CLP(args, vc1);
54         HashMap JavaDoc options = clp.getOptionsList();
55         assertTrue(clp.getOperands().contains("operand"));
56     }
57         
58     public void testOptionWithEqualsSign() throws Exception JavaDoc {
59         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "--terse=false"};
60         CommandLineParser clp = new CLP(args, vc1);
61         HashMap JavaDoc options = clp.getOptionsList();
62         assertEquals("short option for terse if true", "false", (String JavaDoc)options.get("terse"));
63     }
64     
65         
66     public void testShortOptionsGroup() throws Exception JavaDoc {
67         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "-ti"};
68         CommandLineParser clp = new CLP(args, vc1);
69         HashMap JavaDoc options = clp.getOptionsList();
70         assertEquals("short option for terse if true", "true", (String JavaDoc)options.get("terse"));
71         assertEquals("short option for interactive is false", "false", (String JavaDoc)options.get("interactive"));
72     }
73     
74         
75     public void testToString() throws Exception JavaDoc {
76         CommandLineParser clp = new CLP();
77         assertEquals("\n**********\nname = null\nOptions = {}\nOperands = []\n**********\n", clp.toString());
78     }
79         
80     public void testLocalizedString() throws Exception JavaDoc {
81         CommandLineParser clp = new CLP();
82         assertEquals("Key not found (this key)", clp.getLocalizedString("this key", (Object JavaDoc []) null));
83     }
84     
85
86     public void testNullCommand() throws Exception JavaDoc {
87         CommandLineParser clp = new CLP(vc1);
88         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin"};
89         clp.parseCommandLine(args);
90         HashMap JavaDoc options = clp.getOptionsList();
91         assertEquals("terse option is by default is false", "false", (String JavaDoc)options.get("terse"));
92         assertEquals("interactive option is by default is true", "true", (String JavaDoc)options.get("interactive"));
93         assertTrue(clp.getOperands().isEmpty());
94     }
95         
96     public void testSimpleConstruction() throws HelpException {
97         CommandLineParser clp = new CLP();
98         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin"};
99         try {
100             clp.parseCommandLine(args);
101             fail("Expected to get a CommandValidationException saying that there was no command");
102         }
103         catch (CommandValidationException cve){
104             assertEquals("CLI001 Invalid Command, samplecommand.", cve.getMessage());
105         }
106     }
107     
108       // test the default boolean options
109
public void testDefaultBooleanOptions() throws Exception JavaDoc{
110         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin"};
111         CommandLineParser clp = new CLP(args, vc1);
112         HashMap JavaDoc options = clp.getOptionsList();
113         assertEquals("terse option is by default is false", "false", (String JavaDoc)options.get("terse"));
114         assertEquals("interactive option is by default is true", "true", (String JavaDoc)options.get("interactive"));
115     }
116
117     
118       // test the default boolean short option
119
public void testShortBooleanOptions() throws Exception JavaDoc{
120         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "-i", "-t"};
121         CommandLineParser clp = new CLP(args, vc1);
122         HashMap JavaDoc options = clp.getOptionsList();
123         assertEquals("short option for terse if true", "true", (String JavaDoc)options.get("terse"));
124         assertEquals("short option for interactive is false", "false", (String JavaDoc)options.get("interactive"));
125     }
126
127       // test the default boolean short option
128
public void testLongBooleanOptions() throws Exception JavaDoc{
129         String JavaDoc [] args = {"samplecommand", "--user", "admin", "--password", "adminadmin", "--interactive", "--terse"};
130         CommandLineParser clp = new CLP(args, vc1);
131         HashMap JavaDoc options = clp.getOptionsList();
132         assertEquals("long option for terse if true", "true", (String JavaDoc)options.get("terse"));
133         assertEquals("long option for interactive is true", "true", (String JavaDoc)options.get("interactive"));
134     }
135
136   
137     public CommandLineParserTest(String JavaDoc name){
138         super(name);
139     }
140
141     ValidCommand vc1;
142
143     protected void setUp() {
144         ValidOption vo1 = new ValidOption("user", "string", 1, "");
145         ValidOption vo2 = new ValidOption("password", "string", 1, "");
146         ValidOption vo3 = new ValidOption("host", "string", 1, "");
147         ValidOption vo4 = new ValidOption("port", "string", 1, "");
148         ValidOption vo5 = new ValidOption("interactive", "boolean", 3, "true");
149         vo5.setShortName("i");
150         ValidOption vo6 = new ValidOption("terse", "boolean", 3, "false");
151         vo6.setShortName("t");
152         ValidOption[] validOptions = new ValidOption[] {vo3, vo4, vo6};
153         ValidOption[] requiredOptions = new ValidOption[] {vo1, vo2, vo5};
154         vc1 = new ValidCommand("sampleCommand",
155                                "1",
156                                new Vector JavaDoc(Arrays.asList(validOptions)),
157                                new Vector JavaDoc(Arrays.asList(requiredOptions)),
158                                new Vector JavaDoc(),
159                                "sampleCommand");
160     }
161   
162   
163
164     protected void tearDown() {
165     }
166
167     private void nyi(){
168         fail("Not Yet Implemented");
169     }
170
171     public static Test suite(){
172         TestSuite suite = new TestSuite(CommandLineParserTest.class);
173         return suite;
174     }
175
176     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
177         final TestRunner runner= new TestRunner();
178         final TestResult result = runner.doRun(CommandLineParserTest.suite(), false);
179         System.exit(result.errorCount() + result.failureCount());
180     }
181
182     private class CLP extends CommandLineParser
183     {
184         CLP(ValidCommand vc){
185             super(vc);
186         }
187         
188         CLP(){
189             super();
190         }
191         
192         CLP(String JavaDoc [] s, ValidCommand vc) throws CommandValidationException, HelpException {
193             super(s, vc);
194         }
195         
196         HashMap JavaDoc getEnvironment(){
197             return new HashMap JavaDoc();
198         }
199     }
200     
201
202 }
203
204
Popular Tags