aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6838467 aoqi@0: * @summary JSR199 FileObjects don't obey general contract of equals. aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import java.util.zip.*; aoqi@0: import javax.tools.*; aoqi@0: import com.sun.tools.javac.file.JavacFileManager; aoqi@0: import com.sun.tools.javac.util.Context; aoqi@0: import com.sun.tools.javac.util.Options; aoqi@0: aoqi@0: public class T6838467 { aoqi@0: boolean fileSystemIsCaseSignificant = !new File("a").equals(new File("A")); aoqi@0: aoqi@0: enum FileKind { aoqi@0: DIR("dir"), aoqi@0: ZIP("zip"), aoqi@0: ZIPFILEINDEX("zip"); aoqi@0: FileKind(String path) { aoqi@0: file = new File(path); aoqi@0: } aoqi@0: final File file; aoqi@0: }; aoqi@0: aoqi@0: enum CompareKind { aoqi@0: SAME { aoqi@0: File other(File f) { return f; } aoqi@0: }, aoqi@0: ABSOLUTE { aoqi@0: File other(File f) { return f.getAbsoluteFile(); } aoqi@0: }, aoqi@0: DIFFERENT { aoqi@0: File other(File f) { return new File("not_" + f.getPath()); } aoqi@0: }, aoqi@0: CASEEQUIV { aoqi@0: File other(File f) { return new File(f.getPath().toUpperCase()); } aoqi@0: }; aoqi@0: abstract File other(File f); aoqi@0: }; aoqi@0: aoqi@0: String[] paths = { "p/A.java", "p/B.java", "p/C.java" }; aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new T6838467().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: // on Windows, verify file system is not case significant aoqi@0: if (System.getProperty("os.name").toLowerCase().startsWith("windows") aoqi@0: && fileSystemIsCaseSignificant) { aoqi@0: error("fileSystemIsCaseSignificant is set on Windows."); aoqi@0: } aoqi@0: aoqi@0: // create a set of directories and zip files to compare aoqi@0: createTestDir(new File("dir"), paths); aoqi@0: createTestDir(new File("not_dir"), paths); aoqi@0: createTestZip(new File("zip"), paths); aoqi@0: createTestZip(new File("not_zip"), paths); aoqi@0: if (fileSystemIsCaseSignificant) { aoqi@0: createTestDir(new File("DIR"), paths); aoqi@0: createTestZip(new File("ZIP"), paths); aoqi@0: } aoqi@0: aoqi@0: // test the various sorts of file objects that can be obtained from aoqi@0: // the file manager, and for various values that may or may not match. aoqi@0: for (FileKind fk: FileKind.values()) { aoqi@0: for (CompareKind ck: CompareKind.values()) { aoqi@0: test(fk, ck); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // verify that the various different types of file object were all aoqi@0: // tested aoqi@0: Set expectClasses = new HashSet(Arrays.asList( aoqi@0: "RegularFileObject", "ZipFileObject", "ZipFileIndexFileObject" )); aoqi@0: if (!foundClasses.equals(expectClasses)) { aoqi@0: error("expected fileobject classes not found\n" aoqi@0: + "expected: " + expectClasses + "\n" aoqi@0: + "found: " + foundClasses); aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors"); aoqi@0: } aoqi@0: aoqi@0: void test(FileKind fk, CompareKind ck) throws IOException { aoqi@0: File f1 = fk.file; aoqi@0: JavaFileManager fm1 = createFileManager(fk, f1); aoqi@0: aoqi@0: File f2 = ck.other(fk.file); aoqi@0: JavaFileManager fm2 = createFileManager(fk, f2); aoqi@0: aoqi@0: try { aoqi@0: // If the directories or zip files match, we expect "n" matches in aoqi@0: // the "n-squared" comparisons to come, where "n" is the number of aoqi@0: // entries in the the directories or zip files. aoqi@0: // If the directories or zip files don't themselves match, aoqi@0: // we obviously don't expect any of their contents to match either. aoqi@0: int expect = (f1.getAbsoluteFile().equals(f2.getAbsoluteFile()) ? paths.length : 0); aoqi@0: aoqi@0: System.err.println("test " + (++count) + " " + fk + " " + ck + " " + f1 + " " + f2); aoqi@0: test(fm1, fm2, expect); aoqi@0: aoqi@0: } finally { aoqi@0: fm1.close(); aoqi@0: fm2.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // For a pair of file managers that may or may not have similar entries aoqi@0: // on the classpath, compare all files returned from one against all files aoqi@0: // returned from the other. For each pair of files, verify that if they aoqi@0: // are equal, the hashcode is equal as well, and finally verify that the aoqi@0: // expected number of matches was found. aoqi@0: void test(JavaFileManager fm1, JavaFileManager fm2, int expectEqualCount) throws IOException { aoqi@0: boolean foundFiles1 = false; aoqi@0: boolean foundFiles2 = false; aoqi@0: int foundEqualCount = 0; aoqi@0: Set kinds = EnumSet.allOf(JavaFileObject.Kind.class); aoqi@0: for (FileObject fo1: fm1.list(StandardLocation.CLASS_PATH, "p", kinds, false)) { aoqi@0: foundFiles1 = true; aoqi@0: foundClasses.add(fo1.getClass().getSimpleName()); aoqi@0: for (FileObject fo2: fm2.list(StandardLocation.CLASS_PATH, "p", kinds, false)) { aoqi@0: foundFiles2 = true; aoqi@0: foundClasses.add(fo1.getClass().getSimpleName()); aoqi@0: System.err.println("compare " + fo1 + " " + fo2); aoqi@0: if (fo1.equals(fo2)) { aoqi@0: foundEqualCount++; aoqi@0: int hash1 = fo1.hashCode(); aoqi@0: int hash2 = fo2.hashCode(); aoqi@0: if (hash1 != hash2) aoqi@0: error("hashCode error: " + fo1 + " [" + hash1 + "] " aoqi@0: + fo2 + " [" + hash2 + "]"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (!foundFiles1) aoqi@0: error("no files found for file manager 1"); aoqi@0: if (!foundFiles2) aoqi@0: error("no files found for file manager 2"); aoqi@0: // verify the expected number of matches were found aoqi@0: if (foundEqualCount != expectEqualCount) aoqi@0: error("expected matches not found: expected " + expectEqualCount + ", found " + foundEqualCount); aoqi@0: } aoqi@0: aoqi@0: // create a file manager to test a FileKind, with a given directory aoqi@0: // or zip file placed on the classpath aoqi@0: JavaFileManager createFileManager(FileKind fk, File classpath) throws IOException { aoqi@0: StandardJavaFileManager fm = createFileManager(fk == FileKind.ZIP); aoqi@0: fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classpath)); aoqi@0: return fm; aoqi@0: } aoqi@0: aoqi@0: JavacFileManager createFileManager(boolean useOptimizedZip) { aoqi@0: Context ctx = new Context(); aoqi@0: Options options = Options.instance(ctx); aoqi@0: options.put("useOptimizedZip", Boolean.toString(useOptimizedZip)); aoqi@0: return new JavacFileManager(ctx, false, null); aoqi@0: } aoqi@0: aoqi@0: // create a directory containing a given set of paths aoqi@0: void createTestDir(File dir, String[] paths) throws IOException { aoqi@0: for (String p: paths) { aoqi@0: File file = new File(dir, p); aoqi@0: file.getParentFile().mkdirs(); aoqi@0: FileWriter out = new FileWriter(file); aoqi@0: try { aoqi@0: out.write(p); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // create a sip file containing a given set of entries aoqi@0: void createTestZip(File zip, String[] paths) throws IOException { aoqi@0: if (zip.getParentFile() != null) aoqi@0: zip.getParentFile().mkdirs(); aoqi@0: ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip)); aoqi@0: try { aoqi@0: for (String p: paths) { aoqi@0: ZipEntry ze = new ZipEntry(p); aoqi@0: zos.putNextEntry(ze); aoqi@0: byte[] bytes = p.getBytes(); aoqi@0: zos.write(bytes, 0, bytes.length); aoqi@0: zos.closeEntry(); aoqi@0: } aoqi@0: } finally { aoqi@0: zos.close(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int count; aoqi@0: int errors; aoqi@0: Set foundClasses = new HashSet(); aoqi@0: } aoqi@0: