1 /* 2 * Copyright (c) 2015, 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 * @summary Basic test of package-info in named module and duplicate 27 * package-info in unnamed module 28 * @modules java.compiler 29 * java.desktop 30 * java.management 31 * jdk.attach 32 * jdk.jdi 33 * jdk.xml.dom 34 * @compile package-info.java PackageInfoTest.java 35 * @run testng p.PackageInfoTest 36 */ 37 38 package p; 39 40 import java.io.File; 41 import java.io.IOException; 42 import java.io.UncheckedIOException; 43 import java.lang.annotation.Annotation; 44 import java.net.URL; 45 import java.net.URLClassLoader; 46 import java.nio.file.Files; 47 import java.nio.file.Path; 48 import java.nio.file.Paths; 49 import java.nio.file.attribute.BasicFileAttributes; 50 import java.util.ArrayList; 51 import java.util.Arrays; 52 import java.util.List; 53 54 import org.testng.annotations.DataProvider; 55 import org.testng.annotations.Test; 56 57 import javax.tools.JavaCompiler; 58 import javax.tools.JavaFileObject; 59 import javax.tools.StandardJavaFileManager; 60 import javax.tools.ToolProvider; 61 62 import static org.testng.Assert.*; 63 64 public class PackageInfoTest { 65 @DataProvider(name = "jdkClasses") 66 public Object[][] jdkClasses() { 67 return new Object[][] { 68 { java.awt.Button.class, null }, 69 { java.lang.Object.class, null }, 70 { java.lang.management.ManagementFactory.class, null }, 71 { com.sun.tools.attach.VirtualMachine.class, jdk.Exported.class }, 72 { com.sun.jdi.Accessible.class, jdk.Exported.class }, 73 { org.w3c.dom.css.CSSRule.class, null }, 74 }; 75 } 76 77 @Test(dataProvider = "jdkClasses") 78 public void testPackageInfo(Class<?> type, Class<? extends Annotation> annType) { 79 Package pkg = type.getPackage(); 80 assertTrue(pkg.isSealed()); 81 assertTrue(annType == null || pkg.getDeclaredAnnotations().length != 0); 82 if (annType != null) { 83 assertTrue(pkg.isAnnotationPresent(annType)); 84 } 85 } 86 87 private Class<?> loadClass(String name) throws ClassNotFoundException { 88 ClassLoader loader = PackageInfoTest.class.getClassLoader(); 89 Class<?> c = Class.forName(name, true, loader); 90 assertFalse(c.getModule().isNamed()); 91 return c; 92 } 93 94 @DataProvider(name = "classpathClasses") 95 public Object[][] cpClasses() throws IOException, ClassNotFoundException { 96 Path classes = Paths.get(System.getProperty("test.classes", ".")); 97 Path src = Paths.get(System.getProperty("test.src", "."), "org/w3c/dom/css"); 98 99 // compile with -Xmodule:jdk.xml.dom since it's a package in a named module 100 compile("-Xmodule:jdk.xml.dom", 101 classes, 102 src.resolve("Fake.java"), 103 src.resolve("FakePackage.java"), 104 src.resolve("package-info.java")); 105 106 // these classes will be loaded from classpath in unnamed module 107 return new Object[][] { 108 { p.PackageInfoTest.class, Deprecated.class }, 109 { loadClass("org.w3c.dom.css.Fake"), 110 loadClass("org.w3c.dom.css.FakePackage") }, }; 111 } 112 113 @Test(dataProvider = "classpathClasses") 114 public void testClassPathPackage(Class<?> type, Class<? extends Annotation> annType) { 115 Package pkg = type.getPackage(); 116 assertTrue(pkg.isSealed() == false); 117 assertTrue(pkg.isAnnotationPresent(annType)); 118 } 119 120 static final String[] otherClasses = new String[] { 121 "p/package-info.class", 122 "p/Duplicate.class", 123 "p/Bar.class" 124 }; 125 126 @Test 127 public void testDuplicatePackage() throws Exception { 128 // a custom class loader loading another package p annotated with @Duplicate 129 Path classes = Paths.get(System.getProperty("test.classes", "."), "tmp"); 130 Files.createDirectories(classes); 131 URLClassLoader loader = new URLClassLoader(new URL[] { classes.toUri().toURL() }); 132 133 // clean up before compiling classes 134 Arrays.stream(otherClasses) 135 .forEach(c -> { 136 try { 137 Files.deleteIfExists(classes.resolve(c)); 138 } catch (IOException e) { 139 throw new UncheckedIOException(e); 140 } 141 }); 142 143 Path src = Paths.get(System.getProperty("test.src", "."), "src", "p"); 144 compile(classes, 145 src.resolve("package-info.java"), 146 src.resolve("Duplicate.java"), 147 src.resolve("Bar.java")); 148 149 // verify if classes are present 150 Arrays.stream(otherClasses) 151 .forEach(c -> { 152 if (Files.notExists(classes.resolve(c))) { 153 throw new RuntimeException(c + " not exist"); 154 } 155 }); 156 157 Class<?> c = Class.forName("p.Bar", true, loader); 158 assertTrue(c.getClassLoader() == loader); 159 assertTrue(this.getClass().getClassLoader() != loader); 160 161 // package p defined by the custom class loader 162 Package pkg = c.getPackage(); 163 assertTrue(pkg.getName().equals("p")); 164 assertTrue(pkg.isSealed() == false); 165 166 // package p defined by the application class loader 167 Package p = this.getClass().getPackage(); 168 assertTrue(p.getName().equals("p")); 169 assertTrue(p != pkg); 170 171 Arrays.stream(pkg.getDeclaredAnnotations()) 172 .forEach(ann -> System.out.format("%s @%s%n", pkg.getName(), ann)); 173 174 Arrays.stream(p.getDeclaredAnnotations()) 175 .forEach(ann -> System.out.format("%s @%s%n", p.getName(), ann)); 176 177 // local package p defined by loader 178 Class<? extends Annotation> ann = 179 (Class<? extends Annotation>)Class.forName("p.Duplicate", false, loader); 180 assertTrue(pkg.isAnnotationPresent(ann)); 181 } 182 183 private void compile(Path destDir, Path... files) throws IOException { 184 compile(null, destDir, files); 185 } 186 187 private void compile(String option, Path destDir, Path... files) 188 throws IOException 189 { 190 System.err.println("compile..."); 191 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 192 try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { 193 Iterable<? extends JavaFileObject> fileObjects = 194 fm.getJavaFileObjectsFromPaths(Arrays.asList(files)); 195 196 List<String> options = new ArrayList<>(); 197 if (option != null) { 198 options.add(option); 199 } 200 if (destDir != null) { 201 options.add("-d"); 202 options.add(destDir.toString()); 203 } 204 options.add("-cp"); 205 options.add(System.getProperty("test.classes", ".")); 206 207 JavaCompiler.CompilationTask task = 208 compiler.getTask(null, fm, null, options, null, fileObjects); 209 if (!task.call()) 210 throw new AssertionError("compilation failed"); 211 } 212 } 213 214 }