test/tools/javac/annotations/repeatingAnnotations/combo/DocumentedAnnoCombo.java

Wed, 07 Nov 2012 17:01:19 -0800

author
jjg
date
Wed, 07 Nov 2012 17:01:19 -0800
changeset 1401
2986e7052952
child 1492
df694c775e8a
permissions
-rw-r--r--

8002157: Write combo compiler tests for repeating annotations for JDK8
Reviewed-by: darcy, jjg
Contributed-by: sonali.goel@oracle.com

jjg@1401 1 /*
jjg@1401 2 * Copyright (c) 2012, 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 Positive combo test for use of Documented on baseAnno/containerAnno
jjg@1401 29 * @build Helper
jjg@1401 30 * @compile DocumentedAnnoCombo.java
jjg@1401 31 * @run main DocumentedAnnoCombo
jjg@1401 32 */
jjg@1401 33
jjg@1401 34 import javax.tools.DiagnosticCollector;
jjg@1401 35 import javax.tools.JavaFileObject;
jjg@1401 36
jjg@1401 37 /*
jjg@1401 38 * Generate valid test src for the use of @Documented on container anno
jjg@1401 39 * or on both base anno and container anno. Both test src should compile.
jjg@1401 40 * Repeating annotations used only on class for these generated test src.
jjg@1401 41 */
jjg@1401 42 public class DocumentedAnnoCombo extends Helper {
jjg@1401 43 static int errors = 0;
jjg@1401 44
jjg@1401 45 enum TestCases {
jjg@1401 46 DocumentedonBothAnno(true),
jjg@1401 47 DocumentedonContainer(true);
jjg@1401 48
jjg@1401 49 TestCases(boolean compile) {
jjg@1401 50 this.compile = compile;
jjg@1401 51 }
jjg@1401 52
jjg@1401 53 boolean compile;
jjg@1401 54 boolean shouldCompile() {
jjg@1401 55 return compile;
jjg@1401 56 }
jjg@1401 57 }
jjg@1401 58
jjg@1401 59 public static void main(String[] args) throws Exception {
jjg@1401 60 new DocumentedAnnoCombo().runTest();
jjg@1401 61 }
jjg@1401 62
jjg@1401 63 public void runTest() throws Exception {
jjg@1401 64 boolean ok = false;
jjg@1401 65 int testCtr = 0;
jjg@1401 66
jjg@1401 67 // Create test source content
jjg@1401 68 for (TestCases className : TestCases.values()) {
jjg@1401 69 testCtr++;
jjg@1401 70 String contents = getContent(className.toString());
jjg@1401 71
jjg@1401 72 // Compile the generated source file
jjg@1401 73 DiagnosticCollector<JavaFileObject> diagnostics =
jjg@1401 74 new DiagnosticCollector<JavaFileObject>();
jjg@1401 75 ok = compileCode(className.toString(), contents, diagnostics);
jjg@1401 76 if (!ok) {
jjg@1401 77 error("Class="+ className +" did not compile as expected", contents);
jjg@1401 78 } else {
jjg@1401 79 System.out.println("Test passed for className: " + className);
jjg@1401 80 }
jjg@1401 81 }
jjg@1401 82
jjg@1401 83 System.out.println("Total number of tests run: " + testCtr);
jjg@1401 84 System.out.println("Total number of errors: " + errors);
jjg@1401 85
jjg@1401 86 if (errors > 0)
jjg@1401 87 throw new Exception(errors + " errors found");
jjg@1401 88 }
jjg@1401 89
jjg@1401 90 private String getContent(String className) {
jjg@1401 91
jjg@1401 92 StringBuilder annoData = new StringBuilder();
jjg@1401 93 switch(className) {
jjg@1401 94 case "DocumentedonBothAnno":
jjg@1401 95 annoData.append(Helper.ContentVars.DOCUMENTED.getVal())
jjg@1401 96 .append(Helper.ContentVars.CONTAINERFOR.getVal())
jjg@1401 97 .append(Helper.ContentVars.CONTAINER.getVal())
jjg@1401 98 .append(Helper.ContentVars.DOCUMENTED.getVal())
jjg@1401 99 .append(Helper.ContentVars.CONTAINEDBY.getVal())
jjg@1401 100 .append(Helper.ContentVars.BASE.getVal());
jjg@1401 101 break;
jjg@1401 102 case "DocumentedonContainer":
jjg@1401 103 annoData.append(Helper.ContentVars.DOCUMENTED.getVal())
jjg@1401 104 .append(Helper.ContentVars.CONTAINERFOR.getVal())
jjg@1401 105 .append(Helper.ContentVars.CONTAINER.getVal())
jjg@1401 106 .append(Helper.ContentVars.CONTAINEDBY.getVal())
jjg@1401 107 .append(Helper.ContentVars.BASE.getVal());
jjg@1401 108 break;
jjg@1401 109 }
jjg@1401 110
jjg@1401 111 String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()
jjg@1401 112 + Helper.ContentVars.IMPORTDOCUMENTED.getVal()
jjg@1401 113 + annoData
jjg@1401 114 + Helper.ContentVars.REPEATABLEANNO.getVal()
jjg@1401 115 + "\nclass "+ className + "{}";
jjg@1401 116 return contents;
jjg@1401 117 }
jjg@1401 118
jjg@1401 119 private void error(String msg, String... contents) {
jjg@1401 120 System.out.println("error: " + msg);
jjg@1401 121 errors++;
jjg@1401 122 if (contents.length == 1) {
jjg@1401 123 System.out.println("Contents = " + contents[0]);
jjg@1401 124 }
jjg@1401 125 }
jjg@1401 126 }
jjg@1401 127

mercurial