52 private static final String JAVA_HOME = System.getProperty("java.home"); 53 54 private static final Path TEST_SRC = Paths.get(System.getProperty("test.src")); 55 private static final Path PATCH_SRC_DIR = TEST_SRC.resolve("src1"); 56 57 private static final Path JMODS = Paths.get(JAVA_HOME, "jmods"); 58 private static final Path MODS_DIR = Paths.get("mods"); 59 private static final Path JARS_DIR = Paths.get("jars"); 60 private static final Path PATCH_DIR = Paths.get("patches"); 61 private static final Path IMAGE = Paths.get("image"); 62 63 private static final String JAVA_BASE = "java.base"; 64 private final String[] modules = new String[] { "m1", "m2" }; 65 66 @BeforeTest 67 private void setup() throws Throwable { 68 Path src = TEST_SRC.resolve("src"); 69 for (String name : modules) { 70 assertTrue(CompilerUtils.compile(src.resolve(name), 71 MODS_DIR, 72 "-modulesourcepath", src.toString())); 73 } 74 75 // compile patched source 76 assertTrue(CompilerUtils.compile(PATCH_SRC_DIR.resolve(JAVA_BASE), 77 PATCH_DIR.resolve(JAVA_BASE), 78 "-Xmodule:java.base")); 79 assertTrue(CompilerUtils.compile(PATCH_SRC_DIR.resolve("m2"), 80 PATCH_DIR.resolve("m2"))); 81 82 // create an image with only m1 and m2 83 if (Files.exists(JMODS)) { 84 // create an image with m1,m2 85 createImage(); 86 } 87 } 88 89 @Test 90 public void test() throws Throwable { 91 Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE); 92 Path patchedM2 = PATCH_DIR.resolve("m2"); 93 94 Path home = Paths.get(JAVA_HOME); 95 runTest(home, 96 "-mp", MODS_DIR.toString(), 97 "-m", "m1/p1.Main", "1"); 98 runTest(home, 99 "-Xpatch:java.base=" + patchedJavaBase.toString(), 100 "-mp", MODS_DIR.toString(), 101 "-m", "m1/p1.Main", "1"); 102 103 runTest(home, 104 "-Xpatch:m2=" + patchedM2.toString(), 105 "-mp", MODS_DIR.toString(), 106 "-m", "m1/p1.Main", "2"); 107 } 108 109 @Test 110 public void testImage() throws Throwable { 111 if (Files.notExists(JMODS)) 112 return; 113 114 Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE); 115 Path patchedM2 = PATCH_DIR.resolve("m2"); 116 117 runTest(IMAGE, 118 "-m", "m1/p1.Main", "1"); 119 runTest(IMAGE, 120 "-Xpatch:java.base=" + patchedJavaBase.toString(), 121 "-m", "m1/p1.Main", "1"); 122 runTest(IMAGE, 123 "-Xpatch:m2=" + patchedM2.toString(), 124 "-m", "m1/p1.Main", "2"); 125 } 126 127 @Test 128 public void upgradeTiedModule() throws Throwable { 129 if (Files.notExists(JMODS)) 130 return; 131 132 Path m1 = MODS_DIR.resolve("m1.jar"); 133 134 // create another m1.jar 135 jar("--create", 136 "--file=" + m1.toString(), 137 "-C", MODS_DIR.resolve("m1").toString(), "."); 138 139 // Fail to upgrade m1.jar with mismatched hash 140 runTestWithExitCode(getJava(IMAGE), 141 "-upgrademodulepath", m1.toString(), 142 "-m", "m1/p1.Main"); 143 144 runTestWithExitCode(getJava(IMAGE), 145 "-Xpatch:java.base=" + PATCH_DIR.resolve(JAVA_BASE).toString(), 146 "-upgrademodulepath", m1.toString(), 147 "-m", "m1/p1.Main", "1"); 148 } 149 150 private void runTestWithExitCode(String... options) throws Throwable { 151 assertTrue(executeCommand(options) 152 .outputTo(System.out) 153 .errorTo(System.out) 154 .shouldContain("differs to expected hash") 155 .getExitValue() != 0); 156 } 157 158 private void runTest(Path image, String... opts) throws Throwable { 159 String[] options = 160 Stream.concat(Stream.of(getJava(image)), 161 Stream.of(opts)) 162 .toArray(String[]::new); 163 164 ProcessBuilder pb = new ProcessBuilder(options); 165 int exitValue = executeCommand(pb) 166 .outputTo(System.out) 168 .getExitValue(); 169 170 assertTrue(exitValue == 0); 171 } 172 173 static void createImage() throws Throwable { 174 FileUtils.deleteFileTreeUnchecked(JARS_DIR); 175 FileUtils.deleteFileTreeUnchecked(IMAGE); 176 177 Files.createDirectories(JARS_DIR); 178 Path m1 = JARS_DIR.resolve("m1.jar"); 179 Path m2 = JARS_DIR.resolve("m2.jar"); 180 181 // hash m1 in m2's Hashes attribute 182 jar("--create", 183 "--file=" + m1.toString(), 184 "-C", MODS_DIR.resolve("m1").toString(), "."); 185 186 jar("--create", 187 "--file=" + m2.toString(), 188 "--modulepath", JARS_DIR.toString(), 189 "--hash-modules", "m1", 190 "-C", MODS_DIR.resolve("m2").toString(), "."); 191 192 193 String mpath = JARS_DIR.toString() + File.pathSeparator + JMODS.toString(); 194 execTool("jlink", "--modulepath", mpath, 195 "--addmods", "m1", 196 "--output", IMAGE.toString()); 197 } 198 199 static void jar(String... args) throws Throwable { 200 execTool("jar", args); 201 } 202 203 static void execTool(String tool, String... args) throws Throwable { 204 String path = JDKToolFinder.getJDKTool(tool); 205 List<String> commands = new ArrayList<>(); 206 commands.add(path); 207 Stream.of(args).forEach(commands::add); 208 ProcessBuilder pb = new ProcessBuilder(commands); 209 int exitValue = executeCommand(pb) 210 .outputTo(System.out) 211 .errorTo(System.out) 212 .shouldNotContain("no module is recorded in hash") 213 .getExitValue(); 214 215 assertTrue(exitValue == 0); 216 } 217 218 static String getJava(Path image) { 219 boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("win"); 220 Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java"); 221 if (Files.notExists(java)) 222 throw new RuntimeException(java + " not found"); 223 return java.toAbsolutePath().toString(); 224 } 225 } | 52 private static final String JAVA_HOME = System.getProperty("java.home"); 53 54 private static final Path TEST_SRC = Paths.get(System.getProperty("test.src")); 55 private static final Path PATCH_SRC_DIR = TEST_SRC.resolve("src1"); 56 57 private static final Path JMODS = Paths.get(JAVA_HOME, "jmods"); 58 private static final Path MODS_DIR = Paths.get("mods"); 59 private static final Path JARS_DIR = Paths.get("jars"); 60 private static final Path PATCH_DIR = Paths.get("patches"); 61 private static final Path IMAGE = Paths.get("image"); 62 63 private static final String JAVA_BASE = "java.base"; 64 private final String[] modules = new String[] { "m1", "m2" }; 65 66 @BeforeTest 67 private void setup() throws Throwable { 68 Path src = TEST_SRC.resolve("src"); 69 for (String name : modules) { 70 assertTrue(CompilerUtils.compile(src.resolve(name), 71 MODS_DIR, 72 "--module-source-path", src.toString())); 73 } 74 75 // compile patched source 76 assertTrue(CompilerUtils.compile(PATCH_SRC_DIR.resolve(JAVA_BASE), 77 PATCH_DIR.resolve(JAVA_BASE), 78 "-Xmodule:java.base")); 79 assertTrue(CompilerUtils.compile(PATCH_SRC_DIR.resolve("m2"), 80 PATCH_DIR.resolve("m2"))); 81 82 // create an image with only m1 and m2 83 if (Files.exists(JMODS)) { 84 // create an image with m1,m2 85 createImage(); 86 } 87 } 88 89 @Test 90 public void test() throws Throwable { 91 Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE); 92 Path patchedM2 = PATCH_DIR.resolve("m2"); 93 94 Path home = Paths.get(JAVA_HOME); 95 runTest(home, 96 "--module-path", MODS_DIR.toString(), 97 "-m", "m1/p1.Main", "1"); 98 runTest(home, 99 "--patch-module", "java.base=" + patchedJavaBase, 100 "--module-path", MODS_DIR.toString(), 101 "-m", "m1/p1.Main", "1"); 102 103 runTest(home, 104 "--patch-module", "m2=" + patchedM2.toString(), 105 "--module-path", MODS_DIR.toString(), 106 "-m", "m1/p1.Main", "2"); 107 } 108 109 @Test 110 public void testImage() throws Throwable { 111 if (Files.notExists(JMODS)) 112 return; 113 114 Path patchedJavaBase = PATCH_DIR.resolve(JAVA_BASE); 115 Path patchedM2 = PATCH_DIR.resolve("m2"); 116 117 runTest(IMAGE, 118 "-m", "m1/p1.Main", "1"); 119 runTest(IMAGE, 120 "--patch-module", "java.base=" + patchedJavaBase, 121 "-m", "m1/p1.Main", "1"); 122 runTest(IMAGE, 123 "--patch-module", "m2=" + patchedM2.toString(), 124 "-m", "m1/p1.Main", "2"); 125 } 126 127 @Test 128 public void upgradeTiedModule() throws Throwable { 129 if (Files.notExists(JMODS)) 130 return; 131 132 Path m1 = MODS_DIR.resolve("m1.jar"); 133 134 // create another m1.jar 135 jar("--create", 136 "--file=" + m1.toString(), 137 "-C", MODS_DIR.resolve("m1").toString(), "."); 138 139 // Fail to upgrade m1.jar with mismatched hash 140 runTestWithExitCode(getJava(IMAGE), 141 "--upgrade-module-path", m1.toString(), 142 "-m", "m1/p1.Main"); 143 144 runTestWithExitCode(getJava(IMAGE), 145 "--patch-module", "java.base=" + PATCH_DIR.resolve(JAVA_BASE), 146 "--upgrade-module-path", m1.toString(), 147 "-m", "m1/p1.Main", "1"); 148 } 149 150 private void runTestWithExitCode(String... options) throws Throwable { 151 assertTrue(executeCommand(options) 152 .outputTo(System.out) 153 .errorTo(System.out) 154 .shouldContain("differs to expected hash") 155 .getExitValue() != 0); 156 } 157 158 private void runTest(Path image, String... opts) throws Throwable { 159 String[] options = 160 Stream.concat(Stream.of(getJava(image)), 161 Stream.of(opts)) 162 .toArray(String[]::new); 163 164 ProcessBuilder pb = new ProcessBuilder(options); 165 int exitValue = executeCommand(pb) 166 .outputTo(System.out) 168 .getExitValue(); 169 170 assertTrue(exitValue == 0); 171 } 172 173 static void createImage() throws Throwable { 174 FileUtils.deleteFileTreeUnchecked(JARS_DIR); 175 FileUtils.deleteFileTreeUnchecked(IMAGE); 176 177 Files.createDirectories(JARS_DIR); 178 Path m1 = JARS_DIR.resolve("m1.jar"); 179 Path m2 = JARS_DIR.resolve("m2.jar"); 180 181 // hash m1 in m2's Hashes attribute 182 jar("--create", 183 "--file=" + m1.toString(), 184 "-C", MODS_DIR.resolve("m1").toString(), "."); 185 186 jar("--create", 187 "--file=" + m2.toString(), 188 "--module-path", JARS_DIR.toString(), 189 "--hash-modules", "m1", 190 "-C", MODS_DIR.resolve("m2").toString(), "."); 191 192 193 String mpath = JARS_DIR.toString() + File.pathSeparator + JMODS.toString(); 194 execTool("jlink", "--module-path", mpath, 195 "--add-modules", "m1", 196 "--output", IMAGE.toString()); 197 } 198 199 static void jar(String... args) throws Throwable { 200 execTool("jar", args); 201 } 202 203 static void execTool(String tool, String... args) throws Throwable { 204 String path = JDKToolFinder.getJDKTool(tool); 205 List<String> commands = new ArrayList<>(); 206 commands.add(path); 207 Stream.of(args).forEach(commands::add); 208 ProcessBuilder pb = new ProcessBuilder(commands); 209 int exitValue = executeCommand(pb) 210 .outputTo(System.out) 211 .errorTo(System.out) 212 .shouldNotContain("no module is recorded in hash") 213 .getExitValue(); 214 215 assertTrue(exitValue == 0); 216 } 217 218 static String getJava(Path image) { 219 boolean isWindows = System.getProperty("os.name").startsWith("Windows"); 220 Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java"); 221 if (Files.notExists(java)) 222 throw new RuntimeException(java + " not found"); 223 return java.toAbsolutePath().toString(); 224 } 225 } |