1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/T6838467.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,228 @@ 1.4 +/* 1.5 + * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6838467 1.30 + * @summary JSR199 FileObjects don't obey general contract of equals. 1.31 + */ 1.32 + 1.33 +import java.io.*; 1.34 +import java.util.*; 1.35 +import java.util.zip.*; 1.36 +import javax.tools.*; 1.37 +import com.sun.tools.javac.file.JavacFileManager; 1.38 +import com.sun.tools.javac.util.Context; 1.39 +import com.sun.tools.javac.util.Options; 1.40 + 1.41 +public class T6838467 { 1.42 + boolean fileSystemIsCaseSignificant = !new File("a").equals(new File("A")); 1.43 + 1.44 + enum FileKind { 1.45 + DIR("dir"), 1.46 + ZIP("zip"), 1.47 + ZIPFILEINDEX("zip"); 1.48 + FileKind(String path) { 1.49 + file = new File(path); 1.50 + } 1.51 + final File file; 1.52 + }; 1.53 + 1.54 + enum CompareKind { 1.55 + SAME { 1.56 + File other(File f) { return f; } 1.57 + }, 1.58 + ABSOLUTE { 1.59 + File other(File f) { return f.getAbsoluteFile(); } 1.60 + }, 1.61 + DIFFERENT { 1.62 + File other(File f) { return new File("not_" + f.getPath()); } 1.63 + }, 1.64 + CASEEQUIV { 1.65 + File other(File f) { return new File(f.getPath().toUpperCase()); } 1.66 + }; 1.67 + abstract File other(File f); 1.68 + }; 1.69 + 1.70 + String[] paths = { "p/A.java", "p/B.java", "p/C.java" }; 1.71 + 1.72 + public static void main(String... args) throws Exception { 1.73 + new T6838467().run(); 1.74 + } 1.75 + 1.76 + void run() throws Exception { 1.77 + // on Windows, verify file system is not case significant 1.78 + if (System.getProperty("os.name").toLowerCase().startsWith("windows") 1.79 + && fileSystemIsCaseSignificant) { 1.80 + error("fileSystemIsCaseSignificant is set on Windows."); 1.81 + } 1.82 + 1.83 + // create a set of directories and zip files to compare 1.84 + createTestDir(new File("dir"), paths); 1.85 + createTestDir(new File("not_dir"), paths); 1.86 + createTestZip(new File("zip"), paths); 1.87 + createTestZip(new File("not_zip"), paths); 1.88 + if (fileSystemIsCaseSignificant) { 1.89 + createTestDir(new File("DIR"), paths); 1.90 + createTestZip(new File("ZIP"), paths); 1.91 + } 1.92 + 1.93 + // test the various sorts of file objects that can be obtained from 1.94 + // the file manager, and for various values that may or may not match. 1.95 + for (FileKind fk: FileKind.values()) { 1.96 + for (CompareKind ck: CompareKind.values()) { 1.97 + test(fk, ck); 1.98 + } 1.99 + } 1.100 + 1.101 + // verify that the various different types of file object were all 1.102 + // tested 1.103 + Set<String> expectClasses = new HashSet<String>(Arrays.asList( 1.104 + "RegularFileObject", "ZipFileObject", "ZipFileIndexFileObject" )); 1.105 + if (!foundClasses.equals(expectClasses)) { 1.106 + error("expected fileobject classes not found\n" 1.107 + + "expected: " + expectClasses + "\n" 1.108 + + "found: " + foundClasses); 1.109 + } 1.110 + 1.111 + if (errors > 0) 1.112 + throw new Exception(errors + " errors"); 1.113 + } 1.114 + 1.115 + void test(FileKind fk, CompareKind ck) throws IOException { 1.116 + File f1 = fk.file; 1.117 + JavaFileManager fm1 = createFileManager(fk, f1); 1.118 + 1.119 + File f2 = ck.other(fk.file); 1.120 + JavaFileManager fm2 = createFileManager(fk, f2); 1.121 + 1.122 + try { 1.123 + // If the directories or zip files match, we expect "n" matches in 1.124 + // the "n-squared" comparisons to come, where "n" is the number of 1.125 + // entries in the the directories or zip files. 1.126 + // If the directories or zip files don't themselves match, 1.127 + // we obviously don't expect any of their contents to match either. 1.128 + int expect = (f1.getAbsoluteFile().equals(f2.getAbsoluteFile()) ? paths.length : 0); 1.129 + 1.130 + System.err.println("test " + (++count) + " " + fk + " " + ck + " " + f1 + " " + f2); 1.131 + test(fm1, fm2, expect); 1.132 + 1.133 + } finally { 1.134 + fm1.close(); 1.135 + fm2.close(); 1.136 + } 1.137 + } 1.138 + 1.139 + // For a pair of file managers that may or may not have similar entries 1.140 + // on the classpath, compare all files returned from one against all files 1.141 + // returned from the other. For each pair of files, verify that if they 1.142 + // are equal, the hashcode is equal as well, and finally verify that the 1.143 + // expected number of matches was found. 1.144 + void test(JavaFileManager fm1, JavaFileManager fm2, int expectEqualCount) throws IOException { 1.145 + boolean foundFiles1 = false; 1.146 + boolean foundFiles2 = false; 1.147 + int foundEqualCount = 0; 1.148 + Set<JavaFileObject.Kind> kinds = EnumSet.allOf(JavaFileObject.Kind.class); 1.149 + for (FileObject fo1: fm1.list(StandardLocation.CLASS_PATH, "p", kinds, false)) { 1.150 + foundFiles1 = true; 1.151 + foundClasses.add(fo1.getClass().getSimpleName()); 1.152 + for (FileObject fo2: fm2.list(StandardLocation.CLASS_PATH, "p", kinds, false)) { 1.153 + foundFiles2 = true; 1.154 + foundClasses.add(fo1.getClass().getSimpleName()); 1.155 + System.err.println("compare " + fo1 + " " + fo2); 1.156 + if (fo1.equals(fo2)) { 1.157 + foundEqualCount++; 1.158 + int hash1 = fo1.hashCode(); 1.159 + int hash2 = fo2.hashCode(); 1.160 + if (hash1 != hash2) 1.161 + error("hashCode error: " + fo1 + " [" + hash1 + "] " 1.162 + + fo2 + " [" + hash2 + "]"); 1.163 + } 1.164 + } 1.165 + } 1.166 + if (!foundFiles1) 1.167 + error("no files found for file manager 1"); 1.168 + if (!foundFiles2) 1.169 + error("no files found for file manager 2"); 1.170 + // verify the expected number of matches were found 1.171 + if (foundEqualCount != expectEqualCount) 1.172 + error("expected matches not found: expected " + expectEqualCount + ", found " + foundEqualCount); 1.173 + } 1.174 + 1.175 + // create a file manager to test a FileKind, with a given directory 1.176 + // or zip file placed on the classpath 1.177 + JavaFileManager createFileManager(FileKind fk, File classpath) throws IOException { 1.178 + StandardJavaFileManager fm = createFileManager(fk == FileKind.ZIP); 1.179 + fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classpath)); 1.180 + return fm; 1.181 + } 1.182 + 1.183 + JavacFileManager createFileManager(boolean useOptimizedZip) { 1.184 + Context ctx = new Context(); 1.185 + Options options = Options.instance(ctx); 1.186 + options.put("useOptimizedZip", Boolean.toString(useOptimizedZip)); 1.187 + return new JavacFileManager(ctx, false, null); 1.188 + } 1.189 + 1.190 + // create a directory containing a given set of paths 1.191 + void createTestDir(File dir, String[] paths) throws IOException { 1.192 + for (String p: paths) { 1.193 + File file = new File(dir, p); 1.194 + file.getParentFile().mkdirs(); 1.195 + FileWriter out = new FileWriter(file); 1.196 + try { 1.197 + out.write(p); 1.198 + } finally { 1.199 + out.close(); 1.200 + } 1.201 + } 1.202 + } 1.203 + 1.204 + // create a sip file containing a given set of entries 1.205 + void createTestZip(File zip, String[] paths) throws IOException { 1.206 + if (zip.getParentFile() != null) 1.207 + zip.getParentFile().mkdirs(); 1.208 + ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip)); 1.209 + try { 1.210 + for (String p: paths) { 1.211 + ZipEntry ze = new ZipEntry(p); 1.212 + zos.putNextEntry(ze); 1.213 + byte[] bytes = p.getBytes(); 1.214 + zos.write(bytes, 0, bytes.length); 1.215 + zos.closeEntry(); 1.216 + } 1.217 + } finally { 1.218 + zos.close(); 1.219 + } 1.220 + } 1.221 + 1.222 + void error(String msg) { 1.223 + System.err.println("Error: " + msg); 1.224 + errors++; 1.225 + } 1.226 + 1.227 + int count; 1.228 + int errors; 1.229 + Set<String> foundClasses = new HashSet<String>(); 1.230 +} 1.231 +