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.io.*; |
36 | |
|
37 | |
import junit.framework.*; |
38 | |
|
39 | 12 | public class TestTextPrinter extends TestCase { |
40 | |
private CommandLine commandLine; |
41 | |
private TextPrinter printer; |
42 | |
|
43 | |
protected void setUp() throws Exception { |
44 | 12 | super.setUp(); |
45 | |
|
46 | 12 | commandLine = new CommandLine(false); |
47 | 12 | printer = new TextPrinter(getName()); |
48 | 12 | } |
49 | |
|
50 | |
public void testNoArgs() throws CommandLineException, IOException { |
51 | 1 | commandLine.addToggleSwitch("switch1"); |
52 | 1 | commandLine.addToggleSwitch("switch2"); |
53 | 1 | commandLine.parse(new String[0]); |
54 | 1 | commandLine.accept(printer); |
55 | |
|
56 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
57 | 1 | int i = 1; |
58 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
59 | 1 | assertEquals("line " + i++, null, in.readLine()); |
60 | 1 | } |
61 | |
|
62 | |
public void testPartialSwitches() throws CommandLineException, IOException { |
63 | 1 | commandLine.addToggleSwitch("switch1"); |
64 | 1 | commandLine.addToggleSwitch("switch2"); |
65 | 1 | commandLine.parse(new String[] {"-switch1"}); |
66 | 1 | commandLine.accept(printer); |
67 | |
|
68 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
69 | 1 | int i = 1; |
70 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
71 | 1 | assertEquals("line " + i++, " -switch1", in.readLine()); |
72 | 1 | assertEquals("line " + i++, null, in.readLine()); |
73 | 1 | } |
74 | |
|
75 | |
public void testToggleSwitch() throws CommandLineException, IOException { |
76 | 1 | commandLine.addToggleSwitch("switch1"); |
77 | 1 | commandLine.parse(new String[] {"-switch1"}); |
78 | 1 | commandLine.accept(printer); |
79 | |
|
80 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
81 | 1 | int i = 1; |
82 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
83 | 1 | assertEquals("line " + i++, " -switch1", in.readLine()); |
84 | 1 | assertEquals("line " + i++, null, in.readLine()); |
85 | 1 | } |
86 | |
|
87 | |
public void testSingleValueSwitch() throws CommandLineException, IOException { |
88 | 1 | commandLine.addSingleValueSwitch("switch1"); |
89 | 1 | commandLine.parse(new String[] {"-switch1", "value"}); |
90 | 1 | commandLine.accept(printer); |
91 | |
|
92 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
93 | 1 | int i = 1; |
94 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
95 | 1 | assertEquals("line " + i++, " -switch1 value", in.readLine()); |
96 | 1 | assertEquals("line " + i++, null, in.readLine()); |
97 | 1 | } |
98 | |
|
99 | |
public void testOptionalValueSwitchWithNoValue() throws CommandLineException, IOException { |
100 | 1 | commandLine.addOptionalValueSwitch("switch1"); |
101 | 1 | commandLine.parse(new String[] {"-switch1"}); |
102 | 1 | commandLine.accept(printer); |
103 | |
|
104 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
105 | 1 | int i = 1; |
106 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
107 | 1 | assertEquals("line " + i++, " -switch1", in.readLine()); |
108 | 1 | assertEquals("line " + i++, null, in.readLine()); |
109 | 1 | } |
110 | |
|
111 | |
public void testOptionalValueSwitchWithOneValue() throws CommandLineException, IOException { |
112 | 1 | commandLine.addOptionalValueSwitch("switch1"); |
113 | 1 | commandLine.parse(new String[] {"-switch1", "value"}); |
114 | 1 | commandLine.accept(printer); |
115 | |
|
116 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
117 | 1 | int i = 1; |
118 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
119 | 1 | assertEquals("line " + i++, " -switch1 value", in.readLine()); |
120 | 1 | assertEquals("line " + i++, null, in.readLine()); |
121 | 1 | } |
122 | |
|
123 | |
public void testMultipleValuesSwitchWithOneValue() throws CommandLineException, IOException { |
124 | 1 | commandLine.addMultipleValuesSwitch("switch1"); |
125 | 1 | commandLine.parse(new String[] {"-switch1", "value"}); |
126 | 1 | commandLine.accept(printer); |
127 | |
|
128 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
129 | 1 | int i = 1; |
130 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
131 | 1 | assertEquals("line " + i++, " -switch1 value", in.readLine()); |
132 | 1 | assertEquals("line " + i++, null, in.readLine()); |
133 | 1 | } |
134 | |
|
135 | |
public void testMultipleValuesSwitchWithMultipleValues() throws CommandLineException, IOException { |
136 | 1 | commandLine.addMultipleValuesSwitch("switch1"); |
137 | 1 | commandLine.parse(new String[] {"-switch1", "value1", "-switch1", "value2"}); |
138 | 1 | commandLine.accept(printer); |
139 | |
|
140 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
141 | 1 | int i = 1; |
142 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
143 | 1 | assertEquals("line " + i++, " -switch1 value1", in.readLine()); |
144 | 1 | assertEquals("line " + i++, " -switch1 value2", in.readLine()); |
145 | 1 | assertEquals("line " + i++, null, in.readLine()); |
146 | 1 | } |
147 | |
|
148 | |
public void testUnknownSwitch() throws CommandLineException, IOException { |
149 | 1 | commandLine.parse(new String[] {"-switch1"}); |
150 | 1 | commandLine.accept(printer); |
151 | |
|
152 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
153 | 1 | int i = 1; |
154 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
155 | 1 | assertEquals("line " + i++, " -switch1", in.readLine()); |
156 | 1 | assertEquals("line " + i++, null, in.readLine()); |
157 | 1 | } |
158 | |
|
159 | |
public void testOneParameter() throws CommandLineException, IOException { |
160 | 1 | commandLine.parse(new String[] {"param"}); |
161 | 1 | commandLine.accept(printer); |
162 | |
|
163 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
164 | 1 | int i = 1; |
165 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
166 | 1 | assertEquals("line " + i++, " param", in.readLine()); |
167 | 1 | assertEquals("line " + i++, null, in.readLine()); |
168 | 1 | } |
169 | |
|
170 | |
public void testMultipleParameter() throws CommandLineException, IOException { |
171 | 1 | commandLine.parse(new String[] {"param1", "param2"}); |
172 | 1 | commandLine.accept(printer); |
173 | |
|
174 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
175 | 1 | int i = 1; |
176 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
177 | 1 | assertEquals("line " + i++, " param1", in.readLine()); |
178 | 1 | assertEquals("line " + i++, " param2", in.readLine()); |
179 | 1 | assertEquals("line " + i++, null, in.readLine()); |
180 | 1 | } |
181 | |
|
182 | |
public void testSingleValueSwitchAndParameter() throws CommandLineException, IOException { |
183 | 1 | commandLine.parse(new String[] {"-switch", "value", "param"}); |
184 | 1 | commandLine.accept(printer); |
185 | |
|
186 | 1 | BufferedReader in = new BufferedReader(new StringReader(printer.toString())); |
187 | 1 | int i = 1; |
188 | 1 | assertEquals("line " + i++, getName(), in.readLine()); |
189 | 1 | assertEquals("line " + i++, " -switch value", in.readLine()); |
190 | 1 | assertEquals("line " + i++, " param", in.readLine()); |
191 | 1 | assertEquals("line " + i++, null, in.readLine()); |
192 | 1 | } |
193 | |
} |