001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.sendopts;
042:
043: import java.util.ArrayList;
044: import java.util.Locale;
045: import java.util.Map;
046: import junit.framework.TestCase;
047: import org.netbeans.spi.sendopts.OptionGroups;
048: import org.netbeans.spi.sendopts.Env;
049: import org.netbeans.spi.sendopts.Option;
050:
051: /** Checks that error reports from the system are sane.
052: *
053: * @author Jaroslav Tulach
054: */
055: public class ErrorMessagesTest extends TestCase implements Processor {
056: private CommandLine l;
057:
058: private ArrayList<Option> options = new ArrayList<Option>();
059:
060: public ErrorMessagesTest(String s) {
061: super (s);
062: }
063:
064: protected void tearDown() throws Exception {
065:
066: super .tearDown();
067: }
068:
069: protected void setUp() throws Exception {
070: Locale.setDefault(Locale.US);
071:
072: Option no = Option.withoutArgument((char) -1, "no");
073: Option open = Option.additionalArguments('o', "open");
074: Option close = Option.additionalArguments('c', "close");
075: Option one = Option.requiredArgument('1', "one");
076: Option two = Option.requiredArgument('2', "two");
077: Option optional = Option.optionalArgument((char) -1, "option");
078: Option both = OptionGroups.allOf(no, one);
079: Option bothDef = OptionGroups.someOf(both, open);
080: Option allOf = OptionGroups.oneOf(open, two, both);
081:
082: Provider.clearAll();
083: Provider.add(this , no, open, close, one, two, optional, both,
084: bothDef, allOf);
085:
086: l = CommandLine.getDefault();
087: }
088:
089: public void testMissingArgument() {
090: try {
091: l.process(new String[] { "--one" });
092: fail("This is going to fail");
093: } catch (CommandException ex) {
094: if (ex.getLocalizedMessage().indexOf("Option --one") == -1) {
095: fail(ex.getLocalizedMessage());
096: }
097:
098: if (ex.getLocalizedMessage().indexOf("needs") == -1) {
099: fail(ex.getLocalizedMessage());
100: }
101:
102: if (ex.getLocalizedMessage().indexOf("argument") == -1) {
103: fail(ex.getLocalizedMessage());
104: }
105: }
106: }
107:
108: public void testShortMissingArgument() {
109: try {
110: l.process(new String[] { "-1" });
111: fail("This is going to fail");
112: } catch (CommandException ex) {
113: if (ex.getLocalizedMessage().indexOf("Option -1") == -1) {
114: fail(ex.getLocalizedMessage());
115: }
116:
117: if (ex.getLocalizedMessage().indexOf("needs") == -1) {
118: fail(ex.getLocalizedMessage());
119: }
120:
121: if (ex.getLocalizedMessage().indexOf("argument") == -1) {
122: fail(ex.getLocalizedMessage());
123: }
124: }
125: }
126:
127: public void testCannotBeUsedAtOnce() {
128: try {
129: l.process(new String[] { "-c", "-o" });
130: fail("Cannot be used at once");
131: } catch (CommandException ex) {
132: if (ex.getLocalizedMessage().indexOf("-c") == -1) {
133: fail("-c should be there: " + ex.getLocalizedMessage());
134: }
135: if (ex.getLocalizedMessage().indexOf("-o") == -1) {
136: fail("-o should be there: " + ex.getLocalizedMessage());
137: }
138: }
139: }
140:
141: public void testCannotBeUsedAtOnce2() {
142: try {
143: l.process(new String[] { "--close", "-o" });
144: fail("Cannot be used at once");
145: } catch (CommandException ex) {
146: if (ex.getLocalizedMessage().indexOf("--close") == -1) {
147: fail("-c should be there: " + ex.getLocalizedMessage());
148: }
149: if (ex.getLocalizedMessage().indexOf("-o") == -1) {
150: fail("-o should be there: " + ex.getLocalizedMessage());
151: }
152: }
153: }
154:
155: public void testCannotBeUsedAtOnce3() {
156: try {
157: l.process(new String[] { "--close", "--open" });
158: fail("Cannot be used at once");
159: } catch (CommandException ex) {
160: if (ex.getLocalizedMessage().indexOf("--close") == -1) {
161: fail("-c should be there: " + ex.getLocalizedMessage());
162: }
163: if (ex.getLocalizedMessage().indexOf("--open") == -1) {
164: fail("-o should be there: " + ex.getLocalizedMessage());
165: }
166: }
167: }
168:
169: public void testCannotBeUsedAtOnce4() {
170: try {
171: l.process(new String[] { "-c", "--open" });
172: fail("Cannot be used at once");
173: } catch (CommandException ex) {
174: if (ex.getLocalizedMessage().indexOf("-c") == -1) {
175: fail("-c should be there: " + ex.getLocalizedMessage());
176: }
177: if (ex.getLocalizedMessage().indexOf("--open") == -1) {
178: fail("-o should be there: " + ex.getLocalizedMessage());
179: }
180: }
181: }
182:
183: public void testNoOneCannotBeWithTwo() {
184: try {
185: l.process(new String[] { "--no", "--one", "anArg", "--two",
186: "anotherArg" });
187: fail("Cannot be used at once: " + options);
188: } catch (CommandException ex) {
189: if (ex.getLocalizedMessage().indexOf("--two") == -1) {
190: fail("--two should be there: "
191: + ex.getLocalizedMessage());
192: }
193: if (ex.getLocalizedMessage().indexOf("--no") == -1) {
194: fail("--no should be there: "
195: + ex.getLocalizedMessage());
196: }
197: }
198: }
199:
200: public void testNoOneCannotBeWithTwo2() {
201: try {
202: l.process(new String[] { "--no", "--one", "anArg",
203: "-2anotherArg" });
204: fail("Cannot be used at once: " + options);
205: } catch (CommandException ex) {
206: if (ex.getLocalizedMessage().indexOf("-2") == -1) {
207: fail("--two should be there: "
208: + ex.getLocalizedMessage());
209: }
210: if (ex.getLocalizedMessage().indexOf("--no") == -1) {
211: fail("--no should be there: "
212: + ex.getLocalizedMessage());
213: }
214: }
215: }
216:
217: public void process(Env env, Map<Option, String[]> values)
218: throws CommandException {
219: options.addAll(values.keySet());
220: }
221: }
|