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