1 /* 2 * Copyright (c) 2012, 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 * @bug 8013497 25 * @summary Tests for type annotation. 26 * @author Charlie Wang 27 */ 28 import java.io.File; 29 import java.util.LinkedHashMap; 30 import java.util.Map; 31 32 public abstract class AnnotationTest { 33 34 // combinations of annotations to test 35 public final String[] annotationCombinations = new String[]{ 36 "", 37 Helper.Declaration.ANNOTATION_TYPE_1 + " ", 38 Helper.Declaration.ANNOTATION_TYPE_1 + " " 39 + Helper.Declaration.ANNOTATION_TYPE_2 + " " 40 + Helper.Declaration.ANNOTATION_TYPE_3 + " ", 41 Helper.Declaration.ANNOTATION_TYPE_2 + " " 42 + Helper.Declaration.ANNOTATION_TYPE_2 + " " 43 + Helper.Declaration.ANNOTATION_TYPE_2 + " ", 44 Helper.Declaration.ANNOTATION_TYPE_3 + " " 45 + Helper.Declaration.ANNOTATION_TYPE_3 + " " 46 + Helper.Declaration.ANNOTATION_TYPE_3 + " ", 47 Helper.Declaration.ANNOTATION_TYPE_3 + " "}; 48 49 private String testSource = null; 50 private String testSourceName = null; 51 52 public void test() throws Exception { 53 boolean result = true; 54 try { 55 if (!checkResult()) { 56 throw new RuntimeException("test fail."); 57 } 58 } catch (Exception e) { 59 if (null == testSource) { 60 System.out.println("Test fail."); 61 } else { 62 Helper.debugPrintToFile(testSource, testSourceName); 63 System.out.println("Test fail. Test source output to " 64 + System.getProperty("test.classes", ".") 65 + File.separator 66 + testSourceName 67 + File.separator 68 + "TypeAnnoClsBase.java"); 69 } 70 throw e; 71 } 72 73 System.out.println("Pass."); 74 } 75 76 public static synchronized String genBaseClass(String str) { 77 TestCodeGenerator tcg = new TestCodeGenerator(); 78 String testCode = ""; 79 tcg.put(Helper.Declaration.IMPORT, null); 80 tcg.put(Helper.Declaration.CLASS, str); 81 tcg.put(Helper.Declaration.CLASS_BODY_START, ""); 82 tcg.put(Helper.Declaration.CLASS_BODY_END, ""); 83 testCode += tcg.genTestCode(); 84 85 tcg = new TestCodeGenerator(); 86 tcg.put(Helper.Declaration.ANNOTATION_TYPE_1, ""); 87 tcg.put(Helper.Declaration.ANNOTATION_TYPE_2, ""); 88 tcg.put(Helper.Declaration.ANNOTATION_TYPE_3, ""); 89 tcg.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_1, ""); 90 tcg.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_2, ""); 91 tcg.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_3, ""); 92 testCode += tcg.genTestCode(); 93 94 return testCode; 95 } 96 97 public static synchronized String genBaseClassStart(String str) { 98 String testCode = ""; 99 TestCodeGenerator tcg = new TestCodeGenerator(); 100 tcg.put(Helper.Declaration.IMPORT, null); 101 tcg.put(Helper.Declaration.CLASS, str); 102 tcg.put(Helper.Declaration.CLASS_BODY_START, ""); 103 testCode += tcg.genTestCode(Helper.Declaration.CLASS); 104 return testCode; 105 } 106 107 public static synchronized String genBaseClassEnd(String str) { 108 String testCode = ""; 109 TestCodeGenerator tcg = new TestCodeGenerator(); 110 tcg.put(Helper.Declaration.CLASS_BODY_END, ""); 111 tcg.put(Helper.Declaration.ANNOTATION_TYPE_1, ""); 112 tcg.put(Helper.Declaration.ANNOTATION_TYPE_2, ""); 113 tcg.put(Helper.Declaration.ANNOTATION_TYPE_3, ""); 114 tcg.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_1, ""); 115 tcg.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_2, ""); 116 tcg.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_3, ""); 117 testCode += tcg.genTestCode(); 118 return testCode; 119 } 120 121 private Class getPrimitiveType(String name) { 122 switch (name) { 123 case "byte.class": 124 return byte.class; 125 case "short.class": 126 return short.class; 127 case "int.class": 128 return int.class; 129 case "long.class": 130 return long.class; 131 case "char.class": 132 return char.class; 133 case "float.class": 134 return float.class; 135 case "double.class": 136 return double.class; 137 case "boolean.class": 138 return boolean.class; 139 case "void.class": 140 return void.class; 141 case "Object.class": 142 return void.class; 143 case "int[].class": 144 return int[].class; 145 default: 146 return null; 147 } 148 } 149 150 protected Class loadClass(String clsName) throws Exception { 151 // Get Class object for the compiled class 152 Class cls = getPrimitiveType(clsName); 153 if (null == cls) { 154 cls = Helper.loadClass((String) clsName); 155 } 156 return cls; 157 } 158 159 protected void compileCode(Class<? extends TestCaseGenerator> tcg) throws Exception { 160 testSource = genTestCode(tcg); 161 testSourceName = tcg.getDeclaringClass().getSimpleName(); 162 ClassLoader parentClassLoader = getClass().getClassLoader(); 163 // compile files into classes 164 Helper.genClass(testSource, parentClassLoader, 165 TestCaseGenerator.testBaseClsName); 166 } 167 168 protected String genTestCode(Class<? extends TestCaseGenerator> tcg) { 169 String testCode = ""; 170 for (TestCaseGenerator tc : tcg.getEnumConstants()) { 171 for (String anno : annotationCombinations) { 172 testCode += tc.genTestCase(anno); 173 } 174 } 175 return testCode; 176 } 177 178 protected abstract boolean checkResult() throws Exception; 179 }