/* * Copyright (c) 2012, 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. */ /** * @bug 8013497 * @summary Tests for type annotation. * @author Charlie Wang */ import java.util.LinkedHashMap; import java.util.Map; public abstract class AnnotationTest { // combinations of annotations to test public final String[] annotationCombinations = new String[]{ "", Helper.Declaration.ANNOTATION_TYPE_1 + " ", Helper.Declaration.ANNOTATION_TYPE_1 + " " + Helper.Declaration.ANNOTATION_TYPE_2 + " " + Helper.Declaration.ANNOTATION_TYPE_3 + " ", Helper.Declaration.ANNOTATION_TYPE_2 + " " + Helper.Declaration.ANNOTATION_TYPE_2 + " " + Helper.Declaration.ANNOTATION_TYPE_2 + " ", Helper.Declaration.ANNOTATION_TYPE_3 + " " + Helper.Declaration.ANNOTATION_TYPE_3 + " " + Helper.Declaration.ANNOTATION_TYPE_3 + " ", Helper.Declaration.ANNOTATION_TYPE_3 + " "}; // unique index number for class names generated for test public static int clsIdx = 0; // a place to hold class name -- annotation correspondence public static Map testInput = new LinkedHashMap<>(); public AnnotationTest() { clsIdx = 0; testInput.clear(); } public void test() throws Exception { if (!checkResult()) { throw new RuntimeException("test fail."); } else { System.out.println("Pass."); } } public static synchronized String genBaseClass(String str) { Map lhm = new LinkedHashMap<>(); String testCode = ""; lhm.put(Helper.Declaration.IMPORT, null); lhm.put(Helper.Declaration.CLASS, str); testCode += Helper.genDeclaration(lhm); lhm.clear(); lhm.put(Helper.Declaration.ANNOTATION_TYPE_1, ""); lhm.put(Helper.Declaration.ANNOTATION_TYPE_2, ""); lhm.put(Helper.Declaration.ANNOTATION_TYPE_3, ""); lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_1, ""); lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_2, ""); lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_3, ""); testCode += Helper.genDeclaration(lhm); lhm.clear(); return testCode; } protected Class loadClass(Object clsName) throws Exception { // Get Class object for the compiled class Class cls = null; if (clsName instanceof String) { cls = Helper.loadClass((String) clsName); } else { cls = (Class) clsName; } return cls; } protected void compileCode(Class tcg) throws Exception { String testCode = GenTestCode(tcg); Helper.debugPrint(testCode); ClassLoader parentClassLoader = getClass().getClassLoader(); // compile files into classes Helper.genClass(testCode, parentClassLoader, TestCaseGenerator.testBaseClsName); } protected String GenTestCode(Class tcg) { String testCode = ""; for (Object tc : tcg.getEnumConstants()) { for (String anno : annotationCombinations) { testCode += ((TestCaseGenerator) tc).genTestCase(anno); } } return testCode; } protected abstract boolean checkResult() throws Exception; }