Java Code Examples for jdk.testlibrary.OutputAnalyzer#getExitValue()

The following examples show how to use jdk.testlibrary.OutputAnalyzer#getExitValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NoPremainAgentTest.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:NoPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 2
Source File: ZeroArgPremainAgentTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:ZeroArgPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 3
Source File: NoPremainAgentTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:NoPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 4
Source File: NoPremainAgentTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:NoPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 5
Source File: ZeroArgPremainAgentTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:ZeroArgPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 6
Source File: ZeroArgPremainAgentTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:ZeroArgPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = ProcessTools.executeProcess(pb);
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 7
Source File: ZeroArgPremainAgentTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:ZeroArgPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 8
Source File: NoPremainAgentTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] a) throws Exception {
    String testArgs = String.format(
            "-javaagent:NoPremainAgent.jar -classpath %s DummyMain",
            System.getProperty("test.classes", "."));

    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
            Utils.addTestJavaOpts(testArgs.split("\\s+")));
    System.out.println("testjvm.cmd:" + Utils.getCommandLine(pb));

    OutputAnalyzer output = new OutputAnalyzer(pb.start());
    System.out.println("testjvm.stdout:" + output.getStdout());
    System.out.println("testjvm.stderr:" + output.getStderr());

    output.stderrShouldContain("java.lang.NoSuchMethodException");
    if (0 == output.getExitValue()) {
        throw new RuntimeException("Expected error but got exit value 0");
    }
}
 
Example 9
Source File: ExecuteOOMApp.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Executes OOM application with JFR recording and returns true if OOM error happened in the
 * test thread. Adds -XX:-UseGCOverheadLimit option to avoid "java.lang.OutOfMemoryError: GC overhead limit exceeded".
 *
 * @param settings JFR settings file
 * @param jfrFilename JFR resulting recording filename
 * @param additionalVmFlags additional VM flags passed to the java
 * @param bytesToAllocate number of bytes to allocate in new object every cycle in OOM application
 * @return true - OOM application is finished as expected,i.e. OOM happened in the test thead
 *         false - OOM application is finished with OOM error which happened in the non test thread
 */
public static boolean execute(String settings, String jfrFilename, String[] additionalVmFlags, int bytesToAllocate) throws Exception {
    List<String> additionalVmFlagsList = new ArrayList<>(Arrays.asList(additionalVmFlags));
    additionalVmFlagsList.add("-XX:-UseGCOverheadLimit");

    OutputAnalyzer out = AppExecutorHelper.executeAndRecord(settings, jfrFilename, additionalVmFlagsList.toArray(new String[0]),
                                                            OOMApp.class.getName(), String.valueOf(bytesToAllocate));

    if ((out.getExitValue() == 1 && out.getOutput().contains("Exception: java.lang.OutOfMemoryError"))) {
        return false;
    }

    out.shouldHaveExitValue(0);
    System.out.println(out.getOutput());

    return true;
}
 
Example 10
Source File: BasicTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void execute(Path image, String scriptName) throws Throwable {
    String cmd = image.resolve("bin").resolve(scriptName).toString();
    OutputAnalyzer analyzer;
    if (System.getProperty("os.name").startsWith("Windows")) {
        analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");
    } else {
        analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");
    }
    if (analyzer.getExitValue() != 0) {
        throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());
    }
}
 
Example 11
Source File: NativeErrors.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String executeCmd(String... toolArgs) throws Throwable {
    JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
    for (String s : toolArgs) {
        cmd.addToolArg(s);
    }
    OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
    if (output == null || output.getStdout() == null) {
        throw new Exception("Output was null. Process did not finish correctly.");
    }
    if (output.getExitValue() == 0) {
        throw new Exception("Process exit code was 0, but error was expected.");
    }
    return output.getStdout();
}
 
Example 12
Source File: NativeErrors.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static String executeCmd(String... toolArgs) throws Throwable {
    JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
    for (String s : toolArgs) {
        cmd.addToolArg(s);
    }
    OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
    if (output == null || output.getStdout() == null) {
        throw new Exception("Output was null. Process did not finish correctly.");
    }
    if (output.getExitValue() == 0) {
        throw new Exception("Process exit code was 0, but error was expected.");
    }
    return output.getStdout();
}
 
Example 13
Source File: NativeErrors.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static String executeCmd(String... toolArgs) throws Throwable {
    JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
    for (String s : toolArgs) {
        cmd.addToolArg(s);
    }
    OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
    if (output == null || output.getStdout() == null) {
        throw new Exception("Output was null. Process did not finish correctly.");
    }
    if (output.getExitValue() == 0) {
        throw new Exception("Process exit code was 0, but error was expected.");
    }
    return output.getStdout();
}
 
Example 14
Source File: IllegalAccessTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks an expected result with the output captured by the given
 * OutputAnalyzer.
 */
void checkResult(Result expectedResult, OutputAnalyzer outputAnalyzer) {
    expectedResult.expectedOutput().forEach(outputAnalyzer::shouldContain);
    expectedResult.notExpectedOutput().forEach(outputAnalyzer::shouldNotContain);
    int exitValue = outputAnalyzer.getExitValue();
    if (expectedResult.shouldSucceed()) {
        assertTrue(exitValue == 0);
    } else {
        assertTrue(exitValue != 0);
    }
}
 
