1 58 package org.krysalis.barcode.cli; 59 60 import java.io.ByteArrayOutputStream ; 61 import java.io.File ; 62 import java.io.PrintStream ; 63 64 import org.apache.avalon.framework.ExceptionUtil; 65 import org.krysalis.barcode.AbstractBarcodeTestCase; 66 67 71 public class CommandLineTestCase extends AbstractBarcodeTestCase { 72 73 private ByteArrayOutputStream out; 74 private ByteArrayOutputStream err; 75 private ExitHandlerForTests exitHandler; 76 77 80 public CommandLineTestCase(String name) { 81 super(name); 82 } 83 84 private void dumpResults() throws Exception { 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 (this.out.toByteArray(), "US-ASCII")); 93 System.out.println("--- stderr (" + this.err.size() + ") ---"); 94 System.out.println(new String (this.err.toByteArray(), "US-ASCII")); 95 System.out.println("---"); 96 } 97 98 private void callCLI(String [] args) { 99 Main app = new Main(); 100 try { 101 app.handleCommandLine(args); 102 } catch (SimulateVMExitError se) { 103 } 105 } 106 107 108 protected void setUp() throws Exception { 109 this.out = new ByteArrayOutputStream (); 110 this.err = new ByteArrayOutputStream (); 111 Main.stdout = new PrintStream (this.out); 112 Main.stderr = new PrintStream (this.err); 113 this.exitHandler = new ExitHandlerForTests(); 114 Main.setExitHandler(this.exitHandler); 115 } 116 117 public void testSVG() throws Exception { 118 final String [] 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 { 128 final String [] 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 { 138 final String [] 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 { 148 final String [] args = new String [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 { 158 final String [] 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 { 168 final String [] 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 { 179 File cfgFile = new File (getBaseDir(), "src/test/xml/good-cfg.xml"); 180 final String [] 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 { 187 File cfgFile = new File (getBaseDir(), "src/test/xml/bad-cfg.xml"); 188 final String [] 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 { 200 File out = File.createTempFile("krba", ".tmp"); 201 final String [] 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 { 218 File out100 = File.createTempFile("krba", ".tmp"); 219 final String [] 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 out300 = File.createTempFile("krba", ".tmp"); 228 final String [] 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 |