test/tools/javac/6508981/TestInferBinaryName.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/6508981/TestInferBinaryName.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,175 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6508981
    1.30 + * @summary cleanup file separator handling in JavacFileManager
    1.31 + * (This test is specifically to test the new impl of inferBinaryName)
    1.32 + * @build p.A
    1.33 + * @run main TestInferBinaryName
    1.34 + */
    1.35 +
    1.36 +import java.io.*;
    1.37 +import java.util.*;
    1.38 +import javax.tools.*;
    1.39 +
    1.40 +import com.sun.tools.javac.file.JavacFileManager;
    1.41 +import com.sun.tools.javac.util.Context;
    1.42 +import com.sun.tools.javac.util.Options;
    1.43 +
    1.44 +import static javax.tools.JavaFileObject.Kind.*;
    1.45 +import static javax.tools.StandardLocation.*;
    1.46 +
    1.47 +
    1.48 +/**
    1.49 + * Verify the various implementations of inferBinaryName, but configuring
    1.50 + * different instances of a file manager, getting a file object, and checking
    1.51 + * the impl of inferBinaryName for that file object.
    1.52 + */
    1.53 +public class TestInferBinaryName {
    1.54 +    static final boolean IGNORE_SYMBOL_FILE = false;
    1.55 +    static final boolean USE_SYMBOL_FILE = true;
    1.56 +    static final boolean DONT_USE_ZIP_FILE_INDEX = false;
    1.57 +    static final boolean USE_ZIP_FILE_INDEX = true;
    1.58 +
    1.59 +    public static void main(String... args) throws Exception {
    1.60 +        new TestInferBinaryName().run();
    1.61 +    }
    1.62 +
    1.63 +    void run() throws Exception {
    1.64 +        //System.err.println(System.getProperties());
    1.65 +        testDirectory();
    1.66 +        testSymbolArchive();
    1.67 +        testZipArchive();
    1.68 +        testZipFileIndexArchive();
    1.69 +        testZipFileIndexArchive2();
    1.70 +        if (errors > 0)
    1.71 +            throw new Exception(errors + " error found");
    1.72 +    }
    1.73 +
    1.74 +    void testDirectory() throws IOException {
    1.75 +        String testClassName = "p.A";
    1.76 +        JavaFileManager fm =
    1.77 +            getFileManager("test.classes", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);
    1.78 +        test("testDirectory",
    1.79 +             fm, testClassName, "com.sun.tools.javac.file.RegularFileObject");
    1.80 +    }
    1.81 +
    1.82 +    void testSymbolArchive() throws IOException {
    1.83 +        String testClassName = "java.lang.String";
    1.84 +        JavaFileManager fm =
    1.85 +            getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX);
    1.86 +        test("testSymbolArchive",
    1.87 +             fm, testClassName, "com.sun.tools.javac.file.SymbolArchive$SymbolFileObject");
    1.88 +    }
    1.89 +
    1.90 +    void testZipArchive() throws IOException {
    1.91 +        String testClassName = "java.lang.String";
    1.92 +        JavaFileManager fm =
    1.93 +            getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX);
    1.94 +        test("testZipArchive",
    1.95 +             fm, testClassName, "com.sun.tools.javac.file.ZipArchive$ZipFileObject");
    1.96 +    }
    1.97 +
    1.98 +    void testZipFileIndexArchive() throws IOException {
    1.99 +        String testClassName = "java.lang.String";
   1.100 +        JavaFileManager fm =
   1.101 +            getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);
   1.102 +        test("testZipFileIndexArchive",
   1.103 +             fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");
   1.104 +    }
   1.105 +
   1.106 +    void testZipFileIndexArchive2() throws IOException {
   1.107 +        String testClassName = "java.lang.String";
   1.108 +        JavaFileManager fm =
   1.109 +            getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);
   1.110 +        test("testZipFileIndexArchive2",
   1.111 +             fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");
   1.112 +    }
   1.113 +
   1.114 +    /**
   1.115 +     * @param testName for debugging
   1.116 +     * @param fm suitably configured file manager
   1.117 +     * @param testClassName the classname to test
   1.118 +     * @param implClassName the expected classname of the JavaFileObject impl,
   1.119 +     *     used for checking that we are checking the expected impl of
   1.120 +     *     inferBinaryName
   1.121 +     */
   1.122 +    void test(String testName,
   1.123 +              JavaFileManager fm, String testClassName, String implClassName) throws IOException {
   1.124 +        JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS);
   1.125 +        if (fo == null) {
   1.126 +            System.err.println("Can't find " + testClassName);
   1.127 +            errors++;
   1.128 +            return;
   1.129 +        }
   1.130 +
   1.131 +        String cn = fo.getClass().getName();
   1.132 +        String bn = fm.inferBinaryName(CLASS_PATH, fo);
   1.133 +        System.err.println(testName + " " + cn + " " + bn);
   1.134 +        check(cn, implClassName);
   1.135 +        check(bn, testClassName);
   1.136 +        System.err.println("OK");
   1.137 +    }
   1.138 +
   1.139 +    JavaFileManager getFileManager(String classpathProperty,
   1.140 +                                   boolean symFileKind,
   1.141 +                                   boolean zipFileIndexKind)
   1.142 +            throws IOException {
   1.143 +        Context ctx = new Context();
   1.144 +        Options options = Options.instance(ctx);
   1.145 +        options.put("useOptimizedZip",
   1.146 +                Boolean.toString(zipFileIndexKind == USE_ZIP_FILE_INDEX));
   1.147 +
   1.148 +        if (symFileKind == IGNORE_SYMBOL_FILE)
   1.149 +            options.put("ignore.symbol.file", "true");
   1.150 +        JavacFileManager fm = new JavacFileManager(ctx, false, null);
   1.151 +        List<File> path = getPath(System.getProperty(classpathProperty));
   1.152 +        fm.setLocation(CLASS_PATH, path);
   1.153 +        return fm;
   1.154 +    }
   1.155 +
   1.156 +    List<File> getPath(String s) {
   1.157 +        List<File> path = new ArrayList<File>();
   1.158 +        for (String f: s.split(File.pathSeparator)) {
   1.159 +            if (f.length() > 0)
   1.160 +                path.add(new File(f));
   1.161 +        }
   1.162 +        //System.err.println("path: " + path);
   1.163 +        return path;
   1.164 +    }
   1.165 +
   1.166 +    void check(String found, String expect) {
   1.167 +        if (!found.equals(expect)) {
   1.168 +            System.err.println("Expected: " + expect);
   1.169 +            System.err.println("   Found: " + found);
   1.170 +            errors++;
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +    private int errors;
   1.175 +}
   1.176 +
   1.177 +class A { }
   1.178 +

mercurial