Example 15
Source File: NativeErrors.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String executeCmd(String... toolArgs) throws Throwable {
    JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
    for (String s : toolArgs) {
        cmd.addToolArg(s);
    }
    OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
    if (output == null || output.getStdout() == null) {
        throw new Exception("Output was null. Process did not finish correctly.");
    }
    if (output.getExitValue() == 0) {
        throw new Exception("Process exit code was 0, but error was expected.");
    }
    return output.getStdout();
}
 
Example 16
Source File: NativeErrors.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private static String executeCmd(String... toolArgs) throws Throwable {
    JDKToolLauncher cmd = JDKToolLauncher.createUsingTestJDK("native2ascii");
    for (String s : toolArgs) {
        cmd.addToolArg(s);
    }
    OutputAnalyzer output = ProcessTools.executeProcess(cmd.getCommand());
    if (output == null || output.getStdout() == null) {
        throw new Exception("Output was null. Process did not finish correctly.");
    }
    if (output.getExitValue() == 0) {
        throw new Exception("Process exit code was 0, but error was expected.");
    }
    return output.getStdout();
}
 
Example 17
Source File: ModularTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test method for TestNG.
 */
@Test(dataProvider = "TestParams")
public void runTest(MODULE_TYPE cModuleType, MODULE_TYPE sModuletype,
        boolean addMetaDesc, String failureMsgExpected, String[] args)
        throws Exception {

    String testName = new StringJoiner("_").add(cModuleType.toString())
            .add(sModuletype.toString()).add(
                    (addMetaDesc) ? "WITH_SERVICE" : "NO_SERVICE")
            .toString();

    System.out.format("%nStarting Test case: '%s'", testName);
    Path cJarPath = findJarPath(false, cModuleType, false,
            (sModuletype == MODULE_TYPE.EXPLICIT));
    Path sJarPath = findJarPath(true, sModuletype, addMetaDesc, false);
    System.out.format("%nClient jar path : %s ", cJarPath);
    System.out.format("%nService jar path : %s ", sJarPath);
    OutputAnalyzer output = executeTestClient(cModuleType, cJarPath,
            sModuletype, sJarPath, args);

    if (output.getExitValue() != 0) {
        if (failureMsgExpected != null
                && output.getOutput().contains(failureMsgExpected)) {
            System.out.println("PASS: Test is expected to fail here.");
        } else {
            System.out.format("%nUnexpected failure occured with exit code"
                    + " '%s'.", output.getExitValue());
            throw new RuntimeException("Unexpected failure occured.");
        }
    }
}
 
Example 18
Source File: TLSRestrictions.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void testConstraint(String[] trustNames, String[] certNames,
        String serverConstraint, String clientConstraint,
        boolean needClientAuth, boolean pass) throws Throwable {
    String trustNameStr = trustNames == null ? ""
            : String.join(DELIMITER, trustNames);
    String certNameStr = certNames == null ? ""
            : String.join(DELIMITER, certNames);

    System.out.printf("Case:%n"
            + "  trustNames=%s; certNames=%s%n"
            + "  serverConstraint=%s; clientConstraint=%s%n"
            + "  needClientAuth=%s%n"
            + "  pass=%s%n%n",
            trustNameStr, certNameStr,
            serverConstraint, clientConstraint,
            needClientAuth,
            pass);
    setConstraint("Server", serverConstraint);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    try {
        JSSEServer server = new JSSEServer(
                createSSLContext(trustNames, certNames),
                needClientAuth);
        int port = server.getPort();
        Future<Exception> serverFuture = executor.submit(() -> server.start());

        // Run client on another JVM so that its properties cannot be in conflict
        // with server's.
        OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
                "-Dcert.dir=" + CERT_DIR,
                "-Djava.security.debug=certpath",
                "-classpath",
                TEST_CLASSES,
                "JSSEClient",
                port + "",
                trustNameStr,
                certNameStr,
                clientConstraint);
        int clientExitValue = outputAnalyzer.getExitValue();
        String clientOut = outputAnalyzer.getOutput();
        System.out.println("---------- Client output start ----------");
        System.out.println(clientOut);
        System.out.println("---------- Client output end ----------");

        Exception serverException = serverFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);
        if (serverException instanceof SocketTimeoutException
                || clientOut.contains("SocketTimeoutException")) {
            System.out.println("The communication gets timeout and skips the test.");
            return;
        }

        if (pass) {
            if (serverException != null || clientExitValue != 0) {
                throw new RuntimeException(
                        "Unexpected failure. Operation was blocked.");
            }
        } else {
            if (serverException == null && clientExitValue == 0) {
                throw new RuntimeException(
                        "Unexpected pass. Operation was allowed.");
            }

            // The test may encounter non-SSL issues, like network problem.
            if (!(serverException instanceof SSLHandshakeException
                    || clientOut.contains("SSLHandshakeException"))) {
                throw new RuntimeException("Failure with unexpected exception.");
            }
        }
    } finally {
        executor.shutdown();
    }
}
 
