01: package com.jeantessier.commandline;
02:
03: import junit.framework.*;
04:
05: public class TestOptionalValueSwitch extends TestCase {
06: private static final String DEFAULT_VALUE = "default value";
07:
08: private OptionalValueSwitch commandLineSwitch;
09: private static final String EXPECTED_VALUE = "expected value";
10:
11: protected void setUp() throws Exception {
12: super .setUp();
13:
14: commandLineSwitch = new OptionalValueSwitch("switch",
15: DEFAULT_VALUE);
16: }
17:
18: public void testSetToNull() {
19: assertFalse(commandLineSwitch.isPresent());
20: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
21: commandLineSwitch.setValue(null);
22: assertTrue(commandLineSwitch.isPresent());
23: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
24: }
25:
26: public void testSetToObject() {
27: assertFalse(commandLineSwitch.isPresent());
28: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
29: commandLineSwitch.setValue(EXPECTED_VALUE);
30: assertTrue(commandLineSwitch.isPresent());
31: assertEquals(EXPECTED_VALUE, commandLineSwitch.getValue());
32: }
33:
34: public void testParseNull() throws CommandLineException {
35: assertFalse(commandLineSwitch.isPresent());
36: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
37: commandLineSwitch.parse(null);
38: assertTrue(commandLineSwitch.isPresent());
39: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
40: }
41:
42: public void testParseEmptyString() throws CommandLineException {
43: assertFalse(commandLineSwitch.isPresent());
44: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
45: commandLineSwitch.parse("");
46: assertTrue(commandLineSwitch.isPresent());
47: assertEquals("", commandLineSwitch.getValue());
48: }
49:
50: public void testParseString() throws CommandLineException {
51: assertFalse(commandLineSwitch.isPresent());
52: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
53: commandLineSwitch.parse(EXPECTED_VALUE);
54: assertTrue(commandLineSwitch.isPresent());
55: assertEquals(EXPECTED_VALUE, commandLineSwitch.getValue());
56: }
57:
58: public void testValidateWhenNotMandatory()
59: throws CommandLineException {
60: commandLineSwitch.validate();
61: commandLineSwitch.parse(EXPECTED_VALUE);
62: commandLineSwitch.validate();
63: }
64:
65: public void testValidateWhenMandatory() throws CommandLineException {
66: commandLineSwitch = new OptionalValueSwitch("switch",
67: "default", true);
68: try {
69: commandLineSwitch.validate();
70: fail("Missing mandatory switch should not validate.");
71: } catch (CommandLineException e) {
72: // Expected
73: }
74: commandLineSwitch.parse(EXPECTED_VALUE);
75: commandLineSwitch.validate();
76: }
77: }
|