KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import junit.framework.TestCase;
27 import org.netbeans.modules.sendopts.OptionImpl;
28 import org.netbeans.spi.sendopts.Env;
29 import org.netbeans.spi.sendopts.Option;
30
31 /** The basic test to check semantics of getopts behaviour.
32  *
33  * @author Jaroslav Tulach
34  */

35 public class NoArgumentOptsTest extends TestCase {
36     private CommandLine l;
37     private NoArgProc proc = new NoArgProc();
38     private Option help;
39     private NoArgProc okProc = new NoArgProc();
40     private Option ok;
41     
42     public NoArgumentOptsTest(String JavaDoc s) {
43         super(s);
44     }
45
46     protected void tearDown() throws Exception JavaDoc {
47
48         super.tearDown();
49     }
50
51     protected void setUp() throws Exception JavaDoc {
52         
53         help = Option.withoutArgument('h', "help");
54         Provider.clearAll();
55         Provider.add(proc, help);
56         ok = Option.withoutArgument('o', "ok");
57         Provider.add(okProc, ok);
58         
59         l = CommandLine.getDefault();
60     }
61     
62     public void testSingleNoArgOptionIsRecognized() throws Exception JavaDoc {
63         l.process(new String JavaDoc[] { "-h" });
64         assertEquals("Processor found", help, proc.option);
65     }
66     
67     public void testLongOptionRecognized() throws Exception JavaDoc {
68         l.process(new String JavaDoc[] { "--help" });
69         assertEquals("Processor found for long name", help, proc.option);
70     }
71
72     public void testTwoOptionsRecognized() throws Exception JavaDoc {
73         l.process(new String JavaDoc[] { "-ho" });
74         assertEquals("Processor for help", help, proc.option);
75         assertEquals("Processor for ok", ok, okProc.option);
76     }
77     
78     public void testAbrevatedNameRecognized() throws Exception JavaDoc {
79         l.process(new String JavaDoc[] { "--he" });
80         assertEquals("Processor found for abbrevated name", help, proc.option);
81         
82         proc.option = null;
83         l.process(new String JavaDoc[] { "--he" });
84         assertEquals("Processor found for abbrevated name again", help, proc.option);
85     }
86     
87
88     public void testIncorrectOptionIdentified() throws Exception JavaDoc {
89         try {
90             l.process(new String JavaDoc[] { "--hell" });
91             fail("This option does not exists");
92         } catch (CommandException ex) {
93             // ok
94
}
95         assertNull("No processor called", proc.option);
96     }
97
98     public void testNoProcessorCalledWhenOneOptionIsNotKnown() throws Exception JavaDoc {
99         try {
100             l.process(new String JavaDoc[] { "-h", "--hell" });
101             fail("One option does not exists");
102         } catch (CommandException ex) {
103             // ok
104
}
105         assertNull("No processor called", proc.option);
106     }
107
108     public void testPrintedUsage() throws Exception JavaDoc {
109         StringWriter JavaDoc w = new StringWriter JavaDoc();
110         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
111
112         l.usage(pw);
113
114         Matcher JavaDoc m = Pattern.compile("-h.*--help").matcher(w.toString());
115         if (!m.find()) {
116             fail("-h, --help should be there:\n" + w.toString());
117         }
118         m = Pattern.compile("-o.*--ok").matcher(w.toString());
119         if (!m.find()) {
120             fail("-o, --ok should be there:\n" + w.toString());
121         }
122
123         int x = w.toString().indexOf('\n');
124         if (x == -1) {
125             fail("There should be two lines: " + w.toString());
126         }
127         x = w.toString().indexOf('\n', x + 1);
128         if (x == -1) {
129             fail("There should be two lines2: " + w.toString());
130         }
131     }
132     public void testPrintedUsageEmpty() throws Exception JavaDoc {
133         Provider.clearAll();
134         
135         help = Option.withoutArgument('h', null);
136         Provider.add(proc, help);
137         ok = Option.withoutArgument(Option.NO_SHORT_NAME, "ok");
138         Provider.add(okProc, ok);
139         
140         l = CommandLine.getDefault();
141
142         StringWriter JavaDoc w = new StringWriter JavaDoc();
143         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(w);
144
145         l.usage(pw);
146
147         Matcher JavaDoc m = Pattern.compile(" *-h *").matcher(w.toString());
148         if (!m.find()) {
149             fail("Only -h should be there:\n" + w.toString());
150         }
151         m = Pattern.compile(" *--ok *").matcher(w.toString());
152         if (!m.find()) {
153             fail(" --ok should be there:\n" + w.toString());
154         }
155
156         int x = w.toString().indexOf('\n');
157         if (x == -1) {
158             fail("There should be two lines: " + w.toString());
159         }
160         x = w.toString().indexOf('\n', x + 1);
161         if (x == -1) {
162             fail("There should be two lines2: " + w.toString());
163         }
164     }
165     
166     static final class NoArgProc implements Processor {
167         Option option;
168         
169         public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
170             assertNull("Not processed yet", option);
171             assertEquals(1, values.size());
172             option = values.keySet().iterator().next();
173             assertNotNull("An option is provided", option);
174
175             String JavaDoc[] args = values.get(option);
176             assertNotNull("Values is always [0]", args);
177             assertEquals("Values is always [0]", 0, args.length);
178         }
179     }
180 }
181
Popular Tags