TestInferBinaryName.java revision 3155:30e288cb2d22
1/*
2 * Copyright (c) 2008, 2015, 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 */
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 * @library /tools/lib
30 * @modules jdk.compiler/com.sun.tools.javac.api
31 *          jdk.compiler/com.sun.tools.javac.file
32 *          jdk.compiler/com.sun.tools.javac.main
33 *          jdk.compiler/com.sun.tools.javac.util
34 * @build ToolBox p.A
35 * @run main TestInferBinaryName
36 */
37
38import java.io.*;
39import java.util.*;
40import javax.tools.*;
41
42import com.sun.tools.javac.file.JavacFileManager;
43import com.sun.tools.javac.util.Context;
44import com.sun.tools.javac.util.Options;
45
46import static javax.tools.JavaFileObject.Kind.*;
47import static javax.tools.StandardLocation.*;
48
49
50/**
51 * Verify the various implementations of inferBinaryName, but configuring
52 * different instances of a file manager, getting a file object, and checking
53 * the impl of inferBinaryName for that file object.
54 */
55public class TestInferBinaryName {
56    public static void main(String... args) throws Exception {
57        new TestInferBinaryName().run();
58    }
59
60    void run() throws Exception {
61        testDirectory();
62
63        File testJar = createJar();
64        testZipArchive(testJar);
65
66        if (errors > 0)
67            throw new Exception(errors + " error found");
68    }
69
70    File createJar() throws IOException {
71        File f = new File("test.jar");
72        try (JavaFileManager fm = new JavacFileManager(new Context(), false, null)) {
73            ToolBox tb = new ToolBox();
74            tb.new JarTask(f.getPath())
75                .files(fm, StandardLocation.PLATFORM_CLASS_PATH, "java.lang.*")
76                .run();
77        }
78        return f;
79    }
80
81    void testDirectory() throws IOException {
82        String testClassName = "p.A";
83        List<File> testClasses = Arrays.asList(new File(System.getProperty("test.classes")));
84        try (JavaFileManager fm = getFileManager(testClasses)) {
85            test("testDirectory",
86                fm, testClassName, "SimpleFileObject");
87        }
88    }
89
90    void testZipArchive(File testJar) throws IOException {
91        String testClassName = "java.lang.String";
92        List<File> path = Arrays.asList(testJar);
93        try (JavaFileManager fm = getFileManager(path)) {
94            test("testZipArchive",
95                 fm, testClassName, "JarFileObject");
96        }
97    }
98
99    /**
100     * @param testName for debugging
101     * @param fm suitably configured file manager
102     * @param testClassName the classname to test
103     * @param implClassName the expected classname of the JavaFileObject impl,
104     *     used for checking that we are checking the expected impl of
105     *     inferBinaryName
106     */
107    void test(String testName,
108              JavaFileManager fm, String testClassName, String implClassName) throws IOException {
109        JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS);
110        if (fo == null) {
111            System.err.println("Can't find " + testClassName);
112            errors++;
113            return;
114        }
115
116        String cn = fo.getClass().getSimpleName();
117        String bn = fm.inferBinaryName(CLASS_PATH, fo);
118        System.err.println(testName + " " + cn + " " + bn);
119        checkEqual(cn, implClassName);
120        checkEqual(bn, testClassName);
121        System.err.println("OK");
122    }
123
124    JavaFileManager getFileManager(List<File> path)
125            throws IOException {
126        Context ctx = new Context();
127
128        JavacFileManager fm = new JavacFileManager(ctx, false, null);
129        fm.setLocation(CLASS_PATH, path);
130        return fm;
131    }
132
133    List<File> getPath(String s) {
134        List<File> path = new ArrayList<>();
135        for (String f: s.split(File.pathSeparator)) {
136            if (f.length() > 0)
137                path.add(new File(f));
138        }
139        //System.err.println("path: " + path);
140        return path;
141    }
142
143    void checkEqual(String found, String expect) {
144        if (!found.equals(expect)) {
145            System.err.println("Expected: " + expect);
146            System.err.println("   Found: " + found);
147            errors++;
148        }
149    }
150
151    private int errors;
152}
153
154class A { }
155
156