KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.Map JavaDoc;
24 import junit.framework.TestCase;
25 import org.netbeans.spi.sendopts.OptionGroups;
26 import org.netbeans.spi.sendopts.Env;
27 import org.netbeans.spi.sendopts.Option;
28
29 /** Checks that error reports from the system are sane.
30  *
31  * @author Jaroslav Tulach
32  */

33 public class ErrorMessagesTest extends TestCase
34 implements Processor {
35     private CommandLine l;
36
37     private ArrayList JavaDoc<Option> options = new ArrayList JavaDoc<Option>();
38     
39     public ErrorMessagesTest(String JavaDoc s) {
40         super(s);
41     }
42
43     protected void tearDown() throws Exception JavaDoc {
44
45         super.tearDown();
46     }
47
48     protected void setUp() throws Exception JavaDoc {
49         Locale.setDefault(Locale.US);
50         
51         
52         Option no = Option.withoutArgument((char)-1, "no");
53         Option open = Option.additionalArguments('o', "open");
54         Option close = Option.additionalArguments('c', "close");
55         Option one = Option.requiredArgument('1', "one");
56         Option two = Option.requiredArgument('2', "two");
57         Option optional = Option.optionalArgument((char)-1, "option");
58         Option both = OptionGroups.allOf(no, one);
59         Option bothDef = OptionGroups.someOf(both, open);
60         Option allOf = OptionGroups.oneOf(open, two, both);
61         
62         Provider.clearAll();
63         Provider.add(this, no, open, close, one, two, optional, both, bothDef, allOf);
64         
65         l = CommandLine.getDefault();
66     }
67     
68     public void testMissingArgument() {
69         try {
70             l.process(new String JavaDoc[] { "--one" });
71             fail("This is going to fail");
72         } catch (CommandException ex) {
73             if (ex.getLocalizedMessage().indexOf("Option --one") == -1) {
74                 fail(ex.getLocalizedMessage());
75             }
76             
77             if (ex.getLocalizedMessage().indexOf("needs") == -1) {
78                 fail(ex.getLocalizedMessage());
79             }
80
81             if (ex.getLocalizedMessage().indexOf("argument") == -1) {
82                 fail(ex.getLocalizedMessage());
83             }
84         }
85     }
86     
87     public void testShortMissingArgument() {
88         try {
89             l.process(new String JavaDoc[] { "-1" });
90             fail("This is going to fail");
91         } catch (CommandException ex) {
92             if (ex.getLocalizedMessage().indexOf("Option -1") == -1) {
93                 fail(ex.getLocalizedMessage());
94             }
95             
96             if (ex.getLocalizedMessage().indexOf("needs") == -1) {
97                 fail(ex.getLocalizedMessage());
98             }
99
100             if (ex.getLocalizedMessage().indexOf("argument") == -1) {
101                 fail(ex.getLocalizedMessage());
102             }
103         }
104     }
105     
106     public void testCannotBeUsedAtOnce() {
107         try {
108             l.process(new String JavaDoc[] { "-c", "-o" });
109             fail("Cannot be used at once");
110         } catch (CommandException ex) {
111             if (ex.getLocalizedMessage().indexOf("-c") == -1) {
112                 fail("-c should be there: " + ex.getLocalizedMessage());
113             }
114             if (ex.getLocalizedMessage().indexOf("-o") == -1) {
115                 fail("-o should be there: " + ex.getLocalizedMessage());
116             }
117         }
118     }
119     
120     public void testCannotBeUsedAtOnce2() {
121         try {
122             l.process(new String JavaDoc[] { "--close", "-o" });
123             fail("Cannot be used at once");
124         } catch (CommandException ex) {
125             if (ex.getLocalizedMessage().indexOf("--close") == -1) {
126                 fail("-c should be there: " + ex.getLocalizedMessage());
127             }
128             if (ex.getLocalizedMessage().indexOf("-o") == -1) {
129                 fail("-o should be there: " + ex.getLocalizedMessage());
130             }
131         }
132     }
133     public void testCannotBeUsedAtOnce3() {
134         try {
135             l.process(new String JavaDoc[] { "--close", "--open" });
136             fail("Cannot be used at once");
137         } catch (CommandException ex) {
138             if (ex.getLocalizedMessage().indexOf("--close") == -1) {
139                 fail("-c should be there: " + ex.getLocalizedMessage());
140             }
141             if (ex.getLocalizedMessage().indexOf("--open") == -1) {
142                 fail("-o should be there: " + ex.getLocalizedMessage());
143             }
144         }
145     }
146     public void testCannotBeUsedAtOnce4() {
147         try {
148             l.process(new String JavaDoc[] { "-c", "--open" });
149             fail("Cannot be used at once");
150         } catch (CommandException ex) {
151             if (ex.getLocalizedMessage().indexOf("-c") == -1) {
152                 fail("-c should be there: " + ex.getLocalizedMessage());
153             }
154             if (ex.getLocalizedMessage().indexOf("--open") == -1) {
155                 fail("-o should be there: " + ex.getLocalizedMessage());
156             }
157         }
158     }
159     public void testNoOneCannotBeWithTwo() {
160         try {
161             l.process(new String JavaDoc[] { "--no", "--one", "anArg", "--two", "anotherArg" });
162             fail("Cannot be used at once: " + options);
163         } catch (CommandException ex) {
164             if (ex.getLocalizedMessage().indexOf("--two") == -1) {
165                 fail("--two should be there: " + ex.getLocalizedMessage());
166             }
167             if (ex.getLocalizedMessage().indexOf("--no") == -1) {
168                 fail("--no should be there: " + ex.getLocalizedMessage());
169             }
170         }
171     }
172     public void testNoOneCannotBeWithTwo2() {
173         try {
174             l.process(new String JavaDoc[] { "--no", "--one", "anArg", "-2anotherArg" });
175             fail("Cannot be used at once: " + options);
176         } catch (CommandException ex) {
177             if (ex.getLocalizedMessage().indexOf("-2") == -1) {
178                 fail("--two should be there: " + ex.getLocalizedMessage());
179             }
180             if (ex.getLocalizedMessage().indexOf("--no") == -1) {
181                 fail("--no should be there: " + ex.getLocalizedMessage());
182             }
183         }
184     }
185
186     public void process(Env env, Map JavaDoc<Option, String JavaDoc[]> values) throws CommandException {
187         options.addAll(values.keySet());
188     }
189 }
190
Popular Tags