001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.commandline;
034:
035: import java.io.*;
036:
037: import junit.framework.*;
038:
039: public class TestTextPrinter extends TestCase {
040: private CommandLine commandLine;
041: private TextPrinter printer;
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045:
046: commandLine = new CommandLine(false);
047: printer = new TextPrinter(getName());
048: }
049:
050: public void testNoArgs() throws CommandLineException, IOException {
051: commandLine.addToggleSwitch("switch1");
052: commandLine.addToggleSwitch("switch2");
053: commandLine.parse(new String[0]);
054: commandLine.accept(printer);
055:
056: BufferedReader in = new BufferedReader(new StringReader(printer
057: .toString()));
058: int i = 1;
059: assertEquals("line " + i++, getName(), in.readLine());
060: assertEquals("line " + i++, null, in.readLine());
061: }
062:
063: public void testPartialSwitches() throws CommandLineException,
064: IOException {
065: commandLine.addToggleSwitch("switch1");
066: commandLine.addToggleSwitch("switch2");
067: commandLine.parse(new String[] { "-switch1" });
068: commandLine.accept(printer);
069:
070: BufferedReader in = new BufferedReader(new StringReader(printer
071: .toString()));
072: int i = 1;
073: assertEquals("line " + i++, getName(), in.readLine());
074: assertEquals("line " + i++, " -switch1", in.readLine());
075: assertEquals("line " + i++, null, in.readLine());
076: }
077:
078: public void testToggleSwitch() throws CommandLineException,
079: IOException {
080: commandLine.addToggleSwitch("switch1");
081: commandLine.parse(new String[] { "-switch1" });
082: commandLine.accept(printer);
083:
084: BufferedReader in = new BufferedReader(new StringReader(printer
085: .toString()));
086: int i = 1;
087: assertEquals("line " + i++, getName(), in.readLine());
088: assertEquals("line " + i++, " -switch1", in.readLine());
089: assertEquals("line " + i++, null, in.readLine());
090: }
091:
092: public void testSingleValueSwitch() throws CommandLineException,
093: IOException {
094: commandLine.addSingleValueSwitch("switch1");
095: commandLine.parse(new String[] { "-switch1", "value" });
096: commandLine.accept(printer);
097:
098: BufferedReader in = new BufferedReader(new StringReader(printer
099: .toString()));
100: int i = 1;
101: assertEquals("line " + i++, getName(), in.readLine());
102: assertEquals("line " + i++, " -switch1 value", in.readLine());
103: assertEquals("line " + i++, null, in.readLine());
104: }
105:
106: public void testOptionalValueSwitchWithNoValue()
107: throws CommandLineException, IOException {
108: commandLine.addOptionalValueSwitch("switch1");
109: commandLine.parse(new String[] { "-switch1" });
110: commandLine.accept(printer);
111:
112: BufferedReader in = new BufferedReader(new StringReader(printer
113: .toString()));
114: int i = 1;
115: assertEquals("line " + i++, getName(), in.readLine());
116: assertEquals("line " + i++, " -switch1", in.readLine());
117: assertEquals("line " + i++, null, in.readLine());
118: }
119:
120: public void testOptionalValueSwitchWithOneValue()
121: throws CommandLineException, IOException {
122: commandLine.addOptionalValueSwitch("switch1");
123: commandLine.parse(new String[] { "-switch1", "value" });
124: commandLine.accept(printer);
125:
126: BufferedReader in = new BufferedReader(new StringReader(printer
127: .toString()));
128: int i = 1;
129: assertEquals("line " + i++, getName(), in.readLine());
130: assertEquals("line " + i++, " -switch1 value", in.readLine());
131: assertEquals("line " + i++, null, in.readLine());
132: }
133:
134: public void testMultipleValuesSwitchWithOneValue()
135: throws CommandLineException, IOException {
136: commandLine.addMultipleValuesSwitch("switch1");
137: commandLine.parse(new String[] { "-switch1", "value" });
138: commandLine.accept(printer);
139:
140: BufferedReader in = new BufferedReader(new StringReader(printer
141: .toString()));
142: int i = 1;
143: assertEquals("line " + i++, getName(), in.readLine());
144: assertEquals("line " + i++, " -switch1 value", in.readLine());
145: assertEquals("line " + i++, null, in.readLine());
146: }
147:
148: public void testMultipleValuesSwitchWithMultipleValues()
149: throws CommandLineException, IOException {
150: commandLine.addMultipleValuesSwitch("switch1");
151: commandLine.parse(new String[] { "-switch1", "value1",
152: "-switch1", "value2" });
153: commandLine.accept(printer);
154:
155: BufferedReader in = new BufferedReader(new StringReader(printer
156: .toString()));
157: int i = 1;
158: assertEquals("line " + i++, getName(), in.readLine());
159: assertEquals("line " + i++, " -switch1 value1", in
160: .readLine());
161: assertEquals("line " + i++, " -switch1 value2", in
162: .readLine());
163: assertEquals("line " + i++, null, in.readLine());
164: }
165:
166: public void testUnknownSwitch() throws CommandLineException,
167: IOException {
168: commandLine.parse(new String[] { "-switch1" });
169: commandLine.accept(printer);
170:
171: BufferedReader in = new BufferedReader(new StringReader(printer
172: .toString()));
173: int i = 1;
174: assertEquals("line " + i++, getName(), in.readLine());
175: assertEquals("line " + i++, " -switch1", in.readLine());
176: assertEquals("line " + i++, null, in.readLine());
177: }
178:
179: public void testOneParameter() throws CommandLineException,
180: IOException {
181: commandLine.parse(new String[] { "param" });
182: commandLine.accept(printer);
183:
184: BufferedReader in = new BufferedReader(new StringReader(printer
185: .toString()));
186: int i = 1;
187: assertEquals("line " + i++, getName(), in.readLine());
188: assertEquals("line " + i++, " param", in.readLine());
189: assertEquals("line " + i++, null, in.readLine());
190: }
191:
192: public void testMultipleParameter() throws CommandLineException,
193: IOException {
194: commandLine.parse(new String[] { "param1", "param2" });
195: commandLine.accept(printer);
196:
197: BufferedReader in = new BufferedReader(new StringReader(printer
198: .toString()));
199: int i = 1;
200: assertEquals("line " + i++, getName(), in.readLine());
201: assertEquals("line " + i++, " param1", in.readLine());
202: assertEquals("line " + i++, " param2", in.readLine());
203: assertEquals("line " + i++, null, in.readLine());
204: }
205:
206: public void testSingleValueSwitchAndParameter()
207: throws CommandLineException, IOException {
208: commandLine.parse(new String[] { "-switch", "value", "param" });
209: commandLine.accept(printer);
210:
211: BufferedReader in = new BufferedReader(new StringReader(printer
212: .toString()));
213: int i = 1;
214: assertEquals("line " + i++, getName(), in.readLine());
215: assertEquals("line " + i++, " -switch value", in.readLine());
216: assertEquals("line " + i++, " param", in.readLine());
217: assertEquals("line " + i++, null, in.readLine());
218: }
219: }
|