Mon, 28 Feb 2011 13:42:24 -0800
7022711: compiler crash in try-with-resources
Reviewed-by: mcimadamore
jjg@424 | 1 | /* |
ksrini@882 | 2 | * Copyright (c) 2009, 2011 Oracle and/or its affiliates. All rights reserved. |
jjg@424 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@424 | 4 | * |
jjg@424 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@424 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@424 | 7 | * published by the Free Software Foundation. |
jjg@424 | 8 | * |
jjg@424 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@424 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@424 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@424 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@424 | 13 | * accompanied this code). |
jjg@424 | 14 | * |
jjg@424 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@424 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@424 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@424 | 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@424 | 22 | */ |
jjg@424 | 23 | |
jjg@424 | 24 | /* |
jjg@424 | 25 | * @test |
jjg@424 | 26 | * @bug 6838467 |
jjg@424 | 27 | * @summary JSR199 FileObjects don't obey general contract of equals. |
jjg@424 | 28 | */ |
jjg@424 | 29 | |
jjg@424 | 30 | import java.io.*; |
jjg@424 | 31 | import java.util.*; |
jjg@424 | 32 | import java.util.zip.*; |
jjg@424 | 33 | import javax.tools.*; |
jjg@424 | 34 | import com.sun.tools.javac.file.JavacFileManager; |
ksrini@882 | 35 | import com.sun.tools.javac.main.OptionName; |
jjg@424 | 36 | import com.sun.tools.javac.util.Context; |
ksrini@882 | 37 | import com.sun.tools.javac.util.Options; |
jjg@424 | 38 | |
jjg@424 | 39 | public class T6838467 { |
jjg@424 | 40 | boolean fileSystemIsCaseSignificant = !new File("a").equals(new File("A")); |
jjg@424 | 41 | |
jjg@424 | 42 | enum FileKind { |
jjg@424 | 43 | DIR("dir"), |
jjg@424 | 44 | ZIP("zip"), |
jjg@424 | 45 | ZIPFILEINDEX("zip"); |
jjg@424 | 46 | FileKind(String path) { |
jjg@424 | 47 | file = new File(path); |
jjg@424 | 48 | } |
jjg@424 | 49 | final File file; |
jjg@424 | 50 | }; |
jjg@424 | 51 | |
jjg@424 | 52 | enum CompareKind { |
jjg@424 | 53 | SAME { |
jjg@424 | 54 | File other(File f) { return f; } |
jjg@424 | 55 | }, |
jjg@424 | 56 | ABSOLUTE { |
jjg@424 | 57 | File other(File f) { return f.getAbsoluteFile(); } |
jjg@424 | 58 | }, |
jjg@424 | 59 | DIFFERENT { |
jjg@424 | 60 | File other(File f) { return new File("not_" + f.getPath()); } |
jjg@424 | 61 | }, |
jjg@424 | 62 | CASEEQUIV { |
jjg@424 | 63 | File other(File f) { return new File(f.getPath().toUpperCase()); } |
jjg@424 | 64 | }; |
jjg@424 | 65 | abstract File other(File f); |
jjg@424 | 66 | }; |
jjg@424 | 67 | |
jjg@424 | 68 | String[] paths = { "p/A.java", "p/B.java", "p/C.java" }; |
jjg@424 | 69 | |
jjg@424 | 70 | public static void main(String... args) throws Exception { |
jjg@424 | 71 | new T6838467().run(); |
jjg@424 | 72 | } |
jjg@424 | 73 | |
jjg@424 | 74 | void run() throws Exception { |
jjg@424 | 75 | // on Windows, verify file system is not case significant |
jjg@424 | 76 | if (System.getProperty("os.name").toLowerCase().startsWith("windows") |
jjg@424 | 77 | && fileSystemIsCaseSignificant) { |
jjg@424 | 78 | error("fileSystemIsCaseSignificant is set on Windows."); |
jjg@424 | 79 | } |
jjg@424 | 80 | |
jjg@424 | 81 | // create a set of directories and zip files to compare |
jjg@424 | 82 | createTestDir(new File("dir"), paths); |
jjg@424 | 83 | createTestDir(new File("not_dir"), paths); |
jjg@424 | 84 | createTestZip(new File("zip"), paths); |
jjg@424 | 85 | createTestZip(new File("not_zip"), paths); |
jjg@424 | 86 | if (fileSystemIsCaseSignificant) { |
jjg@424 | 87 | createTestDir(new File("DIR"), paths); |
jjg@424 | 88 | createTestZip(new File("ZIP"), paths); |
jjg@424 | 89 | } |
jjg@424 | 90 | |
jjg@424 | 91 | // test the various sorts of file objects that can be obtained from |
jjg@424 | 92 | // the file manager, and for various values that may or may not match. |
jjg@424 | 93 | for (FileKind fk: FileKind.values()) { |
jjg@424 | 94 | for (CompareKind ck: CompareKind.values()) { |
jjg@424 | 95 | test(fk, ck); |
jjg@424 | 96 | } |
jjg@424 | 97 | } |
jjg@424 | 98 | |
jjg@424 | 99 | // verify that the various different types of file object were all |
jjg@424 | 100 | // tested |
jjg@424 | 101 | Set<String> expectClasses = new HashSet<String>(Arrays.asList( |
jjg@424 | 102 | "RegularFileObject", "ZipFileObject", "ZipFileIndexFileObject" )); |
jjg@424 | 103 | if (!foundClasses.equals(expectClasses)) { |
jjg@424 | 104 | error("expected fileobject classes not found\n" |
jjg@424 | 105 | + "expected: " + expectClasses + "\n" |
jjg@424 | 106 | + "found: " + foundClasses); |
jjg@424 | 107 | } |
jjg@424 | 108 | |
jjg@424 | 109 | if (errors > 0) |
jjg@424 | 110 | throw new Exception(errors + " errors"); |
jjg@424 | 111 | } |
jjg@424 | 112 | |
jjg@424 | 113 | void test(FileKind fk, CompareKind ck) throws IOException { |
jjg@424 | 114 | File f1 = fk.file; |
jjg@424 | 115 | JavaFileManager fm1 = createFileManager(fk, f1); |
jjg@424 | 116 | |
jjg@424 | 117 | File f2 = ck.other(fk.file); |
jjg@424 | 118 | JavaFileManager fm2 = createFileManager(fk, f2); |
jjg@424 | 119 | |
jjg@424 | 120 | try { |
jjg@424 | 121 | // If the directories or zip files match, we expect "n" matches in |
jjg@424 | 122 | // the "n-squared" comparisons to come, where "n" is the number of |
jjg@424 | 123 | // entries in the the directories or zip files. |
jjg@424 | 124 | // If the directories or zip files don't themselves match, |
jjg@424 | 125 | // we obviously don't expect any of their contents to match either. |
jjg@424 | 126 | int expect = (f1.getAbsoluteFile().equals(f2.getAbsoluteFile()) ? paths.length : 0); |
jjg@424 | 127 | |
jjg@424 | 128 | System.err.println("test " + (++count) + " " + fk + " " + ck + " " + f1 + " " + f2); |
jjg@424 | 129 | test(fm1, fm2, expect); |
jjg@424 | 130 | |
jjg@424 | 131 | } finally { |
jjg@424 | 132 | fm1.close(); |
jjg@424 | 133 | fm2.close(); |
jjg@424 | 134 | } |
jjg@424 | 135 | } |
jjg@424 | 136 | |
jjg@424 | 137 | // For a pair of file managers that may or may not have similar entries |
jjg@424 | 138 | // on the classpath, compare all files returned from one against all files |
jjg@424 | 139 | // returned from the other. For each pair of files, verify that if they |
jjg@424 | 140 | // are equal, the hashcode is equal as well, and finally verify that the |
jjg@424 | 141 | // expected number of matches was found. |
jjg@424 | 142 | void test(JavaFileManager fm1, JavaFileManager fm2, int expectEqualCount) throws IOException { |
jjg@424 | 143 | boolean foundFiles1 = false; |
jjg@424 | 144 | boolean foundFiles2 = false; |
jjg@424 | 145 | int foundEqualCount = 0; |
jjg@424 | 146 | Set<JavaFileObject.Kind> kinds = EnumSet.allOf(JavaFileObject.Kind.class); |
jjg@424 | 147 | for (FileObject fo1: fm1.list(StandardLocation.CLASS_PATH, "p", kinds, false)) { |
jjg@424 | 148 | foundFiles1 = true; |
jjg@424 | 149 | foundClasses.add(fo1.getClass().getSimpleName()); |
jjg@424 | 150 | for (FileObject fo2: fm2.list(StandardLocation.CLASS_PATH, "p", kinds, false)) { |
jjg@424 | 151 | foundFiles2 = true; |
jjg@424 | 152 | foundClasses.add(fo1.getClass().getSimpleName()); |
jjg@424 | 153 | System.err.println("compare " + fo1 + " " + fo2); |
jjg@424 | 154 | if (fo1.equals(fo2)) { |
jjg@424 | 155 | foundEqualCount++; |
jjg@424 | 156 | int hash1 = fo1.hashCode(); |
jjg@424 | 157 | int hash2 = fo2.hashCode(); |
jjg@424 | 158 | if (hash1 != hash2) |
jjg@424 | 159 | error("hashCode error: " + fo1 + " [" + hash1 + "] " |
jjg@424 | 160 | + fo2 + " [" + hash2 + "]"); |
jjg@424 | 161 | } |
jjg@424 | 162 | } |
jjg@424 | 163 | } |
jjg@424 | 164 | if (!foundFiles1) |
jjg@424 | 165 | error("no files found for file manager 1"); |
jjg@424 | 166 | if (!foundFiles2) |
jjg@424 | 167 | error("no files found for file manager 2"); |
jjg@424 | 168 | // verify the expected number of matches were found |
jjg@424 | 169 | if (foundEqualCount != expectEqualCount) |
jjg@424 | 170 | error("expected matches not found: expected " + expectEqualCount + ", found " + foundEqualCount); |
jjg@424 | 171 | } |
jjg@424 | 172 | |
jjg@424 | 173 | // create a file manager to test a FileKind, with a given directory |
jjg@424 | 174 | // or zip file placed on the classpath |
jjg@424 | 175 | JavaFileManager createFileManager(FileKind fk, File classpath) throws IOException { |
jjg@424 | 176 | StandardJavaFileManager fm = createFileManager(fk == FileKind.ZIP); |
jjg@424 | 177 | fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classpath)); |
jjg@424 | 178 | return fm; |
jjg@424 | 179 | } |
jjg@424 | 180 | |
ksrini@882 | 181 | JavacFileManager createFileManager(boolean useOptimedZipIndex) { |
ksrini@882 | 182 | Context ctx = new Context(); |
ksrini@882 | 183 | if (useOptimedZipIndex) { |
ksrini@882 | 184 | Options options = Options.instance(ctx); |
ksrini@882 | 185 | options.put("useOptimizedZip", "true"); |
jjg@424 | 186 | } |
ksrini@882 | 187 | return new JavacFileManager(ctx, false, null); |
jjg@424 | 188 | } |
jjg@424 | 189 | |
jjg@424 | 190 | // create a directory containing a given set of paths |
jjg@424 | 191 | void createTestDir(File dir, String[] paths) throws IOException { |
jjg@424 | 192 | for (String p: paths) { |
jjg@424 | 193 | File file = new File(dir, p); |
jjg@424 | 194 | file.getParentFile().mkdirs(); |
jjg@424 | 195 | FileWriter out = new FileWriter(file); |
jjg@424 | 196 | try { |
jjg@424 | 197 | out.write(p); |
jjg@424 | 198 | } finally { |
jjg@424 | 199 | out.close(); |
jjg@424 | 200 | } |
jjg@424 | 201 | } |
jjg@424 | 202 | } |
jjg@424 | 203 | |
jjg@424 | 204 | // create a sip file containing a given set of entries |
jjg@424 | 205 | void createTestZip(File zip, String[] paths) throws IOException { |
jjg@424 | 206 | if (zip.getParentFile() != null) |
jjg@424 | 207 | zip.getParentFile().mkdirs(); |
jjg@424 | 208 | ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip)); |
jjg@424 | 209 | try { |
jjg@424 | 210 | for (String p: paths) { |
jjg@424 | 211 | ZipEntry ze = new ZipEntry(p); |
jjg@424 | 212 | zos.putNextEntry(ze); |
jjg@424 | 213 | byte[] bytes = p.getBytes(); |
jjg@424 | 214 | zos.write(bytes, 0, bytes.length); |
jjg@424 | 215 | zos.closeEntry(); |
jjg@424 | 216 | } |
jjg@424 | 217 | } finally { |
jjg@424 | 218 | zos.close(); |
jjg@424 | 219 | } |
jjg@424 | 220 | } |
jjg@424 | 221 | |
jjg@424 | 222 | void error(String msg) { |
jjg@424 | 223 | System.err.println("Error: " + msg); |
jjg@424 | 224 | errors++; |
jjg@424 | 225 | } |
jjg@424 | 226 | |
jjg@424 | 227 | int count; |
jjg@424 | 228 | int errors; |
jjg@424 | 229 | Set<String> foundClasses = new HashSet<String>(); |
jjg@424 | 230 | } |
jjg@424 | 231 |