1 17 18 package org.apache.tools.ant.taskdefs; 19 20 import junit.framework.*; 21 import java.io.*; 22 import org.apache.tools.ant.*; 23 import org.apache.tools.ant.util.FileUtils; 24 import org.apache.tools.ant.util.TeeOutputStream; 25 26 29 public class JavaTest extends BuildFileTest { 30 31 private static final int TIME_TO_WAIT = 1; 32 private static final int SECURITY_MARGIN = 2000; 35 36 private boolean runFatalTests=false; 37 38 public JavaTest(String name) { 39 super(name); 40 } 41 42 47 public void setUp() { 48 configureProject("src/etc/testcases/taskdefs/java.xml"); 49 50 String propname="tests-classpath.value"; 54 String runFatal=System.getProperty("junit.run.fatal.tests"); 55 if(runFatal!=null) 56 runFatalTests=true; 57 } 58 59 public void tearDown() { 60 project.executeTarget("cleanup"); 62 } 63 64 public void testNoJarNoClassname(){ 65 expectBuildExceptionContaining("testNoJarNoClassname", 66 "parameter validation", 67 "Classname must not be null."); 68 } 69 70 public void testJarNoFork() { 71 expectBuildExceptionContaining("testJarNoFork", 72 "parameter validation", 73 "Cannot execute a jar in non-forked mode. " 74 + "Please set fork='true'. "); 75 } 76 77 public void testJarAndClassName() { 78 expectBuildException("testJarAndClassName", 79 "Should not be able to set both classname AND jar"); 80 } 81 82 83 public void testClassnameAndJar() { 84 expectBuildException("testClassnameAndJar", 85 "Should not be able to set both classname AND jar"); 86 } 87 88 public void testRun() { 89 executeTarget("testRun"); 90 } 91 92 93 94 97 public void testRunFail() { 98 if(runFatalTests) { 99 executeTarget("testRunFail"); 100 } 101 } 102 103 public void testRunFailFoe() { 104 if(runFatalTests) { 105 expectBuildExceptionContaining("testRunFailFoe", 106 "java failures being propagated", 107 "Java returned:"); 108 } 109 } 110 111 public void testRunFailFoeFork() { 112 expectBuildExceptionContaining("testRunFailFoeFork", 113 "java failures being propagated", 114 "Java returned:"); 115 } 116 117 public void testExcepting() { 118 expectLogContaining("testExcepting", 119 "Exception raised inside called program"); 120 } 121 122 public void testExceptingFork() { 123 expectLogContaining("testExceptingFork", 124 "Java Result:"); 125 } 126 127 public void testExceptingFoe() { 128 expectBuildExceptionContaining("testExceptingFoe", 129 "passes exception through", 130 "Exception raised inside called program"); 131 } 132 133 public void testExceptingFoeFork() { 134 expectBuildExceptionContaining("testExceptingFoeFork", 135 "exceptions turned into error codes", 136 "Java returned:"); 137 } 138 139 public void testResultPropertyZero() { 140 executeTarget("testResultPropertyZero"); 141 assertEquals("0",project.getProperty("exitcode")); 142 } 143 144 public void testResultPropertyNonZero() { 145 executeTarget("testResultPropertyNonZero"); 146 assertEquals("2",project.getProperty("exitcode")); 147 } 148 149 public void testResultPropertyZeroNoFork() { 150 executeTarget("testResultPropertyZeroNoFork"); 151 assertEquals("0",project.getProperty("exitcode")); 152 } 153 154 public void testResultPropertyNonZeroNoFork() { 155 executeTarget("testResultPropertyNonZeroNoFork"); 156 assertEquals("-1",project.getProperty("exitcode")); 157 } 158 159 public void testRunFailWithFailOnError() { 160 expectBuildExceptionContaining("testRunFailWithFailOnError", 161 "non zero return code", 162 "Java returned:"); 163 } 164 165 public void testRunSuccessWithFailOnError() { 166 executeTarget("testRunSuccessWithFailOnError"); 167 } 168 169 public void testSpawn() { 170 FileUtils fileutils = FileUtils.newFileUtils(); 171 File logFile = fileutils.createTempFile("spawn","log", project.getBaseDir()); 172 assertTrue("log file not existing", !logFile.exists()); 174 project.setProperty("logFile", logFile.getAbsolutePath()); 175 project.setProperty("timeToWait", Long.toString(TIME_TO_WAIT)); 176 project.executeTarget("testSpawn"); 177 try { 178 Thread.sleep(TIME_TO_WAIT * 1000 + SECURITY_MARGIN); 179 } catch (Exception ex) { 180 System.out.println("my sleep was interrupted"); 181 } 182 if (!logFile.exists()) { 184 System.out.println("suggestion: increase the constant" 185 + " SECURITY_MARGIN to give more time for java to start."); 186 } 187 assertTrue("log file exists", logFile.exists()); 188 } 189 190 public void testRedirect1() { 191 executeTarget("redirect1"); 192 } 193 194 public void testRedirect2() { 195 executeTarget("redirect2"); 196 } 197 198 public void testRedirect3() { 199 executeTarget("redirect3"); 200 } 201 202 public void testRedirector1() { 203 executeTarget("redirector1"); 204 } 205 206 public void testRedirector2() { 207 executeTarget("redirector2"); 208 } 209 210 214 public static class EntryPoint { 215 216 223 public static void main(String [] argv) { 224 int exitCode=0; 225 if(argv.length>0) { 226 try { 227 exitCode=Integer.parseInt(argv[0]); 228 } catch(NumberFormatException nfe) { 229 exitCode=-1; 230 } 231 } 232 if(argv.length>1) { 233 System.out.println(argv[1]); 234 } 235 if(argv.length>2) { 236 System.err.println(argv[2]); 237 } 238 if(exitCode!=0) { 239 System.exit(exitCode); 240 } 241 } 242 } 243 244 248 public static class ExceptingEntryPoint { 249 250 254 public static void main(String [] argv) { 255 throw new NullPointerException ("Exception raised inside called program"); 256 } 257 } 258 261 public static class SpawnEntryPoint { 262 public static void main(String [] argv) { 263 int sleepTime = 10; 264 String logFile = "spawn.log"; 265 if (argv.length >= 1) { 266 sleepTime = Integer.parseInt(argv[0]); 267 } 268 if (argv.length >= 2) 269 { 270 logFile = argv[1]; 271 } 272 OutputStreamWriter out = null; 273 try { 274 Thread.sleep(sleepTime * 1000); 275 } catch (InterruptedException ex) { 276 System.out.println("my sleep was interrupted"); 277 } 278 279 try { 280 File dest = new File(logFile); 281 FileOutputStream fos = new FileOutputStream(dest); 282 out = new OutputStreamWriter(fos); 283 out.write("bye bye\n"); 284 } catch (Exception ex) {} 285 finally { 286 try {out.close();} catch (IOException ioe) {}} 287 288 } 289 } 290 291 295 public static class PipeEntryPoint { 296 297 300 public static void main(String [] args) { 301 OutputStream os = null; 302 if (args.length > 0) { 303 if ("out".equalsIgnoreCase(args[0])) { 304 os = System.out; 305 } else if ("err".equalsIgnoreCase(args[0])) { 306 os = System.err; 307 } else if ("both".equalsIgnoreCase(args[0])) { 308 os = new TeeOutputStream(System.out, System.err); 309 } 310 } 311 if (os != null) { 312 Thread t = new Thread (new StreamPumper(System.in, os, true)); 313 t.start(); 314 try { 315 t.join(); 316 } catch (InterruptedException eyeEx) { 317 } 318 } 319 } 320 } 321 } 322 | Popular Tags |