KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > sendopts > OneArgumentOptsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.sendopts;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27 import junit.framework.TestCase;
28 import org.netbeans.modules.sendopts.OptionImpl;
29 import org.netbeans.spi.sendopts.Env;
30 import org.netbeans.spi.sendopts.Option;
31
32 /** The basic test to check semantics of getopts behaviour wrt. one argument.
33  *
34  * @author Jaroslav Tulach
35  */

36 public class OneArgumentOptsTest extends TestCase {
37     private CommandLine l;
38     private OneArgProc proc = new OneArgProc();
39     private Option define;
40     
41     public OneArgumentOptsTest(String JavaDoc s) {
42         super(s);
43     }
44
45     protected void tearDown() throws Exception JavaDoc {
46
47         super.tearDown();
48     }
49
50     protected void setUp() throws Exception JavaDoc {
51         Provider.clearAll();
52         
53         // so printed help is in english
54
Locale.setDefault(Locale.US);
55
56         define = Option.requiredArgument('D', "define");
57         Provider.add(proc, define);
58         l = CommandLine.getDefault();
59     }
60     
61     public void testNoArrayIndexOutOfBoundsEx() throws Exception JavaDoc {
62         try {
63             l.process(new String JavaDoc[] { "--define" });
64             fail("Should throw an exception");
65         } catch (CommandException ex) {
66             // ok
67
}
68     }
69     
70     public void testSingleNoArgOptionIsRecognized() throws Exception JavaDoc {
71         l.process(new String JavaDoc[] { "-Dahoj" });
72         assertEquals("Processor found", define, proc.option);
73         assertEquals("Value is correct", "ahoj", proc.value);
74     }
75     
76     public void testLongOptionRecognized() throws Exception JavaDoc {
77         l.process(new String JavaDoc[] { "--define=ahoj" });
78         assertEquals("Processor found for long name", define, proc.option);
79         assertEquals("Value is correct", "ahoj", proc.value);
80     }
81     public void testLongOptionRecognized2() throws Exception JavaDoc {
82         l.process(new String JavaDoc[] { "--define", "ahoj" });
83         assertEquals("Processor found for long name", define, proc.option);
84         assertEquals("Value is correct", "ahoj", proc.value);
85     }
86     
87     public void testAbrevatedNameRecognized() throws Exception JavaDoc {
88         l.process(new String JavaDoc[] { "--def=ahoj" });
89         assertEquals("Processor found for abbrevated name", define, proc.option);
90     }
91     
92     public void testAbrevatedNameRecognized2() throws Exception JavaDoc {
93         l.process(new String JavaDoc[] { "--def", "ahoj" });
94         assertEquals("Processor found for abbrevated name", define, proc.option);
95     }
96
97     public void testIncorrectOptionIdentified() throws Exception JavaDoc {
98         try {
99             l.process(new String JavaDoc[] { "--hell" });
100             fail("This option does not exists");
101         } catch (CommandException ex) {
102             // ok
103
}
104         assertNull("No processor called", proc.option);
105     }
106
107     public void testMissingArgumentIdentified() throws Exception JavaDoc {
108         try {
109             l.process(new String JavaDoc[] { "-D" });
110             fail("This option does not exists");
111         } catch (CommandException ex) {
112             // ok
113
}
114         assertNull("No processor called", proc.option);
115     }
116
117     public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception JavaDoc {
118         try {
119             l.process(new String JavaDoc[] { "-Dx", "--hell" });
120             fail("One option does not exists");
121         } catch (CommandException ex) {
122             // ok
123
}
124         assertNull("No processor called", proc.option);
125     }
126     
127     public void testRequiredArgumentDoesNotSwallowNextOption() throws CommandException {
128         CommandLine l;
129         
130         OneArgProc a = new OneArgProc();
131         OneArgProc b = new OneArgProc();
132         
133         Option one = Option.requiredArgument('1', "one");
134         Provider.add(a, one);
135
136         Option two = Option.optionalArgument((char)-1, "two");
137         Provider.add(b, two);
138         
139         l = CommandLine.getDefault();
140         try {
141             l.process(new String JavaDoc[] { "--one", "--two" });
142             fail("Cannot succeed as --one needs an argument");
143         } catch (CommandException ex) {
144             // ok
145
}
146         
147         assertNull("A not called", a.option);
148         assertNull("B not called", b.option);
149
150         assertNull("A not called", a.option);
151         assertNull("B not called", b.option);
152         
153         l.process(new String JavaDoc[] { "--one", "--", "--two" });
154         
155         assertEquals("A called", one, a.option);
156         assertEquals("B not called", null, b.option);
157         assertEquals("A value", "--two", a.value);
158         
159         a.option = null;
160         b.option = null;
161         a.value = null;
162         
163         l.process(new String JavaDoc[] { "-1--two" });
164         
165         assertEquals("A called", one, a.option);
166         assertEquals("B not called", null, b.option);
167         assertEquals("A value", "--two", a.value);
168         
169     }
170     public void testPrintedUsage() throws Exception JavaDoc {
171         StringWriter JavaDoc w = new StringWriter JavaDoc();
172         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
173
174         l.usage(pw);
175
176         Matcher JavaDoc m = Pattern.compile("--define <arg>").matcher(w.toString());
177         if (!m.find()) {
178             fail("--define <arg> should be there:\n" + w.toString());
179         }
180
181         int x = w.toString().indexOf('\n');
182         if (x == -1) {
183             fail("There should be one line: " + w.toString());
184         }
185         x = w.toString().indexOf('\n', x + 1);
186         assertEquals("No next line", -1, x);
187     }
188     
189     static final class OneArgProc implements Processor {
190         Option option;
191         String JavaDoc value;
192
193         public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
194             assertEquals("One option", 1, values.size());
195             assertNull("Not processed yet", option);
196             Option o = values.keySet().iterator().next();
197             String JavaDoc v = values.values().iterator().next()[0];
198             assertNotNull("An option is provided", o);
199             option = o;
200             value = v;
201         }
202     }
203 }
204
Popular Tags