Path: blob/master/test/hotspot/jtreg/applications/jcstress/TestGenerator.java
40942 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package applications.jcstress;2425import jdk.test.lib.Utils;26import jdk.test.lib.process.OutputAnalyzer;27import jdk.test.lib.process.ProcessTools;2829import java.io.BufferedReader;30import java.io.File;31import java.io.FileNotFoundException;32import java.io.IOException;33import java.io.PrintStream;34import java.nio.file.Files;35import java.nio.file.Path;36import java.nio.file.Paths;37import java.util.Calendar;38import java.util.EnumSet;39import java.util.function.Predicate;404142/**43* @notest THIS IS NOT A TEST.44* This is a test generator, should be run only when jcstress is changed45*46* @library /test/lib /47* @run main applications.jcstress.TestGenerator48*/4950/**51* Use jcstress test suite to generate jtreg tests in 'test.src' or current52* directory. Used version is defined in JcstressRunner class.53*54* This generator depends on testlibrary, therefore it should be compiled and55* added to classpath. One can replace @notest by @test in jtreg test56* description above to run this class with jtreg.57*58* @see <a href=https://wiki.openjdk.java.net/display/CodeTools/jcstress>jcstress</a>59*/60public class TestGenerator {61private static final String COPYRIGHT;62static {63String years;64final int firstYear = 2017;65int currentYear = Calendar.getInstance().get(Calendar.YEAR);66if (firstYear < currentYear) {67years = String.format("%d, %d", firstYear, currentYear);68} else {69years = "" + firstYear;70}71COPYRIGHT = String.format("/*\n" +72" * Copyright (c) %s, Oracle and/or its affiliates. All rights reserved.\n" +73" * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\n" +74" *\n" +75" * This code is free software; you can redistribute it and/or modify it\n" +76" * under the terms of the GNU General Public License version 2 only, as\n" +77" * published by the Free Software Foundation.\n" +78" *\n" +79" * This code is distributed in the hope that it will be useful, but WITHOUT\n" +80" * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n" +81" * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\n" +82" * version 2 for more details (a copy is included in the LICENSE file that\n" +83" * accompanied this code).\n" +84" *\n" +85" * You should have received a copy of the GNU General Public License version\n" +86" * 2 along with this work; if not, write to the Free Software Foundation,\n" +87" * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\n" +88" *\n" +89" * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\n" +90" * or visit www.oracle.com if you need additional information or have any\n" +91" * questions.\n" +92" */\n\n", years);93}9495public static String DESC_FORMAT = "\n"96+ "/**\n"97+ " * @test %1$s\n"98+ " * @library /test/lib /\n"99+ " * @run driver/timeout=21600 " + JcstressRunner.class.getName()100// verbose output101+ " -v"102// test name103+ " -t org.openjdk.jcstress.tests.%1$s\\.\n"104+ " */\n";105106public static void main(String[] args) throws IOException {107Path path = JcstressRunner.pathToArtifact();108Path output;109try {110output = Files.createTempFile("jcstress", ".out");111ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(112"-jar",113path.toAbsolutePath().toString(),114"-l");115pb.redirectOutput(output.toFile());116new OutputAnalyzer(pb.start()).shouldHaveExitValue(0);117} catch (Exception e) {118throw new Error("Can not get list of tests", e);119}120121BufferedReader reader = Files.newBufferedReader(output);122123reader.lines()124.skip(4) // skip first 4 lines: name, -{80}, revision and empty line125.map(s -> s.split("\\.")[4]) // group by the package name following "org.openjdk.jcstress.tests."126.distinct()127.filter(s -> !s.startsWith("sample")) // skip sample test128.forEach(TestGenerator::generate);129130output.toFile().delete();131}132133private static void generate(String group) {134Path testFile = Paths.get(Utils.TEST_SRC).resolve(group + ".java");135136System.out.println("Generating " + testFile);137try (PrintStream ps = new PrintStream(testFile.toFile())) {138ps.print(COPYRIGHT);139ps.printf("/* DO NOT MODIFY THIS FILE. GENERATED BY %s */\n",140TestGenerator.class.getName());141142ps.printf(DESC_FORMAT, group);143ps.print('\n');144} catch (FileNotFoundException e) {145System.out.println("Failed to generate tests for " + group);146}147}148}149150151