jjg@103: /* ksrini@882: * Copyright (c) 2008, 2011 Oracle and/or its affiliates. All rights reserved. jjg@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@103: * jjg@103: * This code is free software; you can redistribute it and/or modify it jjg@103: * under the terms of the GNU General Public License version 2 only, as jjg@103: * published by the Free Software Foundation. jjg@103: * jjg@103: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@103: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@103: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@103: * version 2 for more details (a copy is included in the LICENSE file that jjg@103: * accompanied this code). jjg@103: * jjg@103: * You should have received a copy of the GNU General Public License version jjg@103: * 2 along with this work; if not, write to the Free Software Foundation, jjg@103: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@103: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@103: */ jjg@103: jjg@103: /* jjg@103: * @test jjg@103: * @bug 6508981 jjg@103: * @summary cleanup file separator handling in JavacFileManager jjg@103: * (This test is specifically to test the new impl of inferBinaryName) jjg@103: * @build p.A jjg@103: * @run main TestInferBinaryName jjg@103: */ jjg@103: jjg@103: import java.io.*; jjg@103: import java.util.*; jjg@103: import javax.tools.*; jjg@103: jjg@103: import com.sun.tools.javac.file.JavacFileManager; jjg@103: import com.sun.tools.javac.util.Context; jjg@103: import com.sun.tools.javac.util.Options; jjg@103: jjg@103: import static javax.tools.JavaFileObject.Kind.*; jjg@103: import static javax.tools.StandardLocation.*; jjg@103: jjg@103: jjg@103: /** jjg@103: * Verify the various implementations of inferBinaryName, but configuring jjg@103: * different instances of a file manager, getting a file object, and checking jjg@103: * the impl of inferBinaryName for that file object. jjg@103: */ jjg@103: public class TestInferBinaryName { jjg@103: static final boolean IGNORE_SYMBOL_FILE = false; jjg@103: static final boolean USE_SYMBOL_FILE = true; jjg@103: static final boolean DONT_USE_ZIP_FILE_INDEX = false; jjg@103: static final boolean USE_ZIP_FILE_INDEX = true; jjg@103: jjg@103: public static void main(String... args) throws Exception { jjg@103: new TestInferBinaryName().run(); jjg@103: } jjg@103: jjg@103: void run() throws Exception { jjg@103: //System.err.println(System.getProperties()); jjg@103: testDirectory(); jjg@103: testSymbolArchive(); jjg@103: testZipArchive(); jjg@103: testZipFileIndexArchive(); jjg@103: testZipFileIndexArchive2(); jjg@103: if (errors > 0) jjg@103: throw new Exception(errors + " error found"); jjg@103: } jjg@103: jjg@103: void testDirectory() throws IOException { jjg@103: String testClassName = "p.A"; jjg@103: JavaFileManager fm = jjg@103: getFileManager("test.classes", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX); jjg@103: test("testDirectory", jjg@103: fm, testClassName, "com.sun.tools.javac.file.RegularFileObject"); jjg@103: } jjg@103: jjg@103: void testSymbolArchive() throws IOException { jjg@103: String testClassName = "java.lang.String"; jjg@103: JavaFileManager fm = jjg@103: getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX); jjg@103: test("testSymbolArchive", jjg@103: fm, testClassName, "com.sun.tools.javac.file.SymbolArchive$SymbolFileObject"); jjg@103: } jjg@103: jjg@103: void testZipArchive() throws IOException { jjg@103: String testClassName = "java.lang.String"; jjg@103: JavaFileManager fm = jjg@103: getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, DONT_USE_ZIP_FILE_INDEX); jjg@103: test("testZipArchive", jjg@103: fm, testClassName, "com.sun.tools.javac.file.ZipArchive$ZipFileObject"); jjg@103: } jjg@103: jjg@103: void testZipFileIndexArchive() throws IOException { jjg@103: String testClassName = "java.lang.String"; jjg@103: JavaFileManager fm = jjg@103: getFileManager("sun.boot.class.path", USE_SYMBOL_FILE, USE_ZIP_FILE_INDEX); jjg@103: test("testZipFileIndexArchive", jjg@103: fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject"); jjg@103: } jjg@103: jjg@103: void testZipFileIndexArchive2() throws IOException { jjg@103: String testClassName = "java.lang.String"; jjg@103: JavaFileManager fm = jjg@103: getFileManager("sun.boot.class.path", IGNORE_SYMBOL_FILE, USE_ZIP_FILE_INDEX); jjg@103: test("testZipFileIndexArchive2", jjg@103: fm, testClassName, "com.sun.tools.javac.file.ZipFileIndexArchive$ZipFileIndexFileObject"); jjg@103: } jjg@103: jjg@103: /** jjg@103: * @param testName for debugging jjg@103: * @param fm suitably configured file manager jjg@103: * @param testClassName the classname to test jjg@103: * @param implClassName the expected classname of the JavaFileObject impl, jjg@103: * used for checking that we are checking the expected impl of jjg@103: * inferBinaryName jjg@103: */ jjg@103: void test(String testName, jjg@103: JavaFileManager fm, String testClassName, String implClassName) throws IOException { jjg@103: JavaFileObject fo = fm.getJavaFileForInput(CLASS_PATH, testClassName, CLASS); jjg@103: if (fo == null) { jjg@103: System.err.println("Can't find " + testClassName); jjg@103: errors++; jjg@103: return; jjg@103: } jjg@103: jjg@103: String cn = fo.getClass().getName(); jjg@103: String bn = fm.inferBinaryName(CLASS_PATH, fo); jjg@103: System.err.println(testName + " " + cn + " " + bn); jjg@103: check(cn, implClassName); jjg@103: check(bn, testClassName); jjg@103: System.err.println("OK"); jjg@103: } jjg@103: jjg@103: JavaFileManager getFileManager(String classpathProperty, jjg@103: boolean symFileKind, jjg@103: boolean zipFileIndexKind) jjg@103: throws IOException { jjg@103: Context ctx = new Context(); ksrini@882: Options options = Options.instance(ctx); jjg@103: // uugh, ugly back door, should be cleaned up, someday jjg@103: if (zipFileIndexKind == USE_ZIP_FILE_INDEX) ksrini@882: options.put("useOptimizedZip", "true"); ksrini@882: jjg@103: if (symFileKind == IGNORE_SYMBOL_FILE) jjg@103: options.put("ignore.symbol.file", "true"); jjg@103: JavacFileManager fm = new JavacFileManager(ctx, false, null); jjg@103: List path = getPath(System.getProperty(classpathProperty)); jjg@103: fm.setLocation(CLASS_PATH, path); jjg@103: return fm; jjg@103: } jjg@103: jjg@103: List getPath(String s) { jjg@103: List path = new ArrayList(); jjg@103: for (String f: s.split(File.pathSeparator)) { jjg@103: if (f.length() > 0) jjg@103: path.add(new File(f)); jjg@103: } jjg@103: //System.err.println("path: " + path); jjg@103: return path; jjg@103: } jjg@103: jjg@103: void check(String found, String expect) { jjg@103: if (!found.equals(expect)) { jjg@103: System.err.println("Expected: " + expect); jjg@103: System.err.println(" Found: " + found); jjg@103: errors++; jjg@103: } jjg@103: } jjg@103: jjg@103: private int errors; jjg@103: } jjg@103: jjg@103: class A { } jjg@103: