aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6960424 8022161 aoqi@0: * @summary new option -Xpkginfo for better control of when package-info.class aoqi@0: * is generated, also ensures no failures if package-info.java is aoqi@0: * not available. aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: aoqi@0: public class TestPkgInfo { aoqi@0: enum OptKind { aoqi@0: NONE(null), aoqi@0: ALWAYS("-Xpkginfo:always"), aoqi@0: NONEMPTY("-Xpkginfo:nonempty"), aoqi@0: LEGACY("-Xpkginfo:legacy"); aoqi@0: OptKind(String opt) { this.opt = opt; } aoqi@0: final String opt; aoqi@0: }; aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new TestPkgInfo().run(args); aoqi@0: } aoqi@0: public void run(String... args) throws Exception { aoqi@0: testPositive(); aoqi@0: testNoExceptions(); aoqi@0: } aoqi@0: public void testPositive(String... args) throws Exception { aoqi@0: boolean[] booleanValues = { false, true }; aoqi@0: for (OptKind ok: OptKind.values()) { aoqi@0: for (boolean sr: booleanValues) { aoqi@0: for (boolean cr: booleanValues) { aoqi@0: for (boolean rr: booleanValues) { aoqi@0: try { aoqi@0: test(ok, sr, cr, rr); aoqi@0: } catch (Exception e) { aoqi@0: error("Exception: " + e); aoqi@0: } aoqi@0: if (errors > 0) throw new AssertionError(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors occurred"); aoqi@0: } aoqi@0: aoqi@0: /** this should throw no exceptions **/ aoqi@0: void testNoExceptions() throws Exception { aoqi@0: count++; aoqi@0: System.err.println("Test " + count + ": ALWAYS nofile"); aoqi@0: aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: sb.append("package test; class Hello{}"); aoqi@0: aoqi@0: // test specific tmp directory aoqi@0: File tmpDir = new File("tmp.test" + count); aoqi@0: File classesDir = new File(tmpDir, "classes"); aoqi@0: classesDir.mkdirs(); aoqi@0: File javafile = new File(new File(tmpDir, "src"), "Hello.java"); aoqi@0: writeFile(javafile, sb.toString()); aoqi@0: // build up list of options and files to be compiled aoqi@0: List opts = new ArrayList<>(); aoqi@0: List files = new ArrayList<>(); aoqi@0: aoqi@0: opts.add("-d"); aoqi@0: opts.add(classesDir.getPath()); aoqi@0: opts.add("-Xpkginfo:always"); aoqi@0: files.add(javafile); aoqi@0: aoqi@0: compile(opts, files); aoqi@0: } aoqi@0: aoqi@0: void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception { aoqi@0: count++; aoqi@0: System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr); aoqi@0: aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: aoqi@0: // create annotated package statement with all combinations of retention policy aoqi@0: if (sr) sb.append("@SR\n"); aoqi@0: if (cr) sb.append("@CR\n"); aoqi@0: if (rr) sb.append("@RR\n"); aoqi@0: sb.append("package p;\n"); aoqi@0: sb.append("\n"); aoqi@0: aoqi@0: sb.append("import java.lang.annotation.*;\n"); aoqi@0: sb.append("@Retention(RetentionPolicy.SOURCE) @interface SR { }\n"); aoqi@0: sb.append("@Retention(RetentionPolicy.CLASS) @interface CR { }\n"); aoqi@0: sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n"); aoqi@0: aoqi@0: // test specific tmp directory aoqi@0: File tmpDir = new File("tmp.test" + count); aoqi@0: File classesDir = new File(tmpDir, "classes"); aoqi@0: classesDir.mkdirs(); aoqi@0: File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java"); aoqi@0: writeFile(pkginfo_java, sb.toString()); aoqi@0: aoqi@0: // build up list of options and files to be compiled aoqi@0: List opts = new ArrayList<>(); aoqi@0: List files = new ArrayList<>(); aoqi@0: aoqi@0: opts.add("-d"); aoqi@0: opts.add(classesDir.getPath()); aoqi@0: if (ok.opt != null) aoqi@0: opts.add(ok.opt); aoqi@0: //opts.add("-verbose"); aoqi@0: files.add(pkginfo_java); aoqi@0: aoqi@0: compile(opts, files); aoqi@0: aoqi@0: File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class"); aoqi@0: boolean exists = pkginfo_class.exists(); aoqi@0: aoqi@0: boolean expected; aoqi@0: switch (ok) { aoqi@0: case ALWAYS: aoqi@0: expected = true; aoqi@0: break; aoqi@0: aoqi@0: case LEGACY: aoqi@0: case NONE: aoqi@0: expected = (sr || cr || rr ); // any annotation aoqi@0: break; aoqi@0: aoqi@0: case NONEMPTY: aoqi@0: expected = (cr || rr ); // any annotation in class file aoqi@0: break; aoqi@0: aoqi@0: default: aoqi@0: throw new IllegalStateException(); aoqi@0: } aoqi@0: aoqi@0: if (exists && !expected) aoqi@0: error("package-info.class found but not expected"); aoqi@0: if (!exists && expected) aoqi@0: error("package-info.class expected but not found"); aoqi@0: } aoqi@0: aoqi@0: /** Compile files with options provided. */ aoqi@0: void compile(List opts, List files) throws Exception { aoqi@0: System.err.println("javac: " + opts + " " + files); aoqi@0: List args = new ArrayList<>(); aoqi@0: args.addAll(opts); aoqi@0: for (File f: files) aoqi@0: args.add(f.getPath()); aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); aoqi@0: pw.flush(); aoqi@0: if (sw.getBuffer().length() > 0) aoqi@0: System.err.println(sw.toString()); aoqi@0: if (rc != 0) aoqi@0: throw new Exception("compilation failed: rc=" + rc); aoqi@0: } aoqi@0: aoqi@0: /** Write a file with a given body. */ aoqi@0: void writeFile(File f, String body) throws Exception { aoqi@0: if (f.getParentFile() != null) aoqi@0: f.getParentFile().mkdirs(); aoqi@0: Writer out = new FileWriter(f); aoqi@0: try { aoqi@0: out.write(body); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Report an error. */ aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: /** Test case counter. */ aoqi@0: int count; aoqi@0: aoqi@0: /** Number of errors found. */ aoqi@0: int errors; aoqi@0: }