test/tools/javac/api/6411310/Test.java

Fri, 18 Feb 2011 08:12:06 -0800

author
ksrini
date
Fri, 18 Feb 2011 08:12:06 -0800
changeset 882
3d45cc94ee0f
parent 554
9d9f26857129
child 923
6970d9fb8e02
permissions
-rw-r--r--

7018859: javac turn off the Zip optimization by default
Reviewed-by: jjg

     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 6410367 6411310
    27  * @summary FileObject should support user-friendly names via getName()
    28  */
    30 import java.io.*;
    31 import java.util.*;
    32 import java.util.jar.*;
    33 import java.util.zip.*;
    34 import javax.tools.*;
    36 import com.sun.tools.javac.file.JavacFileManager;
    37 import com.sun.tools.javac.util.Context;
    38 import com.sun.tools.javac.util.Options;
    40 // Test FileObject.getName returned from JavacFileManager and its support classes.
    42 public class Test {
    43     public static void main(String... args) throws Exception {
    44         new Test().run();
    45     }
    47     Set<String> foundClasses = new TreeSet<String>();
    48     Set<String> foundJars = new TreeSet<String>();
    50     void run() throws Exception {
    51         File rt_jar = findRtJar();
    53         // names for entries to be created in directories and jar files
    54         String[] entries = { "p/A.java", "p/A.class", "p/resources/A-1.html" };
    56         // test various combinations of directories and jar files, intended to
    57         // cover all sources of file objects within JavacFileManager's support classes
    59         test(createFileManager(), createDir("dir", entries), "p", entries);
    60         test(createFileManager(), createDir("a b/dir", entries), "p", entries);
    62         for (boolean useOptimizedZip: new boolean[] { false, true }) {
    63             test(createFileManager(useOptimizedZip), createJar("jar", entries), "p", entries);
    64             test(createFileManager(useOptimizedZip), createJar("jar jar", entries), "p", entries);
    66             for (boolean useSymbolFile: new boolean[] { false, true }) {
    67                 test(createFileManager(useOptimizedZip, useSymbolFile), rt_jar, "java.lang.ref", null);
    68             }
    69         }
    71         if (errors > 0)
    72             throw new Exception(errors + " errors found");
    74         // Verify that we hit all the impl classes we intended
    75         checkCoverage("classes", foundClasses,
    76                 "RegularFileObject", "SymbolFileObject", "ZipFileIndexFileObject", "ZipFileObject");
    78         // Verify that we hit the jar files we intended, specifically ct.sym as well as rt.jar
    79         checkCoverage("jar files", foundJars,
    80                 "ct.sym", "jar", "jar jar", "rt.jar");
    81     }
    83     // use a new file manager for each test
    84     void test(StandardJavaFileManager fm, File f, String pkg, String[] entries) throws Exception {
    85         System.err.println("Test " + f);
    86         try {
    87             if (f.isDirectory()) {
    88                 for (File dir: new File[] { f, f.getAbsoluteFile() }) {
    89                     for (String e: entries) {
    90                         JavaFileObject fo = fm.getJavaFileObjects(new File(dir, e)).iterator().next();
    91                         test(fo, dir, e);
    92                     }
    93                 }
    94             }
    96             fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f));
    97             fm.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(f.getAbsoluteFile()));
    98             for (StandardLocation l: EnumSet.of(StandardLocation.CLASS_PATH, StandardLocation.SOURCE_PATH)) {
    99                 for (JavaFileObject fo: fm.list(l, pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) {
   100                     // we could use fm.getLocation but the following guarantees we preserve the original filename
   101                     File dir = (l == StandardLocation.CLASS_PATH ? f : f.getAbsoluteFile());
   102                     char sep = (dir.isDirectory() ? File.separatorChar : '/');
   103                     String b = fm.inferBinaryName(l, fo);
   104                     String e = fo.getKind().extension;
   105                     test(fo, dir, b.replace('.', sep) + e);
   106                 }
   107             }
   108         } finally {
   109             fm.close();
   110         }
   111     }
   113     void test(JavaFileObject fo, File dir, String p) {
   114         System.err.println("Test: " + fo);
   115         String expect = dir.isDirectory() ? new File(dir, p).getPath() : (dir.getPath() + "(" + p + ")");
   116         String found = fo.getName();
   117         // if ct.sym is found, replace it with the equivalent rt.jar
   118         String found2 = found.replaceAll("lib([\\\\/])ct.sym\\(META-INF/sym/rt.jar/", "jre$1lib$1rt.jar(");
   119         if (!expect.equals(found2)) {
   120             System.err.println("expected: " + expect);
   121             System.err.println("   found: " + found);
   122             if (!found.equals(found2))
   123                 System.err.println("  found2: " + found2);
   124             error("Failed: " + fo);
   125         }
   127         // record the file object class name for coverage checks later
   128         foundClasses.add(fo.getClass().getSimpleName());
   130         if (found.contains("(")) {
   131             // record access to the jar file for coverage checks later
   132             foundJars.add(new File(found.substring(0, found.indexOf("("))).getName());
   133         }
   134     }
   136     void checkCoverage(String label, Set<String> found, String... expect) throws Exception {
   137         Set<String> e = new TreeSet<String>(Arrays.asList(expect));
   138         if (!found.equals(e)) {
   139             e.removeAll(found);
   140             throw new Exception("expected " + label + " not used: " + e);
   141         }
   142     }
   144     JavacFileManager createFileManager() {
   145         return createFileManager(false, false);
   146     }
   148     JavacFileManager createFileManager(boolean useOptimizedZip) {
   149         return createFileManager(useOptimizedZip, false);
   150     }
   152     JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) {
   153         Context c = new Context();
   154         Options options = Options.instance(c);
   156             if (useOptimizedZip) {
   157                 options.put("useOptimizedZip", "true");
   158             }
   160             if (!useSymbolFile) {
   161                 options.put("ignore.symbol.file", "true");
   162             }
   163             return new JavacFileManager(c, false, null);
   164     }
   166     File createDir(String name, String... entries) throws Exception {
   167         File dir = new File(name);
   168         if (!dir.mkdirs())
   169             throw new Exception("cannot create directories " + dir);
   170         for (String e: entries) {
   171             writeFile(new File(dir, e), e);
   172         }
   173         return dir;
   174     }
   176     File createJar(String name, String... entries) throws IOException {
   177         File jar = new File(name);
   178         OutputStream out = new FileOutputStream(jar);
   179         try {
   180             JarOutputStream jos = new JarOutputStream(out);
   181             for (String e: entries) {
   182                 jos.putNextEntry(new ZipEntry(e));
   183                 jos.write(e.getBytes());
   184             }
   185             jos.close();
   186         } finally {
   187             out.close();
   188         }
   189         return jar;
   190     }
   192     File findRtJar() throws Exception {
   193         File java_home = new File(System.getProperty("java.home"));
   194         if (java_home.getName().equals("jre"))
   195             java_home = java_home.getParentFile();
   196         File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar");
   197         if (!rt_jar.exists())
   198             throw new Exception("can't find rt.jar");
   199         return rt_jar;
   200     }
   202     byte[] read(InputStream in) throws IOException {
   203         byte[] data = new byte[1024];
   204         int offset = 0;
   205         try {
   206             int n;
   207             while ((n = in.read(data, offset, data.length - offset)) != -1) {
   208                 offset += n;
   209                 if (offset == data.length)
   210                     data = Arrays.copyOf(data, 2 * data.length);
   211             }
   212         } finally {
   213             in.close();
   214         }
   215         return Arrays.copyOf(data, offset);
   216     }
   218     void writeFile(File f, String s) throws IOException {
   219         f.getParentFile().mkdirs();
   220         FileWriter out = new FileWriter(f);
   221         try {
   222             out.write(s);
   223         } finally {
   224             out.close();
   225         }
   226     }
   228     void error(String msg) {
   229         System.err.println(msg);
   230         errors++;
   231     }
   233     int errors;
   234 }

mercurial