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:
042: package org.netbeans.api.sendopts;
043:
044: import java.util.Arrays;
045: import java.util.HashSet;
046: import java.util.Map;
047: import java.util.Set;
048: import junit.framework.TestCase;
049: import org.netbeans.junit.MockServices;
050: import org.netbeans.spi.sendopts.Env;
051: import org.netbeans.spi.sendopts.Option;
052: import org.netbeans.spi.sendopts.OptionProcessor;
053:
054: /** The basic test to check semantics of getopts behaviour.
055: *
056: * @author Jaroslav Tulach
057: */
058: public class OptionProviderTest extends TestCase {
059: private CommandLine l;
060: private static Option help;
061: private static Option ok;
062:
063: static {
064: help = Option.withoutArgument('h', "help");
065: ok = Option.withoutArgument('o', "ok");
066:
067: OP.arr = new Option[] { ok, help };
068: MockServices.setServices(OP.class);
069: }
070:
071: public OptionProviderTest(String s) {
072: super (s);
073: }
074:
075: protected void setUp() throws Exception {
076:
077: OP.values = null;
078:
079: l = CommandLine.getDefault();
080: }
081:
082: public void testSingleNoArgOptionIsRecognized() throws Exception {
083: l.process(new String[] { "-h" });
084: assertEquals("Processor found", true, OP.values
085: .containsKey(help));
086: }
087:
088: public void testLongOptionRecognized() throws Exception {
089: l.process(new String[] { "--help" });
090: assertEquals("Processor found for long name", true, OP.values
091: .containsKey(help));
092: }
093:
094: public void testTwoOptionsRecognized() throws Exception {
095: l.process(new String[] { "-ho" });
096: assertEquals("Processor for help", true, OP.values
097: .containsKey(help));
098: assertEquals("Processor for ok", true, OP.values
099: .containsKey(ok));
100: }
101:
102: public void testAbrevatedNameRecognized() throws Exception {
103: l.process(new String[] { "--he" });
104: assertEquals("Processor found for abbrevated name", true,
105: OP.values.containsKey(help));
106: }
107:
108: public void testIncorrectOptionIdentified() throws Exception {
109: try {
110: l.process(new String[] { "--hell" });
111: fail("This option does not exists");
112: } catch (CommandException ex) {
113: // ok
114: }
115: assertNull("No processor called", OP.values);
116: }
117:
118: public void testNoProcessorCalledWhenOneOptionIsNotKnown()
119: throws Exception {
120: try {
121: l.process(new String[] { "-h", "--hell" });
122: fail("One option does not exists");
123: } catch (CommandException ex) {
124: // ok
125: }
126: assertNull("No processor called", OP.values);
127: }
128:
129: public static final class OP extends OptionProcessor {
130: static Option[] arr;
131: static Map<Option, String[]> values;
132:
133: protected Set<Option> getOptions() {
134: return new HashSet<Option>(Arrays.asList(arr));
135: }
136:
137: protected void process(Env env,
138: Map<Option, String[]> optionValues)
139: throws CommandException {
140: values = optionValues;
141: }
142:
143: }
144: }
|