1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test CtwTest
  26  * @bug 8012447
  27  * @library /testlibrary /testlibrary/whitebox /testlibrary/ctw/src
  28  * @build sun.hotspot.tools.ctw.CompileTheWorld sun.hotspot.WhiteBox foo bar CtwTest
  29  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  30  * @run main ClassFileInstaller foo
  31  * @run main ClassFileInstaller bar
  32  * @run main CtwTest prepare
  33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Dsun.hotspot.tools.ctw.logfile=ctw.log sun.hotspot.tools.ctw.CompileTheWorld classes foo.jar bar.jar jars/* classes.lst
  34  * @run main CtwTest check ctw.log
  35  * @summary testing of CompileTheWorl
  36  * @author igor.ignatyev@oracle.com
  37  */
  38 
  39 import java.util.List;
  40 import java.util.Collections;
  41 import java.util.ArrayList;
  42 
  43 import java.io.File;
  44 import java.io.Writer;
  45 import java.io.FileWriter;
  46 import java.io.IOException;
  47 import java.io.BufferedReader;
  48 
  49 import java.nio.file.Files;
  50 import java.nio.file.Paths;
  51 import java.nio.file.StandardCopyOption;
  52 import java.nio.charset.Charset;
  53 
  54 import com.oracle.java.testlibrary.ProcessTools;
  55 import com.oracle.java.testlibrary.OutputAnalyzer;
  56 import com.oracle.java.testlibrary.JDKToolFinder;
  57 
  58 public class CtwTest {
  59     public static void main(String[] args) throws Exception {
  60         if (args.length == 0) {
  61             throw new Error("args is empty");
  62         }
  63         switch (args[0]) {
  64             case "prepare":
  65                 prepare(args);
  66                 break;
  67             case "check":
  68                 check(args);
  69                 break;
  70             default:
  71                 throw new Error("unregonized action -- " + args[0]);
  72         }
  73     }
  74 
  75     private static void prepare(String[] args) throws Exception {
  76         String path = "classes";
  77         Files.createDirectory(Paths.get(path));
  78         Files.copy(Paths.get("foo.class"), Paths.get(path, "foo.class"),
  79                 StandardCopyOption.REPLACE_EXISTING);
  80         Files.copy(Paths.get("bar.class"), Paths.get(path, "bar.class"),
  81                 StandardCopyOption.REPLACE_EXISTING);
  82 
  83         ProcessBuilder pb = createJarProcessBuilder("cf", "foo.jar",
  84                 "foo.class", "bar.class");
  85         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  86         dump(output, "ctw-foo.jar");
  87         output.shouldHaveExitValue(0);
  88 
  89         pb = createJarProcessBuilder("cf", "bar.jar", "foo.class", "bar.class");
  90         output = new OutputAnalyzer(pb.start());
  91         dump(output, "ctw-bar.jar");
  92         output.shouldHaveExitValue(0);
  93 
  94         path = "jars";
  95         Files.createDirectory(Paths.get(path));
  96         Files.copy(Paths.get("foo.jar"), Paths.get(path, "foo.jar"),
  97                 StandardCopyOption.REPLACE_EXISTING);
  98         Files.copy(Paths.get("bar.jar"), Paths.get(path, "bar.jar"),
  99                 StandardCopyOption.REPLACE_EXISTING);
 100 
 101         path = "classes.lst";
 102         Files.copy(Paths.get(System.getProperty("test.src"), path),
 103                 Paths.get(path), StandardCopyOption.REPLACE_EXISTING);
 104     }
 105 
 106     private static void check(String[] args) throws Exception  {
 107         if (args.length < 2) {
 108             throw new Error("logfile isn't specified");
 109         }
 110         String logfile = args[1];
 111         try (BufferedReader r = Files.newBufferedReader(Paths.get(logfile),
 112                 Charset.defaultCharset())) {
 113             OutputAnalyzer output = readOutput(r);
 114             String[] shouldContain = {"# dir: classes", "# jar: foo.jar",
 115                     "# jar: bar.jar", "# jar_in_dir: jars",
 116                     "# list: classes.lst", "Done (12 classes, "};
 117             for (String test : shouldContain) {
 118                 output.shouldContain(test);
 119             }
 120         }
 121     }
 122 
 123     private static OutputAnalyzer readOutput(BufferedReader reader)
 124             throws IOException {
 125         StringBuilder builder = new StringBuilder();
 126         String eol = String.format("%n");
 127         String line;
 128 
 129         while ((line = reader.readLine()) != null) {
 130             builder.append(line);
 131             builder.append(eol);
 132         }
 133         return new OutputAnalyzer(builder.toString(), "");
 134     }
 135 
 136     private static void dump(OutputAnalyzer output, String name) {
 137         try (Writer w = new FileWriter(name + ".out")) {
 138             String s = output.getStdout();
 139             w.write(s, s.length(), 0);
 140         } catch (IOException io) {
 141             io.printStackTrace();
 142         }
 143         try (Writer w = new FileWriter(name + ".err")) {
 144             String s = output.getStderr();
 145             w.write(s, s.length(), 0);
 146         } catch (IOException io) {
 147             io.printStackTrace();
 148         }
 149     }
 150 
 151     private static ProcessBuilder createJarProcessBuilder(String... command)
 152             throws Exception {
 153         String javapath = JDKToolFinder.getJDKTool("jar");
 154 
 155         ArrayList<String> args = new ArrayList<>();
 156         args.add(javapath);
 157         Collections.addAll(args, command);
 158 
 159         return new ProcessBuilder(args.toArray(new String[args.size()]));
 160     }
 161 
 162 
 163 }