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