62 @Test 63 public class ModuleReaderTest { 64 65 private static final String TEST_SRC = System.getProperty("test.src"); 66 67 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 68 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 69 private static final Path MODS_DIR = Paths.get("mods"); 70 71 // the module name of the base module 72 private static final String BASE_MODULE = "java.base"; 73 74 // the module name of the test module 75 private static final String TEST_MODULE = "m"; 76 77 // resources in the base module 78 private static final String[] BASE_RESOURCES = { 79 "java/lang/Object.class" 80 }; 81 82 // resource names that should not be found in the base module 83 private static final String[] BAD_BASE_RESOURCES = { 84 "NotFound", 85 "java", 86 "/java", 87 "//java", 88 "java/", 89 "java/lang", 90 "/java/lang", 91 "//java/lang", 92 "java/lang/", 93 "java//lang", 94 "/java/lang/Object.class", 95 "//java/lang/Object.class", 96 "java/lang/Object.class/", 97 "java//lang//Object.class", 98 "./java/lang/Object.class", 99 "java/./lang/Object.class", 100 "java/lang/./Object.class", 101 "../java/lang/Object.class", 102 "java/../lang/Object.class", 103 "java/lang/../Object.class", 104 }; 105 106 // resources in test module (can't use module-info.class as a test 107 // resource as it will be modified by the jmod tool) 108 private static final String[] TEST_RESOURCES = { 109 "p/Main.class" 110 }; 111 112 // resource names that should not be found in the test module 113 private static final String[] BAD_TEST_RESOURCES = { 114 "NotFound", 115 "p", 116 "/p", 117 "//p", 118 "p/", 119 "/p/Main.class", 120 "//p/Main.class", 121 "p/Main.class/", 122 "p//Main.class", 123 "./p/Main.class", 124 "p/./Main.class", 125 "../p/Main.class", 126 "p/../p/Main.class" 127 }; 128 129 130 @BeforeTest 131 public void compileTestModule() throws Exception { 132 133 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 134 boolean compiled 135 = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 136 MODS_DIR.resolve(TEST_MODULE)); 137 assertTrue(compiled, "test module did not compile"); 138 } 143 */ 144 public void testImage() throws IOException { 145 146 ModuleFinder finder = ModuleFinder.ofSystem(); 147 ModuleReference mref = finder.find(BASE_MODULE).get(); 148 ModuleReader reader = mref.open(); 149 150 try (reader) { 151 152 for (String name : BASE_RESOURCES) { 153 byte[] expectedBytes; 154 Module baseModule = Object.class.getModule(); 155 try (InputStream in = baseModule.getResourceAsStream(name)) { 156 expectedBytes = in.readAllBytes(); 157 } 158 159 testFind(reader, name, expectedBytes); 160 testOpen(reader, name, expectedBytes); 161 testRead(reader, name, expectedBytes); 162 testList(reader, name); 163 164 } 165 166 // test "not found" 167 for (String name : BAD_BASE_RESOURCES) { 168 assertFalse(reader.find(name).isPresent()); 169 assertFalse(reader.open(name).isPresent()); 170 assertFalse(reader.read(name).isPresent()); 171 } 172 173 // test nulls 174 try { 175 reader.find(null); 176 assertTrue(false); 177 } catch (NullPointerException expected) { } 178 179 try { 180 reader.open(null); 181 assertTrue(false); 182 } catch (NullPointerException expected) { } 183 184 try { 185 reader.read(null); 186 assertTrue(false); 187 } catch (NullPointerException expected) { } 244 new RuntimeException("jmod tool not found") 245 ); 246 assertEquals(jmodTool.run(System.out, System.out, args), 0); 247 248 test(dir); 249 } 250 251 252 /** 253 * The test module is found on the given module path. Open a ModuleReader 254 * to the test module and test the reader. 255 */ 256 void test(Path mp) throws IOException { 257 258 ModuleFinder finder = ModulePath.of(Runtime.version(), true, mp); 259 ModuleReference mref = finder.find(TEST_MODULE).get(); 260 ModuleReader reader = mref.open(); 261 262 try (reader) { 263 264 // test each of the known resources in the module 265 for (String name : TEST_RESOURCES) { 266 byte[] expectedBytes 267 = Files.readAllBytes(MODS_DIR 268 .resolve(TEST_MODULE) 269 .resolve(name.replace('/', File.separatorChar))); 270 271 testFind(reader, name, expectedBytes); 272 testOpen(reader, name, expectedBytes); 273 testRead(reader, name, expectedBytes); 274 testList(reader, name); 275 } 276 277 // test "not found" 278 for (String name : BAD_TEST_RESOURCES) { 279 assertFalse(reader.find(name).isPresent()); 280 assertFalse(reader.open(name).isPresent()); 281 assertFalse(reader.read(name).isPresent()); 282 } 283 284 // test nulls 285 try { 286 reader.find(null); 287 assertTrue(false); 288 } catch (NullPointerException expected) { } 289 290 try { 291 reader.open(null); 292 assertTrue(false); 293 } catch (NullPointerException expected) { } 294 295 try { 296 reader.read(null); 297 assertTrue(false); 298 } catch (NullPointerException expected) { } 377 } finally { 378 reader.release(bb); 379 } 380 } 381 382 /** 383 * Test ModuleReader#list 384 */ 385 void testList(ModuleReader reader, String name) throws IOException { 386 List<String> list = reader.list().collect(Collectors.toList()); 387 Set<String> names = new HashSet<>(list); 388 assertTrue(names.size() == list.size()); // no duplicates 389 390 assertTrue(names.contains("module-info.class")); 391 assertTrue(names.contains(name)); 392 393 // all resources should be locatable via find 394 for (String e : names) { 395 assertTrue(reader.find(e).isPresent()); 396 } 397 398 // should not contain directories 399 names.forEach(e -> assertFalse(e.endsWith("/"))); 400 } 401 402 } | 62 @Test 63 public class ModuleReaderTest { 64 65 private static final String TEST_SRC = System.getProperty("test.src"); 66 67 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 68 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 69 private static final Path MODS_DIR = Paths.get("mods"); 70 71 // the module name of the base module 72 private static final String BASE_MODULE = "java.base"; 73 74 // the module name of the test module 75 private static final String TEST_MODULE = "m"; 76 77 // resources in the base module 78 private static final String[] BASE_RESOURCES = { 79 "java/lang/Object.class" 80 }; 81 82 // (directory) resources that may be in the base module 83 private static final String[] MAYBE_BASE_RESOURCES = { 84 "java", 85 "java/", 86 "java/lang", 87 "java/lang/", 88 }; 89 90 // resource names that should not be found in the base module 91 private static final String[] NOT_BASE_RESOURCES = { 92 "NotFound", 93 "/java", 94 "//java", 95 "/java/lang", 96 "//java/lang", 97 "java//lang", 98 "/java/lang/Object.class", 99 "//java/lang/Object.class", 100 "java/lang/Object.class/", 101 "java//lang//Object.class", 102 "./java/lang/Object.class", 103 "java/./lang/Object.class", 104 "java/lang/./Object.class", 105 "../java/lang/Object.class", 106 "java/../lang/Object.class", 107 "java/lang/../Object.class", 108 }; 109 110 // resources in test module (can't use module-info.class as a test 111 // resource as it will be modified by the jmod tool) 112 private static final String[] TEST_RESOURCES = { 113 "p/Main.class" 114 }; 115 116 // (directory) resources that may be in the test module 117 private static final String[] MAYBE_TEST_RESOURCES = { 118 "p", 119 "p/" 120 }; 121 122 // resource names that should not be found in the test module 123 private static final String[] NOT_TEST_RESOURCES = { 124 "NotFound", 125 "/p", 126 "//p", 127 "/p/Main.class", 128 "//p/Main.class", 129 "p/Main.class/", 130 "p//Main.class", 131 "./p/Main.class", 132 "p/./Main.class", 133 "../p/Main.class", 134 "p/../p/Main.class" 135 }; 136 137 138 @BeforeTest 139 public void compileTestModule() throws Exception { 140 141 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 142 boolean compiled 143 = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 144 MODS_DIR.resolve(TEST_MODULE)); 145 assertTrue(compiled, "test module did not compile"); 146 } 151 */ 152 public void testImage() throws IOException { 153 154 ModuleFinder finder = ModuleFinder.ofSystem(); 155 ModuleReference mref = finder.find(BASE_MODULE).get(); 156 ModuleReader reader = mref.open(); 157 158 try (reader) { 159 160 for (String name : BASE_RESOURCES) { 161 byte[] expectedBytes; 162 Module baseModule = Object.class.getModule(); 163 try (InputStream in = baseModule.getResourceAsStream(name)) { 164 expectedBytes = in.readAllBytes(); 165 } 166 167 testFind(reader, name, expectedBytes); 168 testOpen(reader, name, expectedBytes); 169 testRead(reader, name, expectedBytes); 170 testList(reader, name); 171 } 172 173 // test resources that may be in the base module 174 for (String name : MAYBE_BASE_RESOURCES) { 175 Optional<URI> ouri = reader.find(name); 176 ouri.ifPresent(uri -> { 177 if (name.endsWith("/")) 178 assertTrue(uri.toString().endsWith("/")); 179 }); 180 } 181 182 // test "not found" in java.base module 183 for (String name : NOT_BASE_RESOURCES) { 184 assertFalse(reader.find(name).isPresent()); 185 assertFalse(reader.open(name).isPresent()); 186 assertFalse(reader.read(name).isPresent()); 187 } 188 189 // test nulls 190 try { 191 reader.find(null); 192 assertTrue(false); 193 } catch (NullPointerException expected) { } 194 195 try { 196 reader.open(null); 197 assertTrue(false); 198 } catch (NullPointerException expected) { } 199 200 try { 201 reader.read(null); 202 assertTrue(false); 203 } catch (NullPointerException expected) { } 260 new RuntimeException("jmod tool not found") 261 ); 262 assertEquals(jmodTool.run(System.out, System.out, args), 0); 263 264 test(dir); 265 } 266 267 268 /** 269 * The test module is found on the given module path. Open a ModuleReader 270 * to the test module and test the reader. 271 */ 272 void test(Path mp) throws IOException { 273 274 ModuleFinder finder = ModulePath.of(Runtime.version(), true, mp); 275 ModuleReference mref = finder.find(TEST_MODULE).get(); 276 ModuleReader reader = mref.open(); 277 278 try (reader) { 279 280 // test resources in test module 281 for (String name : TEST_RESOURCES) { 282 byte[] expectedBytes 283 = Files.readAllBytes(MODS_DIR 284 .resolve(TEST_MODULE) 285 .resolve(name.replace('/', File.separatorChar))); 286 287 testFind(reader, name, expectedBytes); 288 testOpen(reader, name, expectedBytes); 289 testRead(reader, name, expectedBytes); 290 testList(reader, name); 291 } 292 293 // test resources that may be in the test module 294 for (String name : MAYBE_TEST_RESOURCES) { 295 System.out.println(name); 296 Optional<URI> ouri = reader.find(name); 297 ouri.ifPresent(uri -> { 298 if (name.endsWith("/")) 299 assertTrue(uri.toString().endsWith("/")); 300 }); 301 } 302 303 // test "not found" in test module 304 for (String name : NOT_TEST_RESOURCES) { 305 assertFalse(reader.find(name).isPresent()); 306 assertFalse(reader.open(name).isPresent()); 307 assertFalse(reader.read(name).isPresent()); 308 } 309 310 // test nulls 311 try { 312 reader.find(null); 313 assertTrue(false); 314 } catch (NullPointerException expected) { } 315 316 try { 317 reader.open(null); 318 assertTrue(false); 319 } catch (NullPointerException expected) { } 320 321 try { 322 reader.read(null); 323 assertTrue(false); 324 } catch (NullPointerException expected) { } 403 } finally { 404 reader.release(bb); 405 } 406 } 407 408 /** 409 * Test ModuleReader#list 410 */ 411 void testList(ModuleReader reader, String name) throws IOException { 412 List<String> list = reader.list().collect(Collectors.toList()); 413 Set<String> names = new HashSet<>(list); 414 assertTrue(names.size() == list.size()); // no duplicates 415 416 assertTrue(names.contains("module-info.class")); 417 assertTrue(names.contains(name)); 418 419 // all resources should be locatable via find 420 for (String e : names) { 421 assertTrue(reader.find(e).isPresent()); 422 } 423 } 424 425 } |