001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.commandline;
034:
035: import junit.framework.*;
036:
037: public class TestCommandLine extends TestCase {
038: private static final String SWITCH_NAME = "switch";
039: private static final String SWITCH_DEFAULT_VALUE = "default value";
040: private static final String SWITCH_VALUE = "value";
041:
042: private CommandLine commandLine;
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: commandLine = new CommandLine();
048: }
049:
050: public void testAddToggleSwitch() {
051: CommandLineSwitch cls = commandLine
052: .addToggleSwitch(SWITCH_NAME);
053: assertTrue("Wrong type", cls instanceof ToggleSwitch);
054: assertEquals("name", SWITCH_NAME, cls.getName());
055: assertFalse("is present", cls.isPresent());
056: }
057:
058: public void addAliasSwitch() throws CommandLineException {
059: ToggleSwitch toggle1 = commandLine.addToggleSwitch("toggle1");
060: ToggleSwitch toggle2 = commandLine.addToggleSwitch("toggle2");
061:
062: AliasSwitch aliasSwitch = commandLine.addAliasSwitch(
063: SWITCH_NAME, "toogle1", "toggle2");
064: assertEquals("Nb switches", 2, aliasSwitch.getSwitches().size());
065: assertTrue("Missing toggle1", aliasSwitch.getSwitches()
066: .contains(toggle1));
067: assertTrue("Missing toggle2", aliasSwitch.getSwitches()
068: .contains(toggle2));
069: }
070:
071: public void testAddAliasForNonExistingSwitch() {
072: try {
073: commandLine.addAliasSwitch(SWITCH_NAME, "foobar");
074: fail("Added alias to non-existing switch");
075: } catch (IllegalArgumentException e) {
076: // Expected
077: }
078: }
079:
080: public void testParseToggleSwitchTwice()
081: throws CommandLineException {
082: commandLine.addToggleSwitch(SWITCH_NAME);
083: String cls = "-" + SWITCH_NAME;
084: commandLine.parse(new String[] { cls, cls });
085: assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
086: }
087:
088: public void testParseSingleValueSwitchTwice()
089: throws CommandLineException {
090: commandLine.addSingleValueSwitch(SWITCH_NAME);
091: String cls = "-" + SWITCH_NAME;
092: commandLine.parse(new String[] { cls, SWITCH_VALUE, cls,
093: SWITCH_VALUE });
094: assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
095: assertEquals("Single value switch value", SWITCH_VALUE,
096: commandLine.getSingleSwitch(SWITCH_NAME));
097: }
098:
099: public void testParseOptionalValueSwitchTwice()
100: throws CommandLineException {
101: commandLine.addOptionalValueSwitch(SWITCH_NAME,
102: SWITCH_DEFAULT_VALUE);
103: String cls = "-" + SWITCH_NAME;
104: commandLine.parse(new String[] { cls, SWITCH_VALUE, cls });
105: assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
106: assertEquals("Optional value switch value",
107: SWITCH_DEFAULT_VALUE, commandLine
108: .getOptionalSwitch(SWITCH_NAME));
109: }
110:
111: public void testParseMultipleValueSwitchTwice()
112: throws CommandLineException {
113: commandLine.addMultipleValuesSwitch(SWITCH_NAME,
114: SWITCH_DEFAULT_VALUE);
115: String cls = "-" + SWITCH_NAME;
116: commandLine.parse(new String[] { cls, SWITCH_VALUE, cls,
117: SWITCH_VALUE });
118: assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
119: assertEquals("Multiple value switch value size", 2, commandLine
120: .getMultipleSwitch(SWITCH_NAME).size());
121: assertEquals("Multiple value switch value 0", SWITCH_VALUE,
122: commandLine.getMultipleSwitch(SWITCH_NAME).get(0));
123: assertEquals("Multiple value switch value 1", SWITCH_VALUE,
124: commandLine.getMultipleSwitch(SWITCH_NAME).get(1));
125: }
126:
127: public void testParseAliasSwitch() throws CommandLineException {
128: commandLine.addToggleSwitch("toggle1");
129: commandLine.addToggleSwitch("toggle2");
130: commandLine.addAliasSwitch(SWITCH_NAME, "toggle1", "toggle2");
131:
132: String cls = "-" + SWITCH_NAME;
133: commandLine.parse(new String[] { cls });
134: assertTrue("Missing toggle1", commandLine.isPresent("toggle1"));
135: assertTrue("Missing toggle2", commandLine.isPresent("toggle2"));
136: }
137: }
|