Path: blob/master/test/langtools/tools/javac/6508981/TestInferBinaryName.java
41149 views
/*1* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 650898126* @summary cleanup file separator handling in JavacFileManager27* (This test is specifically to test the new impl of inferBinaryName)28* @library /tools/lib29* @modules jdk.compiler/com.sun.tools.javac.api30* jdk.compiler/com.sun.tools.javac.main31* @build toolbox.ToolBox toolbox.JarTask p.A32* @run main TestInferBinaryName33*/3435import java.io.*;36import java.util.*;37import javax.tools.*;3839import static javax.tools.JavaFileObject.Kind.*;40import static javax.tools.StandardLocation.*;4142import toolbox.JarTask;43import toolbox.ToolBox;4445/**46* Verify the various implementations of inferBinaryName, but configuring47* different instances of a file manager, getting a file object, and checking48* the impl of inferBinaryName for that file object.49*/50public class TestInferBinaryName {51public static void main(String... args) throws Exception {52new TestInferBinaryName().run();53}5455void run() throws Exception {56testDirectory();5758File testJar = createJar();59testZipArchive(testJar);6061if (errors > 0)62throw new Exception(errors + " error found");63}6465File createJar() throws IOException {66File f = new File("test.jar");67try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler()68.getStandardFileManager(null, null, null)) {69ToolBox tb = new ToolBox();70new JarTask(tb, f.getPath())71.files(fm, StandardLocation.PLATFORM_CLASS_PATH, "java.lang.*")72.run();73}74return f;75}7677void testDirectory() throws IOException {78String testClassName = "p.A";79List<File> testClasses = Arrays.asList(new File(System.getProperty("test.classes")));80try (JavaFileManager fm = getFileManager(testClasses)) {81test("testDirectory",82fm, testClassName, "SimpleFileObject");83}84}8586void testZipArchive(File testJar) throws IOException {87String testClassName = "java.lang.String";88List<File> path = Arrays.asList(testJar);89try (JavaFileManager fm = getFileManager(path)) {90test("testZipArchive",91fm, testClassName, "JarFileObject");92}93}9495/**96* @param testName for debugging97* @param fm suitably configured file manager98* @param testClassName the classname to test99* @param implClassName the expected classname of the JavaFileObject impl,100* used for checking that we are checking the expected impl of101* inferBinaryName102*/103void test(String testName,104JavaFileManager fm, String testClassName, String implClassName) throws IOException {105JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS);106if (fo == null) {107System.err.println("Can't find " + testClassName);108errors++;109return;110}111112String cn = fo.getClass().getSimpleName();113String bn = fm.inferBinaryName(CLASS_PATH, fo);114System.err.println(testName + " " + cn + " " + bn);115checkEqual(cn, implClassName);116checkEqual(bn, testClassName);117System.err.println("OK");118}119120JavaFileManager getFileManager(List<File> path)121throws IOException {122StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler()123.getStandardFileManager(null, null, null);124fm.setLocation(CLASS_PATH, path);125return fm;126}127128List<File> getPath(String s) {129List<File> path = new ArrayList<>();130for (String f: s.split(File.pathSeparator)) {131if (f.length() > 0)132path.add(new File(f));133}134//System.err.println("path: " + path);135return path;136}137138void checkEqual(String found, String expect) {139if (!found.equals(expect)) {140System.err.println("Expected: " + expect);141System.err.println(" Found: " + found);142errors++;143}144}145146private int errors;147}148149class A { }150151152153