test/tools/javac/api/T6838467.java

Wed, 13 Aug 2014 14:50:00 -0700

author
katleman
date
Wed, 13 Aug 2014 14:50:00 -0700
changeset 2549
0b6cc4ea670f
parent 1161
42ffceeceeca
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u40-b01 for changeset bf89a471779d

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

mercurial