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