1 /*
  2  * Copyright (c) 2017, 2018, 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.process.OutputAnalyzer;
 28 import jdk.test.lib.process.ProcessTools;
 29 
 30 import java.io.BufferedReader;
 31 import java.io.File;
 32 import java.io.FileNotFoundException;
 33 import java.io.IOException;
 34 import java.io.PrintStream;
 35 import java.nio.file.Files;
 36 import java.nio.file.Path;
 37 import java.nio.file.Paths;
 38 import java.util.Calendar;
 39 import java.util.EnumSet;
 40 import java.util.function.Predicate;
 41 
 42 
 43 /**
 44  * @test THIS IS NOT A TEST.
 45  * This is a test generator, should be run only when jcstress is changed
 46  *
 47  * @library /test/lib /
 48  * @run main applications.jcstress.TestGenerator
 49  */
 50 
 51 /**
 52  * Use jcstress test suite to generate jtreg tests in 'test.src' or current
 53  * directory. Used version is defined in JcstressRunner class.
 54  *
 55  * This generator depends on testlibrary, therefore it should be compiled and
 56  * added to classpath. One can replace @notest by @test in jtreg test
 57  * description above to run this class with jtreg.
 58  *
 59  * @see <a href=https://wiki.openjdk.java.net/display/CodeTools/jcstress>jcstress</a>
 60  */
 61 public class TestGenerator {
 62     private static final String COPYRIGHT;
 63     static {
 64         String years;
 65         final int firstYear = 2017;
 66         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
 67         if (firstYear < currentYear) {
 68             years = String.format("%d, %d", firstYear, currentYear);
 69         } else {
 70             years = "" + firstYear;
 71         }
 72         COPYRIGHT = String.format("/*\n" +
 73                 " * Copyright (c) %s, Oracle and/or its affiliates. All rights reserved.\n" +
 74                 " * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n" +
 75                 " *\n" +
 76                 " * This code is free software; you can redistribute it and/or modify it\n" +
 77                 " * under the terms of the GNU General Public License version 2 only, as\n" +
 78                 " * published by the Free Software Foundation.\n" +
 79                 " *\n" +
 80                 " * This code is distributed in the hope that it will be useful, but WITHOUT\n" +
 81                 " * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n" +
 82                 " * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n" +
 83                 " * version 2 for more details (a copy is included in the LICENSE file that\n" +
 84                 " * accompanied this code).\n" +
 85                 " *\n" +
 86                 " * You should have received a copy of the GNU General Public License version\n" +
 87                 " * 2 along with this work; if not, write to the Free Software Foundation,\n" +
 88                 " * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\n" +
 89                 " *\n" +
 90                 " * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\n" +
 91                 " * or visit www.oracle.com if you need additional information or have any\n" +
 92                 " * questions.\n" +
 93                 " */\n\n", years);
 94     }
 95 
 96     public static String DESC_FORMAT = "\n"
 97             + "/**\n"
 98             + " * @test %1$s\n"
 99             + " * @library /test/lib /\n"
100             + " * @run driver/timeout=21600 " + JcstressRunner.class.getName()
101                     // verbose output, testing in virtual threads
102                     + " -v -vt -spinStyle LOCKSUPPORT_PARK_NANOS -f 0"
103                     // test name
104                     + " -t org.openjdk.jcstress.tests.%1$s\\.\n"
105             + " */\n";
106 
107     public static void main(String[] args) throws IOException {
108         Path path = JcstressRunner.pathToArtifact();
109         Path output;
110         try {
111             output = Files.createTempFile("jcstress", ".out");
112             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
113                     "-jar",
114                     path.toAbsolutePath().toString(),
115                     "-l");
116             pb.redirectOutput(output.toFile());
117             new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);
118         } catch (Exception e) {
119             throw new Error("Can not get list of tests", e);
120         }
121 
122         BufferedReader reader = Files.newBufferedReader(output);
123 
124         reader.lines()
125                 .skip(6) // skip first 6 lines: name, -{80}, revision CPU info and empty line
126                 .map(s -> s.split("\\.")[4]) // group by the package name following "org.openjdk.jcstress.tests."
127                 .distinct()
128                 .filter(s -> !s.startsWith("sample")) // skip sample test
129                 .forEach(TestGenerator::generate);
130 
131         output.toFile().delete();
132     }
133 
134     private static void generate(String group) {
135         Path testFile = Paths.get(Utils.TEST_SRC).resolve(group + ".java");
136 
137         System.out.println("Generating " + testFile);
138         try (PrintStream ps = new PrintStream(testFile.toFile())) {
139             ps.print(COPYRIGHT);
140             ps.printf("/* DO NOT MODIFY THIS FILE. GENERATED BY %s */\n",
141                     TestGenerator.class.getName());
142 
143             ps.printf(DESC_FORMAT, group);
144             ps.print('\n');
145         } catch (FileNotFoundException e) {
146             System.out.println("Failed to generate tests for " + group);
147         }
148     }
149 }