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.util.Map;
044: import junit.framework.TestCase;
045: import org.netbeans.modules.sendopts.OptionImpl;
046: import org.netbeans.spi.sendopts.Env;
047: import org.netbeans.spi.sendopts.Option;
048:
049: /** The basic test to check semantics of getopts behaviour wrt. one optional argument.
050: *
051: * @author Jaroslav Tulach
052: */
053: public class OptionalArgumentOptsTest extends TestCase {
054: private CommandLine l;
055: private OneArgProc proc = new OneArgProc();
056: private Option define;
057:
058: public OptionalArgumentOptsTest(String s) {
059: super (s);
060: }
061:
062: protected void tearDown() throws Exception {
063:
064: super .tearDown();
065: }
066:
067: protected void setUp() throws Exception {
068: Provider.clearAll();
069:
070: define = Option.optionalArgument('D', "define");
071: Provider.add(proc, define);
072: l = CommandLine.getDefault();
073: }
074:
075: public void testSingleNoArgOptionIsRecognized() throws Exception {
076: l.process(new String[] { "-Dahoj" });
077: assertEquals("Processor found", define, proc.option);
078: assertEquals("Value is correct", "ahoj", proc.value);
079: }
080:
081: public void testLongOptionRecognized() throws Exception {
082: l.process(new String[] { "--define=ahoj" });
083: assertEquals("Processor found for long name", define,
084: proc.option);
085: assertEquals("Value is correct", "ahoj", proc.value);
086: }
087:
088: public void testLongOptionRecognized2() throws Exception {
089: try {
090: l.process(new String[] { "--define", "ahoj" });
091: fail("ahoj is not used as optional argument, according to getopts");
092: } catch (CommandException ex) {
093: // ok
094: }
095:
096: assertNull("Processor found for long name", proc.option);
097: assertNull("Value is unset", proc.value);
098: }
099:
100: public void testAbrevatedNameRecognized() throws Exception {
101: l.process(new String[] { "--def=ahoj" });
102: assertEquals("Processor found for abbrevated name", define,
103: proc.option);
104: }
105:
106: public void testAbrevatedNameRecognized2() throws Exception {
107: try {
108: l.process(new String[] { "--def", "ahoj" });
109: fail("Optional argument must follow the option with = sign, cannot be left out");
110: } catch (CommandException ex) {
111: // ok
112: }
113: assertNull("No Processor called", proc.option);
114: }
115:
116: public void testAbrevatedNameRecognizedWithoutArg()
117: throws Exception {
118: l.process(new String[] { "--def" });
119: assertEquals("Processor found for abbrevated name", define,
120: proc.option);
121: assertNull("Value is null", proc.value);
122: }
123:
124: public void testShortNameRecognizedWithoutArg() throws Exception {
125: l.process(new String[] { "-D" });
126: assertEquals("Processor found for abbrevated name", define,
127: proc.option);
128: assertNotNull("Value is null", proc.value);
129: }
130:
131: public void testIncorrectOptionIdentified() throws Exception {
132: try {
133: l.process(new String[] { "--hell" });
134: fail("This option does not exists");
135: } catch (CommandException ex) {
136: // ok
137: }
138: assertNull("No processor called", proc.option);
139: }
140:
141: public void testNoProcessorCalledWhenOneOptionIsNotKnown()
142: throws Exception {
143: try {
144: l.process(new String[] { "-Dx", "--hell" });
145: fail("One option does not exists");
146: } catch (CommandException ex) {
147: // ok
148: }
149: assertNull("No processor called", proc.option);
150: }
151:
152: static final class OneArgProc implements Processor {
153: Option option;
154: String value;
155:
156: public void process(Env env, Map<Option, String[]> values)
157: throws CommandException {
158: assertNull("Not processed yet", option);
159: assertEquals("One option", 1, values.size());
160: option = values.keySet().iterator().next();
161: String[] arr = values.values().iterator().next();
162: assertNotNull("Never null", arr);
163: if (arr.length == 0) {
164: value = null;
165: } else {
166: assertEquals("Exactly one argument", 1, arr.length);
167: value = arr[0];
168: }
169: }
170: }
171: }
|