langtools/test/tools/javac/api/T6838467.java
changeset 34560 b6a567b677f7
parent 30730 d3ce7619db2c
--- a/langtools/test/tools/javac/api/T6838467.java	Mon Dec 07 09:18:07 2015 -0800
+++ b/langtools/test/tools/javac/api/T6838467.java	Mon Dec 07 14:02:55 2015 -0800
@@ -32,18 +32,18 @@
 import java.io.*;
 import java.util.*;
 import java.util.zip.*;
+
 import javax.tools.*;
+import javax.tools.JavaFileManager.Location;
+
 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");
+        ZIP("zip");
         FileKind(String path) {
             file = new File(path);
         }
@@ -52,15 +52,19 @@
 
     enum CompareKind {
         SAME {
+            @Override
             File other(File f) { return f; }
         },
         ABSOLUTE {
+            @Override
             File other(File f) { return f.getAbsoluteFile(); }
         },
         DIFFERENT {
+            @Override
             File other(File f) { return new File("not_" + f.getPath()); }
         },
         CASEEQUIV {
+            @Override
             File other(File f) { return new File(f.getPath().toUpperCase()); }
         };
         abstract File other(File f);
@@ -73,10 +77,17 @@
     }
 
     void run() throws Exception {
+        boolean fileNameIsCaseSignificant = isFileNameCaseSignificant();
+        boolean fileLookupIsCaseSignificant = isFileLookupCaseSignificant();
+
+        String osName = System.getProperty("os.name");
+        System.err.println("OS: " + osName);
+        System.err.println("fileNameIsCaseSignificant:" + fileNameIsCaseSignificant);
+        System.err.println("fileLookupIsCaseSignificant:" + fileLookupIsCaseSignificant);
+
         // on Windows, verify file system is not case significant
-        if (System.getProperty("os.name").toLowerCase().startsWith("windows")
-                && fileSystemIsCaseSignificant) {
-            error("fileSystemIsCaseSignificant is set on Windows.");
+        if ((osName.startsWith("windows")) && fileNameIsCaseSignificant) {
+            error("fileNameIsCaseSignificant is set on " + osName + ".");
         }
 
         // create a set of directories and zip files to compare
@@ -84,7 +95,7 @@
         createTestDir(new File("not_dir"), paths);
         createTestZip(new File("zip"), paths);
         createTestZip(new File("not_zip"), paths);
-        if (fileSystemIsCaseSignificant) {
+        if (fileNameIsCaseSignificant || fileLookupIsCaseSignificant) {
             createTestDir(new File("DIR"), paths);
             createTestZip(new File("ZIP"), paths);
         }
@@ -99,8 +110,9 @@
 
         // verify that the various different types of file object were all
         // tested
-        Set<String> expectClasses = new HashSet<String>(Arrays.asList(
-                "RegularFileObject", "ZipFileObject", "ZipFileIndexFileObject" ));
+        Set<String> expectClasses = new HashSet<>(Arrays.asList(
+                "DirectoryFileObject",
+                "JarFileObject" ));
         if (!foundClasses.equals(expectClasses)) {
             error("expected fileobject classes not found\n"
                     + "expected: " + expectClasses + "\n"
@@ -112,26 +124,22 @@
     }
 
     void test(FileKind fk, CompareKind ck) throws IOException {
-        File f1 = fk.file;
-        JavaFileManager fm1 = createFileManager(fk, f1);
+        try (StandardJavaFileManager fm = createFileManager()) {
+            File f1 = fk.file;
+            Location l1 = createLocation(fm, "l1", f1);
 
-        File f2 = ck.other(fk.file);
-        JavaFileManager fm2 = createFileManager(fk, f2);
+            File f2 = ck.other(fk.file);
+            Location l2 = createLocation(fm, "l2", 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);
+            int expectEqualCount = (f1.getCanonicalFile().equals(f2.getCanonicalFile()) ? paths.length : 0);
 
             System.err.println("test " + (++count) + " " + fk + " " + ck + " " + f1 + " " + f2);
-            test(fm1, fm2, expect);
-
-        } finally {
-            fm1.close();
-            fm2.close();
+            test(fm, l1, l2, expectEqualCount);
         }
     }
 
@@ -140,17 +148,17 @@
     // 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 {
+    void test(JavaFileManager fm, Location l1, Location l2, 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)) {
+        for (FileObject fo1: fm.list(l1, "p", kinds, false)) {
             foundFiles1 = true;
             foundClasses.add(fo1.getClass().getSimpleName());
-            for (FileObject fo2: fm2.list(StandardLocation.CLASS_PATH, "p", kinds, false)) {
+            for (FileObject fo2: fm.list(l2, "p", kinds, false)) {
                 foundFiles2 = true;
-                foundClasses.add(fo1.getClass().getSimpleName());
+                foundClasses.add(fo2.getClass().getSimpleName());
                 System.err.println("compare " + fo1 + " " + fo2);
                 if (fo1.equals(fo2)) {
                     foundEqualCount++;
@@ -163,26 +171,35 @@
             }
         }
         if (!foundFiles1)
-            error("no files found for file manager 1");
+            error("no files found for location " + l1);
         if (!foundFiles2)
-            error("no files found for file manager 2");
+            error("no files found for location " + l2);
         // 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;
+    // create and initialize a location to test a FileKind, with a given directory
+    // or zip file placed on the path
+    Location createLocation(StandardJavaFileManager fm, String name, File classpath) throws IOException {
+        Location l = new Location() {
+            @Override
+            public String getName() {
+                return name;
+            }
+
+            @Override
+            public boolean isOutputLocation() {
+                return false;
+            }
+
+        };
+        fm.setLocation(l, Arrays.asList(classpath));
+        return l;
     }
 
-    JavacFileManager createFileManager(boolean useOptimizedZip) {
+    JavacFileManager createFileManager() {
         Context ctx = new Context();
-        Options options = Options.instance(ctx);
-        options.put("useOptimizedZip", Boolean.toString(useOptimizedZip));
         return new JavacFileManager(ctx, false, null);
     }
 
@@ -191,21 +208,17 @@
         for (String p: paths) {
             File file = new File(dir, p);
             file.getParentFile().mkdirs();
-            FileWriter out = new FileWriter(file);
-            try {
+            try (FileWriter out = new FileWriter(file)) {
                 out.write(p);
-            } finally {
-                out.close();
             }
         }
     }
 
-    // create a sip file containing a given set of entries
+    // create a zip 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 {
+        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip))) {
             for (String p: paths) {
                 ZipEntry ze = new ZipEntry(p);
                 zos.putNextEntry(ze);
@@ -213,8 +226,6 @@
                 zos.write(bytes, 0, bytes.length);
                 zos.closeEntry();
             }
-        } finally {
-            zos.close();
         }
     }
 
@@ -223,8 +234,24 @@
         errors++;
     }
 
+    boolean isFileNameCaseSignificant() {
+        File lower = new File("test.txt");
+        File upper = new File(lower.getPath().toUpperCase());
+        return !lower.equals(upper);
+    }
+
+    boolean isFileLookupCaseSignificant() throws IOException {
+        File lower = new File("test.txt");
+        File upper = new File(lower.getPath().toUpperCase());
+        if (upper.exists()) {
+            upper.delete();
+        }
+        try (FileWriter out = new FileWriter(lower)) { }
+        return !upper.exists();
+    }
+
     int count;
     int errors;
-    Set<String> foundClasses = new HashSet<String>();
+    Set<String> foundClasses = new HashSet<>();
 }