test/tools/javac/api/T6838467.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1161
42ffceeceeca
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial