langtools/test/tools/javac/6508981/TestInferBinaryName.java
changeset 1205 b316e32eb90c
child 5520 86e4b9a9da40
equal deleted inserted replaced
1109:853d8c191eac 1205:b316e32eb90c
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6508981
       
    27  * @summary cleanup file separator handling in JavacFileManager
       
    28  * (This test is specifically to test the new impl of inferBinaryName)
       
    29  * @build p.A
       
    30  * @run main TestInferBinaryName
       
    31  */
       
    32 
       
    33 import java.io.*;
       
    34 import java.util.*;
       
    35 import javax.tools.*;
       
    36 
       
    37 import com.sun.tools.javac.file.JavacFileManager;
       
    38 import com.sun.tools.javac.util.Context;
       
    39 import com.sun.tools.javac.util.Options;
       
    40 
       
    41 import static javax.tools.JavaFileObject.Kind.*;
       
    42 import static javax.tools.StandardLocation.*;
       
    43 
       
    44 
       
    45 /**
       
    46  * Verify the various implementations of inferBinaryName, but configuring
       
    47  * different instances of a file manager, getting a file object, and checking
       
    48  * the impl of inferBinaryName for that file object.
       
    49  */
       
    50 public class TestInferBinaryName {
       
    51     static final boolean IGNORE_SYMBOL_FILE = false;
       
    52     static final boolean USE_SYMBOL_FILE = true;
       
    53     static final boolean DONT_USE_ZIP_FILE_INDEX = false;
       
    54     static final boolean USE_ZIP_FILE_INDEX = true;
       
    55 
       
    56     public static void main(String... args) throws Exception {
       
    57         new TestInferBinaryName().run();
       
    58     }
       
    59 
       
    60     void run() throws Exception {
       
    61         //System.err.println(System.getProperties());
       
    62         testDirectory();
       
    63         testSymbolArchive();
       
    64         testZipArchive();
       
    65         testZipFileIndexArchive();
       
    66         testZipFileIndexArchive2();
       
    67         if (errors > 0)
       
    68             throw new Exception(errors + " error found");
       
    69     }
       
    70 
       
    71     void testDirectory() throws IOException {
       
    72         String testClassName = "p.A";
       
    73         JavaFileManager fm =
       
    74             getFileManager("test.classes", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);
       
    75         test("testDirectory",
       
    76              fm, testClassName, "com.sun.tools.javac.file.RegularFileObject");
       
    77     }
       
    78 
       
    79     void testSymbolArchive() throws IOException {
       
    80         String testClassName = "java.lang.String";
       
    81         JavaFileManager fm =
       
    82             getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX);
       
    83         test("testSymbolArchive",
       
    84              fm, testClassName, "com.sun.tools.javac.file.SymbolArchive$SymbolFileObject");
       
    85     }
       
    86 
       
    87     void testZipArchive() throws IOException {
       
    88         String testClassName = "java.lang.String";
       
    89         JavaFileManager fm =
       
    90             getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX);
       
    91         test("testZipArchive",
       
    92              fm, testClassName, "com.sun.tools.javac.file.ZipArchive$ZipFileObject");
       
    93     }
       
    94 
       
    95     void testZipFileIndexArchive() throws IOException {
       
    96         String testClassName = "java.lang.String";
       
    97         JavaFileManager fm =
       
    98             getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);
       
    99         test("testZipFileIndexArchive",
       
   100              fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");
       
   101     }
       
   102 
       
   103     void testZipFileIndexArchive2() throws IOException {
       
   104         String testClassName = "java.lang.String";
       
   105         JavaFileManager fm =
       
   106             getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);
       
   107         test("testZipFileIndexArchive2",
       
   108              fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");
       
   109     }
       
   110 
       
   111     /**
       
   112      * @param testName for debugging
       
   113      * @param fm suitably configured file manager
       
   114      * @param testClassName the classname to test
       
   115      * @param implClassName the expected classname of the JavaFileObject impl,
       
   116      *     used for checking that we are checking the expected impl of
       
   117      *     inferBinaryName
       
   118      */
       
   119     void test(String testName,
       
   120               JavaFileManager fm, String testClassName, String implClassName) throws IOException {
       
   121         JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS);
       
   122         if (fo == null) {
       
   123             System.err.println("Can't find " + testClassName);
       
   124             errors++;
       
   125             return;
       
   126         }
       
   127 
       
   128         String cn = fo.getClass().getName();
       
   129         String bn = fm.inferBinaryName(CLASS_PATH, fo);
       
   130         System.err.println(testName + " " + cn + " " + bn);
       
   131         check(cn, implClassName);
       
   132         check(bn, testClassName);
       
   133         System.err.println("OK");
       
   134     }
       
   135 
       
   136     JavaFileManager getFileManager(String classpathProperty,
       
   137                                    boolean symFileKind,
       
   138                                    boolean zipFileIndexKind)
       
   139             throws IOException {
       
   140         Context ctx = new Context();
       
   141         // uugh, ugly back door, should be cleaned up, someday
       
   142         if (zipFileIndexKind == USE_ZIP_FILE_INDEX)
       
   143             System.clearProperty("useJavaUtilZip");
       
   144         else
       
   145             System.setProperty("useJavaUtilZip", "true");
       
   146         Options options = Options.instance(ctx);
       
   147         if (symFileKind == IGNORE_SYMBOL_FILE)
       
   148             options.put("ignore.symbol.file", "true");
       
   149         JavacFileManager fm = new JavacFileManager(ctx, false, null);
       
   150         List<File> path = getPath(System.getProperty(classpathProperty));
       
   151         fm.setLocation(CLASS_PATH, path);
       
   152         return fm;
       
   153     }
       
   154 
       
   155     List<File> getPath(String s) {
       
   156         List<File> path = new ArrayList<File>();
       
   157         for (String f: s.split(File.pathSeparator)) {
       
   158             if (f.length() > 0)
       
   159                 path.add(new File(f));
       
   160         }
       
   161         //System.err.println("path: " + path);
       
   162         return path;
       
   163     }
       
   164 
       
   165     void check(String found, String expect) {
       
   166         if (!found.equals(expect)) {
       
   167             System.err.println("Expected: " + expect);
       
   168             System.err.println("   Found: " + found);
       
   169             errors++;
       
   170         }
       
   171     }
       
   172 
       
   173     private int errors;
       
   174 }
       
   175 
       
   176 class A { }
       
   177