--- /dev/null Mon Sep 23 02:05:47 2013 +++ new/test/type-annotations/java/lang/reflect/Executable/ExecutableGetAnnotatedExceptionTypesTest.java Mon Sep 23 02:05:46 2013 @@ -0,0 +1,212 @@ +/* + * 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: Executable.getAnnotatedExceptionTypes() + * @author Charlie Wang + * @library ../../../../ + * @build AnnotationTest TestCaseGenerator DeclarationGenerator Helper + * @run main ExecutableGetAnnotatedExceptionTypesTest + */ +import java.lang.reflect.AnnotatedType; +import java.util.LinkedHashMap; +import java.util.Map; +import java.lang.reflect.Method; + +/* + * This file is intended to test API: Executable.getAnnotatedExceptionTypes(). + * It can be run directly by java command or by jtreg. + * By setting Helper.DEBUG = true/false, one can display/hide the test code + * on the command line output, which is constructed for the annotation test. + * Here's more details about test code generation: + * 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. No exception + * 2. Single exception + * 3. Multiple exceptions + * 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 ExecutableGetAnnotatedExceptionTypesTest + 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 == testInput.size()) { + testInput.put(null, null); + return AnnotationTest.genBaseClass(testBaseClsName); + } else { + return ""; + } + } + }, + NOEXCEPTION() { + /** + * generate test method: + * class TypeAnnoCls { + * String m() { + * return null;} + * } + * + * input should be like [anno] + */ + public String genTestCase(String str) { + + if (null == str) { + throw new RuntimeException("bad test base."); + } + + String testCode = ""; + int clsIdx = testInput.size(); + TestCodeGenerator tcg = new TestCodeGenerator(); + tcg.put(Helper.Declaration.CLASS, typeAnnoClsName + clsIdx); + tcg.put(Helper.Declaration.CLASS_BODY_START, ""); + tcg.put(Helper.Declaration.METHOD_HEADER, "String m"); + tcg.put(Helper.Declaration.METHOD_PARAMETER, ""); + tcg.put(Helper.Declaration.METHOD_BODY, ""); + tcg.put(Helper.Declaration.CLASS_BODY_END, ""); + testCode += tcg.genTestCode(); + testInput.put(typeAnnoClsName + clsIdx, ""); + return testCode; + } + }, + SINGLEEXCEPTION() { + /** + * generate test method: + * class TypeAnnoCls { + * String m() throws @anno Exception { + * return null; + * } + * } + * + * input should be like [anno] + */ + public String genTestCase(String str) { + if (null == str) { + throw new RuntimeException("bad test base."); + } + String testCode = ""; + int clsIdx = testInput.size(); + TestCodeGenerator tcg = new TestCodeGenerator(); + tcg.put(Helper.Declaration.CLASS, typeAnnoClsName + clsIdx); + tcg.put(Helper.Declaration.CLASS_BODY_START, ""); + tcg.put(Helper.Declaration.METHOD_HEADER, "String m"); + tcg.put(Helper.Declaration.METHOD_PARAMETER, ""); + tcg.put(Helper.Declaration.METHOD_EXCEPTION, + str + " RuntimeException"); + tcg.put(Helper.Declaration.METHOD_BODY, ""); + tcg.put(Helper.Declaration.CLASS_BODY_END, ""); + testCode += tcg.genTestCode(); + testInput.put(typeAnnoClsName + clsIdx, str); + return testCode; + } + }, + MULTIPLEEXCEPTION() { + /** + * generate test method: + * class TypeAnnoCls { + * String m() { + * return null; + * } + * } + * + * input should be like [anno] + */ + public String genTestCase(String str) { + if (null == str) { + throw new RuntimeException("bad test base."); + } + String testCode = ""; + int clsIdx = testInput.size(); + TestCodeGenerator tcg = new TestCodeGenerator(); + tcg.put(Helper.Declaration.CLASS, typeAnnoClsName + clsIdx); + tcg.put(Helper.Declaration.CLASS_BODY_START, ""); + tcg.put(Helper.Declaration.METHOD_HEADER, "String m"); + tcg.put(Helper.Declaration.METHOD_PARAMETER, ""); + tcg.put(Helper.Declaration.METHOD_EXCEPTION, + new String[]{str + " RuntimeException", + str + " ArrayIndexOutOfBoundsException"}); + tcg.put(Helper.Declaration.METHOD_BODY, ""); + tcg.put(Helper.Declaration.CLASS_BODY_END, ""); + testCode += tcg.genTestCode(); + testInput.put(typeAnnoClsName + clsIdx, str); + return testCode; + } + }; + + // a place to hold class name -- annotation correspondence + public static Map testInput = new LinkedHashMap<>(); + + // generate test class of a specific case + public String genTestCase(String str) { + return ""; + } + } + + // compare input with result + protected boolean checkResult() throws Exception { + compileCode(TestCase.class); + // Get Class object for the compiled class + for (String clsName : TestCase.testInput.keySet()) { + if (null == clsName) { + continue; + } + Class cls = Helper.loadClass(clsName); + Method m = cls.getDeclaredMethod( + TestCaseGenerator.testBaseMethodName, (Class[]) null); + AnnotatedType[] aet = m.getAnnotatedExceptionTypes(); + + for (AnnotatedType at : aet) { + if (!Helper.getAT(at).equals(TestCase.testInput.get(clsName))) { + Helper.debugPrint(clsName + + " failed with faulty annotations."); + return false; + } + } + } + return true; + } + + public static void main(String[] args) throws Exception { + new ExecutableGetAnnotatedExceptionTypesTest().test(); + } +}