1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.exec;
20
21 import junit.framework.TestCase;
22 import org.apache.commons.exec.environment.EnvironmentUtils;
23
24 import java.io.BufferedReader;
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import java.io.PipedInputStream;
33 import java.io.PipedOutputStream;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 public class DefaultExecutorTest extends TestCase {
38
39
40 private static final int WAITFOR_TIMEOUT = 15000;
41
42 private Executor exec = new DefaultExecutor();
43 private File testDir = new File("src/test/scripts");
44 private File foreverOutputFile = new File("./target/forever.txt");
45 private ByteArrayOutputStream baos;
46
47 private File testScript = TestUtil.resolveScriptForOS(testDir + "/test");
48 private File errorTestScript = TestUtil.resolveScriptForOS(testDir + "/error");
49 private File foreverTestScript = TestUtil.resolveScriptForOS(testDir + "/forever");
50 private File nonExistingTestScript = TestUtil.resolveScriptForOS(testDir + "/grmpffffff");
51 private File redirectScript = TestUtil.resolveScriptForOS(testDir + "/redirect");
52 private File pingScript = TestUtil.resolveScriptForOS(testDir + "/ping");
53 private File printArgsScript = TestUtil.resolveScriptForOS(testDir + "/printargs");
54 private File acroRd32Script = TestUtil.resolveScriptForOS(testDir + "/acrord32");
55 private File stdinSript = TestUtil.resolveScriptForOS(testDir + "/stdin");
56 private File environmentSript = TestUtil.resolveScriptForOS(testDir + "/environment");
57 private File wrapperScript = TestUtil.resolveScriptForOS(testDir + "/wrapper");
58
59
60
61 private static final int SUCCESS_STATUS;
62 private static final int ERROR_STATUS;
63
64 static{
65
66 int statuses[] = TestUtil.getTestScriptCodesForOS();
67 SUCCESS_STATUS=statuses[0];
68 ERROR_STATUS=statuses[1];
69
70
71 System.setProperty("org.apache.commons.exec.lenient", "false");
72 System.setProperty("org.apache.commons.exec.debug", "true");
73 }
74
75 protected void setUp() throws Exception {
76
77
78 this.foreverOutputFile.getParentFile().mkdirs();
79 if(this.foreverOutputFile.exists()) {
80 this.foreverOutputFile.delete();
81 }
82
83
84 this.baos = new ByteArrayOutputStream();
85 this.exec.setStreamHandler(new PumpStreamHandler(baos, baos));
86 }
87
88 protected void tearDown() throws Exception {
89 this.baos.close();
90 foreverOutputFile.delete();
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public void testExecute() throws Exception {
105 CommandLine cl = new CommandLine(testScript);
106 int exitValue = exec.execute(cl);
107 assertEquals("FOO..", baos.toString().trim());
108 assertFalse(exec.isFailure(exitValue));
109 assertEquals(new File("."), exec.getWorkingDirectory());
110 }
111
112 public void testExecuteWithWorkingDirectory() throws Exception {
113 File workingDir = new File("./target");
114 CommandLine cl = new CommandLine(testScript);
115 exec.setWorkingDirectory(workingDir);
116 int exitValue = exec.execute(cl);
117 assertEquals("FOO..", baos.toString().trim());
118 assertFalse(exec.isFailure(exitValue));
119 assertEquals(exec.getWorkingDirectory(), workingDir);
120 }
121
122 public void testExecuteWithInvalidWorkingDirectory() throws Exception {
123 File workingDir = new File("/foo/bar");
124 CommandLine cl = new CommandLine(testScript);
125 exec.setWorkingDirectory(workingDir);
126 try {
127 exec.execute(cl);
128 fail("Expected exception due to invalid working directory");
129 }
130 catch(IOException e) {
131 return;
132 }
133 }
134
135 public void testExecuteWithError() throws Exception {
136 CommandLine cl = new CommandLine(errorTestScript);
137
138 try{
139 exec.execute(cl);
140 fail("Must throw ExecuteException");
141 } catch(ExecuteException e) {
142 assertTrue(exec.isFailure(e.getExitValue()));
143 }
144 }
145
146 public void testExecuteWithArg() throws Exception {
147 CommandLine cl = new CommandLine(testScript);
148 cl.addArgument("BAR");
149 int exitValue = exec.execute(cl);
150
151 assertEquals("FOO..BAR", baos.toString().trim());
152 assertFalse(exec.isFailure(exitValue));
153 }
154
155
156
157
158
159 public void testExecuteWithSingleEnvironmentVariable() throws Exception {
160 Map env = new HashMap();
161 env.put("TEST_ENV_VAR", "XYZ");
162
163 CommandLine cl = new CommandLine(testScript);
164
165 int exitValue = exec.execute(cl, env);
166
167 assertEquals("FOO.XYZ.", baos.toString().trim());
168 assertFalse(exec.isFailure(exitValue));
169 }
170
171
172
173
174
175
176
177 public void testExecuteAsync() throws Exception {
178 CommandLine cl = new CommandLine(testScript);
179 DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
180 exec.execute(cl, resultHandler);
181 resultHandler.waitFor(2000);
182 assertTrue(resultHandler.hasResult());
183 assertNull(resultHandler.getException());
184 assertFalse(exec.isFailure(resultHandler.getExitValue()));
185 assertEquals("FOO..", baos.toString().trim());
186 }
187
188
189
190
191
192
193
194 public void testExecuteAsyncWithError() throws Exception {
195 CommandLine cl = new CommandLine(errorTestScript);
196 DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
197 exec.execute(cl, resultHandler);
198 resultHandler.waitFor(2000);
199 assertTrue(resultHandler.hasResult());
200 assertTrue(exec.isFailure(resultHandler.getExitValue()));
201 assertNotNull(resultHandler.getException());
202 assertEquals("FOO..", baos.toString().trim());
203 }
204
205
206
207
208
209
210
211 public void testExecuteAsyncWithTimelyUserTermination() throws Exception {
212 CommandLine cl = new CommandLine(foreverTestScript);
213 ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
214 exec.setWatchdog(watchdog);
215 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
216 exec.execute(cl, handler);
217
218 Thread.sleep(2000);
219 assertTrue("Watchdog should watch the process", watchdog.isWatching());
220
221 watchdog.destroyProcess();
222
223 handler.waitFor(WAITFOR_TIMEOUT);
224 assertTrue("Watchdog should have killed the process", watchdog.killedProcess());
225 assertFalse("Watchdog is no longer watching the process", watchdog.isWatching());
226 assertTrue("ResultHandler received a result", handler.hasResult());
227 assertNotNull("ResultHandler received an exception as result", handler.getException());
228 }
229
230
231
232
233
234
235
236
237
238 public void testExecuteAsyncWithTooLateUserTermination() throws Exception {
239 CommandLine cl = new CommandLine(foreverTestScript);
240 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
241 ExecuteWatchdog watchdog = new ExecuteWatchdog(3000);
242 exec.setWatchdog(watchdog);
243 exec.execute(cl, handler);
244
245 Thread.sleep(6000);
246
247 watchdog.destroyProcess();
248
249 handler.waitFor(WAITFOR_TIMEOUT);
250 assertTrue("Watchdog should have killed the process already", watchdog.killedProcess());
251 assertFalse("Watchdog is no longer watching the process", watchdog.isWatching());
252 assertTrue("ResultHandler received a result", handler.hasResult());
253 assertNotNull("ResultHandler received an exception as result", handler.getException());
254 }
255
256
257
258
259
260
261
262
263
264
265 public void testExecuteWatchdogSync() throws Exception {
266
267 if (OS.isFamilyOpenVms()){
268 System.out.println("The test 'testExecuteWatchdogSync' currently hangs on the following OS : "
269 + System.getProperty("os.name"));
270 return;
271 }
272
273 long timeout = 10000;
274
275 CommandLine cl = new CommandLine(foreverTestScript);
276 DefaultExecutor executor = new DefaultExecutor();
277 executor.setWorkingDirectory(new File("."));
278 ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
279 executor.setWatchdog(watchdog);
280
281 try {
282 executor.execute(cl);
283 }
284 catch(ExecuteException e) {
285 Thread.sleep(timeout);
286 int nrOfInvocations = getOccurrences(readFile(this.foreverOutputFile), '.');
287 assertTrue( executor.getWatchdog().killedProcess() );
288 assertTrue("killing the subprocess did not work : " + nrOfInvocations, nrOfInvocations > 5 && nrOfInvocations <= 11);
289 return;
290 }
291 catch(Throwable t) {
292 fail(t.getMessage());
293 }
294
295 assertTrue("Killed process should be true", executor.getWatchdog().killedProcess() );
296 fail("Process did not create ExecuteException when killed");
297 }
298
299
300
301
302
303
304
305
306
307
308 public void testExecuteWatchdogAsync() throws Exception {
309
310 long timeout = 10000;
311
312 CommandLine cl = new CommandLine(foreverTestScript);
313 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
314 DefaultExecutor executor = new DefaultExecutor();
315 executor.setWorkingDirectory(new File("."));
316 executor.setWatchdog(new ExecuteWatchdog(timeout));
317
318 executor.execute(cl, handler);
319 handler.waitFor(WAITFOR_TIMEOUT);
320
321 assertTrue("Killed process should be true", executor.getWatchdog().killedProcess() );
322 assertTrue("ResultHandler received a result", handler.hasResult());
323 assertNotNull("ResultHandler received an exception as result", handler.getException());
324
325 int nrOfInvocations = getOccurrences(readFile(this.foreverOutputFile), '.');
326 assertTrue("Killing the process did not work : " + nrOfInvocations, nrOfInvocations > 5 && nrOfInvocations <= 11);
327 }
328
329
330
331
332
333
334
335
336 public void testExecuteWatchdogVeryLongTimeout() throws Exception {
337 long timeout = Long.MAX_VALUE;
338
339 CommandLine cl = new CommandLine(testScript);
340 DefaultExecutor executor = new DefaultExecutor();
341 executor.setWorkingDirectory(new File("."));
342 ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
343 executor.setWatchdog(watchdog);
344
345 try {
346 executor.execute(cl);
347 } catch (ExecuteException e) {
348 assertFalse("Process should exit normally, not be killed by watchdog", watchdog.killedProcess());
349
350 throw e;
351 }
352 }
353
354
355
356
357
358
359
360 public void testExecuteNonExistingApplication() throws Exception {
361 CommandLine cl = new CommandLine(nonExistingTestScript);
362 DefaultExecutor executor = new DefaultExecutor();
363 try {
364 executor.execute(cl);
365 }
366 catch( IOException e) {
367
368 return;
369 }
370 fail("Got no exception when executing an non-existing application");
371 }
372
373
374
375
376
377
378
379 public void testExecuteAsyncWithNonExistingApplication() throws Exception {
380 CommandLine cl = new CommandLine(nonExistingTestScript);
381 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
382 exec.execute(cl, handler);
383 Thread.sleep(2000);
384 assertNotNull(handler.getException());
385 assertTrue(exec.isFailure(handler.getExitValue()));
386 }
387
388
389
390
391
392
393
394 public void testExecuteWithCustomExitValue1() throws Exception {
395 exec.setExitValue(ERROR_STATUS);
396 CommandLine cl = new CommandLine(errorTestScript);
397 exec.execute(cl);
398 }
399
400
401
402
403
404
405
406 public void testExecuteWithCustomExitValue2() throws Exception {
407 CommandLine cl = new CommandLine(errorTestScript);
408 exec.setExitValue(SUCCESS_STATUS);
409 try{
410 exec.execute(cl);
411 fail("Must throw ExecuteException");
412 } catch(ExecuteException e) {
413 assertTrue(exec.isFailure(e.getExitValue()));
414 return;
415 }
416 }
417
418
419
420
421
422
423 public void testExecuteWithProcessDestroyer() throws Exception {
424
425 CommandLine cl = new CommandLine(testScript);
426 ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
427 exec.setProcessDestroyer(processDestroyer);
428
429 assertTrue(processDestroyer.size() == 0);
430 assertTrue(processDestroyer.isAddedAsShutdownHook() == false);
431
432 int exitValue = exec.execute(cl);
433
434 assertEquals("FOO..", baos.toString().trim());
435 assertFalse(exec.isFailure(exitValue));
436 assertTrue(processDestroyer.size() == 0);
437 assertTrue(processDestroyer.isAddedAsShutdownHook() == false);
438 }
439
440
441
442
443
444
445
446
447 public void testExecuteAsyncWithProcessDestroyer() throws Exception {
448
449 CommandLine cl = new CommandLine(foreverTestScript);
450 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
451 ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer();
452 ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
453
454 assertTrue(exec.getProcessDestroyer() == null);
455 assertTrue(processDestroyer.size() == 0);
456 assertTrue(processDestroyer.isAddedAsShutdownHook() == false);
457
458 exec.setWatchdog(watchdog);
459 exec.setProcessDestroyer(processDestroyer);
460 exec.execute(cl, handler);
461
462
463 Thread.sleep(2000);
464
465
466 assertNotNull("Process destroyer should exist", exec.getProcessDestroyer());
467 assertEquals("Process destroyer size should be 1", 1, processDestroyer.size());
468 assertTrue("Process destroyer should exist as shutdown hook", processDestroyer.isAddedAsShutdownHook());
469
470
471 watchdog.destroyProcess();
472 assertTrue(watchdog.killedProcess());
473 handler.waitFor(WAITFOR_TIMEOUT);
474 assertTrue("ResultHandler received a result", handler.hasResult());
475 assertNotNull(handler.getException());
476 assertEquals("Processor Destroyer size should be 0", 0, processDestroyer.size());
477 assertFalse("Process destroyer should not exist as shutdown hook", processDestroyer.isAddedAsShutdownHook());
478 }
479
480
481
482
483
484
485 public void testExecuteWithFancyArg() throws Exception {
486 CommandLine cl = new CommandLine(testScript);
487 cl.addArgument("test $;`(0)[1]{2}");
488 int exitValue = exec.execute(cl);
489 assertTrue(baos.toString().trim().indexOf("test $;`(0)[1]{2}") > 0);
490 assertFalse(exec.isFailure(exitValue));
491 }
492
493
494
495
496
497
498
499
500
501
502 public void testExecuteWithRedirectedStreams() throws Exception
503 {
504 if(OS.isFamilyUnix())
505 {
506 FileInputStream fis = new FileInputStream("./NOTICE.txt");
507 CommandLine cl = new CommandLine(redirectScript);
508 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( baos, baos, fis );
509 DefaultExecutor executor = new DefaultExecutor();
510 executor.setWorkingDirectory(new File("."));
511 executor.setStreamHandler( pumpStreamHandler );
512 int exitValue = executor.execute(cl);
513 fis.close();
514 String result = baos.toString().trim();
515 assertTrue(result, result.indexOf("Finished reading from stdin") > 0);
516 assertFalse("exitValue=" + exitValue, exec.isFailure(exitValue));
517 }
518 else if(OS.isFamilyWindows()) {
519 System.err.println("The code samples to do that in windows look like a joke ... :-( .., no way I'm doing that");
520 System.err.println("The test 'testExecuteWithRedirectedStreams' does not support the following OS : " + System.getProperty("os.name"));
521 return;
522 }
523 else {
524 System.err.println("The test 'testExecuteWithRedirectedStreams' does not support the following OS : " + System.getProperty("os.name"));
525 return;
526 }
527 }
528
529
530
531
532
533
534 public void testExecuteWithStdOutErr() throws Exception
535 {
536 CommandLine cl = new CommandLine(testScript);
537 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( System.out, System.err );
538 DefaultExecutor executor = new DefaultExecutor();
539 executor.setStreamHandler( pumpStreamHandler );
540 int exitValue = executor.execute(cl);
541 assertFalse(exec.isFailure(exitValue));
542 }
543
544
545
546
547
548
549 public void testExecuteWithNullOutErr() throws Exception
550 {
551 CommandLine cl = new CommandLine(testScript);
552 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( null, null );
553 DefaultExecutor executor = new DefaultExecutor();
554 executor.setStreamHandler( pumpStreamHandler );
555 int exitValue = executor.execute(cl);
556 assertFalse(exec.isFailure(exitValue));
557 }
558
559
560
561
562
563
564 public void testExecuteWithRedirectOutErr() throws Exception
565 {
566 File outfile = File.createTempFile("EXEC", ".test");
567 outfile.deleteOnExit();
568 CommandLine cl = new CommandLine(testScript);
569 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( new FileOutputStream(outfile) );
570 DefaultExecutor executor = new DefaultExecutor();
571 executor.setStreamHandler( pumpStreamHandler );
572 int exitValue = executor.execute(cl);
573 assertFalse(exec.isFailure(exitValue));
574 assertTrue(outfile.exists());
575 }
576
577
578
579
580
581
582
583 public void testExecuteWithComplexArguments() throws Exception {
584 CommandLine cl = new CommandLine(printArgsScript);
585 cl.addArgument("gdal_translate");
586 cl.addArgument("HDF5:\"/home/kk/grass/data/4404.he5\"://HDFEOS/GRIDS/OMI_Column_Amount_O3/Data_Fields/ColumnAmountO3/home/kk/4.tif", false);
587 DefaultExecutor executor = new DefaultExecutor();
588 int exitValue = executor.execute(cl);
589 assertFalse(exec.isFailure(exitValue));
590 }
591
592
593
594
595
596
597
598
599
600 public void testStdInHandling() throws Exception {
601
602 ByteArrayInputStream bais = new ByteArrayInputStream("Foo".getBytes());
603 CommandLine cl = new CommandLine(this.stdinSript);
604 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( this.baos, System.err, bais);
605 DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
606 Executor executor = new DefaultExecutor();
607 executor.setStreamHandler(pumpStreamHandler);
608 executor.execute(cl, resultHandler);
609
610 resultHandler.waitFor(WAITFOR_TIMEOUT);
611 assertTrue("ResultHandler received a result", resultHandler.hasResult());
612
613 assertFalse(exec.isFailure(resultHandler.getExitValue()));
614 String result = baos.toString();
615 assertTrue("Result '"+result+"' should contain 'Hello Foo!'", result.indexOf("Hello Foo!") >= 0);
616 }
617
618
619
620
621
622
623 public void testEnvironmentVariables() throws Exception {
624 exec.execute(new CommandLine(environmentSript));
625 String environment = baos.toString().trim();
626 assertTrue("Found no environment variables", environment.length() > 0);
627 assertFalse(environment.indexOf("NEW_VAR") >= 0);
628 }
629
630
631
632
633
634
635
636 public void testAddEnvironmentVariables() throws Exception {
637 Map myEnvVars = new HashMap();
638 myEnvVars.putAll(EnvironmentUtils.getProcEnvironment());
639 myEnvVars.put("NEW_VAR","NEW_VAL");
640 exec.execute(new CommandLine(environmentSript), myEnvVars);
641 String environment = baos.toString().trim();
642 assertTrue("Expecting NEW_VAR in "+environment,environment.indexOf("NEW_VAR") >= 0);
643 assertTrue("Expecting NEW_VAL in "+environment,environment.indexOf("NEW_VAL") >= 0);
644 }
645
646 public void testAddEnvironmentVariableEmbeddedQuote() throws Exception {
647 Map myEnvVars = new HashMap();
648 myEnvVars.putAll(EnvironmentUtils.getProcEnvironment());
649 String name = "NEW_VAR";
650 String value = "NEW_\"_VAL";
651 myEnvVars.put(name,value);
652 exec.execute(new CommandLine(environmentSript), myEnvVars);
653 String environment = baos.toString().trim();
654 assertTrue("Expecting "+name+" in "+environment,environment.indexOf(name) >= 0);
655 assertTrue("Expecting "+value+" in "+environment,environment.indexOf(value) >= 0);
656 }
657
658
659
660
661
662
663
664
665
666
667
668
669 public void testExec33() throws Exception
670 {
671 CommandLine cl = new CommandLine(testScript);
672 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( System.out, System.err, System.in );
673 DefaultExecutor executor = new DefaultExecutor();
674 executor.setStreamHandler( pumpStreamHandler );
675 int exitValue = executor.execute(cl);
676 assertFalse(exec.isFailure(exitValue));
677 }
678
679
680
681
682
683
684
685
686
687
688 public void testExec34_1() throws Exception {
689
690 CommandLine cmdLine = new CommandLine(pingScript);
691 cmdLine.addArgument("10");
692
693 ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
694 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
695 exec.setWatchdog(watchdog);
696 exec.execute(cmdLine, handler);
697 assertTrue(watchdog.isWatching());
698 watchdog.destroyProcess();
699 assertTrue("Watchdog should have killed the process",watchdog.killedProcess());
700 assertFalse("Watchdog is no longer watching the process",watchdog.isWatching());
701 }
702
703
704
705
706
707
708
709
710
711 public void testExec34_2() throws Exception {
712
713 CommandLine cmdLine = new CommandLine(pingScript);
714 cmdLine.addArgument("10");
715
716 ExecuteWatchdog watchdog = new ExecuteWatchdog(5000);
717 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
718 exec.setWatchdog(watchdog);
719 exec.execute(cmdLine, handler);
720 handler.waitFor();
721 assertTrue("Process has exited", handler.hasResult());
722 assertNotNull("Process was aborted", handler.getException());
723 assertTrue("Watchdog should have killed the process", watchdog.killedProcess());
724 assertFalse("Watchdog is no longer watching the process", watchdog.isWatching());
725 }
726
727
728
729
730
731
732
733
734 public void testExec36_1() throws Exception {
735
736 if(OS.isFamilyUnix()) {
737
738 CommandLine cmdl;
739
740
741
742
743
744
745 String expected = "./script/jrake\n" +
746 "cruise:publish_installers\n" +
747 "INSTALLER_VERSION=unstable_2_1\n" +
748 "INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\"\n" +
749 "INSTALLER_DOWNLOAD_SERVER='something'\n" +
750 "WITHOUT_HELP_DOC=true";
751
752 cmdl = new CommandLine(printArgsScript);
753 cmdl.addArgument("./script/jrake", false);
754 cmdl.addArgument("cruise:publish_installers", false);
755 cmdl.addArgument("INSTALLER_VERSION=unstable_2_1", false);
756 cmdl.addArgument("INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\"", false);
757 cmdl.addArgument("INSTALLER_DOWNLOAD_SERVER='something'", false);
758 cmdl.addArgument("WITHOUT_HELP_DOC=true", false);
759
760 int exitValue = exec.execute(cmdl);
761 String result = baos.toString().trim();
762 assertFalse(exec.isFailure(exitValue));
763 assertEquals(expected, result);
764 }
765 else {
766 System.err.println("The test 'testExec36_1' does not support the following OS : " + System.getProperty("os.name"));
767 return;
768 }
769 }
770
771
772
773
774
775
776
777
778
779
780
781 public void testExec36_2() throws Exception {
782
783 String expected;
784
785
786
787
788 if(OS.isFamilyWindows()) {
789 expected = "dotnetfx.exe\n" +
790 "/q:a\n" +
791 "/c:\"install.exe /l \"\"\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"";
792 }
793 else if(OS.isFamilyUnix()) {
794 expected = "dotnetfx.exe\n" +
795 "/q:a\n" +
796 "/c:\"install.exe /l \"\"/Documents and Settings/myusername/Local Settings/Temp/netfx.log\"\" /q\"";
797 }
798 else {
799 System.err.println("The test 'testExec36_3' does not support the following OS : " + System.getProperty("os.name"));
800 return;
801 }
802
803 CommandLine cmdl;
804 File file = new File("/Documents and Settings/myusername/Local Settings/Temp/netfx.log");
805 Map map = new HashMap();
806 map.put("FILE", file);
807
808 cmdl = new CommandLine(printArgsScript);
809 cmdl.setSubstitutionMap(map);
810 cmdl.addArgument("dotnetfx.exe", false);
811 cmdl.addArgument("/q:a", false);
812 cmdl.addArgument("/c:\"install.exe /l \"\"${FILE}\"\" /q\"", false);
813
814 int exitValue = exec.execute(cmdl);
815 String result = baos.toString().trim();
816 assertFalse(exec.isFailure(exitValue));
817
818 if(OS.isFamilyUnix()) {
819
820 assertEquals(expected, result);
821 }
822 }
823
824
825
826
827
828
829
830
831
832
833
834
835 public void testExec41WithStreams() throws Exception {
836
837 CommandLine cmdLine;
838
839 if(OS.isFamilyWindows()) {
840 cmdLine = CommandLine.parse("ping.exe -n 10 -w 1000 127.0.0.1");
841 }
842 else if("HP-UX".equals(System.getProperty("os.name"))) {
843 cmdLine = CommandLine.parse("ping 127.0.0.1 -n 10");
844 }
845 else if(OS.isFamilyUnix()) {
846 cmdLine = CommandLine.parse("ping -c 10 127.0.0.1");
847 }
848 else {
849 System.err.println("The test 'testExec41WithStreams' does not support the following OS : " + System.getProperty("os.name"));
850 return;
851 }
852
853 DefaultExecutor executor = new DefaultExecutor();
854 ExecuteWatchdog watchdog = new ExecuteWatchdog(2*1000);
855 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler( System.out, System.err);
856
857
858
859 executor.setWatchdog(watchdog);
860 executor.setStreamHandler(pumpStreamHandler);
861
862 long startTime = System.currentTimeMillis();
863
864 try {
865 executor.execute(cmdLine);
866 } catch (ExecuteException e) {
867
868 }
869
870 long duration = System.currentTimeMillis() - startTime;
871
872 System.out.println("Process completed in " + duration +" millis; below is its output");
873
874 if (watchdog.killedProcess()) {
875 System.out.println("Process timed out and was killed by watchdog.");
876 }
877
878 assertTrue("The process was killed by the watchdog", watchdog.killedProcess());
879 assertTrue("Skipping the Thread.join() did not work", duration < 9000);
880 }
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895 public void testExec41WithoutStreams() throws Exception {
896
897 CommandLine cmdLine = new CommandLine(pingScript);
898 cmdLine.addArgument("10");
899 DefaultExecutor executor = new DefaultExecutor();
900 ExecuteWatchdog watchdog = new ExecuteWatchdog(2*1000);
901
902
903 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(null, null, null);
904
905 executor.setWatchdog(watchdog);
906 executor.setStreamHandler(pumpStreamHandler);
907
908 long startTime = System.currentTimeMillis();
909
910 try {
911 executor.execute(cmdLine);
912 } catch (ExecuteException e) {
913 System.out.println(e);
914 }
915
916 long duration = System.currentTimeMillis() - startTime;
917
918 System.out.println("Process completed in " + duration +" millis; below is its output");
919
920 if (watchdog.killedProcess()) {
921 System.out.println("Process timed out and was killed.");
922 }
923
924 assertTrue("The process was killed by the watchdog", watchdog.killedProcess());
925 assertTrue("Skipping the Thread.join() did not work, duration="+duration, duration < 9000);
926 }
927
928
929
930
931
932
933
934
935
936
937
938 public void testExec44() throws Exception {
939
940 CommandLine cl = new CommandLine(foreverTestScript);
941 DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
942 ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
943
944 exec.setWatchdog(watchdog);
945 exec.execute(cl, resultHandler);
946
947
948 Thread.sleep(5000);
949 assertTrue("The watchdog is watching the process", watchdog.isWatching());
950
951
952 watchdog.destroyProcess();
953 assertTrue("The watchdog has killed the process", watchdog.killedProcess());
954 assertFalse("The watchdog is no longer watching any process", watchdog.isWatching());
955 }
956
957
958
959
960
961
962
963
964
965
966
967 public void testExec49_1() throws Exception {
968
969 if(OS.isFamilyUnix()) {
970
971 CommandLine cl = CommandLine.parse("/bin/ls");
972 cl.addArgument("/opt");
973
974 Executor exec = new DefaultExecutor();
975
976
977 PipedOutputStream pipedOutputStream = new PipedOutputStream();
978 PumpStreamHandler psh = new PumpStreamHandler(pipedOutputStream);
979 exec.setStreamHandler(psh);
980
981
982 System.out.println("Preparing to execute process - commandLine=" + cl.toString());
983 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
984 exec.execute(cl, handler);
985 System.out.println("Process spun off successfully - process=" + cl.getExecutable());
986
987 int x;
988 PipedInputStream pis = new PipedInputStream(pipedOutputStream);
989 while ((x = pis.read()) >= 0) {
990
991
992 }
993 pis.close();
994
995 handler.waitFor();
996 }
997 }
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009 public void testExec49_2() throws Exception {
1010
1011 if(OS.isFamilyUnix()) {
1012
1013 CommandLine cl = CommandLine.parse("/bin/ls");
1014 cl.addArgument("/opt");
1015
1016 Executor exec = new DefaultExecutor();
1017
1018
1019 PipedOutputStream pipedOutputStream = new PipedOutputStream();
1020 PumpStreamHandler psh = new PumpStreamHandler(pipedOutputStream, new ByteArrayOutputStream());
1021 exec.setStreamHandler(psh);
1022
1023
1024 System.out.println("Preparing to execute process - commandLine=" + cl.toString());
1025 DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
1026 exec.execute(cl, handler);
1027 System.out.println("Process spun off successfully - process=" + cl.getExecutable());
1028
1029 int x;
1030 PipedInputStream pis = new PipedInputStream(pipedOutputStream);
1031 while ((x = pis.read()) >= 0) {
1032
1033
1034 }
1035 pis.close();
1036
1037 handler.waitFor();
1038 }
1039 }
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052 public void testExec_57() throws IOException {
1053
1054 if(!OS.isFamilyUnix()) {
1055 System.err.println("The test 'testSyncInvocationOfBackgroundProcess' does not support the following OS : " + System.getProperty("os.name"));
1056 return;
1057 }
1058
1059 CommandLine cmdLine = new CommandLine("sh").addArgument("-c").addArgument(testDir + "/invoker.sh", false);
1060
1061 DefaultExecutor executor = new DefaultExecutor();
1062 PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(System.out, System.err);
1063
1064
1065
1066 pumpStreamHandler.setStopTimeout(2000);
1067 executor.setStreamHandler(pumpStreamHandler);
1068 long startTime = System.currentTimeMillis();
1069
1070 System.out.println("Executing " + cmdLine);
1071
1072 try {
1073 executor.execute(cmdLine);
1074 }
1075 catch(ExecuteException e) {
1076 long duration = System.currentTimeMillis() - startTime;
1077 System.out.println("Process completed in " + duration +" millis; above is its output");
1078 return;
1079 }
1080
1081 fail("Expecting an ExecuteException");
1082 }
1083
1084
1085
1086
1087
1088
1089
1090
1091 public void testExec_60() throws Exception {
1092
1093 int start = 0;
1094 final int seconds = 1;
1095 final int offsetMultiplier = 1;
1096 final int maxRetries = 180;
1097 int processTerminatedCounter = 0;
1098 int watchdogKilledProcessCounter = 0;
1099 CommandLine cmdLine = new CommandLine(pingScript);
1100 cmdLine.addArgument(Integer.toString(seconds + 1));
1101
1102 final long startTime = System.currentTimeMillis();
1103 for (int offset = start; offset <= maxRetries; offset++) {
1104
1105
1106
1107
1108 ExecuteWatchdog watchdog = new ExecuteWatchdog(seconds * 1000 + offset * offsetMultiplier);
1109 exec.setWatchdog(watchdog);
1110 try {
1111 exec.execute(cmdLine);
1112 processTerminatedCounter++;
1113
1114 if(processTerminatedCounter > 5) {
1115 break;
1116 }
1117 } catch (ExecuteException ex) {
1118
1119 assertTrue("Watchdog killed the process", watchdog.killedProcess());
1120 watchdogKilledProcessCounter++;
1121 }
1122 }
1123
1124 final long avg = (System.currentTimeMillis() - startTime) /
1125 (watchdogKilledProcessCounter+processTerminatedCounter);
1126 System.out.println("Processes terminated: "+processTerminatedCounter+" killed: "+watchdogKilledProcessCounter
1127 +" Multiplier: "+offsetMultiplier+" MaxRetries: "+maxRetries+" Elapsed (avg ms): "+avg);
1128 assertTrue("Not a single process terminated on its own", processTerminatedCounter > 0);
1129 assertTrue("Not a single process was killed by the watch dog", watchdogKilledProcessCounter > 0);
1130 }
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142 public void _testExecuteStability() throws Exception {
1143
1144
1145 for(int i=0; i<100; i++) {
1146 Map env = new HashMap();
1147 env.put("TEST_ENV_VAR", new Integer(i));
1148 CommandLine cl = new CommandLine(testScript);
1149 int exitValue = exec.execute(cl,env);
1150 assertFalse(exec.isFailure(exitValue));
1151 assertEquals("FOO." + i + ".", baos.toString().trim());
1152 baos.reset();
1153 }
1154
1155
1156 for(int i=0; i<100; i++) {
1157 Map env = new HashMap();
1158 env.put("TEST_ENV_VAR", new Integer(i));
1159 DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
1160 CommandLine cl = new CommandLine(foreverTestScript);
1161 ExecuteWatchdog watchdog = new ExecuteWatchdog(500);
1162 exec.setWatchdog(watchdog);
1163 exec.execute(cl, env, resultHandler);
1164 resultHandler.waitFor(WAITFOR_TIMEOUT);
1165 assertTrue("ResultHandler received a result", resultHandler.hasResult());
1166 assertNotNull(resultHandler.getException());
1167 baos.reset();
1168 }
1169 }
1170
1171
1172
1173
1174
1175
1176 private String readFile(File file) throws Exception {
1177
1178 String text;
1179 StringBuffer contents = new StringBuffer();
1180 BufferedReader reader = new BufferedReader(new FileReader(file));
1181
1182 while ((text = reader.readLine()) != null)
1183 {
1184 contents.append(text)
1185 .append(System.getProperty(
1186 "line.separator"));
1187 }
1188 reader.close();
1189 return contents.toString();
1190 }
1191
1192 private int getOccurrences(String data, char c) {
1193
1194 int result = 0;
1195
1196 for(int i=0; i<data.length(); i++) {
1197 if(data.charAt(i) == c) {
1198 result++;
1199 }
1200 }
1201
1202 return result;
1203 }
1204 }