8180724: move ModuleInfoMaker to the top level testlibrary
authoriignatyev
Wed, 24 May 2017 14:16:09 -0700
changeset 45191 ba48b1679390
parent 45190 9f9d05fc5e15
child 45192 3c481584b5a6
8180724: move ModuleInfoMaker to the top level testlibrary Reviewed-by: psandoz, mchung
jdk/test/lib/testlibrary/ModuleInfoMaker.java
jdk/test/tools/jmod/hashes/HashesTest.java
jdk/test/tools/launcher/modules/addexports/AddExportsTestWarningError.java
jdk/test/tools/launcher/modules/addreads/AddReadsTestWarningError.java
--- a/jdk/test/lib/testlibrary/ModuleInfoMaker.java	Wed May 24 13:55:37 2017 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,127 +0,0 @@
-/*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-import java.io.BufferedWriter;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Arrays;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import java.util.stream.Stream;
-
-import static org.testng.Assert.assertTrue;
-
-/**
- * Utility class for creating test modules.
- */
-public class ModuleInfoMaker {
-    private static String MODULE_INFO_JAVA = "module-info.java";
-    private static Pattern MODULE_PATTERN =
-        Pattern.compile("module\\s+((?:\\w+\\.)*)");
-    private static Pattern PACKAGE_PATTERN =
-                       Pattern.compile("package\\s+(((?:\\w+\\.)*)(?:\\w+))");
-    private static Pattern CLASS_PATTERN =
-          Pattern.compile("(?:public\\s+)?(?:class|enum|interface)\\s+(\\w+)");
-
-    private final Path dir;
-    public ModuleInfoMaker(Path dir) {
-        this.dir = dir;
-    }
-
-    /**
-     * Create java source files of the given module
-     */
-    public void writeJavaFiles(String module, String moduleInfoJava, String... contents)
-        throws IOException
-    {
-        Path msrc = dir.resolve(module);
-        new JavaSource(moduleInfoJava).write(msrc);
-        for (String c : contents) {
-            new JavaSource(c).write(msrc);
-        }
-    }
-
-    /**
-     * Compile the module to the given destination.
-     */
-    public void compile(String module, Path dest, String... options)
-        throws IOException
-    {
-        Path msrc = dir.resolve(module);
-        Stream<String> args =
-            Stream.concat(Arrays.stream(options),
-                          Stream.of("--module-source-path",
-                                    dir.toString()));
-        assertTrue(CompilerUtils.compile(msrc, dest, args.toArray(String[]::new)),
-                   "Fail to compile " + module);
-    }
-
-    static class JavaSource {
-        final String source;
-        JavaSource(String source) {
-            this.source = source;
-        }
-
-        /**
-         * Writes the source code to a file in a specified directory.
-         * @param dir the directory
-         * @throws IOException if there is a problem writing the file
-         */
-        public void write(Path dir) throws IOException {
-            Path file = dir.resolve(getJavaFileNameFromSource(source));
-            Files.createDirectories(file.getParent());
-            try (BufferedWriter out = Files.newBufferedWriter(file)) {
-                out.write(source.replace("\n", System.lineSeparator()));
-            }
-        }
-
-        /**
-         * Extracts the Java file name from the class declaration.
-         * This method is intended for simple files and uses regular expressions,
-         * so comments matching the pattern can make the method fail.
-         */
-        static String getJavaFileNameFromSource(String source) {
-            String packageName = null;
-
-            Matcher matcher = MODULE_PATTERN.matcher(source);
-            if (matcher.find())
-                return MODULE_INFO_JAVA;
-
-            matcher = PACKAGE_PATTERN.matcher(source);
-            if (matcher.find())
-                packageName = matcher.group(1).replace(".", "/");
-
-            matcher = CLASS_PATTERN.matcher(source);
-            if (matcher.find()) {
-                String className = matcher.group(1) + ".java";
-                return (packageName == null) ? className : packageName + "/" + className;
-            } else if (packageName != null) {
-                return packageName + "/package-info.java";
-            } else {
-                throw new Error("Could not extract the java class " +
-                    "name from the provided source");
-            }
-        }
-    }
-}
--- a/jdk/test/tools/jmod/hashes/HashesTest.java	Wed May 24 13:55:37 2017 -0700
+++ b/jdk/test/tools/jmod/hashes/HashesTest.java	Wed May 24 14:16:09 2017 -0700
@@ -25,13 +25,12 @@
  * @test
  * @bug 8160286
  * @summary Test the recording and checking of module hashes
