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.io.PrintWriter;
044: import java.io.StringWriter;
045: import java.util.Map;
046: import java.util.regex.Matcher;
047: import java.util.regex.Pattern;
048: import junit.framework.TestCase;
049: import org.netbeans.spi.sendopts.Env;
050: import org.netbeans.spi.sendopts.Option;
051:
052: /** The basic test to check semantics of always option.
053: *
054: * @author Jaroslav Tulach
055: */
056: public class AlwaysOptionTest extends TestCase {
057: private CommandLine l;
058: private NoArgProc proc = new NoArgProc();
059: private Option help;
060: private NoArgProc okProc = new NoArgProc();
061: private Option ok;
062:
063: private Always always = new Always();
064: private Option all;
065:
066: public AlwaysOptionTest(String s) {
067: super (s);
068: }
069:
070: protected void tearDown() throws Exception {
071:
072: super .tearDown();
073: }
074:
075: protected void setUp() throws Exception {
076:
077: help = Option.withoutArgument('h', "help");
078: Provider.clearAll();
079: Provider.add(proc, help);
080: ok = Option.withoutArgument('o', "ok");
081: Provider.add(okProc, ok);
082:
083: all = Option.always();
084: Provider.add(always, all);
085:
086: l = CommandLine.getDefault();
087: }
088:
089: public void testSingleNoArgOptionIsRecognized() throws Exception {
090: l.process(new String[] { "-h" });
091: assertEquals("Processor found", help, proc.option);
092: assertEquals("Always", all, always.option);
093: }
094:
095: public void testLongOptionRecognized() throws Exception {
096: l.process(new String[] { "--help" });
097: assertEquals("Processor found for long name", help, proc.option);
098: assertEquals("Always", all, always.option);
099: }
100:
101: public void testTwoOptionsRecognized() throws Exception {
102: l.process(new String[] { "-ho" });
103: assertEquals("Processor for help", help, proc.option);
104: assertEquals("Processor for ok", ok, okProc.option);
105: assertEquals("Always", all, always.option);
106: }
107:
108: public void testAbrevatedNameRecognized() throws Exception {
109: l.process(new String[] { "--he" });
110: assertEquals("Processor found for abbrevated name", help,
111: proc.option);
112: assertEquals("Always", all, always.option);
113:
114: proc.option = null;
115: always.option = null;
116: l.process(new String[] { "--he" });
117: assertEquals("Processor found for abbrevated name again", help,
118: proc.option);
119: assertEquals("Always", all, always.option);
120: }
121:
122: public void testIncorrectOptionIdentified() throws Exception {
123: try {
124: l.process(new String[] { "--hell" });
125: fail("This option does not exists");
126: } catch (CommandException ex) {
127: // ok
128: }
129: assertNull("No processor called", proc.option);
130: assertEquals("Always not called in case of failure", null,
131: always.option);
132: }
133:
134: public void testNoProcessorCalledWhenOneOptionIsNotKnown()
135: throws Exception {
136: try {
137: l.process(new String[] { "-h", "--hell" });
138: fail("One option does not exists");
139: } catch (CommandException ex) {
140: // ok
141: }
142: assertNull("No processor called", proc.option);
143: assertEquals("Always not called in case of failure", null,
144: always.option);
145: }
146:
147: static final class NoArgProc implements Processor {
148: Option option;
149:
150: public void process(Env env, Map<Option, String[]> values)
151: throws CommandException {
152: assertNull("Not processed yet", option);
153: assertEquals(1, values.size());
154: option = values.keySet().iterator().next();
155: assertNotNull("An option is provided", option);
156:
157: String[] args = values.get(option);
158: assertNotNull("Values is always [0]", args);
159: assertEquals("Values is always [0]", 0, args.length);
160: }
161: }
162:
163: static final class Always implements Processor {
164: Option option;
165:
166: public void process(Env env, Map<Option, String[]> values)
167: throws CommandException {
168: assertNull("Not processed yet", option);
169: assertEquals(1, values.size());
170: option = values.keySet().iterator().next();
171: assertNotNull("An option is provided", option);
172:
173: String[] args = values.get(option);
174: assertNotNull("Values is always [0]", args);
175: assertEquals("Values is always [0]", 0, args.length);
176: }
177: }
178: }
|