changeset 2972:cf35af6e6fde

improve javac module tests
author jjg
date Fri, 03 Apr 2015 18:25:34 -0700
parents 2640b9d2f527
children 05331b4fc718
files test/tools/javac/modules/HelloWorldTest.java test/tools/javac/modules/ModuleFinderTest.java test/tools/javac/modules/ModuleInfoTest.java test/tools/javac/modules/MultiModuleModeTest.java test/tools/javac/modules/PackageConflictTest.java test/tools/javac/modules/ProvidesTest.java test/tools/javac/modules/RequiresPublicTest.java test/tools/javac/modules/ResolveTest.java test/tools/javac/modules/SingleModuleModeTest.java test/tools/javac/modules/SubpackageTest.java test/tools/javac/modules/UsesTest.java test/tools/lib/ToolBox.java
diffstat 12 files changed, 116 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/test/tools/javac/modules/HelloWorldTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/HelloWorldTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -62,7 +62,7 @@
 
         Path smallRtJar = base.resolve("small-rt.jar");
         try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) {
-            tb.new JarTask(smallRtJar.toString())
+            tb.new JarTask(smallRtJar)
                 .files(fm, StandardLocation.PLATFORM_CLASS_PATH,
                     "java.lang.**", "java.io.*", "java.util.*")
                 .run();
@@ -72,7 +72,7 @@
             .options("-source", "8",
                 "-target", "8",
                 "-bootclasspath", smallRtJar.toString())
-            .outdir(classes.toString())
+            .outdir(classes)
             .files(src.resolve("HelloWorld.java"))
             .run();
 
@@ -88,7 +88,7 @@
         Files.createDirectories(classes);
 
         tb.new JavacTask()
-            .outdir(classes.toString())
+            .outdir(classes)
             .files(src.resolve("HelloWorld.java"))
             .run();
 
@@ -105,7 +105,7 @@
         Files.createDirectories(classes);
 
         tb.new JavacTask(ToolBox.Mode.CMDLINE)
-            .outdir(classes.toString())
+            .outdir(classes)
             .files(src.resolve("module-info.java"), src.resolve("p/HelloWorld.java"))
             .run()
             .writeAll();
@@ -127,7 +127,7 @@
 
         tb.new JavacTask()
             .options("-modulesourcepath", src.toString())
-            .outdir(classes.toString())
+            .outdir(classes)
             .files(src_m1.resolve("p/HelloWorld.java"))
             .run()
             .writeAll();
--- a/test/tools/javac/modules/ModuleFinderTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/ModuleFinderTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -53,18 +53,18 @@
         Files.createDirectories(modules);
 
         tb.new JavacTask(ToolBox.Mode.CMDLINE)
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run()
                 .writeAll();
 
-        tb.new JarTask(modules.resolve("m1-1.jar").toString()) // should allow Path here
-                .baseDir(classes.toString()) // should allow Path here
+        tb.new JarTask(modules.resolve("m1-1.jar"))
+                .baseDir(classes)
                 .files(".")
                 .run();
 
-        tb.new JarTask(modules.resolve("m1-2.jar").toString()) // should allow Path here
-                .baseDir(classes.toString()) // should allow Path here
+        tb.new JarTask(modules.resolve("m1-2.jar"))
+                .baseDir(classes)
                 .files(".")
                 .run();
 
@@ -74,7 +74,7 @@
 
         String log = tb.new JavacTask(ToolBox.Mode.CMDLINE)
                 .options("-XDrawDiagnostics", "-modulepath", modules.toString())
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src2))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
--- a/test/tools/javac/modules/ModuleInfoTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/ModuleInfoTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -182,7 +182,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -208,7 +208,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -231,7 +231,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics")
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -257,7 +257,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
--- a/test/tools/javac/modules/MultiModuleModeTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/MultiModuleModeTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -57,7 +57,7 @@
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics",
                         "-modulesourcepath", src.toString())