- * @library /lib/testlibrary
+ * @library /test/lib
  * @modules java.base/jdk.internal.misc
  *          java.base/jdk.internal.module
  *          jdk.compiler
  *          jdk.jartool
  *          jdk.jlink
- * @build CompilerUtils ModuleInfoMaker
  * @run testng HashesTest
  */
 
@@ -62,6 +61,8 @@
 import jdk.internal.module.ModuleHashes;
 import jdk.internal.module.ModulePath;
 
+import jdk.test.lib.compiler.ModuleInfoMaker;
+
 import org.testng.annotations.Test;
 
 import static org.testng.Assert.*;
@@ -382,7 +383,7 @@
         makeModule(mn, null, deps);
     }
 
-    private void makeModule(String mn, ModuleDescriptor.Requires.Modifier mod,  String... deps)
+    private void makeModule(String mn, ModuleDescriptor.Requires.Modifier mod, String... deps)
         throws IOException
     {
         if (mod != null && mod != TRANSITIVE && mod != STATIC) {
@@ -390,23 +391,23 @@
         }
 
         StringBuilder sb = new StringBuilder();
-        sb.append("module " + mn + " {").append("\n");
-        Arrays.stream(deps).forEach(req -> {
-            sb.append("    requires ");
-            if (mod != null) {
-                sb.append(mod.toString().toLowerCase()).append(" ");
-            }
-            sb.append(req + ";\n");
-        });
+        sb.append("module ")
+          .append(mn)
+          .append(" {")
+          .append("\n");
+        Arrays.stream(deps)
+              .forEach(req -> {
+                  sb.append("    requires ");
+                  if (mod != null) {
+                      sb.append(mod.toString().toLowerCase())
+                        .append(" ");
+                  }
+                  sb.append(req)
+                    .append(";\n");
+              });
         sb.append("}\n");
         builder.writeJavaFiles(mn, sb.toString());
-
-        compileModule(mn, srcDir);
-    }
-
-    private void compileModule(String moduleName, Path src) throws IOException {
-        Path msrc = src.resolve(moduleName);
-        assertTrue(CompilerUtils.compile(msrc, mods, "--module-source-path", src.toString()));
+        builder.compile(mn, mods);
     }
 
     private void jmodHashModules(String moduleName, String hashModulesPattern) {
--- a/jdk/test/tools/launcher/modules/addexports/AddExportsTestWarningError.java	Wed May 24 13:55:37 2017 -0700
+++ b/jdk/test/tools/launcher/modules/addexports/AddExportsTestWarningError.java	Wed May 24 14:16:09 2017 -0700
@@ -25,9 +25,8 @@
  * @test
  * @bug 8168836
  * @summary Basic argument validation for --add-exports
- * @library /lib/testlibrary
+ * @library /lib/testlibrary /test/lib
  * @modules jdk.compiler
- * @build AddExportsTestWarningError CompilerUtils ModuleInfoMaker
  * @build jdk.testlibrary.*
  * @run testng AddExportsTestWarningError
  */
@@ -40,6 +39,7 @@
 import java.util.Arrays;
 import java.util.stream.Stream;
 
+import jdk.test.lib.compiler.ModuleInfoMaker;
 import jdk.testlibrary.OutputAnalyzer;
 import static jdk.testlibrary.ProcessTools.*;
 
@@ -48,7 +48,6 @@
 import org.testng.annotations.Test;
 import static org.testng.Assert.*;
 
-
 @Test
 public class AddExportsTestWarningError {
 
--- a/jdk/test/tools/launcher/modules/addreads/AddReadsTestWarningError.java	Wed May 24 13:55:37 2017 -0700
+++ b/jdk/test/tools/launcher/modules/addreads/AddReadsTestWarningError.java	Wed May 24 14:16:09 2017 -0700
@@ -25,9 +25,9 @@
  * @test
  * @bug 8168836
  * @summary  Basic argument validation for --add-reads
- * @library /lib/testlibrary
+ * @library /lib/testlibrary /test/lib
  * @modules jdk.compiler
- * @build AddReadsTestWarningError CompilerUtils ModuleInfoMaker
+ * @build AddReadsTestWarningError
  * @build jdk.testlibrary.*
  * @run testng AddReadsTestWarningError
  */
@@ -40,6 +40,7 @@
 import java.util.Arrays;
 import java.util.stream.Stream;
 
+import jdk.test.lib.compiler.ModuleInfoMaker;
 import jdk.testlibrary.OutputAnalyzer;
 import static jdk.testlibrary.ProcessTools.*;
 
@@ -48,7 +49,6 @@
 import org.testng.annotations.Test;
 import static org.testng.Assert.*;
 
-
 @Test
 public class AddReadsTestWarningError {