langtools/test/tools/javac/6508981/TestInferBinaryName.java
changeset 34560 b6a567b677f7
parent 30730 d3ce7619db2c
child 36526 3b41f1c69604
equal deleted inserted replaced
34481:e0ff9821f1e8 34560:b6a567b677f7
    51  * Verify the various implementations of inferBinaryName, but configuring
    51  * Verify the various implementations of inferBinaryName, but configuring
    52  * different instances of a file manager, getting a file object, and checking
    52  * different instances of a file manager, getting a file object, and checking
    53  * the impl of inferBinaryName for that file object.
    53  * the impl of inferBinaryName for that file object.
    54  */
    54  */
    55 public class TestInferBinaryName {
    55 public class TestInferBinaryName {
    56     static final boolean DONT_USE_ZIP_FILE_INDEX = false;
       
    57     static final boolean USE_ZIP_FILE_INDEX = true;
       
    58 
       
    59     public static void main(String... args) throws Exception {
    56     public static void main(String... args) throws Exception {
    60         new TestInferBinaryName().run();
    57         new TestInferBinaryName().run();
    61     }
    58     }
    62 
    59 
    63     void run() throws Exception {
    60     void run() throws Exception {
    64         testDirectory();
    61         testDirectory();
    65 
    62 
    66         File testJar = createJar();
    63         File testJar = createJar();
    67 
       
    68         testZipArchive(testJar);
    64         testZipArchive(testJar);
    69         testZipFileIndexArchive(testJar);
       
    70         testZipFileIndexArchive2(testJar);
       
    71 
    65 
    72         if (errors > 0)
    66         if (errors > 0)
    73             throw new Exception(errors + " error found");
    67             throw new Exception(errors + " error found");
    74     }
    68     }
    75 
    69 
    85     }
    79     }
    86 
    80 
    87     void testDirectory() throws IOException {
    81     void testDirectory() throws IOException {
    88         String testClassName = "p.A";
    82         String testClassName = "p.A";
    89         List<File> testClasses = Arrays.asList(new File(System.getProperty("test.classes")));
    83         List<File> testClasses = Arrays.asList(new File(System.getProperty("test.classes")));
    90         try (JavaFileManager fm =
    84         try (JavaFileManager fm = getFileManager(testClasses)) {
    91                 getFileManager(testClasses, USE_ZIP_FILE_INDEX)) {
       
    92             test("testDirectory",
    85             test("testDirectory",
    93                 fm, testClassName, "com.sun.tools.javac.file.RegularFileObject");
    86                 fm, testClassName, "SimpleFileObject");
    94         }
    87         }
    95     }
    88     }
    96 
    89 
    97     void testZipArchive(File testJar) throws IOException {
    90     void testZipArchive(File testJar) throws IOException {
    98         String testClassName = "java.lang.String";
    91         String testClassName = "java.lang.String";
    99         List<File> path = Arrays.asList(testJar);
    92         List<File> path = Arrays.asList(testJar);
   100         try (JavaFileManager fm =
    93         try (JavaFileManager fm = getFileManager(path)) {
   101                 getFileManager(path, DONT_USE_ZIP_FILE_INDEX)) {
       
   102             test("testZipArchive",
    94             test("testZipArchive",
   103                  fm, testClassName, "com.sun.tools.javac.file.ZipArchive$ZipFileObject");
    95                  fm, testClassName, "JarFileObject");
   104         }
       
   105     }
       
   106 
       
   107     void testZipFileIndexArchive(File testJar) throws IOException {
       
   108         String testClassName = "java.lang.String";
       
   109         List<File> path = Arrays.asList(testJar);
       
   110         try (JavaFileManager fm =
       
   111                 getFileManager(path, USE_ZIP_FILE_INDEX)) {
       
   112             test("testZipFileIndexArchive",
       
   113                  fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");
       
   114         }
       
   115     }
       
   116 
       
   117     void testZipFileIndexArchive2(File testJar) throws IOException {
       
   118         String testClassName = "java.lang.String";
       
   119         List<File> path = Arrays.asList(testJar);
       
   120         try (JavaFileManager fm =
       
   121                 getFileManager(path, USE_ZIP_FILE_INDEX)) {
       
   122             test("testZipFileIndexArchive2",
       
   123                  fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");
       
   124         }
    96         }
   125     }
    97     }
   126 
    98 
   127     /**
    99     /**
   128      * @param testName for debugging
   100      * @param testName for debugging
   139             System.err.println("Can't find " + testClassName);
   111             System.err.println("Can't find " + testClassName);
   140             errors++;
   112             errors++;
   141             return;
   113             return;
   142         }
   114         }
   143 
   115 
   144         String cn = fo.getClass().getName();
   116         String cn = fo.getClass().getSimpleName();
   145         String bn = fm.inferBinaryName(CLASS_PATH, fo);
   117         String bn = fm.inferBinaryName(CLASS_PATH, fo);
   146         System.err.println(testName + " " + cn + " " + bn);
   118         System.err.println(testName + " " + cn + " " + bn);
   147         check(cn, implClassName);
   119         checkEqual(cn, implClassName);
   148         check(bn, testClassName);
   120         checkEqual(bn, testClassName);
   149         System.err.println("OK");
   121         System.err.println("OK");
   150     }
   122     }
   151 
   123 
   152     JavaFileManager getFileManager(List<File> path,
   124     JavaFileManager getFileManager(List<File> path)
   153                                    boolean zipFileIndexKind)
       
   154             throws IOException {
   125             throws IOException {
   155         Context ctx = new Context();
   126         Context ctx = new Context();
   156         Options options = Options.instance(ctx);
       
   157         options.put("useOptimizedZip",
       
   158                 Boolean.toString(zipFileIndexKind == USE_ZIP_FILE_INDEX));
       
   159 
   127 
   160         JavacFileManager fm = new JavacFileManager(ctx, false, null);
   128         JavacFileManager fm = new JavacFileManager(ctx, false, null);
   161         fm.setLocation(CLASS_PATH, path);
   129         fm.setLocation(CLASS_PATH, path);
   162         return fm;
   130         return fm;
   163     }
   131     }
   164 
   132 
   165     List<File> getPath(String s) {
   133     List<File> getPath(String s) {
   166         List<File> path = new ArrayList<File>();
   134         List<File> path = new ArrayList<>();
   167         for (String f: s.split(File.pathSeparator)) {
   135         for (String f: s.split(File.pathSeparator)) {
   168             if (f.length() > 0)
   136             if (f.length() > 0)
   169                 path.add(new File(f));
   137                 path.add(new File(f));
   170         }
   138         }
   171         //System.err.println("path: " + path);
   139         //System.err.println("path: " + path);
   172         return path;
   140         return path;
   173     }
   141     }
   174 
   142 
   175     void check(String found, String expect) {
   143     void checkEqual(String found, String expect) {
   176         if (!found.equals(expect)) {
   144         if (!found.equals(expect)) {
   177             System.err.println("Expected: " + expect);
   145             System.err.println("Expected: " + expect);
   178             System.err.println("   Found: " + found);
   146             System.err.println("   Found: " + found);
   179             errors++;
   147             errors++;
   180         }
   148         }