1 /* 2 * Copyright (c) 2013, 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 8023651 27 * @summary Test that the receiver annotations and the return annotations of 28 * constructors behave correctly. 29 * @run testng ConstructorReceiverTest 30 */ 31 32 import java.lang.annotation.*; 33 import java.lang.reflect.*; 34 import java.util.Arrays; 35 import org.testng.annotations.DataProvider; 36 import org.testng.annotations.Test; 37 38 import static org.testng.Assert.*; 39 40 public class ConstructorReceiverTest { 41 // Format is { 42 // { Class to get ctor for, 43 // ctor param class, 44 // value of anno of return type, 45 // value of anno for receiver or null if there should be no receiver anno 46 // }, 47 // ... 48 // } 49 public static final Object[][] TESTS = { 50 { ConstructorReceiverTest.class, null, Integer.valueOf(5), null }, 51 { ConstructorReceiverTest.Middle.class, ConstructorReceiverTest.class, Integer.valueOf(10), Integer.valueOf(15) }, 52 { ConstructorReceiverTest.Middle.Inner.class, ConstructorReceiverTest.Middle.class, Integer.valueOf(100), Integer.valueOf(150) }, 53 { ConstructorReceiverTest.Middle.Inner.Innermost.class, ConstructorReceiverTest.Middle.Inner.class, Integer.valueOf(1000), Integer.valueOf(1500) }, 54 { ConstructorReceiverTest.Middle.InnerNoReceiver.class, ConstructorReceiverTest.Middle.class, Integer.valueOf(300), null }, 55 { ConstructorReceiverTest.Nested.class, null, Integer.valueOf(20), null }, 56 { ConstructorReceiverTest.Nested.NestedMiddle.class, ConstructorReceiverTest.Nested.class, Integer.valueOf(200), Integer.valueOf(250)}, 57 { ConstructorReceiverTest.Nested.NestedMiddle.NestedInner.class, ConstructorReceiverTest.Nested.NestedMiddle.class, Integer.valueOf(2000), Integer.valueOf(2500)}, 58 { ConstructorReceiverTest.Nested.NestedMiddle.NestedInnerNoReceiver.class, ConstructorReceiverTest.Nested.NestedMiddle.class, Integer.valueOf(4000), null}, 59 }; 60 61 @DataProvider 62 public Object[][] data() { return TESTS; } 63 64 @Test(dataProvider = "data") 65 public void testAnnotatedReciver(Class<?> toTest, Class<?> ctorParamType, 66 Integer returnVal, Integer receiverVal) throws NoSuchMethodException { 67 Constructor c; 68 if (ctorParamType == null) 69 c = toTest.getDeclaredConstructor(); 70 else 71 c = toTest.getDeclaredConstructor(ctorParamType); 72 73 AnnotatedType annotatedReceiverType = c.getAnnotatedReceiverType(); 74 Annotation[] receiverAnnotations = annotatedReceiverType.getAnnotations(); 75 76 if (receiverVal == null) { 77 assertEquals(receiverAnnotations.length, 0, Arrays.asList(receiverAnnotations).toString() + 78 " should be empty. Looking at 'length': "); 79 return; 80 } 81 82 assertEquals(receiverAnnotations.length, 1, "expecting a 1 element array. Looking at 'length': "); 83 assertEquals(((Annot)receiverAnnotations[0]).value(), receiverVal.intValue(), " wrong annotation found. Found " + 84 receiverAnnotations[0] + 85 " should find @Annot with value=" + 86 receiverVal); 87 } 88 89 @Test(dataProvider = "data") 90 public void testAnnotatedReturn(Class<?> toTest, Class<?> ctorParamType, 91 Integer returnVal, Integer receiverVal) throws NoSuchMethodException { 92 Constructor c; 93 if (ctorParamType == null) 94 c = toTest.getDeclaredConstructor(); 95 else 96 c = toTest.getDeclaredConstructor(ctorParamType); 97 98 AnnotatedType annotatedReturnType = c.getAnnotatedReturnType(); 99 Annotation[] returnAnnotations = annotatedReturnType.getAnnotations(); 100 101 assertEquals(returnAnnotations.length, 1, "expecting a 1 element array. Looking at 'length': "); 102 assertEquals(((Annot)returnAnnotations[0]).value(), returnVal.intValue(), " wrong annotation found. Found " + 103 returnAnnotations[0] + 104 " should find @Annot with value=" + 105 returnVal); 106 } 107 108 @Annot(5) ConstructorReceiverTest() {} 109 110 private class Middle { 111 @Annot(10) public Middle(@Annot(15) ConstructorReceiverTest ConstructorReceiverTest.this) {} 112 113 public class Inner { 114 @Annot(100) Inner(@Annot(150) Middle Middle.this) {} 115 116 class Innermost { 117 @Annot(1000) private Innermost(@Annot(1500) Inner Inner.this) {} 118 } 119 } 120 121 class InnerNoReceiver { 122 @Annot(300) InnerNoReceiver(Middle Middle.this) {} 123 } 124 } 125 126 public static class Nested { 127 @Annot(20) public Nested() {} 128 129 class NestedMiddle { 130 @Annot(200) public NestedMiddle(@Annot(250) Nested Nested.this) {} 131 132 class NestedInner { 133 @Annot(2000) public NestedInner(@Annot(2500) NestedMiddle NestedMiddle.this) {} 134 } 135 136 class NestedInnerNoReceiver { 137 @Annot(4000) public NestedInnerNoReceiver() {} 138 } 139 } 140 } 141 142 @Retention(RetentionPolicy.RUNTIME) 143 @Target(ElementType.TYPE_USE) 144 public static @interface Annot { 145 int value(); 146 } 147 }