test/tools/javac/TestPkgInfo.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1941
d601238641e6
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

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

mercurial