1/* 2 * Copyright (c) 2016, 2017, 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 * @library src 27 * @build m/* Basic 28 * @run testng/othervm Basic 29 * @summary Basic test for annotations on modules 30 */ 31 32import java.lang.annotation.Annotation; 33 34import p.annotation.Foo; 35import p.annotation.Bar; 36import p.annotation.Baz; 37 38import org.testng.annotations.Test; 39import static org.testng.Assert.*; 40 41public class Basic { 42 43 final Module module = Foo.class.getModule(); 44 45 /** 46 * {@code @Foo} does not have RUNTIME retention policy. 47 */ 48 @Test 49 public void testInvisibleAnnotation() { 50 assertFalse(module.isAnnotationPresent(Foo.class)); 51 assertNull(module.getAnnotation(Foo.class)); 52 } 53 54 /** 55 * {@code @Bar} has RUNTIME retention policy and value "bar" 56 */ 57 @Test 58 public void testBarAnnotation() { 59 assertTrue(module.isAnnotationPresent(Bar.class)); 60 Bar bar = module.getAnnotation(Bar.class); 61 assertNotNull(bar); 62 assertEquals(bar.value(), "bar"); 63 } 64 65 /** 66 * {@code @Baz} has RUNTIME retention policy has a repeating value 67 */ 68 @Test 69 public void testBazAnnotation() { 70 assertTrue(module.isAnnotationPresent(Baz.class)); 71 Baz baz = module.getAnnotation(Baz.class); 72 assertNotNull(baz); 73 String[] expected = { "one", "two", "three" }; 74 assertEquals(baz.value(), expected); 75 } 76 77 /** 78 * Test annotations with RUNTIME retention policy 79 */ 80 @Test 81 public void testRuntimeAnnotations() { 82 Annotation[] a = module.getAnnotations(); 83 assertEquals(a, module.getDeclaredAnnotations()); 84 assertTrue(a.length == 2); 85 Bar bar; 86 Baz baz; 87 if (a[0] instanceof Bar) { 88 bar = (Bar)a[0]; 89 baz = (Baz)a[1]; 90 } else { 91 bar = (Bar)a[1]; 92 baz = (Baz)a[0]; 93 } 94 assertEquals(bar.value(), "bar"); 95 String[] expected = { "one", "two", "three"}; 96 assertEquals(baz.value(), expected); 97 } 98} 99