test/tools/javac/TestPkgInfo.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1941
d601238641e6
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 6960424 8022161
aoqi@0 27 * @summary new option -Xpkginfo for better control of when package-info.class
aoqi@0 28 * is generated, also ensures no failures if package-info.java is
aoqi@0 29 * not available.
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.io.*;
aoqi@0 33 import java.util.*;
aoqi@0 34
aoqi@0 35 public class TestPkgInfo {
aoqi@0 36 enum OptKind {
aoqi@0 37 NONE(null),
aoqi@0 38 ALWAYS("-Xpkginfo:always"),
aoqi@0 39 NONEMPTY("-Xpkginfo:nonempty"),
aoqi@0 40 LEGACY("-Xpkginfo:legacy");
aoqi@0 41 OptKind(String opt) { this.opt = opt; }
aoqi@0 42 final String opt;
aoqi@0 43 };
aoqi@0 44
aoqi@0 45 public static void main(String... args) throws Exception {
aoqi@0 46 new TestPkgInfo().run(args);
aoqi@0 47 }
aoqi@0 48 public void run(String... args) throws Exception {
aoqi@0 49 testPositive();
aoqi@0 50 testNoExceptions();
aoqi@0 51 }
aoqi@0 52 public void testPositive(String... args) throws Exception {
aoqi@0 53 boolean[] booleanValues = { false, true };
aoqi@0 54 for (OptKind ok: OptKind.values()) {
aoqi@0 55 for (boolean sr: booleanValues) {
aoqi@0 56 for (boolean cr: booleanValues) {
aoqi@0 57 for (boolean rr: booleanValues) {
aoqi@0 58 try {
aoqi@0 59 test(ok, sr, cr, rr);
aoqi@0 60 } catch (Exception e) {
aoqi@0 61 error("Exception: " + e);
aoqi@0 62 }
aoqi@0 63 if (errors > 0) throw new AssertionError();
aoqi@0 64 }
aoqi@0 65 }
aoqi@0 66 }
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 if (errors > 0)
aoqi@0 70 throw new Exception(errors + " errors occurred");
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 /** this should throw no exceptions **/
aoqi@0 74 void testNoExceptions() throws Exception {
aoqi@0 75 count++;
aoqi@0 76 System.err.println("Test " + count + ": ALWAYS nofile");
aoqi@0 77
aoqi@0 78 StringBuilder sb = new StringBuilder();
aoqi@0 79 sb.append("package test; class Hello{}");
aoqi@0 80
aoqi@0 81 // test specific tmp directory
aoqi@0 82 File tmpDir = new File("tmp.test" + count);
aoqi@0 83 File classesDir = new File(tmpDir, "classes");
aoqi@0 84 classesDir.mkdirs();
aoqi@0 85 File javafile = new File(new File(tmpDir, "src"), "Hello.java");
aoqi@0 86 writeFile(javafile, sb.toString());
aoqi@0 87 // build up list of options and files to be compiled
aoqi@0 88 List<String> opts = new ArrayList<>();
aoqi@0 89 List<File> files = new ArrayList<>();
aoqi@0 90
aoqi@0 91 opts.add("-d");
aoqi@0 92 opts.add(classesDir.getPath());
aoqi@0 93 opts.add("-Xpkginfo:always");
aoqi@0 94 files.add(javafile);
aoqi@0 95
aoqi@0 96 compile(opts, files);
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 void test(OptKind ok, boolean sr, boolean cr, boolean rr) throws Exception {
aoqi@0 100 count++;
aoqi@0 101 System.err.println("Test " + count + ": ok:" + ok + " sr:" + sr + " cr:" + cr + " rr:" + rr);
aoqi@0 102
aoqi@0 103 StringBuilder sb = new StringBuilder();
aoqi@0 104
aoqi@0 105 // create annotated package statement with all combinations of retention policy
aoqi@0 106 if (sr) sb.append("@SR\n");
aoqi@0 107 if (cr) sb.append("@CR\n");
aoqi@0 108 if (rr) sb.append("@RR\n");
aoqi@0 109 sb.append("package p;\n");
aoqi@0 110 sb.append("\n");
aoqi@0 111
aoqi@0 112 sb.append("import java.lang.annotation.*;\n");
aoqi@0 113 sb.append("@Retention(RetentionPolicy.SOURCE) @interface SR { }\n");
aoqi@0 114 sb.append("@Retention(RetentionPolicy.CLASS) @interface CR { }\n");
aoqi@0 115 sb.append("@Retention(RetentionPolicy.RUNTIME) @interface RR { }\n");
aoqi@0 116
aoqi@0 117 // test specific tmp directory
aoqi@0 118 File tmpDir = new File("tmp.test" + count);
aoqi@0 119 File classesDir = new File(tmpDir, "classes");
aoqi@0 120 classesDir.mkdirs();
aoqi@0 121 File pkginfo_java = new File(new File(tmpDir, "src"), "package-info.java");
aoqi@0 122 writeFile(pkginfo_java, sb.toString());
aoqi@0 123
aoqi@0 124 // build up list of options and files to be compiled
aoqi@0 125 List<String> opts = new ArrayList<>();
aoqi@0 126 List<File> files = new ArrayList<>();
aoqi@0 127
aoqi@0 128 opts.add("-d");
aoqi@0 129 opts.add(classesDir.getPath());
aoqi@0 130 if (ok.opt != null)
aoqi@0 131 opts.add(ok.opt);
aoqi@0 132 //opts.add("-verbose");
aoqi@0 133 files.add(pkginfo_java);
aoqi@0 134
aoqi@0 135 compile(opts, files);
aoqi@0 136
aoqi@0 137 File pkginfo_class = new File(new File(classesDir, "p"), "package-info.class");
aoqi@0 138 boolean exists = pkginfo_class.exists();
aoqi@0 139
aoqi@0 140 boolean expected;
aoqi@0 141 switch (ok) {
aoqi@0 142 case ALWAYS:
aoqi@0 143 expected = true;
aoqi@0 144 break;
aoqi@0 145
aoqi@0 146 case LEGACY:
aoqi@0 147 case NONE:
aoqi@0 148 expected = (sr || cr || rr ); // any annotation
aoqi@0 149 break;
aoqi@0 150
aoqi@0 151 case NONEMPTY:
aoqi@0 152 expected = (cr || rr ); // any annotation in class file
aoqi@0 153 break;
aoqi@0 154
aoqi@0 155 default:
aoqi@0 156 throw new IllegalStateException();
aoqi@0 157 }
aoqi@0 158
aoqi@0 159 if (exists && !expected)
aoqi@0 160 error("package-info.class found but not expected");
aoqi@0 161 if (!exists && expected)
aoqi@0 162 error("package-info.class expected but not found");
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 /** Compile files with options provided. */
aoqi@0 166 void compile(List<String> opts, List<File> files) throws Exception {
aoqi@0 167 System.err.println("javac: " + opts + " " + files);
aoqi@0 168 List<String> args = new ArrayList<>();
aoqi@0 169 args.addAll(opts);
aoqi@0 170 for (File f: files)
aoqi@0 171 args.add(f.getPath());
aoqi@0 172 StringWriter sw = new StringWriter();
aoqi@0 173 PrintWriter pw = new PrintWriter(sw);
aoqi@0 174 int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw);
aoqi@0 175 pw.flush();
aoqi@0 176 if (sw.getBuffer().length() > 0)
aoqi@0 177 System.err.println(sw.toString());
aoqi@0 178 if (rc != 0)
aoqi@0 179 throw new Exception("compilation failed: rc=" + rc);
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 /** Write a file with a given body. */
aoqi@0 183 void writeFile(File f, String body) throws Exception {
aoqi@0 184 if (f.getParentFile() != null)
aoqi@0 185 f.getParentFile().mkdirs();
aoqi@0 186 Writer out = new FileWriter(f);
aoqi@0 187 try {
aoqi@0 188 out.write(body);
aoqi@0 189 } finally {
aoqi@0 190 out.close();
aoqi@0 191 }
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 /** Report an error. */
aoqi@0 195 void error(String msg) {
aoqi@0 196 System.err.println("Error: " + msg);
aoqi@0 197 errors++;
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 /** Test case counter. */
aoqi@0 201 int count;
aoqi@0 202
aoqi@0 203 /** Number of errors found. */
aoqi@0 204 int errors;
aoqi@0 205 }

mercurial