1 /* 2 * Copyright (c) 2018, 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 /* 25 * @test 26 * @bug 8201528 27 * @summary The test will create JAR file(s) with the manifest file 28 * that customized package versioning information (different info for 29 * same package if multiple jars). Then verify package versioning info 30 * @library /lib/testlibrary 31 * @library /test/lib 32 * @modules jdk.compiler 33 * @run main PackageFromManifest setup test 34 * @run main PackageFromManifest runJar test1.jar 35 * @run main PackageFromManifest runJar test1.jar test2.jar foo.Foo1 36 * @run main PackageFromManifest runJar test1.jar test2.jar foo.Foo2 37 * @run main/othervm PackageFromManifest runUrlLoader test1.jar 38 * @run main/othervm PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo1 39 * @run main/othervm PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo2 40 */ 41 42 import jdk.test.lib.compiler.CompilerUtils; 43 import jdk.test.lib.process.ProcessTools; 44 import jdk.test.lib.util.FileUtils; 45 46 import java.io.File; 47 import java.io.IOException; 48 import java.io.InputStream; 49 import java.net.MalformedURLException; 50 import java.net.URL; 51 import java.net.URLClassLoader; 52 import java.nio.file.Files; 53 import java.nio.file.Path; 54 import java.nio.file.Paths; 55 import java.util.ArrayList; 56 import java.util.Arrays; 57 import java.util.List; 58 import java.util.jar.Manifest; 59 import java.util.stream.Collectors; 60 import java.util.stream.Stream; 61 62 /** 63 * This test accept at least two input parameters, first one is run type like 64 * 'setup', 'runJar', 'runTest', 'runUrlLoader', the rest parameters are options 65 * to each run type. 'setup' run type should be placed at first since it will 66 * create necessary jars for the rest tests. 'runTest' will be called in test 67 * logic only, it should not be used in @run 68 * 69 * #1 test will do setup only to generate required jars before other tests run 70 * PackageFromManifest setup test 71 * 72 * #2 test will run against single jar file to verify package versioning 73 * PackageFromManifest runJar test1.jar 74 * 75 * #4 test will run against two jar files, load class foo.Foo1 first, then 76 * verify package versioning 77 * PackageFromManifest runJar test1.jar test2.jar foo.Foo1 78 * 79 * #5 test will run against two jar files, load class foo.Foo2 first, then 80 * verify package versioning 81 * PackageFromManifest runJar test1.jar test2.jar foo.Foo2 82 * 83 * #3 test will use URLCLassLoader to load single jar file, then verify 84 * package versioning 85 * PackageFromManifest runUrlLoader test1.jar 86 * 87 * #6 test will use URLCLassLoader to load two jars, load class foo.Foo1 first, 88 * then verify package versioning 89 * PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo1 90 * 91 * #7 test will use URLCLassLoader to load two jars, load class foo.Foo2 first, 92 * then verify package versioning 93 * PackageFromManifest runUrlLoader test1.jar test2.jar foo.Foo2 94 */ 95 public class PackageFromManifest { 96 97 private static final String PACKAGE_NAME = "foo"; 98 private static final String TEST_JAR_FILE1 = "test1.jar"; 99 private static final String TEST_JAR_FILE2 = "test2.jar"; 100 private static final String TEST_SUFFIX1 = "1"; 101 private static final String TEST_SUFFIX2 = "2"; 102 private static final String TEST_CLASS_PREFIX = "Foo"; 103 private static final String TEST_CLASS_NAME1 = 104 TEST_CLASS_PREFIX + TEST_SUFFIX1; 105 private static final String TEST_CLASS_NAME2 = 106 TEST_CLASS_PREFIX + TEST_SUFFIX2; 107 private static final String MANIFEST_FILE = "test.mf"; 108 private static final String SPEC_TITLE = "testSpecTitle"; 109 private static final String SPEC_VENDOR = "testSpecVendor"; 110 private static final String IMPL_TITLE = "testImplTitle"; 111 private static final String IMPL_VENDOR = "testImplVendor"; 112 private static final Path WORKING_PATH = Paths.get("."); 113 114 public static void main(String[] args) throws Exception { 115 if (args != null && args.length > 1) { 116 String runType = args[0]; 117 String[] options = Arrays.copyOfRange(args, 1, args.length); 118 switch (runType) { 119 case "setup": 120 setup(); 121 break; 122 case "runTest": 123 runTest(options); 124 break; 125 case "runJar": 126 runJar(options); 127 break; 128 case "runUrlLoader": 129 testUrlLoader(options); 130 break; 131 default: 132 throw new RuntimeException("Invalid run type : " + runType); 133 } 134 } else { 135 throw new RuntimeException("Invalid input arguments"); 136 } 137 } 138 139 private static void createTestClass(String name) throws IOException { 140 List<String> content = new ArrayList<>(); 141 content.add("package " + PACKAGE_NAME + ";"); 142 content.add("public class " + name + " {"); 143 content.add("}"); 144 145 Path javaFile = WORKING_PATH.resolve(name + ".java"); 146 147 Files.write(javaFile, content); 148 149 CompilerUtils.compile(WORKING_PATH, WORKING_PATH); 150 151 // clean up created java file 152 Files.delete(javaFile); 153 } 154 155 private static void createManifest(String suffix) throws IOException { 156 List<String> content = new ArrayList<>(); 157 content.add("Manifest-version: 1.1"); 158 content.add("Name: " + PACKAGE_NAME + "/"); 159 content.add("Specification-Title: " + SPEC_TITLE + suffix); 160 content.add("Specification-Version: " + suffix); 161 content.add("Specification-Vendor: " + SPEC_VENDOR + suffix); 162 content.add("Implementation-Title: " + IMPL_TITLE + suffix); 163 content.add("Implementation-Version: " + suffix); 164 content.add("Implementation-Vendor: " + IMPL_VENDOR + suffix); 165 166 Files.write(WORKING_PATH.resolve(MANIFEST_FILE), content); 167 } 168 169 private static void buildJar(String jarFileName, boolean isIncludeSelf) 170 throws IOException { 171 try (InputStream is = Files.newInputStream(Paths.get(MANIFEST_FILE))) { 172 if (isIncludeSelf) { 173 Path selfPath = WORKING_PATH 174 .resolve("PackageFromManifest.class"); 175 if (!Files.exists(selfPath)) { 176 Files.copy(Paths.get(System.getProperty("test.classes")) 177 .resolve("PackageFromManifest.class"), selfPath); 178 } 179 JarUtils.createJarFile(Paths.get(jarFileName), new Manifest(is), 180 WORKING_PATH, selfPath, 181 WORKING_PATH.resolve(PACKAGE_NAME)); 182 } else { 183 JarUtils.createJarFile(Paths.get(jarFileName), new Manifest(is), 184 WORKING_PATH, WORKING_PATH.resolve(PACKAGE_NAME)); 185 } 186 } 187 188 // clean up build files 189 FileUtils.deleteFileTreeWithRetry(WORKING_PATH.resolve(PACKAGE_NAME)); 190 Files.delete(WORKING_PATH.resolve(MANIFEST_FILE)); 191 } 192 193 private static void runJar(String[] options) throws Exception { 194 String[] cmds; 195 String classPath = Stream.of(options).takeWhile(s -> s.endsWith(".jar")) 196 .collect(Collectors.joining(File.pathSeparator)); 197 if (options.length == 1) { 198 cmds = new String[] { "-cp", classPath, "PackageFromManifest", 199 "runTest", "single" }; 200 } else { 201 cmds = new String[] { "-cp", classPath, "PackageFromManifest", 202 "runTest", options[options.length - 1] }; 203 } 204 205 ProcessTools.executeTestJava(cmds).outputTo(System.out) 206 .errorTo(System.err).shouldHaveExitValue(0); 207 } 208 209 private static void runTest(String[] options) 210 throws ClassNotFoundException { 211 String option = options[0]; 212 if (option.equalsIgnoreCase("single")) { 213 runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1) 214 .getPackage(), TEST_SUFFIX1); 215 } else { 216 // Load one specified class first 217 System.out.println("Load " + Class.forName(option) + " first"); 218 219 String suffix = option.endsWith(TEST_SUFFIX1) ? 220 TEST_SUFFIX1 : 221 TEST_SUFFIX2; 222 223 runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1) 224 .getPackage(), suffix); 225 runTest(Class.forName(PACKAGE_NAME + "." + TEST_CLASS_NAME2) 226 .getPackage(), suffix); 227 } 228 } 229 230 private static void runTest(Package testPackage, String suffix) { 231 checkValue("Package Name", PACKAGE_NAME, testPackage.getName()); 232 checkValue("Spec Title", SPEC_TITLE + suffix, 233 testPackage.getSpecificationTitle()); 234 checkValue("Spec Vendor", SPEC_VENDOR + suffix, 235 testPackage.getSpecificationVendor()); 236 checkValue("Spec Version", suffix, 237 testPackage.getSpecificationVersion()); 238 checkValue("Impl Title", IMPL_TITLE + suffix, 239 testPackage.getImplementationTitle()); 240 checkValue("Impl Vendor", IMPL_VENDOR + suffix, 241 testPackage.getImplementationVendor()); 242 checkValue("Impl Version", suffix, 243 testPackage.getImplementationVersion()); 244 } 245 246 private static void checkValue(String name, String expect, String actual) { 247 if (!expect.equals(actual)) { 248 throw new RuntimeException( 249 "Failed, unexpected value for " + name + ", expect: " 250 + expect + ", actual: " + actual); 251 } else { 252 System.out.println(name + " : " + actual); 253 } 254 } 255 256 private static void setup() throws IOException { 257 if (!Files.exists(WORKING_PATH.resolve(TEST_JAR_FILE1))) { 258 createTestClass(TEST_CLASS_NAME1); 259 createManifest(TEST_SUFFIX1); 260 buildJar(TEST_JAR_FILE1, true); 261 } 262 263 if (!Files.exists(WORKING_PATH.resolve(TEST_JAR_FILE2))) { 264 createTestClass(TEST_CLASS_NAME2); 265 createManifest(TEST_SUFFIX2); 266 buildJar(TEST_JAR_FILE2, false); 267 } 268 } 269 270 private static void testUrlLoader(String[] options) 271 throws ClassNotFoundException { 272 URLClassLoader cl = new URLClassLoader( 273 Stream.of(options).takeWhile(s -> s.endsWith(".jar")).map(s -> { 274 try { 275 return WORKING_PATH.resolve(s).toUri().toURL(); 276 } catch (MalformedURLException e) { 277 return null; 278 } 279 }).toArray(URL[]::new)); 280 if (options.length == 1) { 281 runTest(Class 282 .forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1, true, cl) 283 .getPackage(), TEST_SUFFIX1); 284 } else { 285 // Load one specified class first 286 System.out.println("Load " + Class 287 .forName(options[options.length - 1], true, cl) + " first"); 288 289 String suffix = options[options.length - 1].endsWith(TEST_SUFFIX1) ? 290 TEST_SUFFIX1 : 291 TEST_SUFFIX2; 292 293 runTest(Class 294 .forName(PACKAGE_NAME + "." + TEST_CLASS_NAME1, true, cl) 295 .getPackage(), suffix); 296 runTest(Class 297 .forName(PACKAGE_NAME + "." + TEST_CLASS_NAME2, true, cl) 298 .getPackage(), suffix); 299 } 300 } 301 }