01: package com.jeantessier.commandline;
02:
03: import junit.framework.*;
04:
05: public class TestSingleValueSwitch extends TestCase {
06: private static final String DEFAULT_VALUE = "default value";
07:
08: private SingleValueSwitch commandLineSwitch;
09: private static final String EXPECTED_VALUE = "expected value";
10:
11: protected void setUp() throws Exception {
12: super .setUp();
13:
14: commandLineSwitch = new SingleValueSwitch("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: try {
36: commandLineSwitch.parse(null);
37: fail("Parsed without a value");
38: } catch (CommandLineException e) {
39: // Expected
40: }
41: }
42:
43: public void testParseEmptyString() throws CommandLineException {
44: assertFalse(commandLineSwitch.isPresent());
45: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
46: commandLineSwitch.parse("");
47: assertTrue(commandLineSwitch.isPresent());
48: assertEquals("", commandLineSwitch.getValue());
49: }
50:
51: public void testParseString() throws CommandLineException {
52: assertFalse(commandLineSwitch.isPresent());
53: assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
54: commandLineSwitch.parse(EXPECTED_VALUE);
55: assertTrue(commandLineSwitch.isPresent());
56: assertEquals(EXPECTED_VALUE, commandLineSwitch.getValue());
57: }
58:
59: public void testValidateWhenNotMandatory()
60: throws CommandLineException {
61: commandLineSwitch.validate();
62: commandLineSwitch.parse(EXPECTED_VALUE);
63: commandLineSwitch.validate();
64: }
65:
66: public void testValidateWhenMandatory() throws CommandLineException {
67: commandLineSwitch = new SingleValueSwitch("switch", "default",
68: true);
69: try {
70: commandLineSwitch.validate();
71: fail("Missing mandatory switch should not validate.");
72: } catch (CommandLineException e) {
73: // Expected
74: }
75: commandLineSwitch.parse(EXPECTED_VALUE);
76: commandLineSwitch.validate();
77: }
78: }
|