1ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller/* 2ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * Copyright (C) 2016 The Android Open Source Project 3ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * 4ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * Licensed under the Apache License, Version 2.0 (the "License"); 5ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * you may not use this file except in compliance with the License. 6ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * You may obtain a copy of the License at 7ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * 8ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * http://www.apache.org/licenses/LICENSE-2.0 9ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * 10ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * Unless required by applicable law or agreed to in writing, software 11ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * distributed under the License is distributed on an "AS IS" BASIS, 12ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * See the License for the specific language governing permissions and 14ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller * limitations under the License. 15ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller */ 16ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 17ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerpackage libcore.java.lang.reflect.annotations; 18ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 19ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport junit.framework.TestCase; 20ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 21ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport java.lang.annotation.Annotation; 22ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Container; 23ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Repeated; 24ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport libcore.java.lang.reflect.annotations.multipleannotation.MultipleAnnotation; 25ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport libcore.java.lang.reflect.annotations.multipleannotationexplicitsingle.MultipleAnnotationExplicitSingle; 26ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport libcore.java.lang.reflect.annotations.multipleannotationoddity.MultipleAnnotationOddity; 27ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport libcore.java.lang.reflect.annotations.noannotation.NoAnnotation; 28ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport libcore.java.lang.reflect.annotations.singleannotation.SingleAnnotation; 29ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 30ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.EXPECT_EMPTY; 31ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertGetDeclaredAnnotation; 32ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerimport static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertIsAnnotationPresent; 33ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 34ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fullerpublic class PackageTest extends TestCase { 35ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 36ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller // Tests for isAnnotationPresent and getDeclaredAnnotation. 37ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller public void testDeclaredAnnotation() throws Exception { 38ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<Repeated> repeated = Repeated.class; 39ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(NoAnnotation.class, repeated, null); 40ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(SingleAnnotation.class, repeated, "@Repeated(1)"); 41ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(MultipleAnnotation.class, repeated, null); 42ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(MultipleAnnotationExplicitSingle.class, repeated, null); 43ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(MultipleAnnotationOddity.class, repeated, "@Repeated(1)"); 44ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 45ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<Container> container = Container.class; 46ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(NoAnnotation.class, container, null); 47ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(SingleAnnotation.class, container, null); 48ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(MultipleAnnotation.class, container, 49ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(1), @Repeated(2)})"); 50ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(MultipleAnnotationExplicitSingle.class, container, 51ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(1)})"); 52ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller checkDeclaredAnnotation(MultipleAnnotationOddity.class, container, 53ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(2), @Repeated(3)})"); 54ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller } 55ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 56ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller private static void checkDeclaredAnnotation( 57ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<?> classInPackage, Class<? extends Annotation> annotationType, 58ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller String expectedAnnotationString) throws Exception { 59ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 60ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Package aPackage = classInPackage.getPackage(); 61ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller // isAnnotationPresent 62ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertIsAnnotationPresent(aPackage, annotationType, expectedAnnotationString != null); 63ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 64ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller // getDeclaredAnnotation 65ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotation(aPackage, annotationType, expectedAnnotationString); 66ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller } 67ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 68ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller public void testGetDeclaredAnnotationsByType() throws Exception { 69ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<Repeated> repeated = Repeated.class; 70ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 71ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(NoAnnotation.class, repeated, EXPECT_EMPTY); 72ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(SingleAnnotation.class, repeated, 73ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Repeated(1)"); 74ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(MultipleAnnotation.class, repeated, 75ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Repeated(1)", "@Repeated(2)"); 76ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(MultipleAnnotationExplicitSingle.class, repeated, 77ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Repeated(1)"); 78ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(MultipleAnnotationOddity.class, repeated, 79ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Repeated(1)", "@Repeated(2)", "@Repeated(3)"); 80ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 81ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<Container> container = Container.class; 82ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(NoAnnotation.class, container, EXPECT_EMPTY); 83ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(SingleAnnotation.class, container, EXPECT_EMPTY); 84ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(MultipleAnnotation.class, container, 85ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(1), @Repeated(2)})"); 86ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(MultipleAnnotationExplicitSingle.class, container, 87ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(1)})"); 88ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetDeclaredAnnotationsByType(MultipleAnnotationOddity.class, container, 89ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(2), @Repeated(3)})"); 90ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller } 91ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 92ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller private static void assertGetDeclaredAnnotationsByType( 93ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<?> classInPackage, Class<? extends Annotation> annotationType, 94ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller String... expectedAnnotationStrings) throws Exception { 95ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Package aPackage = classInPackage.getPackage(); 96ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller AnnotatedElementTestSupport.assertGetDeclaredAnnotationsByType( 97ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller aPackage, annotationType, expectedAnnotationStrings); 98ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller } 99ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 100ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller public void testGetAnnotationsByType() throws Exception { 101ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<Repeated> repeated = Repeated.class; 102ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(NoAnnotation.class, repeated, EXPECT_EMPTY); 103ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(SingleAnnotation.class, repeated, "@Repeated(1)"); 104ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(MultipleAnnotation.class, repeated, 105ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Repeated(1)", "@Repeated(2)"); 106ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(MultipleAnnotationExplicitSingle.class, repeated, 107ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Repeated(1)"); 108ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(MultipleAnnotationOddity.class, repeated, 109ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Repeated(1)", "@Repeated(2)", "@Repeated(3)"); 110ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 111ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<Container> container = Container.class; 112ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(NoAnnotation.class, container, EXPECT_EMPTY); 113ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(SingleAnnotation.class, container, EXPECT_EMPTY); 114ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(MultipleAnnotation.class, container, 115ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(1), @Repeated(2)})"); 116ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(MultipleAnnotationExplicitSingle.class, container, 117ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(1)})"); 118ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller assertGetAnnotationsByType(MultipleAnnotationOddity.class, container, 119ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller "@Container({@Repeated(2), @Repeated(3)})"); 120ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller } 121ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller 122ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller private static void assertGetAnnotationsByType(Class<?> classInPackage, 123ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Class<? extends Annotation> annotationType, 124ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller String... expectedAnnotationStrings) throws Exception { 125ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller Package aPackage = classInPackage.getPackage(); 126ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller AnnotatedElementTestSupport.assertGetAnnotationsByType( 127ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller aPackage, annotationType, expectedAnnotationStrings); 128ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller } 129ee3e080dc11ef39567381faadf149c88e7860ce3Neil Fuller} 130