< prev index next >

test/tools/javac/modules/PatchModulesTest.java

Print this page
rev 3947 : imported patch xmodule-to-patch-module


  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 /*
  25  * @test
  26  * @bug 8160489
  27  * @summary tests for --patch-modules
  28  * @library /tools/lib
  29  * @modules
  30  *      jdk.compiler/com.sun.tools.javac.api
  31  *      jdk.compiler/com.sun.tools.javac.file:+open
  32  *      jdk.compiler/com.sun.tools.javac.main
  33  * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
  34  * @run main PatchModulesTest
  35  */
  36 
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.io.PrintWriter;
  40 import java.io.StringWriter;
  41 import java.lang.reflect.Field;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;

  44 import java.util.List;
  45 import java.util.Map;


  46 import java.util.stream.Collectors;

  47 

  48 import javax.tools.JavaFileObject;
  49 import javax.tools.ToolProvider;


  50 
  51 import com.sun.source.util.JavacTask;
  52 import com.sun.tools.javac.api.JavacTool;
  53 import com.sun.tools.javac.file.BaseFileManager;
  54 import com.sun.tools.javac.file.JavacFileManager;
  55 import com.sun.tools.javac.file.Locations;
  56 
  57 import static java.util.Arrays.asList;
  58 
  59 
  60 public class PatchModulesTest extends ModuleTestBase {
  61 
  62     public static void main(String... args) throws Exception {
  63         PatchModulesTest t = new PatchModulesTest();
  64         t.init();
  65         t.runTests();
  66     }
  67 
  68     private static String PS = File.pathSeparator;
  69 
  70     void init() throws IOException {
  71         tb.createDirectories("a", "b", "c", "d", "e");
  72         tb.writeJavaFiles(Paths.get("."), "class C { }");
  73     }
  74 
  75     @Test


  98 
  99     @Test
 100     public void testEmpty(Path base) throws Exception {
 101         test(asList(""),
 102             false, "no value for --patch-module option");
 103     }
 104 
 105     @Test
 106     public void testInvalid(Path base) throws Exception {
 107         test(asList("java.base/java.lang=."),
 108             false, "bad value for --patch-module option: 'java.base/java.lang=.'");
 109     }
 110 
 111     void test(List<String> patches, String expect) throws Exception {
 112         test(patches, true, expect);
 113     }
 114 
 115     void test(List<String> patches, boolean expectOK, String expect) throws Exception {
 116         JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
 117         StringWriter sw = new StringWriter();
 118         try (PrintWriter pw = new PrintWriter(sw)) {
 119             JavacFileManager fm = tool.getStandardFileManager(null, null, null);
 120             List<String> opts = patches.stream()
 121                 .map(p -> "--patch-module=" + p.replace(":", PS))
 122                 .collect(Collectors.toList());
 123             Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects("C.java");
 124             JavacTask task = tool.getTask(pw, fm, null, opts, null, files);
 125 
 126             Field locationsField = BaseFileManager.class.getDeclaredField("locations");
 127             locationsField.setAccessible(true);
 128             Object locations = locationsField.get(fm);
 129 
 130             Field patchMapField = Locations.class.getDeclaredField("patchMap");
 131             patchMapField.setAccessible(true);
 132             Map<?,?> patchMap = (Map<?,?>) patchMapField.get(locations);








 133             String found = patchMap.toString();
 134 
 135             if (!found.equals(expect)) {
 136                 tb.out.println("Expect: " + expect);
 137                 tb.out.println("Found:  " + found);
 138                 error("output not as expected");
 139             }
 140         } catch (IllegalArgumentException e) {
 141             if (expectOK) {
 142                 error("unexpected exception: " + e);
 143                 throw e;
 144             }
 145             String found = e.getMessage();
 146             if (!found.equals(expect)) {
 147                 tb.out.println("Expect: " + expect);
 148                 tb.out.println("Found:  " + found);
 149                 error("output not as expected");
 150             }





























 151         }
 152     }
 153 }
 154 


  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 /*
  25  * @test
  26  * @bug 8160489
  27  * @summary tests for --patch-modules
  28  * @library /tools/lib
  29  * @modules
  30  *      jdk.compiler/com.sun.tools.javac.api
  31  *      jdk.compiler/com.sun.tools.javac.file
  32  *      jdk.compiler/com.sun.tools.javac.main
  33  * @build toolbox.ToolBox toolbox.JavacTask toolbox.ModuleBuilder ModuleTestBase
  34  * @run main PatchModulesTest
  35  */
  36 
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.io.PrintWriter;
  40 import java.io.StringWriter;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.util.AbstractMap.SimpleEntry;
  45 import java.util.List;
  46 import java.util.Map;
  47 import java.util.Map.Entry;
  48 import java.util.TreeMap;
  49 import java.util.stream.Collectors;
  50 import java.util.stream.StreamSupport;
  51 
  52 import javax.tools.JavaFileManager.Location;
  53 import javax.tools.JavaFileObject;
  54 import javax.tools.ToolProvider;
  55 import javax.tools.StandardJavaFileManager;
  56 import javax.tools.StandardLocation;
  57 
  58 import com.sun.source.util.JavacTask;
  59 import com.sun.tools.javac.api.JavacTool;

  60 import com.sun.tools.javac.file.JavacFileManager;

  61 
  62 import static java.util.Arrays.asList;
  63 
  64 
  65 public class PatchModulesTest extends ModuleTestBase {
  66 
  67     public static void main(String... args) throws Exception {
  68         PatchModulesTest t = new PatchModulesTest();
  69         t.init();
  70         t.runTests();
  71     }
  72 
  73     private static String PS = File.pathSeparator;
  74 
  75     void init() throws IOException {
  76         tb.createDirectories("a", "b", "c", "d", "e");
  77         tb.writeJavaFiles(Paths.get("."), "class C { }");
  78     }
  79 
  80     @Test


 103 
 104     @Test
 105     public void testEmpty(Path base) throws Exception {
 106         test(asList(""),
 107             false, "no value for --patch-module option");
 108     }
 109 
 110     @Test
 111     public void testInvalid(Path base) throws Exception {
 112         test(asList("java.base/java.lang=."),
 113             false, "bad value for --patch-module option: 'java.base/java.lang=.'");
 114     }
 115 
 116     void test(List<String> patches, String expect) throws Exception {
 117         test(patches, true, expect);
 118     }
 119 
 120     void test(List<String> patches, boolean expectOK, String expect) throws Exception {
 121         JavacTool tool = (JavacTool) ToolProvider.getSystemJavaCompiler();
 122         StringWriter sw = new StringWriter();
 123         try (PrintWriter pw = new PrintWriter(sw);
 124             JavacFileManager fm = tool.getStandardFileManager(null, null, null)) {
 125             List<String> opts = patches.stream()
 126                 .map(p -> "--patch-module=" + p.replace(":", PS))
 127                 .collect(Collectors.toList());
 128             Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects("C.java");
 129             JavacTask task = tool.getTask(pw, fm, null, opts, null, files);
 130 
 131             Map<String, List<Location>> mod2Location =
 132                     StreamSupport.stream(fm.listLocationsForModules(StandardLocation.PATCH_MODULE_PATH)
 133                                            .spliterator(),
 134                                         false)
 135                                  .flatMap(sl -> sl.stream())
 136                                  .collect(Collectors.groupingBy(l -> fm.inferModuleName(l)));
 137 
 138             Map<String, List<String>> patchMap = mod2Location.entrySet()
 139                     .stream()
 140                     .map(e -> new SimpleEntry<>(e.getKey(), e.getValue().get(0)))
 141                     .map(e -> new SimpleEntry<>(e.getKey(), locationPaths(fm, e.getValue())))
 142                     .collect(Collectors.toMap(Entry :: getKey,
 143                                               Entry :: getValue,
 144                                               (v1, v2) -> {throw new IllegalStateException();},
 145                                               TreeMap::new));
 146             String found = patchMap.toString();
 147 
 148             if (!found.equals(expect)) {
 149                 tb.out.println("Expect: " + expect);
 150                 tb.out.println("Found:  " + found);
 151                 error("output not as expected");
 152             }
 153         } catch (IllegalArgumentException e) {
 154             if (expectOK) {
 155                 error("unexpected exception: " + e);
 156                 throw e;
 157             }
 158             String found = e.getMessage();
 159             if (!found.equals(expect)) {
 160                 tb.out.println("Expect: " + expect);
 161                 tb.out.println("Found:  " + found);
 162                 error("output not as expected");
 163             }
 164         }
 165     }
 166 
 167     static List<String> locationPaths(StandardJavaFileManager fm, Location loc) {
 168         return StreamSupport.stream(fm.getLocationAsPaths(loc).spliterator(), false)
 169                             .map(p -> p.toString())
 170                             .collect(Collectors.toList());
 171     }
 172 
 173     @Test
 174     public void testPatchWithSource(Path base) throws Exception {
 175         Path patch = base.resolve("patch");
 176         tb.writeJavaFiles(patch, "package javax.lang.model.element; public interface Extra { }");
 177         Path src = base.resolve("src");
 178         tb.writeJavaFiles(src,
 179                           "module m { requires java.compiler; }",
 180                           "package test; public interface Test extends javax.lang.model.element.Extra { }");
 181         Path classes = base.resolve("classes");
 182         tb.createDirectories(classes);
 183 
 184         new toolbox.JavacTask(tb)
 185             .options("--patch-module", "java.compiler=" + patch.toString())
 186             .outdir(classes)
 187             .files(findJavaFiles(src))
 188             .run()
 189             .writeAll();
 190 
 191         if (Files.exists(classes.resolve("javax"))) {
 192             throw new AssertionError();
 193         }
 194     }
 195 }
 196 
< prev index next >