Example 19
Source File: TLSRestrictions.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
static void testConstraint(String[] trustNames, String[] certNames,
        String serverConstraint, String clientConstraint,
        boolean needClientAuth, boolean pass) throws Throwable {
    String trustNameStr = trustNames == null ? ""
            : String.join(DELIMITER, trustNames);
    String certNameStr = certNames == null ? ""
            : String.join(DELIMITER, certNames);

    System.out.printf("Case:%n"
            + "  trustNames=%s; certNames=%s%n"
            + "  serverConstraint=%s; clientConstraint=%s%n"
            + "  needClientAuth=%s%n"
            + "  pass=%s%n%n",
            trustNameStr, certNameStr,
            serverConstraint, clientConstraint,
            needClientAuth,
            pass);
    setConstraint("Server", serverConstraint);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    try {
        JSSEServer server = new JSSEServer(
                createSSLContext(trustNames, certNames),
                needClientAuth);
        int port = server.getPort();
        Future<Exception> serverFuture = executor.submit(() -> server.start());

        // Run client on another JVM so that its properties cannot be in conflict
        // with server's.
        OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
                "-Dcert.dir=" + CERT_DIR,
                "-Djava.security.debug=certpath",
                "-classpath",
                TEST_CLASSES,
                "JSSEClient",
                port + "",
                trustNameStr,
                certNameStr,
                clientConstraint);
        int clientExitValue = outputAnalyzer.getExitValue();
        String clientOut = outputAnalyzer.getOutput();
        System.out.println("---------- Client output start ----------");
        System.out.println(clientOut);
        System.out.println("---------- Client output end ----------");

        Exception serverException = serverFuture.get(TIMEOUT, TimeUnit.MILLISECONDS);
        if (serverException instanceof SocketTimeoutException
                || clientOut.contains("SocketTimeoutException")) {
            System.out.println("The communication gets timeout and skips the test.");
            return;
        }

        if (pass) {
            if (serverException != null || clientExitValue != 0) {
                throw new RuntimeException(
                        "Unexpected failure. Operation was blocked.");
            }
        } else {
            if (serverException == null && clientExitValue == 0) {
                throw new RuntimeException(
                        "Unexpected pass. Operation was allowed.");
            }

            // The test may encounter non-SSL issues, like network problem.
            if (!(serverException instanceof SSLHandshakeException
                    || clientOut.contains("SSLHandshakeException"))) {
                throw new RuntimeException("Failure with unexpected exception.");
            }
        }
    } finally {
        executor.shutdown();
    }
}
 
Example 20
Source File: TLSRestrictions.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
static void testConstraint(String[] trustNames, String[] certNames,
        String serverConstraint, String clientConstraint,
        boolean needClientAuth, boolean pass) throws Throwable {
    String trustNameStr = trustNames == null ? ""
            : String.join(DELIMITER, trustNames);
    String certNameStr = certNames == null ? ""
            : String.join(DELIMITER, certNames);

    System.out.printf("Case:%n"
            + "  trustNames=%s; certNames=%s%n"
            + "  serverConstraint=%s; clientConstraint=%s%n"
            + "  needClientAuth=%s%n"
            + "  pass=%s%n%n",
            trustNameStr, certNameStr,
            serverConstraint, clientConstraint,
            needClientAuth,
            pass);
    setConstraint("Server", serverConstraint);
    JSSEServer server = new JSSEServer(
            createSSLContext(trustNames, certNames),
            needClientAuth);
    int port = server.getPort();
    server.start();

    // Run client on another JVM so that its properties cannot be in conflict
    // with server's.
    OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJvm(
            "-Dcert.dir=" + CERT_DIR,
            "-Djava.security.debug=certpath",
            "-classpath",
            TEST_CLASSES,
            "JSSEClient",
            port + "",
            trustNameStr,
            certNameStr,
            clientConstraint);
    int exitValue = outputAnalyzer.getExitValue();
    String clientOut = outputAnalyzer.getOutput();

    Exception serverException = server.getException();
    if (serverException != null) {
        System.out.println("Server: failed");
    }

    System.out.println("---------- Client output start ----------");
    System.out.println(clientOut);
    System.out.println("---------- Client output end ----------");

    if (serverException instanceof SocketTimeoutException
            || clientOut.contains("SocketTimeoutException")) {
        System.out.println("The communication gets timeout and skips the test.");
        return;
    }

    if (pass) {
        if (serverException != null || exitValue != 0) {
            throw new RuntimeException(
                    "Unexpected failure. Operation was blocked.");
        }
    } else {
        if (serverException == null && exitValue == 0) {
            throw new RuntimeException(
                    "Unexpected pass. Operation was allowed.");
        }

        // The test may encounter non-SSL issues, like network problem.
        if (!(serverException instanceof SSLHandshakeException
                || clientOut.contains("SSLHandshakeException"))) {
            throw new RuntimeException("Failure with unexpected exception.");
        }
    }
}