test/tools/javac/api/T6838467.java

Mon, 06 Sep 2010 12:55:09 -0700

author
jjg
date
Mon, 06 Sep 2010 12:55:09 -0700
changeset 672
ea54372637a5
parent 554
9d9f26857129
child 882
3d45cc94ee0f
permissions
-rw-r--r--

6930507: Symbols for anonymous and local classes made too late for use by java tree API
Reviewed-by: mcimadamore

jjg@424 1 /*
ohair@554 2 * Copyright (c) 2009, 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;
jjg@424 36
jjg@424 37 public class T6838467 {
jjg@424 38 boolean fileSystemIsCaseSignificant = !new File("a").equals(new File("A"));
jjg@424 39
jjg@424 40 enum FileKind {
jjg@424 41 DIR("dir"),
jjg@424 42 ZIP("zip"),
jjg@424 43 ZIPFILEINDEX("zip");
jjg@424 44 FileKind(String path) {
jjg@424 45 file = new File(path);
jjg@424 46 }
jjg@424 47 final File file;
jjg@424 48 };
jjg@424 49
jjg@424 50 enum CompareKind {
jjg@424 51 SAME {
jjg@424 52 File other(File f) { return f; }
jjg@424 53 },
jjg@424 54 ABSOLUTE {
jjg@424 55 File other(File f) { return f.getAbsoluteFile(); }
jjg@424 56 },
jjg@424 57 DIFFERENT {
jjg@424 58 File other(File f) { return new File("not_" + f.getPath()); }
jjg@424 59 },
jjg@424 60 CASEEQUIV {
jjg@424 61 File other(File f) { return new File(f.getPath().toUpperCase()); }
jjg@424 62 };
jjg@424 63 abstract File other(File f);
jjg@424 64 };
jjg@424 65
jjg@424 66 String[] paths = { "p/A.java", "p/B.java", "p/C.java" };
jjg@424 67
jjg@424 68 public static void main(String... args) throws Exception {
jjg@424 69 new T6838467().run();
jjg@424 70 }
jjg@424 71
jjg@424 72 void run() throws Exception {
jjg@424 73 // on Windows, verify file system is not case significant
jjg@424 74 if (System.getProperty("os.name").toLowerCase().startsWith("windows")
jjg@424 75 && fileSystemIsCaseSignificant) {
jjg@424 76 error("fileSystemIsCaseSignificant is set on Windows.");
jjg@424 77 }
jjg@424 78
jjg@424 79 // create a set of directories and zip files to compare
jjg@424 80 createTestDir(new File("dir"), paths);
jjg@424 81 createTestDir(new File("not_dir"), paths);
jjg@424 82 createTestZip(new File("zip"), paths);
jjg@424 83 createTestZip(new File("not_zip"), paths);
jjg@424 84 if (fileSystemIsCaseSignificant) {
jjg@424 85 createTestDir(new File("DIR"), paths);
jjg@424 86 createTestZip(new File("ZIP"), paths);
jjg@424 87 }
jjg@424 88
jjg@424 89 // test the various sorts of file objects that can be obtained from
jjg@424 90 // the file manager, and for various values that may or may not match.
jjg@424 91 for (FileKind fk: FileKind.values()) {
jjg@424 92 for (CompareKind ck: CompareKind.values()) {
jjg@424 93 test(fk, ck);
jjg@424 94 }
jjg@424 95 }
jjg@424 96
jjg@424 97 // verify that the various different types of file object were all
jjg@424 98 // tested
jjg@424 99 Set<String> expectClasses = new HashSet<String>(Arrays.asList(
jjg@424 100 "RegularFileObject", "ZipFileObject", "ZipFileIndexFileObject" ));
jjg@424 101 if (!foundClasses.equals(expectClasses)) {
jjg@424 102 error("expected fileobject classes not found\n"
jjg@424 103 + "expected: " + expectClasses + "\n"
jjg@424 104 + "found: " + foundClasses);
jjg@424 105 }
jjg@424 106
jjg@424 107 if (errors > 0)
jjg@424 108 throw new Exception(errors + " errors");
jjg@424 109 }
jjg@424 110
jjg@424 111 void test(FileKind fk, CompareKind ck) throws IOException {
jjg@424 112 File f1 = fk.file;
jjg@424 113 JavaFileManager fm1 = createFileManager(fk, f1);
jjg@424 114
jjg@424 115 File f2 = ck.other(fk.file);
jjg@424 116 JavaFileManager fm2 = createFileManager(fk, f2);
jjg@424 117
jjg@424 118 try {
jjg@424 119 // If the directories or zip files match, we expect "n" matches in
jjg@424 120 // the "n-squared" comparisons to come, where "n" is the number of
jjg@424 121 // entries in the the directories or zip files.
jjg@424 122 // If the directories or zip files don't themselves match,
jjg@424 123 // we obviously don't expect any of their contents to match either.
jjg@424 124 int expect = (f1.getAbsoluteFile().equals(f2.getAbsoluteFile()) ? paths.length : 0);
jjg@424 125
jjg@424 126 System.err.println("test " + (++count) + " " + fk + " " + ck + " " + f1 + " " + f2);
jjg@424 127 test(fm1, fm2, expect);
jjg@424 128
jjg@424 129 } finally {
jjg@424 130 fm1.close();
jjg@424 131 fm2.close();
jjg@424 132 }
jjg@424 133 }
jjg@424 134
jjg@424 135 // For a pair of file managers that may or may not have similar entries
jjg@424 136 // on the classpath, compare all files returned from one against all files
jjg@424 137 // returned from the other. For each pair of files, verify that if they
jjg@424 138 // are equal, the hashcode is equal as well, and finally verify that the
jjg@424 139 // expected number of matches was found.
jjg@424 140 void test(JavaFileManager fm1, JavaFileManager fm2, int expectEqualCount) throws IOException {
jjg@424 141 boolean foundFiles1 = false;
jjg@424 142 boolean foundFiles2 = false;
jjg@424 143 int foundEqualCount = 0;
jjg@424 144 Set<JavaFileObject.Kind> kinds = EnumSet.allOf(JavaFileObject.Kind.class);
jjg@424 145 for (FileObject fo1: fm1.list(StandardLocation.CLASS_PATH, "p", kinds, false)) {
jjg@424 146 foundFiles1 = true;
jjg@424 147 foundClasses.add(fo1.getClass().getSimpleName());
jjg@424 148 for (FileObject fo2: fm2.list(StandardLocation.CLASS_PATH, "p", kinds, false)) {
jjg@424 149 foundFiles2 = true;
jjg@424 150 foundClasses.add(fo1.getClass().getSimpleName());
jjg@424 151 System.err.println("compare " + fo1 + " " + fo2);
jjg@424 152 if (fo1.equals(fo2)) {
jjg@424 153 foundEqualCount++;
jjg@424 154 int hash1 = fo1.hashCode();
jjg@424 155 int hash2 = fo2.hashCode();
jjg@424 156 if (hash1 != hash2)
jjg@424 157 error("hashCode error: " + fo1 + " [" + hash1 + "] "
jjg@424 158 + fo2 + " [" + hash2 + "]");
jjg@424 159 }
jjg@424 160 }
jjg@424 161 }
jjg@424 162 if (!foundFiles1)
jjg@424 163 error("no files found for file manager 1");
jjg@424 164 if (!foundFiles2)
jjg@424 165 error("no files found for file manager 2");
jjg@424 166 // verify the expected number of matches were found
jjg@424 167 if (foundEqualCount != expectEqualCount)
jjg@424 168 error("expected matches not found: expected " + expectEqualCount + ", found " + foundEqualCount);
jjg@424 169 }
jjg@424 170
jjg@424 171 // create a file manager to test a FileKind, with a given directory
jjg@424 172 // or zip file placed on the classpath
jjg@424 173 JavaFileManager createFileManager(FileKind fk, File classpath) throws IOException {
jjg@424 174 StandardJavaFileManager fm = createFileManager(fk == FileKind.ZIP);
jjg@424 175 fm.setLocation(StandardLocation.CLASS_PATH, Arrays.asList(classpath));
jjg@424 176 return fm;
jjg@424 177 }
jjg@424 178
jjg@424 179 JavacFileManager createFileManager(boolean useJavaUtilZip) {
jjg@424 180 // javac should really not be using system properties like this
jjg@424 181 // -- it should really be using (hidden) options -- but until then
jjg@424 182 // take care to leave system properties as we find them, so as not
jjg@424 183 // to adversely affect other tests that might follow.
jjg@424 184 String prev = System.getProperty("useJavaUtilZip");
jjg@424 185 boolean resetProperties = false;
jjg@424 186 try {
jjg@424 187 if (useJavaUtilZip) {
jjg@424 188 System.setProperty("useJavaUtilZip", "true");
jjg@424 189 resetProperties = true;
jjg@424 190 } else if (System.getProperty("useJavaUtilZip") != null) {
jjg@424 191 System.getProperties().remove("useJavaUtilZip");
jjg@424 192 resetProperties = true;
jjg@424 193 }
jjg@424 194
jjg@424 195 Context c = new Context();
jjg@424 196 return new JavacFileManager(c, false, null);
jjg@424 197 } finally {
jjg@424 198 if (resetProperties) {
jjg@424 199 if (prev == null) {
jjg@424 200 System.getProperties().remove("useJavaUtilZip");
jjg@424 201 } else {
jjg@424 202 System.setProperty("useJavaUtilZip", prev);
jjg@424 203 }
jjg@424 204 }
jjg@424 205 }
jjg@424 206 }
jjg@424 207
jjg@424 208 // create a directory containing a given set of paths
jjg@424 209 void createTestDir(File dir, String[] paths) throws IOException {
jjg@424 210 for (String p: paths) {
jjg@424 211 File file = new File(dir, p);
jjg@424 212 file.getParentFile().mkdirs();
jjg@424 213 FileWriter out = new FileWriter(file);
jjg@424 214 try {
jjg@424 215 out.write(p);
jjg@424 216 } finally {
jjg@424 217 out.close();
jjg@424 218 }
jjg@424 219 }
jjg@424 220 }
jjg@424 221
jjg@424 222 // create a sip file containing a given set of entries
jjg@424 223 void createTestZip(File zip, String[] paths) throws IOException {
jjg@424 224 if (zip.getParentFile() != null)
jjg@424 225 zip.getParentFile().mkdirs();
jjg@424 226 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
jjg@424 227 try {
jjg@424 228 for (String p: paths) {
jjg@424 229 ZipEntry ze = new ZipEntry(p);
jjg@424 230 zos.putNextEntry(ze);
jjg@424 231 byte[] bytes = p.getBytes();
jjg@424 232 zos.write(bytes, 0, bytes.length);
jjg@424 233 zos.closeEntry();
jjg@424 234 }
jjg@424 235 } finally {
jjg@424 236 zos.close();
jjg@424 237 }
jjg@424 238 }
jjg@424 239
jjg@424 240 void error(String msg) {
jjg@424 241 System.err.println("Error: " + msg);
jjg@424 242 errors++;
jjg@424 243 }
jjg@424 244
jjg@424 245 int count;
jjg@424 246 int errors;
jjg@424 247 Set<String> foundClasses = new HashSet<String>();
jjg@424 248 }
jjg@424 249

mercurial