-                .outdir(classes.toString()) // should allow Path here
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -80,7 +80,7 @@
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics",
                         "-modulesourcepath", src.toString())
-                .outdir(classes.toString()) // should allow Path here
+                .outdir(classes)
                 .files(join(findJavaFiles(src), findJavaFiles(misc)))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -100,7 +100,7 @@
 
         tb.new JavacTask()
                 .options("-modulesourcepath", src.toString())
-                .outdir(modules.toString()) // should allow Path here
+                .outdir(modules)
                 .files(src.resolve("m2/module-info.java"))
                 .run()
                 .writeAll();
@@ -115,7 +115,7 @@
 
         tb.new JavacTask()
                 .options("-modulesourcepath", src1.toString())
-                .outdir(modules1.toString()) // should allow Path here
+                .outdir(modules1)
                 .files(src1.resolve("m1/module-info.java"))
                 .run()
                 .writeAll();
@@ -128,7 +128,7 @@
         tb.new JavacTask()
                 .options("-modulepath", modules1.toString(),
                         "-modulesourcepath", src2.toString())
-                .outdir(modules2.toString()) // should allow Path here
+                .outdir(modules2)
                 .files(src2.resolve("m2/module-info.java"))
                 .run()
                 .writeAll();
--- a/test/tools/javac/modules/PackageConflictTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/PackageConflictTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -50,7 +50,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics")
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.SUCCESS)
                 .writeAll()
--- a/test/tools/javac/modules/ProvidesTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/ProvidesTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -51,7 +51,7 @@
         Files.createDirectories(classes);
 
         tb.new JavacTask()
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.SUCCESS)
                 .writeAll();
@@ -71,7 +71,7 @@
 
         tb.new JavacTask()
                 .options("-modulesourcepath", src.toString())
-                .outdir(modules.toString())
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.SUCCESS)
                 .writeAll();
@@ -89,7 +89,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics")
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
--- a/test/tools/javac/modules/RequiresPublicTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/RequiresPublicTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -56,7 +56,7 @@
 
         tb.new JavacTask(ToolBox.Mode.CMDLINE)
                 .files(findJavaFiles(src))
-                .outdir(classes.toString()) // should allow Path here
+                .outdir(classes)
                 .run()
                 .writeAll();
     }
@@ -94,7 +94,7 @@
         tb.new JavacTask(ToolBox.Mode.CMDLINE)
                 .options("-modulesourcepath", src.toString())
                 .files(findJavaFiles(src))
-                .outdir(classes.toString()) // should allow Path here
+                .outdir(classes)
                 .run()
                 .writeAll();
     }
@@ -111,7 +111,7 @@
                 .options("-XDrawDiagnostics",
                         "-modulesourcepath", src.toString())
                 .files(findJavaFiles(src))
-                .outdir(classes.toString()) // should allow Path here
+                .outdir(classes)
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
                 .getOutput(ToolBox.OutputKind.DIRECT);
--- a/test/tools/javac/modules/ResolveTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/ResolveTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -90,7 +90,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(modules.toString()) // should accept Path here
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -114,7 +114,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(modules.toString()) // should accept Path here
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -140,7 +140,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(modules.toString()) // should accept Path here
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -164,7 +164,7 @@
 
         String log = tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(modules.toString()) // should accept Path here
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.FAIL)
                 .writeAll()
@@ -188,7 +188,7 @@
 
         tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(modules.toString()) // should accept Path here
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run()
                 .writeAll();
@@ -208,7 +208,7 @@
 
         tb.new JavacTask()
                 .options("-XDrawDiagnostics", "-modulesourcepath", src.toString())
-                .outdir(modules.toString()) // should accept Path here
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run()
                 .writeAll();
--- a/test/tools/javac/modules/SingleModuleModeTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/SingleModuleModeTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -72,7 +72,7 @@
                 "class C { }");
 
         tb.new JavacTask()
