Path: blob/master/test/jdk/java/lang/Package/annotation/PackageInfoTest.java
41152 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Basic test of package-info in named module and duplicate26* package-info in unnamed module27* @modules java.desktop28* jdk.compiler29* jdk.xml.dom30* @build jdk.xml.dom/org.w3c.dom.css.Fake31* jdk.xml.dom/org.w3c.dom.css.FakePackage32* @compile/module=jdk.xml.dom org/w3c/dom/css/package-info.java33* @compile package-info.java PackageInfoTest.java34* @run testng p.PackageInfoTest35*/3637package p;3839import java.io.IOException;40import java.io.UncheckedIOException;41import java.lang.annotation.Annotation;42import java.net.URL;43import java.net.URLClassLoader;44import java.nio.file.Files;45import java.nio.file.Path;46import java.nio.file.Paths;47import java.util.ArrayList;48import java.util.Arrays;49import java.util.List;5051import org.testng.annotations.DataProvider;52import org.testng.annotations.Test;53import org.w3c.dom.css.CSSRule;5455import javax.tools.JavaCompiler;56import javax.tools.JavaFileObject;57import javax.tools.StandardJavaFileManager;58import javax.tools.ToolProvider;5960import static org.testng.Assert.*;6162public class PackageInfoTest {63@DataProvider(name = "jdkClasses")64public Object[][] jdkClasses() {65return new Object[][] {66{ java.awt.Button.class, null },67{ java.lang.Object.class, null },68{ org.w3c.dom.css.CSSRule.class, null },69{ loadClass("org.w3c.dom.css.Fake"), loadClass("org.w3c.dom.css.FakePackage") },70};71}7273@Test(dataProvider = "jdkClasses")74public void testPackageInfo(Class<?> type, Class<? extends Annotation> annType) {75Package pkg = type.getPackage();76assertTrue(pkg.isSealed());77assertTrue(annType == null || pkg.getDeclaredAnnotations().length != 0);78if (annType != null) {79assertTrue(pkg.isAnnotationPresent(annType));80}81}8283private Class<?> loadClass(String name) {84return Class.forName(CSSRule.class.getModule(), name);85}8687@DataProvider(name = "classpathClasses")88public Object[][] cpClasses() throws IOException, ClassNotFoundException {89// these classes will be loaded from classpath in unnamed module90return new Object[][]{91{ p.PackageInfoTest.class, Deprecated.class }92};93}9495@Test(dataProvider = "classpathClasses")96public void testClassPathPackage(Class<?> type, Class<? extends Annotation> annType) {97Package pkg = type.getPackage();98assertTrue(pkg.isSealed() == false);99assertTrue(pkg.isAnnotationPresent(annType));100}101102static final String[] otherClasses = new String[] {103"p/package-info.class",104"p/Duplicate.class",105"p/Bar.class"106};107108@Test109public void testDuplicatePackage() throws Exception {110// a custom class loader loading another package p annotated with @Duplicate111Path classes = Paths.get(System.getProperty("test.classes", "."), "tmp");112Files.createDirectories(classes);113URLClassLoader loader = new URLClassLoader(new URL[] { classes.toUri().toURL() });114115// clean up before compiling classes116Arrays.stream(otherClasses)117.forEach(c -> {118try {119Files.deleteIfExists(classes.resolve(c));120} catch (IOException e) {121throw new UncheckedIOException(e);122}123});124125Path src = Paths.get(System.getProperty("test.src", "."), "src", "p");126compile(classes,127src.resolve("package-info.java"),128src.resolve("Duplicate.java"),129src.resolve("Bar.java"));130131// verify if classes are present132Arrays.stream(otherClasses)133.forEach(c -> {134if (Files.notExists(classes.resolve(c))) {135throw new RuntimeException(c + " not exist");136}137});138139Class<?> c = Class.forName("p.Bar", true, loader);140assertTrue(c.getClassLoader() == loader);141assertTrue(this.getClass().getClassLoader() != loader);142143// package p defined by the custom class loader144Package pkg = c.getPackage();145assertTrue(pkg.getName().equals("p"));146assertTrue(pkg.isSealed() == false);147148// package p defined by the application class loader149Package p = this.getClass().getPackage();150assertTrue(p.getName().equals("p"));151assertTrue(p != pkg);152153Arrays.stream(pkg.getDeclaredAnnotations())154.forEach(ann -> System.out.format("%s @%s%n", pkg.getName(), ann));155156Arrays.stream(p.getDeclaredAnnotations())157.forEach(ann -> System.out.format("%s @%s%n", p.getName(), ann));158159// local package p defined by loader160Class<? extends Annotation> ann =161(Class<? extends Annotation>)Class.forName("p.Duplicate", false, loader);162assertTrue(pkg.isAnnotationPresent(ann));163}164165private void compile(Path destDir, Path... files) throws IOException {166compile(null, destDir, files);167}168169private void compile(String option, Path destDir, Path... files)170throws IOException171{172System.err.println("compile...");173JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();174try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {175Iterable<? extends JavaFileObject> fileObjects =176fm.getJavaFileObjectsFromPaths(Arrays.asList(files));177178List<String> options = new ArrayList<>();179if (option != null) {180options.add(option);181}182if (destDir != null) {183options.add("-d");184options.add(destDir.toString());185}186options.add("-cp");187options.add(System.getProperty("test.classes", "."));188189JavaCompiler.CompilationTask task =190compiler.getTask(null, fm, null, options, null, fileObjects);191if (!task.call())192throw new AssertionError("compilation failed");193}194}195196}197198199