KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.api.sendopts;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import junit.framework.TestCase;
27 import org.netbeans.junit.MockServices;
28 import org.netbeans.spi.sendopts.Env;
29 import org.netbeans.spi.sendopts.Option;
30 import org.netbeans.spi.sendopts.OptionProcessor;
31
32 /** The basic test to check semantics of getopts behaviour.
33  *
34  * @author Jaroslav Tulach
35  */

36 public class OptionProviderTest extends TestCase {
37     private CommandLine l;
38     private static Option help;
39     private static Option ok;
40     
41     static {
42         help = Option.withoutArgument('h', "help");
43         ok = Option.withoutArgument('o', "ok");
44
45         OP.arr = new Option[] { ok, help };
46         MockServices.setServices(OP.class);
47     }
48     
49     public OptionProviderTest(String JavaDoc s) {
50         super(s);
51     }
52
53     protected void setUp() throws Exception JavaDoc {
54         
55         OP.values = null;
56         
57         l = CommandLine.getDefault();
58     }
59     
60     public void testSingleNoArgOptionIsRecognized() throws Exception JavaDoc {
61         l.process(new String JavaDoc[] { "-h" });
62         assertEquals("Processor found", true, OP.values.containsKey(help));
63     }
64     
65     public void testLongOptionRecognized() throws Exception JavaDoc {
66         l.process(new String JavaDoc[] { "--help" });
67         assertEquals("Processor found for long name", true, OP.values.containsKey(help));
68     }
69
70     public void testTwoOptionsRecognized() throws Exception JavaDoc {
71         l.process(new String JavaDoc[] { "-ho" });
72         assertEquals("Processor for help", true, OP.values.containsKey(help));
73         assertEquals("Processor for ok", true, OP.values.containsKey(ok));
74     }
75     
76     public void testAbrevatedNameRecognized() throws Exception JavaDoc {
77         l.process(new String JavaDoc[] { "--he" });
78         assertEquals("Processor found for abbrevated name", true, OP.values.containsKey(help));
79     }
80     
81
82     public void testIncorrectOptionIdentified() throws Exception JavaDoc {
83         try {
84             l.process(new String JavaDoc[] { "--hell" });
85             fail("This option does not exists");
86         } catch (CommandException ex) {
87             // ok
88
}
89         assertNull("No processor called", OP.values);
90     }
91
92     public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception JavaDoc {
93         try {
94             l.process(new String JavaDoc[] { "-h", "--hell" });
95             fail("One option does not exists");
96         } catch (CommandException ex) {
97             // ok
98
}
99         assertNull("No processor called", OP.values);
100     }
101     
102     public static final class OP extends OptionProcessor {
103         static Option[] arr;
104         static Map JavaDoc<Option, String JavaDoc[]> values;
105         
106         protected Set JavaDoc<Option> getOptions() {
107             return new HashSet JavaDoc<Option>(Arrays.asList(arr));
108         }
109
110         protected void process(Env env, Map JavaDoc<Option, String JavaDoc[]> optionValues) throws CommandException {
111             values = optionValues;
112         }
113         
114     }
115 }
116
Popular Tags