KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > cli > CommandLineTestCase


1 /*
2  * $Id: CommandLineTestCase.java,v 1.1 2003/06/10 17:23:38 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.cli;
59
60 import java.io.ByteArrayOutputStream JavaDoc;
61 import java.io.File JavaDoc;
62 import java.io.PrintStream JavaDoc;
63
64 import org.apache.avalon.framework.ExceptionUtil;
65 import org.krysalis.barcode.AbstractBarcodeTestCase;
66
67 /**
68  * Tests the command line application
69  * @author Jeremias Maerki
70  */

71 public class CommandLineTestCase extends AbstractBarcodeTestCase {
72
73     private ByteArrayOutputStream JavaDoc out;
74     private ByteArrayOutputStream JavaDoc err;
75     private ExitHandlerForTests exitHandler;
76
77     /**
78      * @see junit.framework.TestCase#Constructor(String)
79      */

80     public CommandLineTestCase(String JavaDoc name) {
81         super(name);
82     }
83
84     private void dumpResults() throws Exception JavaDoc {
85         System.out.println("Msg: " + this.exitHandler.getLastMsg());
86         System.out.println("Exit code: " + this.exitHandler.getLastExitCode());
87         if (this.exitHandler.getLastThrowable() != null) {
88             System.out.println(ExceptionUtil.printStackTrace(
89                 this.exitHandler.getLastThrowable()));
90         }
91         System.out.println("--- stdout (" + this.out.size() + ") ---");
92         System.out.println(new String JavaDoc(this.out.toByteArray(), "US-ASCII"));
93         System.out.println("--- stderr (" + this.err.size() + ") ---");
94         System.out.println(new String JavaDoc(this.err.toByteArray(), "US-ASCII"));
95         System.out.println("---");
96     }
97
98     private void callCLI(String JavaDoc[] args) {
99         Main app = new Main();
100         try {
101             app.handleCommandLine(args);
102         } catch (SimulateVMExitError se) {
103             //ignore
104
}
105     }
106
107     /** {@inheritDoc} */
108     protected void setUp() throws Exception JavaDoc {
109         this.out = new ByteArrayOutputStream JavaDoc();
110         this.err = new ByteArrayOutputStream JavaDoc();
111         Main.stdout = new PrintStream JavaDoc(this.out);
112         Main.stderr = new PrintStream JavaDoc(this.err);
113         this.exitHandler = new ExitHandlerForTests();
114         Main.setExitHandler(this.exitHandler);
115     }
116     
117     public void testSVG() throws Exception JavaDoc {
118         final String JavaDoc[] args = {"-s", "ean13", "9771422985503+00006"};
119         callCLI(args);
120         assertEquals("Exit code must be 0", 0, this.exitHandler.getLastExitCode());
121         assertNull(this.exitHandler.getLastMsg());
122         assertNull(this.exitHandler.getLastThrowable());
123         assertTrue("No output", this.out.size() > 0);
124         assertTrue("No output on stderr expected", this.err.size() == 0);
125     }
126
127     public void testEPS() throws Exception JavaDoc {
128         final String JavaDoc[] args = {"-s", "ean13", "-f", "eps", "9771422985503+00006"};
129         callCLI(args);
130         assertEquals("Exit code must be 0", 0, this.exitHandler.getLastExitCode());
131         assertNull(this.exitHandler.getLastMsg());
132         assertNull(this.exitHandler.getLastThrowable());
133         assertTrue("No output", this.out.size() > 0);
134         assertTrue("No output on stderr expected", this.err.size() == 0);
135     }
136
137     public void testBitmapJPEG() throws Exception JavaDoc {
138         final String JavaDoc[] args = {"-s", "ean13", "-f", "image/jpeg", "9771422985503+00006"};
139         callCLI(args);
140         assertEquals("Exit code must be 0", 0, this.exitHandler.getLastExitCode());
141         assertNull(this.exitHandler.getLastMsg());
142         assertNull(this.exitHandler.getLastThrowable());
143         assertTrue("No output", this.out.size() > 0);
144         assertTrue("No output on stderr expected", this.err.size() == 0);
145     }
146
147     public void testNoArgs() throws Exception JavaDoc {
148         final String JavaDoc[] args = new String JavaDoc[0];
149         callCLI(args);
150         assertEquals("Exit code must be -2", -2, this.exitHandler.getLastExitCode());
151         assertNotNull(this.exitHandler.getLastMsg());
152         assertNull(this.exitHandler.getLastThrowable());
153         assertTrue("CLI help expected on stdout", this.out.size() > 0);
154         assertTrue("Error message expected on stderr", this.err.size() > 0);
155     }
156
157     public void testUnknownArg() throws Exception JavaDoc {
158         final String JavaDoc[] args = {"--badArgument"};
159         callCLI(args);
160         assertEquals("Exit code must be -2", -2, this.exitHandler.getLastExitCode());
161         assertNotNull(this.exitHandler.getLastMsg());
162         assertNull(this.exitHandler.getLastThrowable());
163         assertTrue("CLI help expected on stdout", this.out.size() > 0);
164         assertTrue("Error message expected on stderr", this.err.size() > 0);
165     }
166     
167     public void testWrongConfigFile() throws Exception JavaDoc {
168         final String JavaDoc[] args = {"-c", "NonExistingConfigFile", "9771422985503+00006"};
169         callCLI(args);
170         assertEquals("Exit code must be -3", -3, this.exitHandler.getLastExitCode());
171         assertNotNull(this.exitHandler.getLastMsg());
172         assertNull(this.exitHandler.getLastThrowable());
173         assertTrue("In case of error stdout may only be written to if there's "
174             + "a problem with the command-line", this.out.size() == 0);
175         assertTrue("Error message expected on stderr", this.err.size() > 0);
176     }
177
178     public void testValidConfigFile() throws Exception JavaDoc {
179         File JavaDoc cfgFile = new File JavaDoc(getBaseDir(), "src/test/xml/good-cfg.xml");
180         final String JavaDoc[] args = {"-c", cfgFile.getAbsolutePath(),
181             "9771422985503+00006"};
182         callCLI(args);
183         assertEquals("Exit code must be 0", 0, this.exitHandler.getLastExitCode());
184     }
185
186     public void testBadConfigFile() throws Exception JavaDoc {
187         File JavaDoc cfgFile = new File JavaDoc(getBaseDir(), "src/test/xml/bad-cfg.xml");
188         final String JavaDoc[] args = {"-c", cfgFile.getAbsolutePath(),
189             "9771422985503+00006"};
190         callCLI(args);
191         assertEquals("Exit code must be -3", -3, this.exitHandler.getLastExitCode());
192         assertNotNull(this.exitHandler.getLastMsg());
193         assertNotNull(this.exitHandler.getLastThrowable());
194         assertTrue("In case of error stdout may only be written to if there's "
195             + "a problem with the command-line", this.out.size() == 0);
196         assertTrue("Error message expected on stderr", this.err.size() > 0);
197     }
198
199     public void testToFile() throws Exception JavaDoc {
200         File JavaDoc out = File.createTempFile("krba", ".tmp");
201         final String JavaDoc[] args = {"-s", "ean-13", "-o", out.getAbsolutePath(),
202                  "9771422985503+00006"};
203         callCLI(args);
204         assertEquals("Exit code must be 0", 0, this.exitHandler.getLastExitCode());
205         assertNull(this.exitHandler.getLastMsg());
206         assertNull(this.exitHandler.getLastThrowable());
207         assertTrue("Application header expected on stdout",
208             this.out.size() > 0);
209         assertTrue("No output expected on stderr", this.err.size() == 0);
210         assertTrue("Target file does not exist", out.exists());
211         assertTrue("Target file must not be empty", out.length() > 0);
212         if (!out.delete()) {
213             fail("Target file could not be deleted. Not closed?");
214         }
215     }
216
217     public void testDPI() throws Exception JavaDoc {
218         File JavaDoc out100 = File.createTempFile("krba", ".tmp");
219         final String JavaDoc[] args100 = {"-s", "ean-13",
220                  "-o", out100.getAbsolutePath(),
221                  "-f", "jpeg",
222                  "-d", "100", "9771422985503+00006"};
223         callCLI(args100);
224         assertEquals("Exit code must be 0", 0, this.exitHandler.getLastExitCode());
225         assertTrue("Target file does not exist", out100.exists());
226
227         File JavaDoc out300 = File.createTempFile("krba", ".tmp");
228         final String JavaDoc[] args300 = {"-s", "ean-13",
229                  "-o", out300.getAbsolutePath(),
230                  "-f", "jpeg",
231                  "--dpi", "300", "9771422985503+00006"};
232         callCLI(args300);
233         assertEquals("Exit code must be 0", 0, this.exitHandler.getLastExitCode());
234         assertTrue("Target file does not exist", out300.exists());
235
236         assertTrue("300dpi file must be greater than the 100dpi file",
237             out300.length() > out100.length());
238         if (!out100.delete()) {
239             fail("Target file could not be deleted. Not closed?");
240         }
241         if (!out300.delete()) {
242             fail("Target file could not be deleted. Not closed?");
243         }
244     }
245
246 }
247
Popular Tags