001: package com.jeantessier.commandline;
002:
003: import java.util.*;
004:
005: import junit.framework.*;
006:
007: public class TestMultipleValuesSwitch extends TestCase {
008: private static final String DEFAULT_VALUE1 = "default value 1";
009: private static final String DEFAULT_VALUE2 = "default value 2";
010: private static final List<String> DEFAULT_VALUE = new ArrayList<String>();
011:
012: private static final String EXPECTED_VALUE1 = "expected value 1";
013: private static final String EXPECTED_VALUE2 = "expected value 2";
014: private static final List<String> EXPECTED_VALUE = new ArrayList<String>();
015:
016: private static final List<String> NULL_VALUE = new ArrayList<String>();
017:
018: static {
019: DEFAULT_VALUE.add(DEFAULT_VALUE1);
020: DEFAULT_VALUE.add(DEFAULT_VALUE2);
021:
022: EXPECTED_VALUE.add(EXPECTED_VALUE1);
023: EXPECTED_VALUE.add(EXPECTED_VALUE2);
024:
025: NULL_VALUE.add(null);
026: }
027:
028: private MultipleValuesSwitch commandLineSwitch;
029:
030: protected void setUp() throws Exception {
031: super .setUp();
032:
033: commandLineSwitch = new MultipleValuesSwitch("switch",
034: DEFAULT_VALUE);
035: }
036:
037: public void testSetToNull() {
038: assertFalse(commandLineSwitch.isPresent());
039: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
040: commandLineSwitch.setValue(null);
041: assertTrue(commandLineSwitch.isPresent());
042: assertEquals(NULL_VALUE, commandLineSwitch.getValue());
043: }
044:
045: public void testSetToObject() {
046: assertFalse(commandLineSwitch.isPresent());
047: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
048: commandLineSwitch.setValue(EXPECTED_VALUE1);
049: commandLineSwitch.setValue(EXPECTED_VALUE2);
050: assertTrue(commandLineSwitch.isPresent());
051: assertEquals(EXPECTED_VALUE, commandLineSwitch.getValue());
052: }
053:
054: public void testParseNull() throws CommandLineException {
055: try {
056: commandLineSwitch.parse(null);
057: fail("Parsed without a value");
058: } catch (CommandLineException e) {
059: // Expected
060: }
061: }
062:
063: public void testParseEmptyString() throws CommandLineException {
064: assertFalse(commandLineSwitch.isPresent());
065: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
066: commandLineSwitch.parse("");
067: assertTrue(commandLineSwitch.isPresent());
068: assertEquals(Collections.singletonList(""), commandLineSwitch
069: .getValue());
070: }
071:
072: public void testParseString() throws CommandLineException {
073: assertFalse(commandLineSwitch.isPresent());
074: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
075: commandLineSwitch.parse(EXPECTED_VALUE1);
076: commandLineSwitch.parse(EXPECTED_VALUE2);
077: assertTrue(commandLineSwitch.isPresent());
078: assertEquals(EXPECTED_VALUE, commandLineSwitch.getValue());
079: }
080:
081: public void testValidateWhenNotMandatory()
082: throws CommandLineException {
083: commandLineSwitch.validate();
084: commandLineSwitch.parse(EXPECTED_VALUE1);
085: commandLineSwitch.parse(EXPECTED_VALUE2);
086: commandLineSwitch.validate();
087: }
088:
089: public void testValidateWhenMandatory() throws CommandLineException {
090: commandLineSwitch = new MultipleValuesSwitch("switch",
091: "default", true);
092: try {
093: commandLineSwitch.validate();
094: fail("Missing mandatory switch should not validate.");
095: } catch (CommandLineException e) {
096: // Expected
097: }
098: commandLineSwitch.parse(EXPECTED_VALUE1);
099: commandLineSwitch.parse(EXPECTED_VALUE2);
100: commandLineSwitch.validate();
101: }
102: }
|