--- /dev/null Thu Aug 8 00:10:13 2013 +++ new/test/type-annotations/java/lang/Class/ClassGetAnnotationsByTypeTest.java Thu Aug 8 00:10:13 2013 @@ -0,0 +1,185 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +/** + * @test + * @bug 8013497 + * @summary test for API: Class.getAnnotationsByType() + * @library ../../../ + * @build AnnotationTest TestCaseGenerator DeclarationGenerator Helper + * @author Charlie Wang + * @run main ClassGetAnnotationsByTypeTest + */ +import java.lang.annotation.Annotation; + +/* + * Construct the test code in a structure specified in TestCase. + * In each of the following cases, different combinations of annotation + * specified in AnnotationTest.annotationCombinations are tested. The output + * annotations must be the same as input both in value and order. + * 1. Single interface + * 2. Class extends class + * 3. Class implements interface + * The program test all the cases in TestCase by looping over each one of it. + * Test cases are constructed with small code snippet from Helper.SrcType + * using changable annotations, type and name. After these values are specified, + * the method GenTestCode() generates corresponding java code, encapsulate them + * with a class. Then this class is compiled by Helper.compileCode() into + * classes which is later loaded into memory by Helper.loadClass(). + * The test fails if any of the cases fail. + */ +public class ClassGetAnnotationsByTypeTest extends AnnotationTest { + + enum TestCase implements TestCaseGenerator { + TESTBASECLS() { + /** + * generate test base, which is the class name used to compile the code: + * import java.io.*; + * import java.util.*; + * import java.lang.*; + * import java.lang.reflect.*; + * import java.lang.annotation.*; + * class testBaseClsName { } + */ + public String genTestCase(String str) { + if (0 == clsIdx) { + clsIdx++; + return AnnotationTest.genBaseClass(testBaseClsName); + } else { + return ""; + } + } + }, + SINGLECLS() { + /** + * generate single class: + * [#ANNO] class TypeAnnoCls1 { } + * then append annotations on [#ANNO] + * + * input should be like [@anno] + */ + public String genTestCase(String str) { + if (null == str) { + throw new RuntimeException("bad test case."); + } + String testCode = ""; + lhm.put(Helper.Declaration.CLASS, + new String[]{str + " " + typeAnnoClsName + clsIdx}); + testCode += Helper.genDeclaration(lhm); + lhm.clear(); + testInput.put(typeAnnoClsName + clsIdx, str); + clsIdx++; + return testCode; + } + }, + CLSEXTCLS() { + /** + * generate cls extends cls: + * [#ANNO] class TypeAnnoCls2 extends [#ANNO] Object {} + * then append annotations on [#ANNO] + * + * input should be like [@anno] + */ + public String genTestCase(String str) { + if (null == str) { + throw new RuntimeException("bad test case."); + } + String testCode = ""; + lhm.put(Helper.Declaration.CLASS, new String[]{ + str + " " + typeAnnoClsName + clsIdx}); + lhm.put(Helper.Declaration.EXTENDS, new String[]{ + str + " Object"}); + testCode += Helper.genDeclaration(lhm); + lhm.clear(); + testInput.put(typeAnnoClsName + clsIdx, str); + clsIdx++; + return testCode; + } + }, + CLSEXTCLSIMPINT() { + /** + * generate cls implements interface: + * [#ANNO] class TypeAnnoCls3 extends + * [#ANNO] Object implements [#ANNO] Serializable {} + * then append annotations on [#ANNO] + * + * input should be like [@anno] + */ + public String genTestCase(String str) { + if (null == str) { + throw new RuntimeException("bad test case."); + } + String testCode = ""; + lhm.put(Helper.Declaration.CLASS, new String[]{ + str + " " + typeAnnoClsName + clsIdx}); + lhm.put(Helper.Declaration.EXTENDS, new String[]{ + str + " Object"}); + lhm.put(Helper.Declaration.IMPLEMENTS, new String[]{ + str + " Serializable"}); + testCode += Helper.genDeclaration(lhm); + lhm.clear(); + testInput.put(typeAnnoClsName + clsIdx, str); + clsIdx++; + return testCode; + } + }; + + // generate test class of a specific case + public String genTestCase(String str) { + return ""; + } + } + + // compare input with result + protected boolean checkResult() throws Exception { + boolean ret = true; + compileCode(TestCase.class); + for (Object clsName : testInput.keySet()) { + Class cls = loadClass(clsName); + Class argCls = Helper.loadClass(TestCaseGenerator.argClsName); + Annotation[] as = cls.getAnnotationsByType(argCls); + String result = Helper.getAnno(as); + if (null == result || 0 == result.length()) { + if (-1 != testInput.get(clsName) + .indexOf(TestCaseGenerator.argClsName)) { + // should be empty output + ret = false; + Helper.debugPrint(clsName.toString() + + " failed without empty output."); + } + } else { + if (-1 == testInput.get(clsName) + .indexOf(TestCaseGenerator.argClsName)) { + // should not be empty output + ret = false; + Helper.debugPrint(clsName.toString() + + " failed with faulty annotations."); + } + } + } + return ret; + } + + public static void main(String[] args) throws Exception { + new ClassGetAnnotationsByTypeTest().test(); + } +}