414N/A/*
1011N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
414N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
414N/A *
414N/A * This code is free software; you can redistribute it and/or modify it
414N/A * under the terms of the GNU General Public License version 2 only, as
414N/A * published by the Free Software Foundation.
414N/A *
414N/A * This code is distributed in the hope that it will be useful, but WITHOUT
414N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
414N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
414N/A * version 2 for more details (a copy is included in the LICENSE file that
414N/A * accompanied this code).
414N/A *
414N/A * You should have received a copy of the GNU General Public License version
414N/A * 2 along with this work; if not, write to the Free Software Foundation,
414N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
414N/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.
414N/A */
414N/A
414N/A/*
414N/A * @test
414N/A * @bug 6410367 6411310
414N/A * @summary FileObject should support user-friendly names via getName()
414N/A */
414N/A
414N/Aimport java.io.*;
414N/Aimport java.util.*;
414N/Aimport java.util.jar.*;
414N/Aimport java.util.zip.*;
414N/Aimport javax.tools.*;
414N/A
414N/Aimport com.sun.tools.javac.file.JavacFileManager;
414N/Aimport com.sun.tools.javac.util.Context;
414N/Aimport com.sun.tools.javac.util.Options;
414N/A
414N/A// Test FileObject.getName returned from JavacFileManager and its support classes.
414N/A
414N/Apublic class Test {
414N/A public static void main(String... args) throws Exception {
414N/A new Test().run();
414N/A }
414N/A
414N/A Set<String> foundClasses = new TreeSet<String>();
414N/A Set<String> foundJars = new TreeSet<String>();
414N/A
414N/A void run() throws Exception {
414N/A File rt_jar = findRtJar();
414N/A
414N/A // names for entries to be created in directories and jar files
414N/A String[] entries = { "p/A.java", "p/A.class", "p/resources/A-1.html" };
414N/A
414N/A // test various combinations of directories and jar files, intended to
414N/A // cover all sources of file objects within JavacFileManager's support classes
414N/A
414N/A test(createFileManager(), createDir("dir", entries), "p", entries);
414N/A test(createFileManager(), createDir("a b/dir", entries), "p", entries);
414N/A
881N/A for (boolean useOptimizedZip: new boolean[] { false, true }) {
881N/A test(createFileManager(useOptimizedZip), createJar("jar", entries), "p", entries);
881N/A test(createFileManager(useOptimizedZip), createJar("jar jar", entries), "p", entries);
414N/A
414N/A for (boolean useSymbolFile: new boolean[] { false, true }) {
881N/A test(createFileManager(useOptimizedZip, useSymbolFile), rt_jar, "java.lang.ref", null);
414N/A }
414N/A }
414N/A
414N/A if (errors > 0)
414N/A throw new Exception(errors + " errors found");
414N/A
414N/A // Verify that we hit all the impl classes we intended
414N/A checkCoverage("classes", foundClasses,
414N/A "RegularFileObject", "SymbolFileObject", "ZipFileIndexFileObject", "ZipFileObject");
414N/A
414N/A // Verify that we hit the jar files we intended, specifically ct.sym as well as rt.jar
414N/A checkCoverage("jar files", foundJars,
414N/A "ct.sym", "jar", "jar jar", "rt.jar");
414N/A }
414N/A
414N/A // use a new file manager for each test
414N/A void test(StandardJavaFileManager fm, File f, String pkg, String[] entries) throws Exception {
414N/A System.err.println("Test " + f);
414N/A try {
414N/A if (f.isDirectory()) {
414N/A for (File dir: new File[] { f, f.getAbsoluteFile() }) {
414N/A for (String e: entries) {
414N/A JavaFileObject fo = fm.getJavaFileObjects(new File(dir, e)).iterator().next();
414N/A test(fo, dir, e);
414N/A }
414N/A }
414N/A }
414N/A
414N/A fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f));
414N/A fm.setLocation(StandardLocation.SOURCE_PATH, Collections.singleton(f.getAbsoluteFile()));
414N/A for (StandardLocation l: EnumSet.of(StandardLocation.CLASS_PATH, StandardLocation.SOURCE_PATH)) {
414N/A for (JavaFileObject fo: fm.list(l, pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) {
414N/A // we could use fm.getLocation but the following guarantees we preserve the original filename
414N/A File dir = (l == StandardLocation.CLASS_PATH ? f : f.getAbsoluteFile());
414N/A char sep = (dir.isDirectory() ? File.separatorChar : '/');
414N/A String b = fm.inferBinaryName(l, fo);
414N/A String e = fo.getKind().extension;
414N/A test(fo, dir, b.replace('.', sep) + e);
414N/A }
414N/A }
414N/A } finally {
414N/A fm.close();
414N/A }
414N/A }
414N/A
414N/A void test(JavaFileObject fo, File dir, String p) {
414N/A System.err.println("Test: " + fo);
414N/A String expect = dir.isDirectory() ? new File(dir, p).getPath() : (dir.getPath() + "(" + p + ")");
414N/A String found = fo.getName();
414N/A // if ct.sym is found, replace it with the equivalent rt.jar
414N/A String found2 = found.replaceAll("lib([\\\\/])ct.sym\\(META-INF/sym/rt.jar/", "jre$1lib$1rt.jar(");
414N/A if (!expect.equals(found2)) {
414N/A System.err.println("expected: " + expect);
414N/A System.err.println(" found: " + found);
414N/A if (!found.equals(found2))
414N/A System.err.println(" found2: " + found2);
414N/A error("Failed: " + fo);
414N/A }
414N/A
414N/A // record the file object class name for coverage checks later
414N/A foundClasses.add(fo.getClass().getSimpleName());
414N/A
414N/A if (found.contains("(")) {
414N/A // record access to the jar file for coverage checks later
414N/A foundJars.add(new File(found.substring(0, found.indexOf("("))).getName());
414N/A }
414N/A }
414N/A
414N/A void checkCoverage(String label, Set<String> found, String... expect) throws Exception {
414N/A Set<String> e = new TreeSet<String>(Arrays.asList(expect));
414N/A if (!found.equals(e)) {
414N/A e.removeAll(found);
414N/A throw new Exception("expected " + label + " not used: " + e);
414N/A }
414N/A }
414N/A
414N/A JavacFileManager createFileManager() {
414N/A return createFileManager(false, false);
414N/A }
414N/A
881N/A JavacFileManager createFileManager(boolean useOptimizedZip) {
881N/A return createFileManager(useOptimizedZip, false);
414N/A }
414N/A
881N/A JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) {
881N/A Context c = new Context();
881N/A Options options = Options.instance(c);
881N/A
922N/A options.put("useOptimizedZip", Boolean.toString(useOptimizedZip));
414N/A
922N/A if (!useSymbolFile) {
922N/A options.put("ignore.symbol.file", "true");
922N/A }
922N/A return new JavacFileManager(c, false, null);
414N/A }
414N/A
414N/A File createDir(String name, String... entries) throws Exception {
414N/A File dir = new File(name);
414N/A if (!dir.mkdirs())
414N/A throw new Exception("cannot create directories " + dir);
414N/A for (String e: entries) {
414N/A writeFile(new File(dir, e), e);
414N/A }
414N/A return dir;
414N/A }
414N/A
414N/A File createJar(String name, String... entries) throws IOException {
414N/A File jar = new File(name);
414N/A OutputStream out = new FileOutputStream(jar);
414N/A try {
414N/A JarOutputStream jos = new JarOutputStream(out);
414N/A for (String e: entries) {
414N/A jos.putNextEntry(new ZipEntry(e));
414N/A jos.write(e.getBytes());
414N/A }
414N/A jos.close();
414N/A } finally {
414N/A out.close();
414N/A }
414N/A return jar;
414N/A }
414N/A
414N/A File findRtJar() throws Exception {
414N/A File java_home = new File(System.getProperty("java.home"));
414N/A if (java_home.getName().equals("jre"))
414N/A java_home = java_home.getParentFile();
414N/A File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar");
414N/A if (!rt_jar.exists())
414N/A throw new Exception("can't find rt.jar");
414N/A return rt_jar;
414N/A }
414N/A
414N/A byte[] read(InputStream in) throws IOException {
414N/A byte[] data = new byte[1024];
414N/A int offset = 0;
414N/A try {
414N/A int n;
414N/A while ((n = in.read(data, offset, data.length - offset)) != -1) {
414N/A offset += n;
414N/A if (offset == data.length)
414N/A data = Arrays.copyOf(data, 2 * data.length);
414N/A }
414N/A } finally {
414N/A in.close();
414N/A }
414N/A return Arrays.copyOf(data, offset);
414N/A }
414N/A
414N/A void writeFile(File f, String s) throws IOException {
414N/A f.getParentFile().mkdirs();
414N/A FileWriter out = new FileWriter(f);
414N/A try {
414N/A out.write(s);
414N/A } finally {
414N/A out.close();
414N/A }
414N/A }
414N/A
414N/A void error(String msg) {
414N/A System.err.println(msg);
414N/A errors++;
414N/A }
414N/A
414N/A int errors;
414N/A}