langtools/test/tools/javac/EarlyAssertWrapper.java
changeset 4940 256e46854879
child 5520 86e4b9a9da40
equal deleted inserted replaced
4939:94c48ae38df4 4940:256e46854879
       
     1 /*
       
     2  * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 import java.io.*;
       
    25 import java.util.*;
       
    26 
       
    27 /*
       
    28  * Wrapper for the EarlyAssert test to run the test in a JVM without assertions
       
    29  * enabled.
       
    30  */
       
    31 public class EarlyAssertWrapper {
       
    32     public static void main(String... args) throws Exception {
       
    33         EarlyAssertWrapper w = new EarlyAssertWrapper();
       
    34         w.run();
       
    35     }
       
    36 
       
    37     void run() throws Exception {
       
    38         List<String> cmd = new ArrayList<String>();
       
    39         File java_home = new File(System.getProperty("java.home"));
       
    40         if (java_home.getName().equals("jre"))
       
    41             java_home = java_home.getParentFile();
       
    42         cmd.add(new File(new File(java_home, "bin"), "java").getPath());
       
    43 
       
    44         // ensure we run with the same bootclasspath as this test,
       
    45         // in case this test is being run with -Xbootclasspath
       
    46         cmd.add("-Xbootclasspath:" + System.getProperty("sun.boot.class.path"));
       
    47 
       
    48         // propogate classpath
       
    49         cmd.add("-classpath");
       
    50         cmd.add(System.getProperty("java.class.path"));
       
    51 
       
    52         // ensure all assertions disabled in target VM
       
    53         cmd.add("-da");
       
    54         cmd.add("-dsa");
       
    55 
       
    56         cmd.add("EarlyAssert");
       
    57 
       
    58         System.err.println("Running command: " + cmd);
       
    59 
       
    60         ProcessBuilder pb = new ProcessBuilder(cmd);
       
    61         pb.redirectErrorStream(true);
       
    62         Process p = pb.start();
       
    63         p.getOutputStream().close();
       
    64 
       
    65         StringWriter sw = new StringWriter();
       
    66         PrintWriter pw = new PrintWriter(sw);
       
    67 
       
    68         String line;
       
    69         DataInputStream in = new DataInputStream(p.getInputStream());
       
    70         try {
       
    71         while ((line = in.readLine()) != null)
       
    72             pw.println(line);
       
    73         } finally {
       
    74             in.close();
       
    75         }
       
    76         pw.close();
       
    77 
       
    78         String out = sw.toString();
       
    79         int rc = p.waitFor();
       
    80         if (rc != 0 || out.length() > 0)
       
    81             throw new Error("failed: rc=" + rc + (out.length() > 0 ? ": " + out : ""));
       
    82     }
       
    83 }