test/tools/javac/api/T6838467.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1161
42ffceeceeca
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

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

mercurial