-                .classpath(src.toString()) // should allow Path here
+                .classpath(src)
                 .files(src.resolve("C.java"))
                 .run()
                 .writeAll();
@@ -88,13 +88,13 @@
         Files.createDirectories(classes);
 
         tb.new JavacTask()
-                .outdir(classes.toString()) // should allow Path here
+                .outdir(classes)
                 .files(src.resolve("module-info.java"))
                 .run()
                 .writeAll();
 
         tb.new JavacTask()
-                .classpath(classes.toString()) // should allow Path here
+                .classpath(classes)
                 .files(src.resolve("C.java"))
                 .run()
                 .writeAll();
--- a/test/tools/javac/modules/SubpackageTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/SubpackageTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -55,7 +55,7 @@
         Path libclasses = base.resolve("lib/classes");
         Files.createDirectories(libclasses);
         tb.new JavacTask()
-                .outdir(libclasses.toString())
+                .outdir(libclasses)
                 .files(findJavaFiles(libsrc))
                 .run()
                 .writeAll();
@@ -71,8 +71,8 @@
         Files.createDirectories(classes);
 
         tb.new JavacTask()
-                .classpath(libclasses.toString())
-                .outdir(classes.toString())
+                .classpath(libclasses)
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run()
                 .writeAll();
@@ -107,7 +107,7 @@
 
         tb.new JavacTask()
                 .options("-modulesourcepath", src.toString())
-                .outdir(modules.toString())
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run()
                 .writeAll();
--- a/test/tools/javac/modules/UsesTest.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/javac/modules/UsesTest.java	Fri Apr 03 18:25:34 2015 -0700
@@ -50,7 +50,7 @@
         Files.createDirectories(classes);
 
         tb.new JavacTask()
-                .outdir(classes.toString())
+                .outdir(classes)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.SUCCESS)
                 .writeAll();
@@ -69,7 +69,7 @@
 
         tb.new JavacTask()
                 .options("-modulesourcepath", src.toString())
-                .outdir(modules.toString())
+                .outdir(modules)
                 .files(findJavaFiles(src))
                 .run(ToolBox.Expect.SUCCESS)
                 .writeAll();
--- a/test/tools/lib/ToolBox.java	Fri Apr 03 18:24:25 2015 -0700
+++ b/test/tools/lib/ToolBox.java	Fri Apr 03 18:25:34 2015 -0700
@@ -840,9 +840,9 @@
      */
     public class JavacTask extends AbstractTask<JavacTask> {
         private boolean includeStandardOptions;
-        private String classpath;
-        private String sourcepath;
-        private String outdir;
+        private List<Path> classpath;
+        private List<Path> sourcepath;
+        private Path outdir;
         private List<String> options;
         private List<String> classes;
         private List<String> files;
@@ -870,7 +870,20 @@
          * @return this task object
          */
         public JavacTask classpath(String classpath) {
-            this.classpath = classpath;
+            this.classpath = Stream.of(classpath.split(File.pathSeparator))
+                    .filter(s -> !s.isEmpty())
+                    .map(s -> Paths.get(s))
+                    .collect(Collectors.toList());
+            return this;
+        }
+
+        /**
+         * Sets the classpath.
+         * @param classpath the classpath
+         * @return this task object
+         */
+        public JavacTask classpath(Path... classpath) {
+            this.classpath = Arrays.asList(classpath);
             return this;
         }
 
@@ -880,7 +893,20 @@
          * @return this task object
          */
         public JavacTask sourcepath(String sourcepath) {
-            this.sourcepath = sourcepath;
+            this.sourcepath = Stream.of(sourcepath.split(File.pathSeparator))
+                    .filter(s -> !s.isEmpty())
+                    .map(s -> Paths.get(s))
+                    .collect(Collectors.toList());
+            return this;
+        }
+
+        /**
+         * Sets the sourcepath.
+         * @param classpath the sourcepath
+         * @return this task object
+         */
+        public JavacTask sourcepath(Path... sourcepath) {
+            this.sourcepath = Arrays.asList(sourcepath);
             return this;
         }
 
@@ -890,6 +916,16 @@
          * @return this task object
          */
         public JavacTask outdir(String outdir) {
+            this.outdir = Paths.get(outdir);
+            return this;
+        }
+
+        /**
+         * Sets the output directory.
+         * @param outdir the output directory
+         * @return this task object
+         */
+        public JavacTask outdir(Path outdir) {
             this.outdir = outdir;
             return this;
         }
@@ -1020,11 +1056,11 @@
             if (fileManager == null)
                 fileManager = compiler.getStandardFileManager(null, null, null);
             if (outdir != null)
-                setLocation(StandardLocation.CLASS_OUTPUT, toFiles(outdir));
+                setLocationFromPaths(StandardLocation.CLASS_OUTPUT, Collections.singletonList(outdir));
             if (classpath != null)
-                setLocation(StandardLocation.CLASS_PATH, toFiles(classpath));
+                setLocationFromPaths(StandardLocation.CLASS_PATH, classpath);
             if (sourcepath != null)
-                setLocation(StandardLocation.SOURCE_PATH, toFiles(sourcepath));
+                setLocationFromPaths(StandardLocation.SOURCE_PATH, sourcepath);
             List<String> allOpts = new ArrayList<>();
             if (options != null)
                 allOpts.addAll(options);
@@ -1045,6 +1081,12 @@
             ((StandardJavaFileManager) fileManager).setLocation(location, files);
         }
 
