1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/TestPkgInfo.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,205 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 2013, 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 8022161 1.30 + * @summary new option -Xpkginfo for better control of when package-info.class 1.31 + * is generated, also ensures no failures if package-info.java is 1.32 + * not available. 1.33 + */ 1.34 + 1.35 +import java.io.*; 1.36 +import java.util.*; 1.37 + 1.38 +public class TestPkgInfo { 1.39 + enum OptKind { 1.40 + NONE(null), 1.41 + ALWAYS("-Xpkginfo:always"), 1.42 + NONEMPTY("-Xpkginfo:nonempty"), 1.43 + LEGACY("-Xpkginfo:legacy"); 1.44 + OptKind(String opt) { this.opt = opt; } 1.45 + final String opt; 1.46 + }; 1.47 + 1.48 + public static void main(String... args) throws Exception { 1.49 + new TestPkgInfo().run(args); 1.50 + } 1.51 + public void run(String... args) throws Exception { 1.52 + testPositive(); 1.53 + testNoExceptions(); 1.54 + } 1.55 + public void testPositive(String... args) throws Exception { 1.56 + boolean[] booleanValues = { false, true }; 1.57 + for (OptKind ok: OptKind.values()) { 1.58 + for (boolean sr: booleanValues) { 1.59 + for (boolean cr: booleanValues) { 1.60 + for (boolean rr: booleanValues) { 1.61 + try { 1.62 + test(ok, sr, cr, rr); 1.63 + } catch (Exception e) { 1.64 + error("Exception: " + e); 1.65 + } 1.66 + if (errors > 0) throw new AssertionError(); 1.67 + } 1.68 + } 1.69 + } 1.70 + } 1.71 + 1.72 + if (errors > 0) 1.73 + throw new Exception(errors + " errors occurred"); 1.74 + } 1.75 + 1.76 + /** this should throw no exceptions **/ 1.77 + void testNoExceptions() throws Exception { 1.78 + count++; 1.79 + System.err.println("Test " + count + ": ALWAYS nofile"); 1.80 + 1.81 + StringBuilder sb = new StringBuilder(); 1.82 + sb.append("package test; class Hello{}"); 1.83 + 1.84 + // test specific tmp directory 1.85 + File tmpDir = new File("tmp.test" + count); 1.86 + File classesDir = new File(tmpDir, "classes"); 1.87 + classesDir.mkdirs(); 1.88 + File javafile = new File(new File(tmpDir, "src"), "Hello.java"); 1.89 + writeFile(javafile, sb.toString()); 1.90 + // build up list of options and files to be compiled 1.91 + List<String> opts = new ArrayList<>(); 1.92 + List<File> files = new ArrayList<>(); 1.93 + 1.94 + opts.add("-d"); 1.95 + opts.add(classesDir.getPath()); 1.96 + opts.add("-Xpkginfo:always"); 1.97 + files.add(javafile); 1.98 + 1.99 + compile(opts, files); 1.100 + } 1.101 + 1.102 + void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception { 1.103 + count++; 1.104 + System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr); 1.105 + 1.106 + StringBuilder sb = new StringBuilder(); 1.107 + 1.108 + // create annotated package statement with all combinations of retention policy 1.109 + if (sr) sb.append("@SR\n"); 1.110 + if (cr) sb.append("@CR\n"); 1.111 + if (rr) sb.append("@RR\n"); 1.112 + sb.append("package p;\n"); 1.113 + sb.append("\n"); 1.114 + 1.115 + sb.append("import java.lang.annotation.*;\n"); 1.116 + sb.append("@Retention(RetentionPolicy.SOURCE) @interface SR { }\n"); 1.117 + sb.append("@Retention(RetentionPolicy.CLASS) @interface CR { }\n"); 1.118 + sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n"); 1.119 + 1.120 + // test specific tmp directory 1.121 + File tmpDir = new File("tmp.test" + count); 1.122 + File classesDir = new File(tmpDir, "classes"); 1.123 + classesDir.mkdirs(); 1.124 + File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java"); 1.125 + writeFile(pkginfo_java, sb.toString()); 1.126 + 1.127 + // build up list of options and files to be compiled 1.128 + List<String> opts = new ArrayList<>(); 1.129 + List<File> files = new ArrayList<>(); 1.130 + 1.131 + opts.add("-d"); 1.132 + opts.add(classesDir.getPath()); 1.133 + if (ok.opt != null) 1.134 + opts.add(ok.opt); 1.135 + //opts.add("-verbose"); 1.136 + files.add(pkginfo_java); 1.137 + 1.138 + compile(opts, files); 1.139 + 1.140 + File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class"); 1.141 + boolean exists = pkginfo_class.exists(); 1.142 + 1.143 + boolean expected; 1.144 + switch (ok) { 1.145 + case ALWAYS: 1.146 + expected = true; 1.147 + break; 1.148 + 1.149 + case LEGACY: 1.150 + case NONE: 1.151 + expected = (sr || cr || rr ); // any annotation 1.152 + break; 1.153 + 1.154 + case NONEMPTY: 1.155 + expected = (cr || rr ); // any annotation in class file 1.156 + break; 1.157 + 1.158 + default: 1.159 + throw new IllegalStateException(); 1.160 + } 1.161 + 1.162 + if (exists && !expected) 1.163 + error("package-info.class found but not expected"); 1.164 + if (!exists && expected) 1.165 + error("package-info.class expected but not found"); 1.166 + } 1.167 + 1.168 + /** Compile files with options provided. */ 1.169 + void compile(List<String> opts, List<File> files) throws Exception { 1.170 + System.err.println("javac: " + opts + " " + files); 1.171 + List<String> args = new ArrayList<>(); 1.172 + args.addAll(opts); 1.173 + for (File f: files) 1.174 + args.add(f.getPath()); 1.175 + StringWriter sw = new StringWriter(); 1.176 + PrintWriter pw = new PrintWriter(sw); 1.177 + int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); 1.178 + pw.flush(); 1.179 + if (sw.getBuffer().length() > 0) 1.180 + System.err.println(sw.toString()); 1.181 + if (rc != 0) 1.182 + throw new Exception("compilation failed: rc=" + rc); 1.183 + } 1.184 + 1.185 + /** Write a file with a given body. */ 1.186 + void writeFile(File f, String body) throws Exception { 1.187 + if (f.getParentFile() != null) 1.188 + f.getParentFile().mkdirs(); 1.189 + Writer out = new FileWriter(f); 1.190 + try { 1.191 + out.write(body); 1.192 + } finally { 1.193 + out.close(); 1.194 + } 1.195 + } 1.196 + 1.197 + /** Report an error. */ 1.198 + void error(String msg) { 1.199 + System.err.println("Error: " + msg); 1.200 + errors++; 1.201 + } 1.202 + 1.203 + /** Test case counter. */ 1.204 + int count; 1.205 + 1.206 + /** Number of errors found. */ 1.207 + int errors; 1.208 +}