Mon, 18 Feb 2013 14:29:40 -0800
8008339: Test TargetAnnoCombo.java is broken
Reviewed-by: jjh
jjg@1401 | 1 | /* |
jjg@1492 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1401 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1401 | 4 | * |
jjg@1401 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1401 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1401 | 7 | * published by the Free Software Foundation. |
jjg@1401 | 8 | * |
jjg@1401 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1401 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1401 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1401 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1401 | 13 | * accompanied this code). |
jjg@1401 | 14 | * |
jjg@1401 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1401 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1401 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1401 | 18 | * |
jjg@1401 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1401 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1401 | 21 | * questions. |
jjg@1401 | 22 | */ |
jjg@1401 | 23 | |
jjg@1401 | 24 | /** |
jjg@1401 | 25 | * @test |
jjg@1401 | 26 | * @bug 8002157 |
jjg@1401 | 27 | * @author sogoel |
jjg@1401 | 28 | * @summary Combo test to check for usage of Deprecated |
jjg@1401 | 29 | * @build Helper |
jjg@1401 | 30 | * @compile DeprecatedAnnoCombo.java |
jjg@1401 | 31 | * @run main DeprecatedAnnoCombo |
jjg@1401 | 32 | */ |
jjg@1401 | 33 | |
jjg@1401 | 34 | import java.util.List; |
jjg@1401 | 35 | import javax.tools.Diagnostic; |
jjg@1401 | 36 | import javax.tools.DiagnosticCollector; |
jjg@1401 | 37 | import javax.tools.JavaFileObject; |
jjg@1401 | 38 | |
jjg@1401 | 39 | /* |
jjg@1401 | 40 | * Generate test src for use of @Deprecated on base anno |
jjg@1401 | 41 | * or container anno or on both. In all cases, test src should compile and a |
jjg@1401 | 42 | * warning should be generated. Repeating annotations used only on class for |
jjg@1401 | 43 | * these generated test src. |
jjg@1401 | 44 | */ |
jjg@1401 | 45 | |
jjg@1401 | 46 | public class DeprecatedAnnoCombo extends Helper { |
jjg@1401 | 47 | static int errors = 0; |
jjg@1401 | 48 | |
jjg@1401 | 49 | enum TestCases { |
jjg@1401 | 50 | DeprecatedonBoth, |
jjg@1401 | 51 | DeprecatedonContainer, |
jjg@1401 | 52 | DeprecatedonBase; |
jjg@1401 | 53 | } |
jjg@1401 | 54 | |
jjg@1401 | 55 | public static void main(String[] args) throws Exception { |
jjg@1401 | 56 | new DeprecatedAnnoCombo().runTest(); |
jjg@1401 | 57 | } |
jjg@1401 | 58 | |
jjg@1401 | 59 | public void runTest() throws Exception { |
jjg@1401 | 60 | boolean ok = false; |
jjg@1401 | 61 | int testCtr = 0; |
jjg@1401 | 62 | |
jjg@1401 | 63 | for (TestCases clName : TestCases.values()) { |
jjg@1401 | 64 | testCtr++; |
jjg@1401 | 65 | |
jjg@1401 | 66 | // Create test source content |
jjg@1401 | 67 | String contents = getContent(clName.toString()); |
jjg@1401 | 68 | |
jjg@1401 | 69 | // Compile the generated source file |
jjg@1401 | 70 | DiagnosticCollector<JavaFileObject> diagnostics = |
jjg@1401 | 71 | new DiagnosticCollector<JavaFileObject>(); |
jjg@1401 | 72 | ok = compileCode(clName.toString(), contents, diagnostics); |
jjg@1401 | 73 | |
jjg@1401 | 74 | String errorKey1 = "compiler.note.deprecated.filename"; |
jjg@1401 | 75 | String errorKey2 = "compiler.note.deprecated.recompile"; |
jjg@1401 | 76 | List<Diagnostic<? extends JavaFileObject>> diags = diagnostics.getDiagnostics(); |
jjg@1401 | 77 | |
jjg@1401 | 78 | //Check for deprecated warnings |
jjg@1401 | 79 | if (ok) { |
jjg@1401 | 80 | if (diags.size() == 0) { |
jjg@1401 | 81 | error("Did not get any warnings for @Deprecated usage"); |
jjg@1401 | 82 | } else { |
jjg@1401 | 83 | for (Diagnostic<?> d : diags) { |
jjg@1401 | 84 | if (d.getKind() == Diagnostic.Kind.NOTE) { |
jjg@1401 | 85 | if (d.getCode().contains(errorKey1) |
jjg@1401 | 86 | || d.getCode().contains(errorKey2)) { |
jjg@1401 | 87 | System.out.println("TestCase =" + clName + " passed as expected"); |
jjg@1401 | 88 | } else { |
jjg@1401 | 89 | error("TestCase =" + clName + " did not give correct warnings" + |
jjg@1401 | 90 | "Expected warning keys: " + |
jjg@1401 | 91 | "errorKey1 = " + errorKey1 + |
jjg@1401 | 92 | "errorKey2 = " + errorKey2 + |
jjg@1401 | 93 | "actualErrorKey = " + d.getCode(), contents); |
jjg@1401 | 94 | } |
jjg@1401 | 95 | } else { |
jjg@1401 | 96 | error("Diagnostic Kind is incorrect, expected = " + |
jjg@1401 | 97 | Diagnostic.Kind.NOTE + "actual = " + d.getKind(), contents); |
jjg@1401 | 98 | } |
jjg@1401 | 99 | } |
jjg@1401 | 100 | } |
jjg@1401 | 101 | } else { |
jjg@1401 | 102 | error("TestCase =" + clName + " did not compile as expected", contents); |
jjg@1401 | 103 | } |
jjg@1401 | 104 | } |
jjg@1401 | 105 | |
jjg@1401 | 106 | System.out.println("Total number of tests run: " + testCtr); |
jjg@1401 | 107 | System.out.println("Total number of errors: " + errors); |
jjg@1401 | 108 | if (errors > 0) |
jjg@1401 | 109 | throw new Exception(errors + " errors found"); |
jjg@1401 | 110 | } |
jjg@1401 | 111 | |
jjg@1401 | 112 | private String getContent(String className) { |
jjg@1401 | 113 | StringBuilder annoData = new StringBuilder(); |
jjg@1401 | 114 | |
jjg@1401 | 115 | switch(className) { |
jjg@1401 | 116 | case "DeprecatedonBoth": |
jjg@1401 | 117 | annoData.append(Helper.ContentVars.DEPRECATED.getVal()) |
jjg@1401 | 118 | .append(Helper.ContentVars.CONTAINER.getVal()) |
jjg@1401 | 119 | .append(Helper.ContentVars.DEPRECATED.getVal()) |
jjg@1492 | 120 | .append(Helper.ContentVars.REPEATABLE.getVal()) |
jjg@1401 | 121 | .append(Helper.ContentVars.BASE.getVal()); |
jjg@1401 | 122 | break; |
jjg@1401 | 123 | case "DeprecatedonBase": |
jjg@1492 | 124 | annoData.append(Helper.ContentVars.CONTAINER.getVal()) |
jjg@1401 | 125 | .append(Helper.ContentVars.DEPRECATED.getVal()) |
jjg@1492 | 126 | .append(Helper.ContentVars.REPEATABLE.getVal()) |
jjg@1401 | 127 | .append(Helper.ContentVars.BASE.getVal()); |
jjg@1401 | 128 | break; |
jjg@1401 | 129 | case "DeprecatedonContainer": |
jjg@1401 | 130 | annoData.append(Helper.ContentVars.DEPRECATED.getVal()) |
jjg@1401 | 131 | .append(Helper.ContentVars.CONTAINER.getVal()) |
jjg@1492 | 132 | .append(Helper.ContentVars.REPEATABLE.getVal()) |
jjg@1401 | 133 | .append(Helper.ContentVars.BASE.getVal()); |
jjg@1401 | 134 | break; |
jjg@1401 | 135 | } |
jjg@1401 | 136 | |
jjg@1401 | 137 | String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal() |
jjg@1401 | 138 | + Helper.ContentVars.IMPORTDEPRECATED.getVal() |
jjg@1401 | 139 | + annoData |
jjg@1401 | 140 | + Helper.ContentVars.REPEATABLEANNO.getVal() |
jjg@1401 | 141 | + "\nclass "+ className + "{}"; |
jjg@1401 | 142 | return contents; |
jjg@1401 | 143 | } |
jjg@1401 | 144 | |
jjg@1401 | 145 | private void error(String msg, String... contents) { |
jjg@1401 | 146 | System.out.println("error: " + msg); |
jjg@1401 | 147 | errors++; |
jjg@1401 | 148 | if (contents.length == 1) { |
jjg@1401 | 149 | System.out.println("Contents = " + contents[0]); |
jjg@1401 | 150 | } |
jjg@1401 | 151 | } |
jjg@1401 | 152 | } |