KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import junit.framework.TestCase;
25 import org.netbeans.junit.MockServices;
26 import org.netbeans.spi.sendopts.OptionGroups;
27 import org.netbeans.spi.sendopts.Env;
28 import org.netbeans.spi.sendopts.Option;
29 import org.netbeans.spi.sendopts.OptionProcessor;
30
31 /** Can one option be shared among two processors?
32  *
33  * @author Jaroslav Tulach
34  */

35 public class SharedOptionTest extends TestCase {
36     /** a shared option part of some API */
37     static final Option SHARED = Option.requiredArgument(Option.NO_SHORT_NAME, "shared");
38     /** a method to convert the option to something meaningful */
39     static String JavaDoc getSharedMessage(Map JavaDoc<Option,String JavaDoc[]> args, Class JavaDoc<?> who) {
40         String JavaDoc[] v = args.get(SHARED);
41         return v == null ? "NOMSG" : "Shared msg " + v[0] + "from " + who.getName();
42     }
43     
44     
45     public SharedOptionTest(String JavaDoc s) {
46         super(s);
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         MockServices.setServices(P1.class, P2.class);
51         P1.called = null;
52         P2.called = null;
53     }
54     
55     public void testP1IsSelected() throws Exception JavaDoc {
56         CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj", "--p1" });
57         
58         assertNull("No P2", P2.called);
59         assertNotNull("P1 called", P1.called);
60         
61         CommandException ex = (CommandException) P1.called;
62         if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
63             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
64         }
65         if (ex.getLocalizedMessage().indexOf("P1") == -1) {
66             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
67         }
68     }
69
70     public void testP2IsSelected() throws Exception JavaDoc {
71         CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj", "--p2" });
72
73     
74         assertNull("No P1", P1.called);
75         assertNotNull("P2 called", P2.called);
76         
77         CommandException ex = (CommandException) P2.called;
78         if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
79             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
80         }
81         if (ex.getLocalizedMessage().indexOf("P2") == -1) {
82             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
83         }
84     }
85     
86     public void testBothSelected() throws Exception JavaDoc {
87         CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj", "--p2", "--p1" });
88
89     
90         assertNotNull("P1 called", P1.called);
91         CommandException ex = (CommandException) P1.called;
92         if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
93             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
94         }
95         if (ex.getLocalizedMessage().indexOf("P1") == -1) {
96             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
97         }
98
99         assertNotNull("P2 called", P2.called);
100         
101         ex = (CommandException) P2.called;
102         if (ex.getLocalizedMessage().indexOf("Shared msg Ahoj") == -1) {
103             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
104         }
105         if (ex.getLocalizedMessage().indexOf("P2") == -1) {
106             fail("Value of shared option influences the localized message: " + ex.getLocalizedMessage());
107         }
108     }
109
110     public void testNothingCalled() throws Exception JavaDoc {
111         try {
112             CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj" });
113             fail("Just shared is not valid option");
114         } catch (CommandException ex) {
115             // ok
116
}
117         
118         assertNull("No P2", P2.called);
119         assertNull("No P1", P1.called);
120     }
121
122     public void testJustP1Called() throws Exception JavaDoc {
123         CommandLine.getDefault().process(new String JavaDoc[] { "--p1" });
124         
125         assertNull("No P2", P2.called);
126         assertNotNull("Yes P1", P1.called);
127         assertEquals("NOMSG", P1.called.getLocalizedMessage());
128     }
129     
130     public void testJustP2Called() throws Exception JavaDoc {
131         CommandLine.getDefault().process(new String JavaDoc[] { "--p2" });
132         
133         assertNull("No P1", P1.called);
134         assertNotNull("Yes P2", P2.called);
135         assertEquals("NOMSG", P2.called.getLocalizedMessage());
136     }
137
138     public void testJustBothPsCalled() throws Exception JavaDoc {
139         CommandLine.getDefault().process(new String JavaDoc[] { "--p1", "--p2" });
140         
141         assertNotNull("Yes P1", P1.called);
142         assertNotNull("Yes P2", P2.called);
143         assertEquals("NOMSG", P1.called.getLocalizedMessage());
144         assertEquals("NOMSG", P2.called.getLocalizedMessage());
145     }
146     
147     static final Option createMasterSlaveOption(Option master, Option slave) {
148         return OptionGroups.allOf(master, OptionGroups.anyOf(slave));
149     }
150     
151     public static final class P1 extends OptionProcessor {
152         private static final Option P1 = Option.withoutArgument(Option.NO_SHORT_NAME, "p1");
153         static Throwable JavaDoc called;
154         
155         protected Set JavaDoc<Option> getOptions() {
156             return Collections.singleton(createMasterSlaveOption(P1, SHARED));
157         }
158         
159         protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
160             assertNull("Not called yet", called);
161             // signal P1 was called
162
called = new CommandException(1, getSharedMessage(optionValues, getClass()));
163         }
164     }
165     public static final class P2 extends OptionProcessor {
166         private static final Option P2 = Option.withoutArgument(Option.NO_SHORT_NAME, "p2");
167         static Throwable JavaDoc called;
168
169         protected Set JavaDoc<Option> getOptions() {
170             return Collections.singleton(createMasterSlaveOption(P2, SHARED));
171         }
172         
173         protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
174             assertNull("Not called yet", called);
175             // signal P2 was called
176
called = new CommandException(2, getSharedMessage(optionValues, getClass()));
177         }
178     }
179 }
180
181
Popular Tags