KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
22 import junit.framework.TestCase;
23 import org.netbeans.modules.sendopts.OptionImpl;
24 import org.netbeans.spi.sendopts.Env;
25 import org.netbeans.spi.sendopts.Option;
26
27 /** The basic test to check semantics of getopts behaviour wrt. one optional argument.
28  *
29  * @author Jaroslav Tulach
30  */

31 public class OptionalArgumentOptsTest extends TestCase {
32     private CommandLine l;
33     private OneArgProc proc = new OneArgProc();
34     private Option define;
35
36     public OptionalArgumentOptsTest(String JavaDoc s) {
37         super(s);
38    }
39
40     protected void tearDown() throws Exception JavaDoc {
41
42         super.tearDown();
43     }
44
45     protected void setUp() throws Exception JavaDoc {
46         Provider.clearAll();
47         
48         define = Option.optionalArgument('D', "define");
49         Provider.add(proc, define);
50         l = CommandLine.getDefault();
51     }
52     
53     public void testSingleNoArgOptionIsRecognized() throws Exception JavaDoc {
54         l.process(new String JavaDoc[] { "-Dahoj" });
55         assertEquals("Processor found", define, proc.option);
56         assertEquals("Value is correct", "ahoj", proc.value);
57     }
58     
59     public void testLongOptionRecognized() throws Exception JavaDoc {
60         l.process(new String JavaDoc[] { "--define=ahoj" });
61         assertEquals("Processor found for long name", define, proc.option);
62         assertEquals("Value is correct", "ahoj", proc.value);
63     }
64     public void testLongOptionRecognized2() throws Exception JavaDoc {
65         try {
66             l.process(new String JavaDoc[] { "--define", "ahoj" });
67             fail("ahoj is not used as optional argument, according to getopts");
68         } catch (CommandException ex) {
69             // ok
70
}
71             
72         assertNull("Processor found for long name", proc.option);
73         assertNull("Value is unset", proc.value);
74     }
75     
76     public void testAbrevatedNameRecognized() throws Exception JavaDoc {
77         l.process(new String JavaDoc[] { "--def=ahoj" });
78         assertEquals("Processor found for abbrevated name", define, proc.option);
79     }
80     
81     public void testAbrevatedNameRecognized2() throws Exception JavaDoc {
82         try {
83             l.process(new String JavaDoc[] { "--def", "ahoj" });
84             fail("Optional argument must follow the option with = sign, cannot be left out");
85         } catch (CommandException ex) {
86             // ok
87
}
88         assertNull("No Processor called", proc.option);
89     }
90
91     public void testAbrevatedNameRecognizedWithoutArg() throws Exception JavaDoc {
92         l.process(new String JavaDoc[] { "--def" });
93         assertEquals("Processor found for abbrevated name", define, proc.option);
94         assertNull("Value is null", proc.value);
95     }
96     public void testShortNameRecognizedWithoutArg() throws Exception JavaDoc {
97         l.process(new String JavaDoc[] { "-D" });
98         assertEquals("Processor found for abbrevated name", define, proc.option);
99         assertNotNull("Value is null", proc.value);
100     }
101
102
103     public void testIncorrectOptionIdentified() throws Exception JavaDoc {
104         try {
105             l.process(new String JavaDoc[] { "--hell" });
106             fail("This option does not exists");
107         } catch (CommandException ex) {
108             // ok
109
}
110         assertNull("No processor called", proc.option);
111     }
112
113     public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception JavaDoc {
114         try {
115             l.process(new String JavaDoc[] { "-Dx", "--hell" });
116             fail("One option does not exists");
117         } catch (CommandException ex) {
118             // ok
119
}
120         assertNull("No processor called", proc.option);
121     }
122     
123     static final class OneArgProc implements Processor {
124         Option option;
125         String JavaDoc value;
126         
127         public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
128             assertNull("Not processed yet", option);
129             assertEquals("One option", 1, values.size());
130             option = values.keySet().iterator().next();
131             String JavaDoc[] arr = values.values().iterator().next();
132             assertNotNull("Never null", arr);
133             if (arr.length == 0) {
134                 value = null;
135             } else {
136                 assertEquals("Exactly one argument", 1, arr.length);
137                 value = arr[0];
138             }
139         }
140     }
141 }
142
Popular Tags