KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet 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 inside one processor?
32  *
33  * @author Jaroslav Tulach
34  */

35 public class SingleSharedOptionTest extends TestCase {
36     /** a shared option part of some API */
37     static final Option SHARED = Option.requiredArgument(Option.NO_SHORT_NAME, "shared");
38     
39     public SingleSharedOptionTest(String JavaDoc s) {
40         super(s);
41     }
42
43     protected void setUp() throws Exception JavaDoc {
44         MockServices.setServices(Proc.class);
45         Proc.called = null;
46     }
47     
48     public void testP1IsSelected() throws Exception JavaDoc {
49         CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj", "--p1" });
50         
51         assertNotNull("Processor called", Proc.called);
52         assertTrue(Proc.called.containsKey(Proc.P1));
53         assertFalse(Proc.called.containsKey(Proc.P2));
54         assertTrue(Proc.called.containsKey(SHARED));
55     }
56
57     public void testP2IsSelected() throws Exception JavaDoc {
58         CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj", "--p2" });
59
60     
61         assertNotNull("Processor called", Proc.called);
62         assertFalse(Proc.called.containsKey(Proc.P1));
63         assertTrue(Proc.called.containsKey(Proc.P2));
64         assertTrue(Proc.called.containsKey(SHARED));
65     }
66     
67     public void testBothSelected() throws Exception JavaDoc {
68         CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj", "--p2", "--p1" });
69         
70     
71         assertNotNull("Processor called", Proc.called);
72         assertTrue(Proc.called.containsKey(Proc.P1));
73         assertTrue(Proc.called.containsKey(Proc.P2));
74         assertTrue(Proc.called.containsKey(SHARED));
75     }
76
77     public void testNothingCalled() throws Exception JavaDoc {
78         try {
79             CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj" });
80             fail("Just shared is not valid option");
81         } catch (CommandException ex) {
82             // ok
83
}
84         
85         assertNull("Processor not called", Proc.called);
86     }
87     
88     static final Option createMasterSlaveOption(Option master, Option slave) {
89         return OptionGroups.allOf(master, OptionGroups.anyOf(slave));
90     }
91     
92     public static final class Proc extends OptionProcessor {
93         static final Option P1 = Option.withoutArgument(Option.NO_SHORT_NAME, "p1");
94         static final Option P2 = Option.withoutArgument(Option.NO_SHORT_NAME, "p2");
95         static Map JavaDoc<Option,String JavaDoc[]> called;
96
97         protected Set JavaDoc<Option> getOptions() {
98             Set JavaDoc<Option> set = new HashSet JavaDoc<Option>();
99             set.add(createMasterSlaveOption(P1, SHARED));
100             set.add(createMasterSlaveOption(P2, SHARED));
101             return set;
102         }
103         
104         protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
105             assertNull("Not called yet", called);
106             called = optionValues;
107         }
108     }
109 }
110
111
Popular Tags