changeset 4942:09276c87a4be

Adding tests for Automatic-Module-Name, checking the name validity.
author jlahoda
date Fri, 02 Jun 2017 17:02:39 +0200
parents a5e8b958d3da
children 89a9c0aca926
files src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java test/tools/javac/modules/AutomaticModules.java
diffstat 2 files changed, 228 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java	Thu Jun 01 19:43:35 2017 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java	Fri Jun 02 17:02:39 2017 +0200
@@ -1351,7 +1351,14 @@
                                 Attributes attrs = man.getMainAttributes();
                                 if (attrs != null) {
                                     String moduleName = attrs.getValue(new Attributes.Name("Automatic-Module-Name"));
-                                    if (moduleName != null) return new Pair<>(moduleName, p);
+                                    if (moduleName != null) {
+                                        if (isModuleName(moduleName)) {
+                                            return new Pair<>(moduleName, p);
+                                        } else {
+                                            log.error(Errors.LocnCantGetModuleNameForJar(p));
+                                            return null;
+                                        }
+                                    }
                                 }
                             }
                         }
@@ -1442,6 +1449,22 @@
             }
         }
 
+        //from jdk.internal.module.Checks:
+        /**
+         * Returns {@code true} if the given name is a legal module name.
+         */
+        private boolean isModuleName(String name) {
+            int next;
+            int off = 0;
+            while ((next = name.indexOf('.', off)) != -1) {
+                String id = name.substring(off, next);
+                if (!SourceVersion.isName(id))
+                    return false;
+                off = next+1;
+            }
+            String last = name.substring(off);
+            return SourceVersion.isName(last);
+        }
     }
 
     private class ModuleSourcePathLocationHandler extends BasicLocationHandler {
--- a/test/tools/javac/modules/AutomaticModules.java	Thu Jun 01 19:43:35 2017 +0100
+++ b/test/tools/javac/modules/AutomaticModules.java	Fri Jun 02 17:02:39 2017 +0200
@@ -34,14 +34,18 @@
  * @run main AutomaticModules
  */
 
+import java.io.File;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.List;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 import toolbox.JarTask;
 import toolbox.JavacTask;
 import toolbox.Task;
+import toolbox.Task.Mode;
 
 public class AutomaticModules extends ModuleTestBase {
 
@@ -614,4 +618,204 @@
         }
     }
 