+        private void setLocationFromPaths(StandardLocation location, List<Path> files) throws IOException {
+            if (!(fileManager instanceof StandardJavaFileManager))
+                throw new IllegalStateException("not a StandardJavaFileManager");
+            ((StandardJavaFileManager) fileManager).setLocationFromPaths(location, files);
+        }
+
         private int runCommand(PrintWriter pw) {
             List<String> args = getAllArgs();
             String[] argsArray = args.toArray(new String[args.size()]);
@@ -1077,15 +1119,15 @@
                 args.addAll(options);
             if (outdir != null) {
                 args.add("-d");
-                args.add(outdir);
+                args.add(outdir.toString());
             }
             if (classpath != null) {
                 args.add("-classpath");
-                args.add(classpath);
+                args.add(toSearchPath(classpath));
             }
             if (sourcepath != null) {
                 args.add("-sourcepath");
-                args.add(sourcepath);
+                args.add(toSearchPath(sourcepath));
             }
             if (classes != null)
                 args.addAll(classes);
@@ -1095,13 +1137,8 @@
             return args;
         }
 
-        private List<File> toFiles(String path) {
-            List<File> result = new ArrayList<>();
-            for (String s : path.split(File.pathSeparator)) {
-                if (!s.isEmpty())
-                    result.add(new File(s));
-            }
-            return result;
+        private String toSearchPath(List<Path> files) {
+            return files.stream().map(Path::toString).collect(Collectors.joining(File.pathSeparator));
         }
 
         private Iterable<? extends JavaFileObject> joinFiles(
@@ -1336,6 +1373,15 @@
         }
 
         /**
+         * Creates a JarTask for use with a given jar file.
+         * @param path the file
+         */
+        public JarTask(Path path) {
+            this();
+            jar = path;
+        }
+
+        /**
          * Sets a manifest for the jar file.
          * @param manifest the manifest
          * @return this task object
@@ -1389,6 +1435,16 @@
         }
 
         /**
+         * Sets the base directory for files to be written into the jar file.
+         * @param baseDir the base directory
+         * @return this task object
+         */
+        public JarTask baseDir(Path baseDir) {
+            this.baseDir = baseDir;
+            return this;
+        }
+
+        /**
          * Sets the files to be written into the jar file.
          * @param files the files
          * @return this task object