File: ChildProcessTest.java

package info (click to toggle)
jython 2.7.2%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,676 kB
  • sloc: python: 640,908; java: 306,458; xml: 1,984; sh: 522; ansic: 126; makefile: 76
file content (80 lines) | stat: -rw-r--r-- 2,818 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package org.python.util.install;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import junit.framework.TestCase;

public class ChildProcessTest extends TestCase {

    private final static String CLASS_NAME = "org.python.util.install.ChildProcessExample";

    /**
     * test a default child process
     */
    public void testDefault() {
        ChildProcess childProcess = new ChildProcess();
        String command[] = buildJavaCommand(CLASS_NAME);
        childProcess.setCommand(command);
        childProcess.setDebug(true);
        int exitValue = childProcess.run();
        assertEquals("Expected child process to end normally instead of " + exitValue, 0, exitValue);
    }

    /**
     * test the child process with a timeout
     */
    public void testTimeout() {
        ChildProcess childProcess = new ChildProcess();
        String command[] = buildJavaCommand(CLASS_NAME);
        childProcess.setCommand(command);
        childProcess.setDebug(true);
        childProcess.setTimeout(2000); // timeout to 2 seconds
        int exitValue = childProcess.run();
        assertEquals("Expected child process to be destroyed instead of " + exitValue,
                     ChildProcess.DESTROYED_AFTER_TIMEOUT,
                     exitValue);
    }

    /**
     * test silent mode
     */
    public void testSilent() throws IOException {
        ChildProcess childProcess = new ChildProcess();
        String command[] = new String[] {"lwiklsl", "-siwK"};
        childProcess.setCommand(command);
        childProcess.setDebug(false);
        childProcess.setSilent(true);
        ByteArrayOutputStream redirectedErr = new ByteArrayOutputStream();
        ByteArrayOutputStream redirectedOut = new ByteArrayOutputStream();
        int exitValue = 0;
        PrintStream oldErr = System.err;
        PrintStream oldOut = System.out;
        try {
            System.setErr(new PrintStream(redirectedErr));
            System.setOut(new PrintStream(redirectedOut));
            exitValue = childProcess.run();
        } finally {
            System.setErr(oldErr);
            System.setOut(oldOut);
        }
        assertTrue(0 != exitValue);
        redirectedErr.flush();
        redirectedOut.flush();
        assertEquals(0, redirectedErr.size());
        assertEquals(0, redirectedOut.size());
    }

    //
    // private methods
    //
    private String[] buildJavaCommand(String classAndArguments) {
        String quote = "";
        if (System.getProperty("os.name", "unknown").toLowerCase().indexOf("windows") >= 0) {
            quote = "\"";
        }
        String classpath = System.getProperty("java.class.path");
        return new String[] {"java",  "-classpath",  quote.concat(classpath).concat(quote), classAndArguments};
    }
}