+    @Test
+    public void testAutomaticModuleNameCorrect(Path base) throws Exception {
+        Path modulePath = base.resolve("module-path");
+
+        Files.createDirectories(modulePath);
+
+        Path automaticSrc = base.resolve("automaticSrc");
+        tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
+        Path automaticClasses = base.resolve("automaticClasses");
+        tb.createDirectories(automaticClasses);
+
+        String automaticLog = new JavacTask(tb)
+                                .outdir(automaticClasses)
+                                .files(findJavaFiles(automaticSrc))
+                                .run()
+                                .writeAll()
+                                .getOutput(Task.OutputKind.DIRECT);
+
+        if (!automaticLog.isEmpty())
+            throw new Exception("expected output not found: " + automaticLog);
+
+        Path automaticJar = modulePath.resolve("automatic-1.0.jar");
+
+        new JarTask(tb, automaticJar)
+          .baseDir(automaticClasses)
+          .files("api/Api.class")
+          .manifest("Automatic-Module-Name: custom.module.name\n\n")
+          .run();
+
+        Path src = base.resolve("src");
+
+        tb.writeJavaFiles(src,
+                          "module m { requires custom.module.name; }",
+                          "package impl; public class Impl { api.Api a; }");
+
+        Path classes = base.resolve("classes");
+
+        Files.createDirectories(classes);
+
+        new JavacTask(tb)
+                .options("--module-path", modulePath.toString(),
+                         "-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(Task.Expect.SUCCESS)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        tb.writeJavaFiles(src,
+                          "module m { requires automatic; }");
+
+        List<String> log = new JavacTask(tb)
+                .options("--module-path", modulePath.toString(),
+                         "-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected =
+                Arrays.asList("module-info.java:1:21: compiler.err.module.not.found: automatic",
+                              "1 error");
+
+        if (!expected.equals(log)) {
+            throw new Exception("expected output not found: " + log);
+        }
+    }
+
+    @Test
+    public void testAutomaticModuleNameIncorrect(Path base) throws Exception {
+        for (String name : new String[] {"", "999", "foo.class", "foo._"}) {
+            if (Files.isDirectory(base)) {
+                tb.cleanDirectory(base);
+            }
+            Path modulePath = base.resolve("module-path");
+
+            Files.createDirectories(modulePath);
+
+            Path automaticSrc = base.resolve("automaticSrc");
+            tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
+            Path automaticClasses = base.resolve("automaticClasses");
+            tb.createDirectories(automaticClasses);
+
+            String automaticLog = new JavacTask(tb)
+                                    .outdir(automaticClasses)
+                                    .files(findJavaFiles(automaticSrc))
+                                    .run()
+                                    .writeAll()
+                                    .getOutput(Task.OutputKind.DIRECT);
+
+            if (!automaticLog.isEmpty())
+                throw new Exception("expected output not found: " + automaticLog);
+
+            Path automaticJar = modulePath.resolve("automatic-1.0.jar");
+
+            new JarTask(tb, automaticJar)
+              .baseDir(automaticClasses)
+              .files("api/Api.class")
+              .manifest("Automatic-Module-Name: " + name + "\n\n")
+              .run();
+
+            Path src = base.resolve("src");
+
+            tb.writeJavaFiles(src,
+                              "package impl; public class Impl { api.Api a; }");
+
+            Path classes = base.resolve("classes");
+
+            Files.createDirectories(classes);
+
+            List<String> log = new JavacTask(tb, Mode.CMDLINE)
+                    .options("--module-path", modulePath.toString(),
+                             "--add-modules", "ALL-MODULE-PATH",
+                             "-XDrawDiagnostics")
+                    .outdir(classes)
+                    .files(findJavaFiles(src))
+                    .run(Task.Expect.FAIL)
+                    .writeAll()
+                    .getOutputLines(Task.OutputKind.DIRECT);
+
+            List<String> expected =
+                    Arrays.asList("- compiler.err.locn.cant.get.module.name.for.jar: " +
+                                      "testAutomaticModuleNameIncorrect/module-path/automatic-1.0.jar".replace("/", File.separator),
+                                  "1 error");
+
+            if (!expected.equals(log)) {
+                throw new Exception("expected output not found: " + log);
+            }
+        }
+    }
+
+    @Test
+    public void testAutomaticModuleNameBroken(Path base) throws Exception {
+        Path modulePath = base.resolve("module-path");
+
+        Files.createDirectories(modulePath);
+
+        Path automaticSrc = base.resolve("automaticSrc");
+        tb.writeJavaFiles(automaticSrc, "package api; public class Api {}");
+        Path automaticClasses = base.resolve("automaticClasses");
+        tb.createDirectories(automaticClasses);
+
+        String automaticLog = new JavacTask(tb)
+                                .outdir(automaticClasses)
+                                .files(findJavaFiles(automaticSrc))
+                                .run()
+                                .writeAll()
+                                .getOutput(Task.OutputKind.DIRECT);
+
+        if (!automaticLog.isEmpty())
+            throw new Exception("expected output not found: " + automaticLog);
+
+        Path automaticJar = modulePath.resolve("automatic-1.0.jar");
+
+        try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(automaticJar))) {
+            out.putNextEntry(new ZipEntry("api/Api.class"));
+            Files.copy(automaticClasses.resolve("api").resolve("Api.class"), out);
+        }
+
+        Path src = base.resolve("src");
+
+        tb.writeJavaFiles(src,
+                          "module m { requires automatic; }",
+                          "package impl; public class Impl { api.Api a; }");
+
+        Path classes = base.resolve("classes");
+
+        Files.createDirectories(classes);
+
+        new JavacTask(tb)
+                .options("--module-path", modulePath.toString(),
+                         "-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(Task.Expect.SUCCESS)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        tb.writeJavaFiles(src,
+                          "module m { requires custom.module.name; }");
+
+        List<String> log = new JavacTask(tb)
+                .options("--module-path", modulePath.toString(),
+                         "-XDrawDiagnostics")
+                .outdir(classes)
+                .files(findJavaFiles(src))
+                .run(Task.Expect.FAIL)
+                .writeAll()
+                .getOutputLines(Task.OutputKind.DIRECT);
+
+        List<String> expected =
+                Arrays.asList("module-info.java:1:34: compiler.err.module.not.found: custom.module.name",
+                              "1 error");
+
+        if (!expected.equals(log)) {
+            throw new Exception("expected output not found: " + log);
+        }
+    }
+
 }