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: /** Options that work with additonal arguments.
050: *
051: * @author Jaroslav Tulach
052: */
053: public class AdditionalArgumentOptsTest extends TestCase {
054: private CommandLine l;
055: private AddArgsProc proc = new AddArgsProc();
056: private Option open;
057: private Option close;
058: private AddArgsProc closeProc = new AddArgsProc();
059:
060: public AdditionalArgumentOptsTest(String s) {
061: super (s);
062: }
063:
064: protected void tearDown() throws Exception {
065:
066: super .tearDown();
067: }
068:
069: protected void setUp() throws Exception {
070: Provider.clearAll();
071: open = Option.additionalArguments((char) -1, "open");
072: Provider.add(proc, open);
073: close = Option.additionalArguments('C', "close");
074: Provider.add(closeProc, close);
075:
076: l = CommandLine.getDefault();
077: }
078:
079: public void testCanHypenBeAdditionalArg() throws Exception {
080: l.process(new String[] { "--open", "-", "+", "*", "/" });
081:
082: assertNotNull("Processor called", proc.option);
083: assertNotNull("args provided", proc.values);
084: assertEquals("Four ", 4, proc.values.length);
085: assertEquals("-", proc.values[0]);
086: assertEquals("+", proc.values[1]);
087: assertEquals("*", proc.values[2]);
088: assertEquals("/", proc.values[3]);
089: }
090:
091: public void testOpenCannotHaveAnArgument() throws Exception {
092: try {
093: l.process(new String[] { "--open=ahoj", "1", "2", "3" });
094: fail("Open cannot have an argument");
095: } catch (CommandException ex) {
096: // ok
097: }
098: assertNull("Processor not called", proc.option);
099: assertNull("No args provided", proc.values);
100: }
101:
102: public void testOptionsPassedToOpen() throws Exception {
103: l.process(new String[] { "1", "--open", "2", "3" });
104: assertEquals("Processor found for long name", open, proc.option);
105: assertEquals("Three files provided", 3, proc.values.length);
106: assertEquals("first", "1", proc.values[0]);
107: assertEquals("second", "2", proc.values[1]);
108: assertEquals("third", "3", proc.values[2]);
109:
110: proc.option = null;
111: proc.values = null;
112: l.process(new String[] { "1", "--open", "2", "3" });
113: assertEquals("Processor found for long name", open, proc.option);
114: assertEquals("Three files provided", 3, proc.values.length);
115: assertEquals("first", "1", proc.values[0]);
116: assertEquals("second", "2", proc.values[1]);
117: assertEquals("third", "3", proc.values[2]);
118: }
119:
120: public void testCannotHaveTwoAdditionalOptionUsedAtOnce()
121: throws Exception {
122: try {
123: l.process(new String[] { "--open", "ahoj", "--close" });
124: fail("open & close cannot be used at once");
125: } catch (CommandException ex) {
126: // ok
127: }
128:
129: assertNull("No processor called1", proc.option);
130: assertNull("No processor called2", closeProc.option);
131: assertNull("Value is unset", proc.values);
132: }
133:
134: public void testProcessingStopsAtDashDash() throws Exception {
135: l.process(new String[] { "--ope", "1", "--", "--close", "2" });
136: assertEquals("Processor found for abbrevated name", open,
137: proc.option);
138: assertNull("No close called", closeProc.option);
139: assertEquals("three options provided", 3, proc.values.length);
140: assertEquals("first", "1", proc.values[0]);
141: assertEquals("second", "--close", proc.values[1]);
142: assertEquals("third", "2", proc.values[2]);
143: }
144:
145: public void testShortOptionWorksAsWell() throws Exception {
146: l.process(new String[] { "-C", "1", "--", "--open", "2" });
147: assertEquals("Close Processor found for abbrevated name",
148: close, closeProc.option);
149: assertNull("No open called", proc.option);
150: assertEquals("three options provided", 3,
151: closeProc.values.length);
152: assertEquals("first", "1", closeProc.values[0]);
153: assertEquals("second", "--open", closeProc.values[1]);
154: assertEquals("third", "2", closeProc.values[2]);
155: }
156:
157: static final class AddArgsProc implements Processor {
158: Option option;
159: String[] values;
160:
161: public void process(Option o, Env env, String[] args)
162: throws CommandException {
163: }
164:
165: public void process(Env env, Map<Option, String[]> values)
166: throws CommandException {
167: assertNull("Not processed yet", option);
168:
169: assertEquals("One value", 1, values.size());
170:
171: option = values.keySet().iterator().next();
172: this .values = values.values().iterator().next();
173:
174: assertNotNull("An option is provided", option);
175: assertNotNull("An array of args is provided", this.values);
176: }
177: }
178: }
|