KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > sendopts > OneOfComplexTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.sendopts;
20
21 import java.util.Arrays JavaDoc;
22 import java.util.Map JavaDoc;
23 import junit.framework.TestCase;
24 import org.netbeans.spi.sendopts.OptionGroups;
25 import org.netbeans.spi.sendopts.Env;
26 import org.netbeans.spi.sendopts.Option;
27
28 /** Test for option alterning for each other.
29  *
30  * @author Jaroslav Tulach
31  */

32 public class OneOfComplexTest extends TestCase implements Processor {
33     protected CommandLine l;
34     private TuneProc tuneProc = new TuneProc();
35     private Option tune;
36     private Option station;
37     private TuneProc stationProc = new TuneProc();
38     private Option channel;
39     private Option stream;
40     private Option record;
41     
42     protected String JavaDoc valueChannel;
43     protected String JavaDoc valueStation;
44     protected String JavaDoc valueStream;
45     protected String JavaDoc valueTune;
46     
47     public OneOfComplexTest(String JavaDoc s) {
48         super(s);
49     }
50
51     protected void tearDown() throws Exception JavaDoc {
52         super.tearDown();
53     }
54
55     protected void setUp() throws Exception JavaDoc {
56         Provider.clearAll();
57         
58         tune = Option.requiredArgument((char)-1, "tune");
59         Provider.add(tuneProc, tune);
60         station = Option.requiredArgument((char)-1, "station");
61         Provider.add(stationProc, station);
62         channel = defineOneOf(tune, station);
63             
64         stream = Option.requiredArgument((char)-1, "stream");
65         
66         record = OptionGroups.allOf(channel, stream);
67         Provider.add(this, record);
68         
69         l = CommandLine.getDefault();
70     }
71
72     protected Option defineOneOf(Option... arr) {
73         return OptionGroups.oneOf(arr);
74     }
75     
76     public void testNothingIsGood() throws Exception JavaDoc {
77         l.process(new String JavaDoc[0]); // ok
78
}
79
80     public void testTuneWithoutStreamIsBad() throws Exception JavaDoc {
81         try {
82             l.process(new String JavaDoc[] { "--stream", "10" });
83             fail("We need --tune");
84         } catch (CommandException ex) {
85             // ok
86
}
87     }
88     
89     public void testTuneIsOk() throws Exception JavaDoc {
90         l.process(new String JavaDoc[] { "--stream", "x.mpeg", "--tune", "10" });
91         
92         assertEquals("Tune is 10", "10", tuneProc.value);
93         assertEquals("Tune is the option", tune, tuneProc.option);
94         
95         assertEquals("Value1 is 10", "10", valueTune);
96         assertEquals("Value2 is x.mpeg", "x.mpeg", valueStream);
97         
98     }
99
100     public void testStationIsOk() throws Exception JavaDoc {
101         l.process(new String JavaDoc[] { "--station", "Radio1", "--stream", "y.mpeg"});
102         
103         assertEquals("Station is ok", "Radio1", stationProc.value);
104         assertEquals("Station is the option", station, stationProc.option);
105         
106         assertEquals("Value1 is Radio1", "Radio1", valueStation);
107         assertEquals("Value2 is mpeg", "y.mpeg", valueStream);
108     }
109
110     public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
111         this.valueChannel = getString(values.get(channel));
112         this.valueStream = getString(values.get(stream));
113         this.valueStation = getString(values.get(station));
114         this.valueTune = getString(values.get(tune));
115     }
116     
117     private static String JavaDoc getString(String JavaDoc[] arr) {
118         if (arr == null) {
119             return null;
120         }
121         if (arr.length > 1) {
122             fail("Too long: " + Arrays.asList(arr));
123         }
124         return arr.length == 1 ? arr[0] : null;
125     }
126     
127     static final class TuneProc implements Processor {
128         Option option;
129         String JavaDoc value;
130
131         public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
132             assertNull("Not processed yet", this.option);
133             assertEquals("An option is provided", 1, values.size());
134             this.option = values.keySet().iterator().next();
135             this.value = values.values().iterator().next()[0];
136             assertNotNull("A value is here", value);
137         }
138     }
139 }
140
Popular Tags