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