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.Arrays;
044: import java.util.Map;
045: import junit.framework.TestCase;
046: import org.netbeans.spi.sendopts.OptionGroups;
047: import org.netbeans.spi.sendopts.Env;
048: import org.netbeans.spi.sendopts.Option;
049:
050: /** Test for option alterning for each other.
051: *
052: * @author Jaroslav Tulach
053: */
054: public class OneOfComplexTest extends TestCase implements Processor {
055: protected CommandLine l;
056: private TuneProc tuneProc = new TuneProc();
057: private Option tune;
058: private Option station;
059: private TuneProc stationProc = new TuneProc();
060: private Option channel;
061: private Option stream;
062: private Option record;
063:
064: protected String valueChannel;
065: protected String valueStation;
066: protected String valueStream;
067: protected String valueTune;
068:
069: public OneOfComplexTest(String s) {
070: super (s);
071: }
072:
073: protected void tearDown() throws Exception {
074: super .tearDown();
075: }
076:
077: protected void setUp() throws Exception {
078: Provider.clearAll();
079:
080: tune = Option.requiredArgument((char) -1, "tune");
081: Provider.add(tuneProc, tune);
082: station = Option.requiredArgument((char) -1, "station");
083: Provider.add(stationProc, station);
084: channel = defineOneOf(tune, station);
085:
086: stream = Option.requiredArgument((char) -1, "stream");
087:
088: record = OptionGroups.allOf(channel, stream);
089: Provider.add(this , record);
090:
091: l = CommandLine.getDefault();
092: }
093:
094: protected Option defineOneOf(Option... arr) {
095: return OptionGroups.oneOf(arr);
096: }
097:
098: public void testNothingIsGood() throws Exception {
099: l.process(new String[0]); // ok
100: }
101:
102: public void testTuneWithoutStreamIsBad() throws Exception {
103: try {
104: l.process(new String[] { "--stream", "10" });
105: fail("We need --tune");
106: } catch (CommandException ex) {
107: // ok
108: }
109: }
110:
111: public void testTuneIsOk() throws Exception {
112: l
113: .process(new String[] { "--stream", "x.mpeg", "--tune",
114: "10" });
115:
116: assertEquals("Tune is 10", "10", tuneProc.value);
117: assertEquals("Tune is the option", tune, tuneProc.option);
118:
119: assertEquals("Value1 is 10", "10", valueTune);
120: assertEquals("Value2 is x.mpeg", "x.mpeg", valueStream);
121:
122: }
123:
124: public void testStationIsOk() throws Exception {
125: l.process(new String[] { "--station", "Radio1", "--stream",
126: "y.mpeg" });
127:
128: assertEquals("Station is ok", "Radio1", stationProc.value);
129: assertEquals("Station is the option", station,
130: stationProc.option);
131:
132: assertEquals("Value1 is Radio1", "Radio1", valueStation);
133: assertEquals("Value2 is mpeg", "y.mpeg", valueStream);
134: }
135:
136: public void process(Env env, Map<Option, String[]> values)
137: throws CommandException {
138: this .valueChannel = getString(values.get(channel));
139: this .valueStream = getString(values.get(stream));
140: this .valueStation = getString(values.get(station));
141: this .valueTune = getString(values.get(tune));
142: }
143:
144: private static String getString(String[] arr) {
145: if (arr == null) {
146: return null;
147: }
148: if (arr.length > 1) {
149: fail("Too long: " + Arrays.asList(arr));
150: }
151: return arr.length == 1 ? arr[0] : null;
152: }
153:
154: static final class TuneProc implements Processor {
155: Option option;
156: String value;
157:
158: public void process(Env env, Map<Option, String[]> values)
159: throws CommandException {
160: assertNull("Not processed yet", this .option);
161: assertEquals("An option is provided", 1, values.size());
162: this .option = values.keySet().iterator().next();
163: this .value = values.values().iterator().next()[0];
164: assertNotNull("A value is here", value);
165: }
166: }
167: }
|