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