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.Collections;
044: import java.util.Map;
045: import java.util.Set;
046: import junit.framework.TestCase;
047: import org.netbeans.junit.MockServices;
048: import org.netbeans.spi.sendopts.OptionGroups;
049: import org.netbeans.spi.sendopts.Env;
050: import org.netbeans.spi.sendopts.Option;
051: import org.netbeans.spi.sendopts.OptionProcessor;
052:
053: /** Can one option be shared among two processors?
054: *
055: * @author Jaroslav Tulach
056: */
057: public class SharedOptionTest extends TestCase {
058: /** a shared option part of some API */
059: static final Option SHARED = Option.requiredArgument(
060: Option.NO_SHORT_NAME, "shared");
061:
062: /** a method to convert the option to something meaningful */
063: static String getSharedMessage(Map<Option, String[]> args,
064: Class<?> who) {
065: String[] v = args.get(SHARED);
066: return v == null ? "NOMSG" : "Shared msg " + v[0] + "from "
067: + who.getName();
068: }
069:
070: public SharedOptionTest(String s) {
071: super (s);
072: }
073:
074: protected void setUp() throws Exception {
075: MockServices.setServices(P1.class, P2.class);
076: P1.called = null;
077: P2.called = null;
078: }
079:
080: public void testP1IsSelected() throws Exception {
081: CommandLine.getDefault().process(
082: new String[] { "--shared", "Ahoj", "--p1" });
083:
084: assertNull("No P2", P2.called);
085: assertNotNull("P1 called", P1.called);
086:
087: CommandException ex = (CommandException) P1.called;
088: if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
089: fail("Value of shared option influences the localized message: "
090: + ex.getLocalizedMessage());
091: }
092: if (ex.getLocalizedMessage().indexOf("P1") == -1) {
093: fail("Value of shared option influences the localized message: "
094: + ex.getLocalizedMessage());
095: }
096: }
097:
098: public void testP2IsSelected() throws Exception {
099: CommandLine.getDefault().process(
100: new String[] { "--shared", "Ahoj", "--p2" });
101:
102: assertNull("No P1", P1.called);
103: assertNotNull("P2 called", P2.called);
104:
105: CommandException ex = (CommandException) P2.called;
106: if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
107: fail("Value of shared option influences the localized message: "
108: + ex.getLocalizedMessage());
109: }
110: if (ex.getLocalizedMessage().indexOf("P2") == -1) {
111: fail("Value of shared option influences the localized message: "
112: + ex.getLocalizedMessage());
113: }
114: }
115:
116: public void testBothSelected() throws Exception {
117: CommandLine.getDefault().process(
118: new String[] { "--shared", "Ahoj", "--p2", "--p1" });
119:
120: assertNotNull("P1 called", P1.called);
121: CommandException ex = (CommandException) P1.called;
122: if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
123: fail("Value of shared option influences the localized message: "
124: + ex.getLocalizedMessage());
125: }
126: if (ex.getLocalizedMessage().indexOf("P1") == -1) {
127: fail("Value of shared option influences the localized message: "
128: + ex.getLocalizedMessage());
129: }
130:
131: assertNotNull("P2 called", P2.called);
132:
133: ex = (CommandException) P2.called;
134: if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
135: fail("Value of shared option influences the localized message: "
136: + ex.getLocalizedMessage());
137: }
138: if (ex.getLocalizedMessage().indexOf("P2") == -1) {
139: fail("Value of shared option influences the localized message: "
140: + ex.getLocalizedMessage());
141: }
142: }
143:
144: public void testNothingCalled() throws Exception {
145: try {
146: CommandLine.getDefault().process(
147: new String[] { "--shared", "Ahoj" });
148: fail("Just shared is not valid option");
149: } catch (CommandException ex) {
150: // ok
151: }
152:
153: assertNull("No P2", P2.called);
154: assertNull("No P1", P1.called);
155: }
156:
157: public void testJustP1Called() throws Exception {
158: CommandLine.getDefault().process(new String[] { "--p1" });
159:
160: assertNull("No P2", P2.called);
161: assertNotNull("Yes P1", P1.called);
162: assertEquals("NOMSG", P1.called.getLocalizedMessage());
163: }
164:
165: public void testJustP2Called() throws Exception {
166: CommandLine.getDefault().process(new String[] { "--p2" });
167:
168: assertNull("No P1", P1.called);
169: assertNotNull("Yes P2", P2.called);
170: assertEquals("NOMSG", P2.called.getLocalizedMessage());
171: }
172:
173: public void testJustBothPsCalled() throws Exception {
174: CommandLine.getDefault().process(
175: new String[] { "--p1", "--p2" });
176:
177: assertNotNull("Yes P1", P1.called);
178: assertNotNull("Yes P2", P2.called);
179: assertEquals("NOMSG", P1.called.getLocalizedMessage());
180: assertEquals("NOMSG", P2.called.getLocalizedMessage());
181: }
182:
183: static final Option createMasterSlaveOption(Option master,
184: Option slave) {
185: return OptionGroups.allOf(master, OptionGroups.anyOf(slave));
186: }
187:
188: public static final class P1 extends OptionProcessor {
189: private static final Option P1 = Option.withoutArgument(
190: Option.NO_SHORT_NAME, "p1");
191: static Throwable called;
192:
193: protected Set<Option> getOptions() {
194: return Collections.singleton(createMasterSlaveOption(P1,
195: SHARED));
196: }
197:
198: protected void process(Env env,
199: Map<Option, String[]> optionValues)
200: throws CommandException {
201: assertNull("Not called yet", called);
202: // signal P1 was called
203: called = new CommandException(1, getSharedMessage(
204: optionValues, getClass()));
205: }
206: }
207:
208: public static final class P2 extends OptionProcessor {
209: private static final Option P2 = Option.withoutArgument(
210: Option.NO_SHORT_NAME, "p2");
211: static Throwable called;
212:
213: protected Set<Option> getOptions() {
214: return Collections.singleton(createMasterSlaveOption(P2,
215: SHARED));
216: }
217:
218: protected void process(Env env,
219: Map<Option, String[]> optionValues)
220: throws CommandException {
221: assertNull("Not called yet", called);
222: // signal P2 was called
223: called = new CommandException(2, getSharedMessage(
224: optionValues, getClass()));
225: }
226: }
227: }
|