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 // resources in test module (can't use module-info.class as a test 83 // resource as it will be modified by the jmod tool) 84 private static final String[] TEST_RESOURCES = { 85 "p/Main.class" 86 }; 87 88 // a resource that is not in the base or test module 89 private static final String NOT_A_RESOURCE = "NotAResource"; 90 91 92 @BeforeTest 93 public void compileTestModule() throws Exception { 94 95 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 96 boolean compiled 97 = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 98 MODS_DIR.resolve(TEST_MODULE)); 99 assertTrue(compiled, "test module did not compile"); 100 } 101 102 103 /** 104 * Test ModuleReader to module in runtime image 105 */ 106 public void testImage() throws IOException { 107 108 ModuleFinder finder = ModuleFinder.ofSystem(); 109 ModuleReference mref = finder.find(BASE_MODULE).get(); 110 ModuleReader reader = mref.open(); 111 112 try (reader) { 113 114 for (String name : BASE_RESOURCES) { 115 byte[] expectedBytes; 116 Module baseModule = Object.class.getModule(); 117 try (InputStream in = baseModule.getResourceAsStream(name)) { 118 expectedBytes = in.readAllBytes(); 119 } 120 121 testFind(reader, name, expectedBytes); 122 testOpen(reader, name, expectedBytes); 123 testRead(reader, name, expectedBytes); 124 testList(reader, name); 125 126 } 127 128 // test "not found" 129 assertFalse(reader.find(NOT_A_RESOURCE).isPresent()); 130 assertFalse(reader.open(NOT_A_RESOURCE).isPresent()); 131 assertFalse(reader.read(NOT_A_RESOURCE).isPresent()); 132 133 134 // test nulls 135 try { 136 reader.find(null); 137 assertTrue(false); 138 } catch (NullPointerException expected) { } 139 140 try { 141 reader.open(null); 142 assertTrue(false); 143 } catch (NullPointerException expected) { } 144 145 try { 146 reader.read(null); 147 assertTrue(false); 148 } catch (NullPointerException expected) { } 149 150 try { 151 reader.release(null); 152 assertTrue(false); 219 ModuleFinder finder = new ModulePath(Runtime.version(), true, mp); 220 ModuleReference mref = finder.find(TEST_MODULE).get(); 221 ModuleReader reader = mref.open(); 222 223 try (reader) { 224 225 // test each of the known resources in the module 226 for (String name : TEST_RESOURCES) { 227 byte[] expectedBytes 228 = Files.readAllBytes(MODS_DIR 229 .resolve(TEST_MODULE) 230 .resolve(name.replace('/', File.separatorChar))); 231 232 testFind(reader, name, expectedBytes); 233 testOpen(reader, name, expectedBytes); 234 testRead(reader, name, expectedBytes); 235 testList(reader, name); 236 } 237 238 // test "not found" 239 assertFalse(reader.find(NOT_A_RESOURCE).isPresent()); 240 assertFalse(reader.open(NOT_A_RESOURCE).isPresent()); 241 assertFalse(reader.read(NOT_A_RESOURCE).isPresent()); 242 243 // test nulls 244 try { 245 reader.find(null); 246 assertTrue(false); 247 } catch (NullPointerException expected) { } 248 249 try { 250 reader.open(null); 251 assertTrue(false); 252 } catch (NullPointerException expected) { } 253 254 try { 255 reader.read(null); 256 assertTrue(false); 257 } catch (NullPointerException expected) { } 258 259 try { 260 reader.release(null); 261 throw new RuntimeException(); | 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 } 139 140 141 /** 142 * Test ModuleReader to module in runtime image 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) { } 188 189 try { 190 reader.release(null); 191 assertTrue(false); 258 ModuleFinder finder = new ModulePath(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) { } 299 300 try { 301 reader.release(null); 302 throw new RuntimeException(); |