1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/TestPkgInfo.java Wed Aug 25 11:40:25 2010 -0700 1.3 @@ -0,0 +1,174 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6960424 1.30 + * @summary new option -Xpkginfo for better control of when package-info.class is generated 1.31 + */ 1.32 + 1.33 +import java.io.*; 1.34 +import java.util.*; 1.35 + 1.36 +public class TestPkgInfo { 1.37 + enum OptKind { 1.38 + NONE(null), 1.39 + ALWAYS("-Xpkginfo:always"), 1.40 + NONEMPTY("-Xpkginfo:nonempty"), 1.41 + LEGACY("-Xpkginfo:legacy"); 1.42 + OptKind(String opt) { this.opt = opt; } 1.43 + final String opt; 1.44 + }; 1.45 + 1.46 + public static void main(String... args) throws Exception { 1.47 + new TestPkgInfo().run(args); 1.48 + } 1.49 + 1.50 + public void run(String... args) throws Exception { 1.51 + boolean[] booleanValues = { false, true }; 1.52 + for (OptKind ok: OptKind.values()) { 1.53 + for (boolean sr: booleanValues) { 1.54 + for (boolean cr: booleanValues) { 1.55 + for (boolean rr: booleanValues) { 1.56 + try { 1.57 + test(ok, sr, cr, rr); 1.58 + } catch (Exception e) { 1.59 + error("Exception: " + e); 1.60 + } 1.61 + if (errors > 0) throw new AssertionError(); 1.62 + } 1.63 + } 1.64 + } 1.65 + } 1.66 + 1.67 + if (errors > 0) 1.68 + throw new Exception(errors + " errors occurred"); 1.69 + } 1.70 + 1.71 + void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception { 1.72 + count++; 1.73 + System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr); 1.74 + 1.75 + StringBuilder sb = new StringBuilder(); 1.76 + 1.77 + // create annotated package statement with all combinations of retention policy 1.78 + if (sr) sb.append("@SR\n"); 1.79 + if (cr) sb.append("@CR\n"); 1.80 + if (rr) sb.append("@RR\n"); 1.81 + sb.append("package p;\n"); 1.82 + sb.append("\n"); 1.83 + 1.84 + sb.append("import java.lang.annotation.*;\n"); 1.85 + sb.append("@Retention(RetentionPolicy.SOURCE) @interface SR { }\n"); 1.86 + sb.append("@Retention(RetentionPolicy.CLASS) @interface CR { }\n"); 1.87 + sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n"); 1.88 + 1.89 + // test specific tmp directory 1.90 + File tmpDir = new File("tmp.test" + count); 1.91 + File classesDir = new File(tmpDir, "classes"); 1.92 + classesDir.mkdirs(); 1.93 + File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java"); 1.94 + writeFile(pkginfo_java, sb.toString()); 1.95 + 1.96 + // build up list of options and files to be compiled 1.97 + List<String> opts = new ArrayList<String>(); 1.98 + List<File> files = new ArrayList<File>(); 1.99 + 1.100 + opts.add("-d"); 1.101 + opts.add(classesDir.getPath()); 1.102 + if (ok.opt != null) 1.103 + opts.add(ok.opt); 1.104 + //opts.add("-verbose"); 1.105 + files.add(pkginfo_java); 1.106 + 1.107 + compile(opts, files); 1.108 + 1.109 + File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class"); 1.110 + boolean exists = pkginfo_class.exists(); 1.111 + 1.112 + boolean expected; 1.113 + switch (ok) { 1.114 + case ALWAYS: 1.115 + expected = true; 1.116 + break; 1.117 + 1.118 + case LEGACY: 1.119 + case NONE: 1.120 + expected = (sr || cr || rr ); // any annotation 1.121 + break; 1.122 + 1.123 + case NONEMPTY: 1.124 + expected = (cr || rr ); // any annotation in class file 1.125 + break; 1.126 + 1.127 + default: 1.128 + throw new IllegalStateException(); 1.129 + } 1.130 + 1.131 + if (exists && !expected) 1.132 + error("package-info.class found but not expected"); 1.133 + if (!exists && expected) 1.134 + error("package-info.class expected but not found"); 1.135 + } 1.136 + 1.137 + /** Compile files with options provided. */ 1.138 + void compile(List<String> opts, List<File> files) throws Exception { 1.139 + System.err.println("javac: " + opts + " " + files); 1.140 + List<String> args = new ArrayList<String>(); 1.141 + args.addAll(opts); 1.142 + for (File f: files) 1.143 + args.add(f.getPath()); 1.144 + StringWriter sw = new StringWriter(); 1.145 + PrintWriter pw = new PrintWriter(sw); 1.146 + int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); 1.147 + pw.flush(); 1.148 + if (sw.getBuffer().length() > 0) 1.149 + System.err.println(sw.toString()); 1.150 + if (rc != 0) 1.151 + throw new Exception("compilation failed: rc=" + rc); 1.152 + } 1.153 + 1.154 + /** Write a file with a given body. */ 1.155 + void writeFile(File f, String body) throws Exception { 1.156 + if (f.getParentFile() != null) 1.157 + f.getParentFile().mkdirs(); 1.158 + Writer out = new FileWriter(f); 1.159 + try { 1.160 + out.write(body); 1.161 + } finally { 1.162 + out.close(); 1.163 + } 1.164 + } 1.165 + 1.166 + /** Report an error. */ 1.167 + void error(String msg) { 1.168 + System.err.println("Error: " + msg); 1.169 + errors++; 1.170 + } 1.171 + 1.172 + /** Test case counter. */ 1.173 + int count; 1.174 + 1.175 + /** Number of errors found. */ 1.176 + int errors; 1.177 +}