KickJava   Java API By Example, From Geeks To Geeks.

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


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 /** Simple anyOf test.
32  *
33  * @author Jaroslav Tulach
34  */

35 public class MasterTest 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 MasterTest(String JavaDoc s) {
40         super(s);
41     }
42
43     protected void setUp() throws Exception JavaDoc {
44         MockServices.setServices(P1.class);
45     }
46     
47     public void testSharedSelected() throws Exception JavaDoc {
48         try {
49             CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "Ahoj" });
50             fail("Should fail with CommandException");
51         } catch (CommandException ex) {
52             if (ex.getExitCode() == 1) {
53                 fail("Real working that p1 needs to be used shall be printed");
54             }
55         }
56     }
57     public void testP1() throws Exception JavaDoc {
58         try {
59             CommandLine.getDefault().process(new String JavaDoc[] { "--p1" });
60             fail("Should fail with CommandException thrown during processing of our OptionProcessor");
61         } catch (CommandException ex) {
62             if (ex.getExitCode() != 1) {
63                 throw ex;
64             }
65         }
66     }
67     public void testNothing() throws Exception JavaDoc {
68         CommandLine.getDefault().process(new String JavaDoc[] { });
69     }
70     public void testAll() throws Exception JavaDoc {
71         try {
72             CommandLine.getDefault().process(new String JavaDoc[] { "--shared", "ble", "--p1" });
73             fail("Should fail with CommandException thrown during processing of our OptionProcessor");
74         } catch (CommandException ex) {
75             if (ex.getExitCode() != 1) {
76                 throw ex;
77             }
78         }
79     }
80
81     public static final class P1 extends OptionProcessor {
82         private static final Option P1 = Option.withoutArgument(Option.NO_SHORT_NAME, "p1");
83         
84         protected Set JavaDoc<Option> getOptions() {
85             return Collections.singleton(OptionGroups.allOf(P1, OptionGroups.anyOf(SHARED)));
86         }
87         
88         protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
89             // signal P1 was called
90
throw new CommandException(1);
91         }
92     }
93 }
94
95
Popular Tags