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 TestAliasSwitch extends TestCase {
038: private static final String SWITCH_NAME = "switch";
039: private static final String SWITCH_VALUE = "value";
040:
041: private CommandLineSwitch switch1;
042: private CommandLineSwitch switch2;
043:
044: private AliasSwitch aliasSwitch;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048:
049: switch1 = new SingleValueSwitch("switch1");
050: switch2 = new OptionalValueSwitch("switch2");
051:
052: aliasSwitch = new AliasSwitch(SWITCH_NAME, switch1, switch2);
053: }
054:
055: public void testConstructor() {
056: assertEquals("Name", SWITCH_NAME, aliasSwitch.getName());
057: assertEquals("Nb switches", 2, aliasSwitch.getSwitches().size());
058: assertTrue("Missing switch1", aliasSwitch.getSwitches()
059: .contains(switch1));
060: assertTrue("Missing switch2", aliasSwitch.getSwitches()
061: .contains(switch2));
062: }
063:
064: public void testSetValueToNull() {
065: aliasSwitch.setValue(null);
066: assertEquals("Switch1 not default value", switch1
067: .getDefaultValue(), switch1.getValue());
068: assertEquals("Switch2 not default value", switch2
069: .getDefaultValue(), switch2.getValue());
070: }
071:
072: public void testSetValueToValue() {
073: aliasSwitch.setValue(SWITCH_VALUE);
074: assertEquals("Switch1 not new value", SWITCH_VALUE, switch1
075: .getValue());
076: assertEquals("Switch2 not new value", SWITCH_VALUE, switch2
077: .getValue());
078: }
079:
080: public void testIsPresent() {
081: aliasSwitch.setValue(SWITCH_VALUE);
082: assertTrue("Switch1 not present", switch1.isPresent());
083: assertTrue("Switch2 not present", switch2.isPresent());
084: assertTrue("Not present", aliasSwitch.isPresent());
085: }
086:
087: public void testIsPresentWithNoSwitches() {
088: aliasSwitch = new AliasSwitch(SWITCH_NAME);
089: aliasSwitch.setValue(SWITCH_VALUE);
090: assertFalse("Present", aliasSwitch.isPresent());
091: }
092:
093: public void testParseNull() throws CommandLineException {
094: aliasSwitch = new AliasSwitch(SWITCH_NAME, switch2);
095: int step = aliasSwitch.parse(null);
096: assertEquals("step", 1, step);
097: assertFalse("Switch1 was set", switch1.isPresent());
098: assertEquals("Switch2 not default value", switch2
099: .getDefaultValue(), switch2.getValue());
100: }
101:
102: public void testParseNullWithError() throws CommandLineException {
103: try {
104: aliasSwitch.parse(null);
105: fail("Alias with SingleValueSwitch parsed null");
106: } catch (CommandLineException e) {
107: // Expected
108: }
109: }
110:
111: public void testParseValue() throws CommandLineException {
112: int step = aliasSwitch.parse(SWITCH_VALUE);
113: assertEquals("step", 2, step);
114: assertEquals("Switch1 not new value", SWITCH_VALUE, switch1
115: .getValue());
116: assertEquals("Switch2 not new value", SWITCH_VALUE, switch2
117: .getValue());
118: }
119:
120: public void testParseNullWithNoSwitches()
121: throws CommandLineException {
122: aliasSwitch = new AliasSwitch(SWITCH_NAME);
123: int step = aliasSwitch.parse(null);
124: assertEquals("step", 1, step);
125: }
126:
127: public void testParseValueWithNoSwitches()
128: throws CommandLineException {
129: aliasSwitch = new AliasSwitch(SWITCH_NAME);
130: int step = aliasSwitch.parse(SWITCH_VALUE);
131: assertEquals("step", 1, step);
132: }
133:
134: public void testAccept() {
135: MockVisitor visitor = new MockVisitor();
136: aliasSwitch.accept(visitor);
137: assertTrue("visitAliasSwitch() was called", visitor
138: .visitAliasSwitchWasCalled());
139: assertFalse("visit() was called", visitor.visitWasCalled());
140: }
141: }
|