1 /*
   2  * Copyright (c) 2017, 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 package applications.jcstress;
  25 
  26 import jdk.test.lib.Utils;
  27 import jdk.test.lib.artifacts.Artifact;
  28 import jdk.test.lib.artifacts.ArtifactResolver;
  29 import jdk.test.lib.artifacts.ArtifactResolverException;
  30 import jdk.test.lib.process.OutputAnalyzer;
  31 import jdk.test.lib.process.ProcessTools;
  32 
  33 import java.io.File;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.util.ArrayList;
  38 import java.util.Map;
  39 import java.util.List;
  40 
  41 /**
  42  * jcstress tests wrapper
  43  */
  44 @Artifact(organization = "org.openjdk.jcstress", name = "jcstress-tests-all",
  45         revision = "0.5", extension = "jar", unpack = false)
  46 public class JcstressRunner {
  47 
  48     public static final String MAIN_CLASS = "org.openjdk.jcstress.Main";
  49 
  50     public static Path pathToArtifact() {
  51         Map<String, Path> artifacts;
  52         try {
  53             artifacts = ArtifactResolver.resolve(JcstressRunner.class);
  54         } catch (ArtifactResolverException e) {
  55             throw new Error("TESTBUG: Can not resolve artifacts for "
  56                             + JcstressRunner.class.getName(), e);
  57         }
  58         return artifacts.get("org.openjdk.jcstress.jcstress-tests-all-0.5")
  59                         .toAbsolutePath();
  60     }
  61 
  62     public static void main(String[] args) throws Throwable {
  63         if (args.length < 1) {
  64             throw new Error("Usage: [jcstress flag]*");
  65         }
  66         Path out = Paths.get("jcstress.out").toAbsolutePath();
  67 
  68         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(getCmd(args))
  69                                         .redirectErrorStream(true)
  70                                         .redirectOutput(out.toFile());
  71         OutputAnalyzer oa = ProcessTools.executeProcess(pb);
  72         if (0 != oa.getExitValue()) {
  73             String message = "jctress test finished with nonzero exitcode "
  74                               + oa.getExitValue();
  75             System.err.println(message);
  76 
  77             System.err.print("cmd = ");
  78             System.err.println(pb.command());
  79 
  80             System.err.print("cout/cerr(");
  81             System.err.print(out.toString());
  82             System.err.println(")[");
  83             Files.lines(out).forEach(System.err::println);
  84             System.err.println("]cout/cerr");
  85             throw new Error(message);
  86         }
  87     }
  88 
  89     private static String[] getCmd(String[] args) {
  90         List<String> extraFlags = new ArrayList<>();
  91 
  92         // add jar with jcstress tests and harness to CP
  93         extraFlags.add("-cp");
  94         extraFlags.add(System.getProperty("java.class.path")
  95                        + File.pathSeparator
  96                        + pathToArtifact().toString());
  97 
  98         extraFlags.add(MAIN_CLASS);
  99 
 100         String[] javaOpts = Utils.getTestJavaOpts();
 101         // disable flags auto-detection
 102         if (0 == javaOpts.length) {
 103             extraFlags.add("--jvmArgs");
 104             extraFlags.add("");
 105         } else {
 106             for (String jvmArg : Utils.getTestJavaOpts()) {
 107                 extraFlags.add("--jvmArgs");
 108                 extraFlags.add(jvmArg);
 109             }
 110         }
 111 
 112         String[] result = new String[extraFlags.size() + args.length];
 113         extraFlags.toArray(result);
 114         System.arraycopy(args, 0, result, extraFlags.size(), args.length);
 115         return result;
 116     }
 117 }