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