Tue, 08 Jan 2013 13:47:57 +0000
8005167: execution time of combo tests in javac should be improved
Reviewed-by: jjg, jjh
jjg@415 | 1 | /* |
katleman@1013 | 2 | * Copyright (c) 2009, 2011, 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 | |
ksrini@882 | 62 | for (boolean useOptimizedZip: new boolean[] { false, true }) { |
ksrini@882 | 63 | test(createFileManager(useOptimizedZip), createJar("jar", entries), "p", entries); |
ksrini@882 | 64 | test(createFileManager(useOptimizedZip), createJar("jar jar", entries), "p", entries); |
jjg@415 | 65 | |
jjg@415 | 66 | for (boolean useSymbolFile: new boolean[] { false, true }) { |
ksrini@882 | 67 | test(createFileManager(useOptimizedZip, 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 | |
ksrini@882 | 148 | JavacFileManager createFileManager(boolean useOptimizedZip) { |
ksrini@882 | 149 | return createFileManager(useOptimizedZip, false); |
jjg@415 | 150 | } |
jjg@415 | 151 | |
ksrini@882 | 152 | JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) { |
ksrini@882 | 153 | Context c = new Context(); |
ksrini@882 | 154 | Options options = Options.instance(c); |
ksrini@882 | 155 | |
ksrini@923 | 156 | options.put("useOptimizedZip", Boolean.toString(useOptimizedZip)); |
jjg@415 | 157 | |
ksrini@923 | 158 | if (!useSymbolFile) { |
ksrini@923 | 159 | options.put("ignore.symbol.file", "true"); |
ksrini@923 | 160 | } |
ksrini@923 | 161 | return new JavacFileManager(c, false, null); |
jjg@415 | 162 | } |
jjg@415 | 163 | |
jjg@415 | 164 | File createDir(String name, String... entries) throws Exception { |
jjg@415 | 165 | File dir = new File(name); |
jjg@415 | 166 | if (!dir.mkdirs()) |
jjg@415 | 167 | throw new Exception("cannot create directories " + dir); |
jjg@415 | 168 | for (String e: entries) { |
jjg@415 | 169 | writeFile(new File(dir, e), e); |
jjg@415 | 170 | } |
jjg@415 | 171 | return dir; |
jjg@415 | 172 | } |
jjg@415 | 173 | |
jjg@415 | 174 | File createJar(String name, String... entries) throws IOException { |
jjg@415 | 175 | File jar = new File(name); |
jjg@415 | 176 | OutputStream out = new FileOutputStream(jar); |
jjg@415 | 177 | try { |
jjg@415 | 178 | JarOutputStream jos = new JarOutputStream(out); |
jjg@415 | 179 | for (String e: entries) { |
jjg@415 | 180 | jos.putNextEntry(new ZipEntry(e)); |
jjg@415 | 181 | jos.write(e.getBytes()); |
jjg@415 | 182 | } |
jjg@415 | 183 | jos.close(); |
jjg@415 | 184 | } finally { |
jjg@415 | 185 | out.close(); |
jjg@415 | 186 | } |
jjg@415 | 187 | return jar; |
jjg@415 | 188 | } |
jjg@415 | 189 | |
jjg@415 | 190 | File findRtJar() throws Exception { |
jjg@415 | 191 | File java_home = new File(System.getProperty("java.home")); |
jjg@415 | 192 | if (java_home.getName().equals("jre")) |
jjg@415 | 193 | java_home = java_home.getParentFile(); |
jjg@415 | 194 | File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar"); |
jjg@415 | 195 | if (!rt_jar.exists()) |
jjg@415 | 196 | throw new Exception("can't find rt.jar"); |
jjg@415 | 197 | return rt_jar; |
jjg@415 | 198 | } |
jjg@415 | 199 | |
jjg@415 | 200 | byte[] read(InputStream in) throws IOException { |
jjg@415 | 201 | byte[] data = new byte[1024]; |
jjg@415 | 202 | int offset = 0; |
jjg@415 | 203 | try { |
jjg@415 | 204 | int n; |
jjg@415 | 205 | while ((n = in.read(data, offset, data.length - offset)) != -1) { |
jjg@415 | 206 | offset += n; |
jjg@415 | 207 | if (offset == data.length) |
jjg@415 | 208 | data = Arrays.copyOf(data, 2 * data.length); |
jjg@415 | 209 | } |
jjg@415 | 210 | } finally { |
jjg@415 | 211 | in.close(); |
jjg@415 | 212 | } |
jjg@415 | 213 | return Arrays.copyOf(data, offset); |
jjg@415 | 214 | } |
jjg@415 | 215 | |
jjg@415 | 216 | void writeFile(File f, String s) throws IOException { |
jjg@415 | 217 | f.getParentFile().mkdirs(); |
jjg@415 | 218 | FileWriter out = new FileWriter(f); |
jjg@415 | 219 | try { |
jjg@415 | 220 | out.write(s); |
jjg@415 | 221 | } finally { |
jjg@415 | 222 | out.close(); |
jjg@415 | 223 | } |
jjg@415 | 224 | } |
jjg@415 | 225 | |
jjg@415 | 226 | void error(String msg) { |
jjg@415 | 227 | System.err.println(msg); |
jjg@415 | 228 | errors++; |
jjg@415 | 229 | } |
jjg@415 | 230 | |
jjg@415 | 231 | int errors; |
jjg@415 | 232 | } |