Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6508981/TestInferBinaryName.java
38813 views
/*1* Copyright (c) 2008, 2011, 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* @build p.A29* @run main TestInferBinaryName30*/3132import java.io.*;33import java.util.*;34import javax.tools.*;3536import com.sun.tools.javac.file.JavacFileManager;37import com.sun.tools.javac.util.Context;38import com.sun.tools.javac.util.Options;3940import static javax.tools.JavaFileObject.Kind.*;41import static javax.tools.StandardLocation.*;424344/**45* Verify the various implementations of inferBinaryName, but configuring46* different instances of a file manager, getting a file object, and checking47* the impl of inferBinaryName for that file object.48*/49public class TestInferBinaryName {50static final boolean IGNORE_SYMBOL_FILE = false;51static final boolean USE_SYMBOL_FILE = true;52static final boolean DONT_USE_ZIP_FILE_INDEX = false;53static final boolean USE_ZIP_FILE_INDEX = true;5455public static void main(String... args) throws Exception {56new TestInferBinaryName().run();57}5859void run() throws Exception {60//System.err.println(System.getProperties());61testDirectory();62testSymbolArchive();63testZipArchive();64testZipFileIndexArchive();65testZipFileIndexArchive2();66if (errors > 0)67throw new Exception(errors + " error found");68}6970void testDirectory() throws IOException {71String testClassName = "p.A";72JavaFileManager fm =73getFileManager("test.classes", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);74test("testDirectory",75fm, testClassName, "com.sun.tools.javac.file.RegularFileObject");76}7778void testSymbolArchive() throws IOException {79String testClassName = "java.lang.String";80JavaFileManager fm =81getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX);82test("testSymbolArchive",83fm, testClassName, "com.sun.tools.javac.file.SymbolArchive$SymbolFileObject");84}8586void testZipArchive() throws IOException {87String testClassName = "java.lang.String";88JavaFileManager fm =89getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX);90test("testZipArchive",91fm, testClassName, "com.sun.tools.javac.file.ZipArchive$ZipFileObject");92}9394void testZipFileIndexArchive() throws IOException {95String testClassName = "java.lang.String";96JavaFileManager fm =97getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);98test("testZipFileIndexArchive",99fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");100}101102void testZipFileIndexArchive2() throws IOException {103String testClassName = "java.lang.String";104JavaFileManager fm =105getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, USE_ZIP_FILE_INDEX);106test("testZipFileIndexArchive2",107fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject");108}109110/**111* @param testName for debugging112* @param fm suitably configured file manager113* @param testClassName the classname to test114* @param implClassName the expected classname of the JavaFileObject impl,115* used for checking that we are checking the expected impl of116* inferBinaryName117*/118void test(String testName,119JavaFileManager fm, String testClassName, String implClassName) throws IOException {120JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS);121if (fo == null) {122System.err.println("Can't find " + testClassName);123errors++;124return;125}126127String cn = fo.getClass().getName();128String bn = fm.inferBinaryName(CLASS_PATH, fo);129System.err.println(testName + " " + cn + " " + bn);130check(cn, implClassName);131check(bn, testClassName);132System.err.println("OK");133}134135JavaFileManager getFileManager(String classpathProperty,136boolean symFileKind,137boolean zipFileIndexKind)138throws IOException {139Context ctx = new Context();140Options options = Options.instance(ctx);141options.put("useOptimizedZip",142Boolean.toString(zipFileIndexKind == USE_ZIP_FILE_INDEX));143144if (symFileKind == IGNORE_SYMBOL_FILE)145options.put("ignore.symbol.file", "true");146JavacFileManager fm = new JavacFileManager(ctx, false, null);147List<File> path = getPath(System.getProperty(classpathProperty));148fm.setLocation(CLASS_PATH, path);149return fm;150}151152List<File> getPath(String s) {153List<File> path = new ArrayList<File>();154for (String f: s.split(File.pathSeparator)) {155if (f.length() > 0)156path.add(new File(f));157}158//System.err.println("path: " + path);159return path;160}161162void check(String found, String expect) {163if (!found.equals(expect)) {164System.err.println("Expected: " + expect);165System.err.println(" Found: " + found);166errors++;167}168}169170private int errors;171}172173class A { }174175176177