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