Tue, 21 May 2013 13:50:56 +0100
8005207: test has 2 @bug tags
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2010, 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 */
24 /*
25 * @test
26 * @bug 6960424
27 * @summary new option -Xpkginfo for better control of when package-info.class is generated
28 */
30 import java.io.*;
31 import java.util.*;
33 public class TestPkgInfo {
34 enum OptKind {
35 NONE(null),
36 ALWAYS("-Xpkginfo:always"),
37 NONEMPTY("-Xpkginfo:nonempty"),
38 LEGACY("-Xpkginfo:legacy");
39 OptKind(String opt) { this.opt = opt; }
40 final String opt;
41 };
43 public static void main(String... args) throws Exception {
44 new TestPkgInfo().run(args);
45 }
47 public void run(String... args) throws Exception {
48 boolean[] booleanValues = { false, true };
49 for (OptKind ok: OptKind.values()) {
50 for (boolean sr: booleanValues) {
51 for (boolean cr: booleanValues) {
52 for (boolean rr: booleanValues) {
53 try {
54 test(ok, sr, cr, rr);
55 } catch (Exception e) {
56 error("Exception: " + e);
57 }
58 if (errors > 0) throw new AssertionError();
59 }
60 }
61 }
62 }
64 if (errors > 0)
65 throw new Exception(errors + " errors occurred");
66 }
68 void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception {
69 count++;
70 System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr);
72 StringBuilder sb = new StringBuilder();
74 // create annotated package statement with all combinations of retention policy
75 if (sr) sb.append("@SR\n");
76 if (cr) sb.append("@CR\n");
77 if (rr) sb.append("@RR\n");
78 sb.append("package p;\n");
79 sb.append("\n");
81 sb.append("import java.lang.annotation.*;\n");
82 sb.append("@Retention(RetentionPolicy.SOURCE) @interface SR { }\n");
83 sb.append("@Retention(RetentionPolicy.CLASS) @interface CR { }\n");
84 sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n");
86 // test specific tmp directory
87 File tmpDir = new File("tmp.test" + count);
88 File classesDir = new File(tmpDir, "classes");
89 classesDir.mkdirs();
90 File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java");
91 writeFile(pkginfo_java, sb.toString());
93 // build up list of options and files to be compiled
94 List<String> opts = new ArrayList<String>();
95 List<File> files = new ArrayList<File>();
97 opts.add("-d");
98 opts.add(classesDir.getPath());
99 if (ok.opt != null)
100 opts.add(ok.opt);
101 //opts.add("-verbose");
102 files.add(pkginfo_java);
104 compile(opts, files);
106 File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class");
107 boolean exists = pkginfo_class.exists();
109 boolean expected;
110 switch (ok) {
111 case ALWAYS:
112 expected = true;
113 break;
115 case LEGACY:
116 case NONE:
117 expected = (sr || cr || rr ); // any annotation
118 break;
120 case NONEMPTY:
121 expected = (cr || rr ); // any annotation in class file
122 break;
124 default:
125 throw new IllegalStateException();
126 }
128 if (exists && !expected)
129 error("package-info.class found but not expected");
130 if (!exists && expected)
131 error("package-info.class expected but not found");
132 }
134 /** Compile files with options provided. */
135 void compile(List<String> opts, List<File> files) throws Exception {
136 System.err.println("javac: " + opts + " " + files);
137 List<String> args = new ArrayList<String>();
138 args.addAll(opts);
139 for (File f: files)
140 args.add(f.getPath());
141 StringWriter sw = new StringWriter();
142 PrintWriter pw = new PrintWriter(sw);
143 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
144 pw.flush();
145 if (sw.getBuffer().length() > 0)
146 System.err.println(sw.toString());
147 if (rc != 0)
148 throw new Exception("compilation failed: rc=" + rc);
149 }
151 /** Write a file with a given body. */
152 void writeFile(File f, String body) throws Exception {
153 if (f.getParentFile() != null)
154 f.getParentFile().mkdirs();
155 Writer out = new FileWriter(f);
156 try {
157 out.write(body);
158 } finally {
159 out.close();
160 }
161 }
163 /** Report an error. */
164 void error(String msg) {
165 System.err.println("Error: " + msg);
166 errors++;
167 }
169 /** Test case counter. */
170 int count;
172 /** Number of errors found. */
173 int errors;
174 }