test/tools/javah/T5070898.java

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javah/T5070898.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,103 @@
     1.4 +/*
     1.5 + * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +/* @test
    1.28 + * @bug 5070898
    1.29 + * @summary javah command doesn't throw correct exit code in case of error
    1.30 + */
    1.31 +
    1.32 +import java.io.*;
    1.33 +import java.util.*;
    1.34 +import javax.tools.*;
    1.35 +
    1.36 +public class T5070898
    1.37 +{
    1.38 +    public static void main(String... args) throws Exception {
    1.39 +        new T5070898().run();
    1.40 +    }
    1.41 +
    1.42 +    public void run() throws Exception {
    1.43 +        writeFile();
    1.44 +        compileFile();
    1.45 +
    1.46 +        int rc = runJavah();
    1.47 +        System.err.println("exit code: " + rc);
    1.48 +        if (rc == 0)
    1.49 +            throw new Exception("unexpected exit code: " + rc);
    1.50 +    }
    1.51 +
    1.52 +    void writeFile() throws Exception {
    1.53 +        String content =
    1.54 +            "package test;\n" +
    1.55 +            "public class JavahTest{\n" +
    1.56 +            "    public static void main(String args){\n" +
    1.57 +            "        System.out.println(\"Test Message\");" +
    1.58 +            "    }\n" +
    1.59 +            "    private static native Object nativeTest();\n" +
    1.60 +            "}\n";
    1.61 +        FileWriter out = new FileWriter("JavahTest.java");
    1.62 +        try {
    1.63 +            out.write(content);
    1.64 +        } finally {
    1.65 +            out.close();
    1.66 +        }
    1.67 +    }
    1.68 +
    1.69 +    void compileFile() throws Exception {
    1.70 +        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    1.71 +        int rc = javac.run(null, null, null, "JavahTest.java");
    1.72 +        if (rc != 0)
    1.73 +            throw new Exception("compilation failed");
    1.74 +    }
    1.75 +
    1.76 +    int runJavah() throws Exception {
    1.77 +        List<String> cmd = new ArrayList<String>();
    1.78 +        File java_home = new File(System.getProperty("java.home"));
    1.79 +        if (java_home.getName().equals("jre"))
    1.80 +            java_home = java_home.getParentFile();
    1.81 +        cmd.add(new File(new File(java_home, "bin"), "javah").getPath());
    1.82 +
    1.83 +        // ensure we run with the same bootclasspath as this test,
    1.84 +        // in case this test is being run with -Xbootclasspath
    1.85 +        cmd.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"));
    1.86 +
    1.87 +        cmd.add("JavahTest");
    1.88 +
    1.89 +        ProcessBuilder pb = new ProcessBuilder(cmd);
    1.90 +        pb.redirectErrorStream(true);
    1.91 +        pb.environment().remove("CLASSPATH");
    1.92 +        Process p = pb.start();
    1.93 +        p.getOutputStream().close();
    1.94 +
    1.95 +        String line;
    1.96 +        DataInputStream in = new DataInputStream(p.getInputStream());
    1.97 +        try {
    1.98 +        while ((line = in.readLine()) != null)
    1.99 +            System.err.println(line);
   1.100 +        } finally {
   1.101 +            in.close();
   1.102 +        }
   1.103 +
   1.104 +        return p.waitFor();
   1.105 +    }
   1.106 +}

mercurial