1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| package com.jeantessier.commandline; |
34 |
| |
35 |
| import java.util.*; |
36 |
| |
37 |
| import junit.framework.*; |
38 |
| |
39 |
| public class TestMultipleValuesSwitch extends TestCase { |
40 |
| private static final String DEFAULT_VALUE1 = "default value 1"; |
41 |
| private static final String DEFAULT_VALUE2 = "default value 2"; |
42 |
| private static final List<String> DEFAULT_VALUE = new ArrayList<String>(); |
43 |
| |
44 |
| private static final String EXPECTED_VALUE1 = "expected value 1"; |
45 |
| private static final String EXPECTED_VALUE2 = "expected value 2"; |
46 |
| private static final List<String> EXPECTED_VALUE = new ArrayList<String>(); |
47 |
| |
48 |
| private static final List<String> NULL_VALUE = new ArrayList<String>(); |
49 |
| |
50 |
| static { |
51 |
1
| DEFAULT_VALUE.add(DEFAULT_VALUE1);
|
52 |
1
| DEFAULT_VALUE.add(DEFAULT_VALUE2);
|
53 |
| |
54 |
1
| EXPECTED_VALUE.add(EXPECTED_VALUE1);
|
55 |
1
| EXPECTED_VALUE.add(EXPECTED_VALUE2);
|
56 |
| |
57 |
1
| NULL_VALUE.add(null);
|
58 |
| } |
59 |
| |
60 |
| private MultipleValuesSwitch commandLineSwitch; |
61 |
| |
62 |
7
| protected void setUp() throws Exception {
|
63 |
7
| super.setUp();
|
64 |
| |
65 |
7
| commandLineSwitch = new MultipleValuesSwitch("switch", DEFAULT_VALUE);
|
66 |
| } |
67 |
| |
68 |
1
| public void testSetToNull() {
|
69 |
1
| assertFalse(commandLineSwitch.isPresent());
|
70 |
1
| assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
|
71 |
1
| commandLineSwitch.setValue(null);
|
72 |
1
| assertTrue(commandLineSwitch.isPresent());
|
73 |
1
| assertEquals(NULL_VALUE, commandLineSwitch.getValue());
|
74 |
| } |
75 |
| |
76 |
1
| public void testSetToObject() {
|
77 |
1
| assertFalse(commandLineSwitch.isPresent());
|
78 |
1
| assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
|
79 |
1
| commandLineSwitch.setValue(EXPECTED_VALUE1);
|
80 |
1
| commandLineSwitch.setValue(EXPECTED_VALUE2);
|
81 |
1
| assertTrue(commandLineSwitch.isPresent());
|
82 |
1
| assertEquals(EXPECTED_VALUE, commandLineSwitch.getValue());
|
83 |
| } |
84 |
| |
85 |
1
| public void testParseNull() throws CommandLineException {
|
86 |
1
| try {
|
87 |
1
| commandLineSwitch.parse(null);
|
88 |
0
| fail("Parsed without a value");
|
89 |
| } catch (CommandLineException e) { |
90 |
| |
91 |
| } |
92 |
| } |
93 |
| |
94 |
1
| public void testParseEmptyString() throws CommandLineException {
|
95 |
1
| assertFalse(commandLineSwitch.isPresent());
|
96 |
1
| assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
|
97 |
1
| commandLineSwitch.parse("");
|
98 |
1
| assertTrue(commandLineSwitch.isPresent());
|
99 |
1
| assertEquals(Collections.singletonList(""), commandLineSwitch.getValue());
|
100 |
| } |
101 |
| |
102 |
1
| public void testParseString() throws CommandLineException {
|
103 |
1
| assertFalse(commandLineSwitch.isPresent());
|
104 |
1
| assertEquals(DEFAULT_VALUE, commandLineSwitch.getValue());
|
105 |
1
| commandLineSwitch.parse(EXPECTED_VALUE1);
|
106 |
1
| commandLineSwitch.parse(EXPECTED_VALUE2);
|
107 |
1
| assertTrue(commandLineSwitch.isPresent());
|
108 |
1
| assertEquals(EXPECTED_VALUE, commandLineSwitch.getValue());
|
109 |
| } |
110 |
| |
111 |
1
| public void testValidateWhenNotMandatory() throws CommandLineException {
|
112 |
1
| commandLineSwitch.validate();
|
113 |
1
| commandLineSwitch.parse(EXPECTED_VALUE1);
|
114 |
1
| commandLineSwitch.parse(EXPECTED_VALUE2);
|
115 |
1
| commandLineSwitch.validate();
|
116 |
| } |
117 |
| |
118 |
1
| public void testValidateWhenMandatory() throws CommandLineException {
|
119 |
1
| commandLineSwitch = new MultipleValuesSwitch("switch", "default", true);
|
120 |
1
| try {
|
121 |
1
| commandLineSwitch.validate();
|
122 |
0
| fail("Missing mandatory switch should not validate.");
|
123 |
| } catch (CommandLineException e) { |
124 |
| |
125 |
| } |
126 |
1
| commandLineSwitch.parse(EXPECTED_VALUE1);
|
127 |
1
| commandLineSwitch.parse(EXPECTED_VALUE2);
|
128 |
1
| commandLineSwitch.validate();
|
129 |
| } |
130 |
| } |