test/tools/javac/api/6411310/Test.java

Mon, 13 Dec 2010 14:56:50 +0000

author
mcimadamore
date
Mon, 13 Dec 2010 14:56:50 +0000
changeset 792
2199365892b1
parent 554
9d9f26857129
child 882
3d45cc94ee0f
permissions
-rw-r--r--

7006109: Add test library to simplify the task of writing automated type-system tests
Summary: Types.java needs to be more stress-tested
Reviewed-by: jjg

jjg@415 1 /*
ohair@554 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
jjg@415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@415 4 *
jjg@415 5 * This code is free software; you can redistribute it and/or modify it
jjg@415 6 * under the terms of the GNU General Public License version 2 only, as
jjg@415 7 * published by the Free Software Foundation.
jjg@415 8 *
jjg@415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@415 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@415 13 * accompanied this code).
jjg@415 14 *
jjg@415 15 * You should have received a copy of the GNU General Public License version
jjg@415 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@415 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
jjg@415 22 */
jjg@415 23
jjg@415 24 /*
jjg@415 25 * @test
jjg@415 26 * @bug 6410367 6411310
jjg@415 27 * @summary FileObject should support user-friendly names via getName()
jjg@415 28 */
jjg@415 29
jjg@415 30 import java.io.*;
jjg@415 31 import java.util.*;
jjg@415 32 import java.util.jar.*;
jjg@415 33 import java.util.zip.*;
jjg@415 34 import javax.tools.*;
jjg@415 35
jjg@415 36 import com.sun.tools.javac.file.JavacFileManager;
jjg@415 37 import com.sun.tools.javac.util.Context;
jjg@415 38 import com.sun.tools.javac.util.Options;
jjg@415 39
jjg@415 40 // Test FileObject.getName returned from JavacFileManager and its support classes.
jjg@415 41
jjg@415 42 public class Test {
jjg@415 43 public static void main(String... args) throws Exception {
jjg@415 44 new Test().run();
jjg@415 45 }
jjg@415 46
jjg@415 47 Set<String> foundClasses = new TreeSet<String>();
jjg@415 48 Set<String> foundJars = new TreeSet<String>();
jjg@415 49
jjg@415 50 void run() throws Exception {
jjg@415 51 File rt_jar = findRtJar();
jjg@415 52
jjg@415 53 // names for entries to be created in directories and jar files
jjg@415 54 String[] entries = { "p/A.java", "p/A.class", "p/resources/A-1.html" };
jjg@415 55
jjg@415 56 // test various combinations of directories and jar files, intended to
jjg@415 57 // cover all sources of file objects within JavacFileManager's support classes
jjg@415 58
jjg@415 59 test(createFileManager(), createDir("dir", entries), "p", entries);
jjg@415 60 test(createFileManager(), createDir("a b/dir", entries), "p", entries);
jjg@415 61
jjg@415 62 for (boolean useJavaUtilZip: new boolean[] { false, true }) {
jjg@415 63 test(createFileManager(useJavaUtilZip), createJar("jar", entries), "p", entries);
jjg@415 64 test(createFileManager(useJavaUtilZip), createJar("jar jar", entries), "p", entries);
jjg@415 65
jjg@415 66 for (boolean useSymbolFile: new boolean[] { false, true }) {
jjg@415 67 test(createFileManager(useJavaUtilZip, useSymbolFile), rt_jar, "java.lang.ref", null);
jjg@415 68 }
jjg@415 69 }
jjg@415 70
jjg@415 71 if (errors > 0)
jjg@415 72 throw new Exception(errors + " errors found");
jjg@415 73
jjg@415 74 // Verify that we hit all the impl classes we intended
jjg@415 75 checkCoverage("classes", foundClasses,
jjg@415 76 "RegularFileObject", "SymbolFileObject", "ZipFileIndexFileObject", "ZipFileObject");
jjg@415 77
jjg@415 78 // Verify that we hit the jar files we intended, specifically ct.sym as well as rt.jar
jjg@415 79 checkCoverage("jar files", foundJars,
jjg@415 80 "ct.sym", "jar", "jar jar", "rt.jar");
jjg@415 81 }
jjg@415 82
jjg@415 83 // use a new file manager for each test
jjg@415 84 void test(StandardJavaFileManager fm, File f, String pkg, String[] entries) throws Exception {
jjg@415 85 System.err.println("Test " + f);
jjg@415 86 try {
jjg@415 87 if (f.isDirectory()) {
jjg@415 88 for (File dir: new File[] { f, f.getAbsoluteFile() }) {
jjg@415 89 for (String e: entries) {
jjg@415 90 JavaFileObject fo = fm.getJavaFileObjects(new File(dir, e)).iterator().next();
jjg@415 91 test(fo, dir, e);
jjg@415 92 }
jjg@415 93 }
jjg@415 94 }
jjg@415 95
jjg@415 96 fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f));
jjg@415 97 fm.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(f.getAbsoluteFile()));
jjg@415 98 for (StandardLocation l: EnumSet.of(StandardLocation.CLASS_PATH, StandardLocation.SOURCE_PATH)) {
jjg@415 99 for (JavaFileObject fo: fm.list(l, pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) {
jjg@415 100 // we could use fm.getLocation but the following guarantees we preserve the original filename
jjg@415 101 File dir = (l == StandardLocation.CLASS_PATH ? f : f.getAbsoluteFile());
jjg@415 102 char sep = (dir.isDirectory() ? File.separatorChar : '/');
jjg@415 103 String b = fm.inferBinaryName(l, fo);
jjg@415 104 String e = fo.getKind().extension;
jjg@415 105 test(fo, dir, b.replace('.', sep) + e);
jjg@415 106 }
jjg@415 107 }
jjg@415 108 } finally {
jjg@415 109 fm.close();
jjg@415 110 }
jjg@415 111 }
jjg@415 112
jjg@415 113 void test(JavaFileObject fo, File dir, String p) {
jjg@415 114 System.err.println("Test: " + fo);
jjg@415 115 String expect = dir.isDirectory() ? new File(dir, p).getPath() : (dir.getPath() + "(" + p + ")");
jjg@415 116 String found = fo.getName();
jjg@415 117 // if ct.sym is found, replace it with the equivalent rt.jar
jjg@415 118 String found2 = found.replaceAll("lib([\\\\/])ct.sym\\(META-INF/sym/rt.jar/", "jre$1lib$1rt.jar(");
jjg@415 119 if (!expect.equals(found2)) {
jjg@415 120 System.err.println("expected: " + expect);
jjg@415 121 System.err.println(" found: " + found);
jjg@415 122 if (!found.equals(found2))
jjg@415 123 System.err.println(" found2: " + found2);
jjg@415 124 error("Failed: " + fo);
jjg@415 125 }
jjg@415 126
jjg@415 127 // record the file object class name for coverage checks later
jjg@415 128 foundClasses.add(fo.getClass().getSimpleName());
jjg@415 129
jjg@415 130 if (found.contains("(")) {
jjg@415 131 // record access to the jar file for coverage checks later
jjg@415 132 foundJars.add(new File(found.substring(0, found.indexOf("("))).getName());
jjg@415 133 }
jjg@415 134 }
jjg@415 135
jjg@415 136 void checkCoverage(String label, Set<String> found, String... expect) throws Exception {
jjg@415 137 Set<String> e = new TreeSet<String>(Arrays.asList(expect));
jjg@415 138 if (!found.equals(e)) {
jjg@415 139 e.removeAll(found);
jjg@415 140 throw new Exception("expected " + label + " not used: " + e);
jjg@415 141 }
jjg@415 142 }
jjg@415 143
jjg@415 144 JavacFileManager createFileManager() {
jjg@415 145 return createFileManager(false, false);
jjg@415 146 }
jjg@415 147
jjg@415 148 JavacFileManager createFileManager(boolean useJavaUtilZip) {
jjg@415 149 return createFileManager(useJavaUtilZip, false);
jjg@415 150 }
jjg@415 151
jjg@415 152 JavacFileManager createFileManager(boolean useJavaUtilZip, boolean useSymbolFile) {
jjg@415 153 // javac should really not be using system properties like this
jjg@415 154 // -- it should really be using (hidden) options -- but until then
jjg@415 155 // take care to leave system properties as we find them, so as not
jjg@415 156 // to adversely affect other tests that might follow.
jjg@415 157 String prev = System.getProperty("useJavaUtilZip");
jjg@415 158 boolean resetProperties = false;
jjg@415 159 try {
jjg@415 160 if (useJavaUtilZip) {
jjg@415 161 System.setProperty("useJavaUtilZip", "true");
jjg@415 162 resetProperties = true;
jjg@415 163 } else if (System.getProperty("useJavaUtilZip") != null) {
jjg@415 164 System.getProperties().remove("useJavaUtilZip");
jjg@415 165 resetProperties = true;
jjg@415 166 }
jjg@415 167
jjg@415 168 Context c = new Context();
jjg@415 169 if (!useSymbolFile) {
jjg@415 170 Options options = Options.instance(c);
jjg@415 171 options.put("ignore.symbol.file", "true");
jjg@415 172 }
jjg@415 173
jjg@415 174 return new JavacFileManager(c, false, null);
jjg@415 175 } finally {
jjg@415 176 if (resetProperties) {
jjg@415 177 if (prev == null) {
jjg@415 178 System.getProperties().remove("useJavaUtilZip");
jjg@415 179 } else {
jjg@415 180 System.setProperty("useJavaUtilZip", prev);
jjg@415 181 }
jjg@415 182 }
jjg@415 183 }
jjg@415 184 }
jjg@415 185
jjg@415 186 File createDir(String name, String... entries) throws Exception {
jjg@415 187 File dir = new File(name);
jjg@415 188 if (!dir.mkdirs())
jjg@415 189 throw new Exception("cannot create directories " + dir);
jjg@415 190 for (String e: entries) {
jjg@415 191 writeFile(new File(dir, e), e);
jjg@415 192 }
jjg@415 193 return dir;
jjg@415 194 }
jjg@415 195
jjg@415 196 File createJar(String name, String... entries) throws IOException {
jjg@415 197 File jar = new File(name);
jjg@415 198 OutputStream out = new FileOutputStream(jar);
jjg@415 199 try {
jjg@415 200 JarOutputStream jos = new JarOutputStream(out);
jjg@415 201 for (String e: entries) {
jjg@415 202 jos.putNextEntry(new ZipEntry(e));
jjg@415 203 jos.write(e.getBytes());
jjg@415 204 }
jjg@415 205 jos.close();
jjg@415 206 } finally {
jjg@415 207 out.close();
jjg@415 208 }
jjg@415 209 return jar;
jjg@415 210 }
jjg@415 211
jjg@415 212 File findRtJar() throws Exception {
jjg@415 213 File java_home = new File(System.getProperty("java.home"));
jjg@415 214 if (java_home.getName().equals("jre"))
jjg@415 215 java_home = java_home.getParentFile();
jjg@415 216 File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar");
jjg@415 217 if (!rt_jar.exists())
jjg@415 218 throw new Exception("can't find rt.jar");
jjg@415 219 return rt_jar;
jjg@415 220 }
jjg@415 221
jjg@415 222 byte[] read(InputStream in) throws IOException {
jjg@415 223 byte[] data = new byte[1024];
jjg@415 224 int offset = 0;
jjg@415 225 try {
jjg@415 226 int n;
jjg@415 227 while ((n = in.read(data, offset, data.length - offset)) != -1) {
jjg@415 228 offset += n;
jjg@415 229 if (offset == data.length)
jjg@415 230 data = Arrays.copyOf(data, 2 * data.length);
jjg@415 231 }
jjg@415 232 } finally {
jjg@415 233 in.close();
jjg@415 234 }
jjg@415 235 return Arrays.copyOf(data, offset);
jjg@415 236 }
jjg@415 237
jjg@415 238 void writeFile(File f, String s) throws IOException {
jjg@415 239 f.getParentFile().mkdirs();
jjg@415 240 FileWriter out = new FileWriter(f);
jjg@415 241 try {
jjg@415 242 out.write(s);
jjg@415 243 } finally {
jjg@415 244 out.close();
jjg@415 245 }
jjg@415 246 }
jjg@415 247
jjg@415 248 void error(String msg) {
jjg@415 249 System.err.println(msg);
jjg@415 250 errors++;
jjg@415 251 }
jjg@415 252
jjg@415 253 int errors;
jjg@415 254 }

mercurial