langtools/test/tools/javac/modules/ModuleTestBase.java
changeset 36526 3b41f1c69604
child 36778 e04318f39f92
equal deleted inserted replaced
36525:4caf88912b7f 36526:3b41f1c69604
       
     1 /*
       
     2  * Copyright (c) 2013, 2015, 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 import java.io.File;
       
    25 import java.io.IOException;
       
    26 import java.io.PrintStream;
       
    27 import java.lang.annotation.Annotation;
       
    28 import java.lang.annotation.Retention;
       
    29 import java.lang.annotation.RetentionPolicy;
       
    30 import java.lang.reflect.InvocationTargetException;
       
    31 import java.lang.reflect.Method;
       
    32 import java.nio.file.FileVisitResult;
       
    33 import java.nio.file.Files;
       
    34 import java.nio.file.Path;
       
    35 import java.nio.file.Paths;
       
    36 import java.nio.file.SimpleFileVisitor;
       
    37 import java.nio.file.attribute.BasicFileAttributes;
       
    38 import java.util.ArrayList;
       
    39 import java.util.Arrays;
       
    40 import java.util.List;
       
    41 import java.util.Set;
       
    42 import java.util.TreeSet;
       
    43 import java.util.stream.Collectors;
       
    44 
       
    45 /**
       
    46  * Base class for module tests.
       
    47  */
       
    48 public class ModuleTestBase {
       
    49     protected ToolBox tb;
       
    50     protected PrintStream out;
       
    51     private int errors;
       
    52 
       
    53     /** Marker annotation for test methods to be invoked by runTests. */
       
    54     @Retention(RetentionPolicy.RUNTIME)
       
    55     @interface Test { }
       
    56 
       
    57     /**
       
    58      * Run all methods annotated with @Test, and throw an exception if any
       
    59      * errors are reported..
       
    60      *
       
    61      * @throws Exception if any errors occurred
       
    62      */
       
    63     void runTests() throws Exception {
       
    64         if (tb == null)
       
    65             tb = new ToolBox();
       
    66         out = System.err;
       
    67 
       
    68         for (Method m: getClass().getDeclaredMethods()) {
       
    69             Annotation a = m.getAnnotation(Test.class);
       
    70             if (a != null) {
       
    71                 try {
       
    72                     out.println("Running test " + m.getName());
       
    73                     Path baseDir = Paths.get(m.getName());
       
    74                     m.invoke(this, new Object[] { baseDir });
       
    75                 } catch (InvocationTargetException e) {
       
    76                     Throwable cause = e.getCause();
       
    77                     error("Exception: " + e.getCause());
       
    78                     cause.printStackTrace(out);
       
    79                 }
       
    80                 out.println();
       
    81             }
       
    82         }
       
    83         if (errors > 0)
       
    84             throw new Exception(errors + " errors occurred");
       
    85     }
       
    86 
       
    87     // move to ToolBox?
       
    88     // change returntyp to List<Path> -- means updating ToolBox methods
       
    89     Path[] findJavaFiles(Path... paths) throws IOException {
       
    90         Set<Path> files = new TreeSet<>();
       
    91         for (Path p : paths) {
       
    92             Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
       
    93                 @Override
       
    94                 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
       
    95                         throws IOException {
       
    96                     if (file.getFileName().toString().endsWith(".java")) {
       
    97                         files.add(file);
       
    98                     }
       
    99                     return FileVisitResult.CONTINUE;
       
   100                 }
       
   101             });
       
   102         }
       
   103         return files.toArray(new Path[files.size()]);
       
   104     }
       
   105 
       
   106     void error(String message) {
       
   107         out.println("Error: " + message);
       
   108         errors++;
       
   109     }
       
   110 
       
   111     public class ModuleBuilder {
       
   112 
       
   113         private final String name;
       
   114         private String requires = "";
       
   115         private String exports = "";
       
   116         private String uses = "";
       
   117         private String provides = "";
       
   118         private String modulePath = "";
       
   119         private List<String> content = new ArrayList<>();
       
   120 
       
   121         public ModuleBuilder(String name) {
       
   122             this.name = name;
       
   123         }
       
   124 
       
   125         public ModuleBuilder requiresPublic(String requires, Path... modulePath) {
       
   126             return requires("public " + requires, modulePath);
       
   127         }
       
   128 
       
   129         public ModuleBuilder requires(String requires, Path... modulePath) {
       
   130             this.requires += "    requires " + requires + ";\n";
       
   131             this.modulePath += Arrays.stream(modulePath)
       
   132                     .map(Path::toString)
       
   133                     .collect(Collectors.joining(File.pathSeparator));
       
   134             return this;
       
   135         }
       
   136 
       
   137         public ModuleBuilder exportsTo(String pkg, String module) {
       
   138             return exports(pkg + " to " + module);
       
   139         }
       
   140 
       
   141         public ModuleBuilder exports(String pkg) {
       
   142             this.exports += "    exports " + pkg + ";\n";
       
   143             return this;
       
   144         }
       
   145 
       
   146         public ModuleBuilder uses(String uses) {
       
   147             this.uses += "    uses " + uses + ";\n";
       
   148             return this;
       
   149         }
       
   150 
       
   151         public ModuleBuilder provides(String service, String implementation) {
       
   152             this.provides += "    provides " + service + " with " + implementation + ";\n";
       
   153             return this;
       
   154         }
       
   155 
       
   156         public ModuleBuilder classes(String... content) {
       
   157             this.content.addAll(Arrays.asList(content));
       
   158             return this;
       
   159         }
       
   160 
       
   161         public Path write(Path where) throws IOException {
       
   162             Files.createDirectories(where);
       
   163             List<String> sources = new ArrayList<>();
       
   164             sources.add("module " + name + "{"
       
   165                     + requires
       
   166                     + exports
       
   167                     + uses
       
   168                     + provides
       
   169                     + "}");
       
   170             sources.addAll(content);
       
   171             Path moduleSrc = where.resolve(name + "/src");
       
   172             tb.writeJavaFiles(moduleSrc, sources.toArray(new String[]{}));
       
   173             return moduleSrc;
       
   174         }
       
   175 
       
   176         public void build(Path where) throws IOException {
       
   177             Path moduleSrc = write(where);
       
   178             tb.new JavacTask()
       
   179                     .outdir(where.resolve(name))
       
   180                     .options("-mp", modulePath)
       
   181                     .files(findJavaFiles(moduleSrc))
       
   182                     .run()
       
   183                     .writeAll();
       
   184         }
       
   185     }
       
   186 }