001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.sendopts;
042:
043: import java.io.PrintWriter;
044: import java.io.StringWriter;
045: import java.util.Locale;
046: import java.util.Map;
047: import java.util.regex.Matcher;
048: import java.util.regex.Pattern;
049: import junit.framework.TestCase;
050: import org.netbeans.modules.sendopts.OptionImpl;
051: import org.netbeans.spi.sendopts.Env;
052: import org.netbeans.spi.sendopts.Option;
053:
054: /** The basic test to check semantics of getopts behaviour wrt. one argument.
055: *
056: * @author Jaroslav Tulach
057: */
058: public class OneArgumentOptsTest extends TestCase {
059: private CommandLine l;
060: private OneArgProc proc = new OneArgProc();
061: private Option define;
062:
063: public OneArgumentOptsTest(String s) {
064: super (s);
065: }
066:
067: protected void tearDown() throws Exception {
068:
069: super .tearDown();
070: }
071:
072: protected void setUp() throws Exception {
073: Provider.clearAll();
074:
075: // so printed help is in english
076: Locale.setDefault(Locale.US);
077:
078: define = Option.requiredArgument('D', "define");
079: Provider.add(proc, define);
080: l = CommandLine.getDefault();
081: }
082:
083: public void testNoArrayIndexOutOfBoundsEx() throws Exception {
084: try {
085: l.process(new String[] { "--define" });
086: fail("Should throw an exception");
087: } catch (CommandException ex) {
088: // ok
089: }
090: }
091:
092: public void testSingleNoArgOptionIsRecognized() throws Exception {
093: l.process(new String[] { "-Dahoj" });
094: assertEquals("Processor found", define, proc.option);
095: assertEquals("Value is correct", "ahoj", proc.value);
096: }
097:
098: public void testLongOptionRecognized() throws Exception {
099: l.process(new String[] { "--define=ahoj" });
100: assertEquals("Processor found for long name", define,
101: proc.option);
102: assertEquals("Value is correct", "ahoj", proc.value);
103: }
104:
105: public void testLongOptionRecognized2() throws Exception {
106: l.process(new String[] { "--define", "ahoj" });
107: assertEquals("Processor found for long name", define,
108: proc.option);
109: assertEquals("Value is correct", "ahoj", proc.value);
110: }
111:
112: public void testAbrevatedNameRecognized() throws Exception {
113: l.process(new String[] { "--def=ahoj" });
114: assertEquals("Processor found for abbrevated name", define,
115: proc.option);
116: }
117:
118: public void testAbrevatedNameRecognized2() throws Exception {
119: l.process(new String[] { "--def", "ahoj" });
120: assertEquals("Processor found for abbrevated name", define,
121: proc.option);
122: }
123:
124: public void testIncorrectOptionIdentified() throws Exception {
125: try {
126: l.process(new String[] { "--hell" });
127: fail("This option does not exists");
128: } catch (CommandException ex) {
129: // ok
130: }
131: assertNull("No processor called", proc.option);
132: }
133:
134: public void testMissingArgumentIdentified() throws Exception {
135: try {
136: l.process(new String[] { "-D" });
137: fail("This option does not exists");
138: } catch (CommandException ex) {
139: // ok
140: }
141: assertNull("No processor called", proc.option);
142: }
143:
144: public void testNoProcessorCalledWhenOneOptionIsNotKnown()
145: throws Exception {
146: try {
147: l.process(new String[] { "-Dx", "--hell" });
148: fail("One option does not exists");
149: } catch (CommandException ex) {
150: // ok
151: }
152: assertNull("No processor called", proc.option);
153: }
154:
155: public void testRequiredArgumentDoesNotSwallowNextOption()
156: throws CommandException {
157: CommandLine l;
158:
159: OneArgProc a = new OneArgProc();
160: OneArgProc b = new OneArgProc();
161:
162: Option one = Option.requiredArgument('1', "one");
163: Provider.add(a, one);
164:
165: Option two = Option.optionalArgument((char) -1, "two");
166: Provider.add(b, two);
167:
168: l = CommandLine.getDefault();
169: try {
170: l.process(new String[] { "--one", "--two" });
171: fail("Cannot succeed as --one needs an argument");
172: } catch (CommandException ex) {
173: // ok
174: }
175:
176: assertNull("A not called", a.option);
177: assertNull("B not called", b.option);
178:
179: assertNull("A not called", a.option);
180: assertNull("B not called", b.option);
181:
182: l.process(new String[] { "--one", "--", "--two" });
183:
184: assertEquals("A called", one, a.option);
185: assertEquals("B not called", null, b.option);
186: assertEquals("A value", "--two", a.value);
187:
188: a.option = null;
189: b.option = null;
190: a.value = null;
191:
192: l.process(new String[] { "-1--two" });
193:
194: assertEquals("A called", one, a.option);
195: assertEquals("B not called", null, b.option);
196: assertEquals("A value", "--two", a.value);
197:
198: }
199:
200: public void testPrintedUsage() throws Exception {
201: StringWriter w = new StringWriter();
202: PrintWriter pw = new PrintWriter(w);
203:
204: l.usage(pw);
205:
206: Matcher m = Pattern.compile("--define <arg>").matcher(
207: w.toString());
208: if (!m.find()) {
209: fail("--define <arg> should be there:\n" + w.toString());
210: }
211:
212: int x = w.toString().indexOf('\n');
213: if (x == -1) {
214: fail("There should be one line: " + w.toString());
215: }
216: x = w.toString().indexOf('\n', x + 1);
217: assertEquals("No next line", -1, x);
218: }
219:
220: static final class OneArgProc implements Processor {
221: Option option;
222: String value;
223:
224: public void process(Env env, Map<Option, String[]> values)
225: throws CommandException {
226: assertEquals("One option", 1, values.size());
227: assertNull("Not processed yet", option);
228: Option o = values.keySet().iterator().next();
229: String v = values.values().iterator().next()[0];
230: assertNotNull("An option is provided", o);
231: option = o;
232: value = v;
233: }
234: }
235: }
|