test/tools/javac/TestPkgInfo.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2010, 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 /*
25 * @test
26 * @bug 6960424 8022161
27 * @summary new option -Xpkginfo for better control of when package-info.class
28 * is generated, also ensures no failures if package-info.java is
29 * not available.
30 */
31
32 import java.io.*;
33 import java.util.*;
34
35 public class TestPkgInfo {
36 enum OptKind {
37 NONE(null),
38 ALWAYS("-Xpkginfo:always"),
39 NONEMPTY("-Xpkginfo:nonempty"),
40 LEGACY("-Xpkginfo:legacy");
41 OptKind(String opt) { this.opt = opt; }
42 final String opt;
43 };
44
45 public static void main(String... args) throws Exception {
46 new TestPkgInfo().run(args);
47 }
48 public void run(String... args) throws Exception {
49 testPositive();
50 testNoExceptions();
51 }
52 public void testPositive(String... args) throws Exception {
53 boolean[] booleanValues = { false, true };
54 for (OptKind ok: OptKind.values()) {
55 for (boolean sr: booleanValues) {
56 for (boolean cr: booleanValues) {
57 for (boolean rr: booleanValues) {
58 try {
59 test(ok, sr, cr, rr);
60 } catch (Exception e) {
61 error("Exception: " + e);
62 }
63 if (errors > 0) throw new AssertionError();
64 }
65 }
66 }
67 }
68
69 if (errors > 0)
70 throw new Exception(errors + " errors occurred");
71 }
72
73 /** this should throw no exceptions **/
74 void testNoExceptions() throws Exception {
75 count++;
76 System.err.println("Test " + count + ": ALWAYS nofile");
77
78 StringBuilder sb = new StringBuilder();
79 sb.append("package test; class Hello{}");
80
81 // test specific tmp directory
82 File tmpDir = new File("tmp.test" + count);
83 File classesDir = new File(tmpDir, "classes");
84 classesDir.mkdirs();
85 File javafile = new File(new File(tmpDir, "src"), "Hello.java");
86 writeFile(javafile, sb.toString());
87 // build up list of options and files to be compiled
88 List<String> opts = new ArrayList<>();
89 List<File> files = new ArrayList<>();
90
91 opts.add("-d");
92 opts.add(classesDir.getPath());
93 opts.add("-Xpkginfo:always");
94 files.add(javafile);
95
96 compile(opts, files);
97 }
98
99 void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception {
100 count++;
101 System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr);
102
103 StringBuilder sb = new StringBuilder();
104
105 // create annotated package statement with all combinations of retention policy
106 if (sr) sb.append("@SR\n");
107 if (cr) sb.append("@CR\n");
108 if (rr) sb.append("@RR\n");
109 sb.append("package p;\n");
110 sb.append("\n");
111
112 sb.append("import java.lang.annotation.*;\n");
113 sb.append("@Retention(RetentionPolicy.SOURCE) @interface SR { }\n");
114 sb.append("@Retention(RetentionPolicy.CLASS) @interface CR { }\n");
115 sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n");
116
117 // test specific tmp directory
118 File tmpDir = new File("tmp.test" + count);
119 File classesDir = new File(tmpDir, "classes");
120 classesDir.mkdirs();
121 File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java");
122 writeFile(pkginfo_java, sb.toString());
123
124 // build up list of options and files to be compiled
125 List<String> opts = new ArrayList<>();
126 List<File> files = new ArrayList<>();
127
128 opts.add("-d");
129 opts.add(classesDir.getPath());
130 if (ok.opt != null)
131 opts.add(ok.opt);
132 //opts.add("-verbose");
133 files.add(pkginfo_java);
134
135 compile(opts, files);
136
137 File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class");
138 boolean exists = pkginfo_class.exists();
139
140 boolean expected;
141 switch (ok) {
142 case ALWAYS:
143 expected = true;
144 break;
145
146 case LEGACY:
147 case NONE:
148 expected = (sr || cr || rr ); // any annotation
149 break;
150
151 case NONEMPTY:
152 expected = (cr || rr ); // any annotation in class file
153 break;
154
155 default:
156 throw new IllegalStateException();
157 }
158
159 if (exists && !expected)
160 error("package-info.class found but not expected");
161 if (!exists && expected)
162 error("package-info.class expected but not found");
163 }
164
165 /** Compile files with options provided. */
166 void compile(List<String> opts, List<File> files) throws Exception {
167 System.err.println("javac: " + opts + " " + files);
168 List<String> args = new ArrayList<>();
169 args.addAll(opts);
170 for (File f: files)
171 args.add(f.getPath());
172 StringWriter sw = new StringWriter();
173 PrintWriter pw = new PrintWriter(sw);
174 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
175 pw.flush();
176 if (sw.getBuffer().length() > 0)
177 System.err.println(sw.toString());
178 if (rc != 0)
179 throw new Exception("compilation failed: rc=" + rc);
180 }
181
182 /** Write a file with a given body. */
183 void writeFile(File f, String body) throws Exception {
184 if (f.getParentFile() != null)
185 f.getParentFile().mkdirs();
186 Writer out = new FileWriter(f);
187 try {
188 out.write(body);
189 } finally {
190 out.close();
191 }
192 }
193
194 /** Report an error. */
195 void error(String msg) {
196 System.err.println("Error: " + msg);
197 errors++;
198 }
199
200 /** Test case counter. */
201 int count;
202
203 /** Number of errors found. */
204 int errors;
205 }

mercurial