Path: blob/master/test/jdk/java/lang/ModuleTests/annotation/Basic.java
41153 views
/*1* Copyright (c) 2016, 2017, 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* @library src26* @build m/* Basic27* @run testng/othervm Basic28* @summary Basic test for annotations on modules29*/3031import java.lang.annotation.Annotation;3233import p.annotation.Foo;34import p.annotation.Bar;35import p.annotation.Baz;3637import org.testng.annotations.Test;38import static org.testng.Assert.*;3940public class Basic {4142final Module module = Foo.class.getModule();4344/**45* {@code @Foo} does not have RUNTIME retention policy.46*/47@Test48public void testInvisibleAnnotation() {49assertFalse(module.isAnnotationPresent(Foo.class));50assertNull(module.getAnnotation(Foo.class));51}5253/**54* {@code @Bar} has RUNTIME retention policy and value "bar"55*/56@Test57public void testBarAnnotation() {58assertTrue(module.isAnnotationPresent(Bar.class));59Bar bar = module.getAnnotation(Bar.class);60assertNotNull(bar);61assertEquals(bar.value(), "bar");62}6364/**65* {@code @Baz} has RUNTIME retention policy has a repeating value66*/67@Test68public void testBazAnnotation() {69assertTrue(module.isAnnotationPresent(Baz.class));70Baz baz = module.getAnnotation(Baz.class);71assertNotNull(baz);72String[] expected = { "one", "two", "three" };73assertEquals(baz.value(), expected);74}7576/**77* Test annotations with RUNTIME retention policy78*/79@Test80public void testRuntimeAnnotations() {81Annotation[] a = module.getAnnotations();82assertEquals(a, module.getDeclaredAnnotations());83assertTrue(a.length == 2);84Bar bar;85Baz baz;86if (a[0] instanceof Bar) {87bar = (Bar)a[0];88baz = (Baz)a[1];89} else {90bar = (Bar)a[1];91baz = (Baz)a[0];92}93assertEquals(bar.value(), "bar");94String[] expected = { "one", "two", "three"};95assertEquals(baz.value(), expected);96}97}9899100