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: /** Default options that work with additonal arguments.
050: *
051: * @author Jaroslav Tulach
052: */
053: public class DefaultArgumentOptsTest extends TestCase {
054: private CommandLine l, wrong;
055: private AddArgsProc proc = new AddArgsProc();
056: private Option open;
057: private Option close;
058: private AddArgsProc closeProc = new AddArgsProc();
059:
060: public DefaultArgumentOptsTest(String s) {
061: super (s);
062: }
063:
064: protected void tearDown() throws Exception {
065:
066: super .tearDown();
067: }
068:
069: protected void setUpOk() throws Exception {
070: Provider.clearAll();
071:
072: open = Option.additionalArguments((char) -1, "open");
073: Provider.add(proc, open);
074: close = Option.defaultArguments();
075: Provider.add(closeProc, close);
076:
077: l = CommandLine.getDefault();
078: }
079:
080: protected void setUpWrong() throws Exception {
081: setUpOk();
082: Option def = Option.defaultArguments();
083: Provider.add(closeProc, def);
084:
085: l = null;
086: wrong = CommandLine.getDefault();
087: }
088:
089: public void testOptionsPassedToOpen() throws Exception {
090: setUpOk();
091:
092: l.process(new String[] { "1", "--open", "2", "3" });
093: assertEquals("Processor found for long name", open, proc.option);
094: assertEquals("Three files provided", 3, proc.values.length);
095: assertEquals("first", "1", proc.values[0]);
096: assertEquals("second", "2", proc.values[1]);
097: assertEquals("third", "3", proc.values[2]);
098: }
099:
100: public void testOptionsPassedToOpenWrong() throws Exception {
101: setUpWrong();
102:
103: wrong.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:
111: public void testProcessingStopsAtDashDash() throws Exception {
112: setUpOk();
113:
114: l.process(new String[] { "1", "--", "--open", "2" });
115: assertEquals("Close Processor found for abbrevated name",
116: close, closeProc.option);
117: assertNull("No open called", proc.option);
118: assertEquals("three options provided", 3,
119: closeProc.values.length);
120: assertEquals("first", "1", closeProc.values[0]);
121: assertEquals("second", "--open", closeProc.values[1]);
122: assertEquals("third", "2", closeProc.values[2]);
123: }
124:
125: public void testCannotHaveTwoDefaultOptions() throws Exception {
126: setUpWrong();
127:
128: try {
129: wrong.process(new String[] { "1", "--", "--open", "2" });
130: fail("Cannot have two default options");
131: } catch (CommandException ex) {
132: // ok
133: }
134: assertNull("No close", closeProc.option);
135: assertNull("No open", proc.option);
136: }
137:
138: static final class AddArgsProc implements Processor {
139: Option option;
140: String[] values;
141:
142: public void process(Env env, Map<Option, String[]> values)
143: throws CommandException {
144: assertNull("Not processed yet", option);
145:
146: assertEquals("One value", 1, values.size());
147:
148: option = values.keySet().iterator().next();
149: this .values = values.values().iterator().next();
150:
151: assertNotNull("An option is provided", option);
152: assertNotNull("An array of args is provided", this.values);
153: }
154: }
155: }
|