001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.commons.cli;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020:
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024:
025: /**
026: * @author Rob Oxspring roxspring@apache.org
027: * @version $Revision: 544360 $
028: */
029: public class OptionsTest extends TestCase {
030:
031: public static Test suite() {
032: return new TestSuite(OptionsTest.class);
033: }
034:
035: public OptionsTest(String name) {
036: super (name);
037: }
038:
039: public void setUp() {
040: }
041:
042: public void tearDown() {
043: }
044:
045: public void testHelpOptions() {
046:
047: Option longOnly1 = OptionBuilder.withLongOpt("long-only1")
048: .create();
049:
050: Option longOnly2 = OptionBuilder.withLongOpt("long-only2")
051: .create();
052:
053: Option shortOnly1 = OptionBuilder.create("1");
054:
055: Option shortOnly2 = OptionBuilder.create("2");
056:
057: Option bothA = OptionBuilder.withLongOpt("bothA").create("a");
058:
059: Option bothB = OptionBuilder.withLongOpt("bothB").create("b");
060:
061: Options options = new Options();
062: options.addOption(longOnly1);
063: options.addOption(longOnly2);
064: options.addOption(shortOnly1);
065: options.addOption(shortOnly2);
066: options.addOption(bothA);
067: options.addOption(bothB);
068:
069: Collection allOptions = new ArrayList();
070: allOptions.add(longOnly1);
071: allOptions.add(longOnly2);
072: allOptions.add(shortOnly1);
073: allOptions.add(shortOnly2);
074: allOptions.add(bothA);
075: allOptions.add(bothB);
076:
077: Collection helpOptions = options.helpOptions();
078:
079: assertTrue("Everything in all should be in help", helpOptions
080: .containsAll(allOptions));
081: assertTrue("Everything in help should be in all", allOptions
082: .containsAll(helpOptions));
083: }
084:
085: public void testMissingOptionException() throws ParseException {
086: Options options = new Options();
087: options.addOption(OptionBuilder.isRequired().create("f"));
088: try {
089: new PosixParser().parse(options, new String[0]);
090: fail("Expected MissingOptionException to be thrown");
091: } catch (MissingOptionException e) {
092: assertEquals("Missing required option: f", e.getMessage());
093: }
094: }
095:
096: public void testMissingOptionsException() throws ParseException {
097: Options options = new Options();
098: options.addOption(OptionBuilder.isRequired().create("f"));
099: options.addOption(OptionBuilder.isRequired().create("x"));
100: try {
101: new PosixParser().parse(options, new String[0]);
102: fail("Expected MissingOptionException to be thrown");
103: } catch (MissingOptionException e) {
104: assertEquals("Missing required options: fx", e.getMessage());
105: }
106: }
107:
108: }
|