jjg@400: /* katleman@1013: * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. jjg@400: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@400: * jjg@400: * This code is free software; you can redistribute it and/or modify it jjg@400: * under the terms of the GNU General Public License version 2 only, as jjg@400: * published by the Free Software Foundation. jjg@400: * jjg@400: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@400: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@400: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@400: * version 2 for more details (a copy is included in the LICENSE file that jjg@400: * accompanied this code). jjg@400: * jjg@400: * You should have received a copy of the GNU General Public License version jjg@400: * 2 along with this work; if not, write to the Free Software Foundation, jjg@400: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@400: * 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@400: */ jjg@400: jjg@400: /* jjg@400: * @test jjg@400: * @bug 6877206 jjg@400: * @summary JavaFileObject.toUri returns bogus URI (win) jjg@400: */ jjg@400: jjg@400: import java.io.*; jjg@400: import java.net.*; jjg@400: import java.util.*; jjg@400: import java.util.jar.*; jjg@400: import java.util.zip.*; jjg@400: import javax.tools.*; jjg@400: jjg@400: import com.sun.tools.javac.file.JavacFileManager; jjg@400: import com.sun.tools.javac.util.Context; jjg@400: import com.sun.tools.javac.util.Options; jjg@400: jjg@400: // Test URIs returned from JavacFileManager and its support classes. jjg@400: // For a variety of file objects, verify the validity of FileObject.toUri() jjg@400: // by verifying the URI exists and points to the same contents as the file jjg@400: // object itself jjg@400: jjg@400: public class T6877206 { jjg@400: public static void main(String... args) throws Exception { jjg@400: new T6877206().run(); jjg@400: } jjg@400: jjg@400: Set foundClasses = new TreeSet(); jjg@400: Set foundJars = new TreeSet(); jjg@400: jjg@400: void run() throws Exception { jjg@400: File rt_jar = findRtJar(); jjg@400: jjg@400: // names for entries to be created in directories and jar files jjg@400: String[] entries = { "p/A.class", "p/resources/A-1.jpg" }; jjg@400: jjg@400: // test various combinations of directories and jar files, intended to jjg@400: // cover all sources of URIs within JavacFileManager's support classes jjg@400: jjg@400: test(createFileManager(), createDir("dir", entries), "p", entries.length); jjg@400: test(createFileManager(), createDir("a b/dir", entries), "p", entries.length); jjg@400: ksrini@882: for (boolean useOptimizedZip: new boolean[] { false, true }) { ksrini@882: test(createFileManager(useOptimizedZip), createJar("jar", entries), "p", entries.length); ksrini@882: test(createFileManager(useOptimizedZip), createJar("jar jar", entries), "p", entries.length); jjg@400: jjg@400: for (boolean useSymbolFile: new boolean[] { false, true }) { ksrini@882: test(createFileManager(useOptimizedZip, useSymbolFile), rt_jar, "java.lang.ref", -1); jjg@400: } jjg@400: } jjg@400: jjg@400: // Verify that we hit all the impl classes we intended jjg@400: checkCoverage("classes", foundClasses, jjg@400: "RegularFileObject", "SymbolFileObject", "ZipFileIndexFileObject", "ZipFileObject"); jjg@400: jjg@400: // Verify that we hit the jar files we intended, specifically ct.sym as well as rt.jar jjg@400: checkCoverage("jar files", foundJars, jjg@400: "ct.sym", "jar", "jar jar", "rt.jar"); jjg@400: } jjg@400: jjg@400: // use a new file manager for each test jjg@400: void test(StandardJavaFileManager fm, File f, String pkg, int expect) throws Exception { jjg@400: JarURLConnection c; jjg@400: System.err.println("Test " + f); jjg@400: try { jjg@400: fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f)); jjg@400: jjg@400: int count = 0; jjg@400: for (JavaFileObject fo: fm.list(StandardLocation.CLASS_PATH, jjg@400: pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) { jjg@400: System.err.println("checking " + fo); jjg@400: // record the file object class name for coverage checks later jjg@400: foundClasses.add(fo.getClass().getSimpleName()); jjg@400: testFileObject(fo); jjg@400: count++; jjg@400: } jjg@400: jjg@400: if (expect > 0 && count != expect) jjg@400: throw new Exception("wrong number of entries found: " jjg@400: + count + ", expected " + expect); jjg@400: } finally { jjg@400: fm.close(); jjg@400: } jjg@400: } jjg@400: jjg@400: void testFileObject(JavaFileObject fo) throws Exception { jjg@400: // test the validity of the result of toUri() by using URLConnection jjg@400: // and comparing the results of reading from the connection with the jjg@400: // result of reading from the file object directly. jjg@400: URI uri = fo.toUri(); jjg@400: System.err.println("uri: " + uri); jjg@400: jjg@400: URLConnection urlconn = uri.toURL().openConnection(); jjg@400: if (urlconn instanceof JarURLConnection) { jjg@400: JarURLConnection jarconn = (JarURLConnection) urlconn; jjg@400: File f = new File(jarconn.getJarFile().getName()); jjg@400: // record access to the jar file for coverage checks later jjg@400: foundJars.add(f.getName()); jjg@400: } jjg@400: jjg@400: try { jjg@400: byte[] uriData = read(urlconn.getInputStream()); jjg@400: byte[] foData = read(fo.openInputStream()); jjg@400: if (!Arrays.equals(uriData, foData)) { jjg@400: if (uriData.length != foData.length) jjg@400: throw new Exception("data size differs: uri data " jjg@400: + uriData.length + " bytes, fo data " + foData.length+ " bytes"); jjg@400: for (int i = 0; i < uriData.length; i++) { jjg@400: if (uriData[i] != foData[i]) jjg@400: throw new Exception("unexpected data returned at offset " + i jjg@400: + ", uri data " + uriData[i] + ", fo data " + foData[i]); jjg@400: } jjg@400: throw new AssertionError("cannot find difference"); jjg@400: } jjg@400: } finally { jjg@400: // In principle, simply closing the result of urlconn.getInputStream() jjg@400: // should have been sufficient. But the internal JarURLConnection jjg@400: // does not close the JarFile in an expeditious manner, thus preventing jjg@400: // jtreg from deleting the jar file before starting the next test. jjg@400: // Therefore we force access to the JarURLConnection to close the jjg@400: // JarFile when necessary. jjg@400: if (urlconn instanceof JarURLConnection) { jjg@400: JarURLConnection jarconn = (JarURLConnection) urlconn; jjg@400: jarconn.getJarFile().close(); jjg@400: } jjg@400: } jjg@400: } jjg@400: jjg@400: void checkCoverage(String label, Set found, String... expect) throws Exception { jjg@400: Set e = new TreeSet(Arrays.asList(expect)); jjg@400: if (!found.equals(e)) { jjg@400: e.removeAll(found); jjg@400: throw new Exception("expected " + label + " not used: " + e); jjg@400: } jjg@400: } jjg@400: jjg@400: JavacFileManager createFileManager() { jjg@400: return createFileManager(false, false); jjg@400: } jjg@400: ksrini@882: JavacFileManager createFileManager(boolean useOptimizedZip) { ksrini@882: return createFileManager(useOptimizedZip, false); jjg@400: } jjg@400: ksrini@882: JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) { ksrini@882: Context ctx = new Context(); ksrini@882: Options options = Options.instance(ctx); ksrini@923: options.put("useOptimizedZip", Boolean.toString(useOptimizedZip)); ksrini@882: if (!useSymbolFile) { ksrini@882: options.put("ignore.symbol.file", "true"); ksrini@882: } ksrini@882: return new JavacFileManager(ctx, false, null); jjg@400: } jjg@400: jjg@400: File createDir(String name, String... entries) throws Exception { jjg@400: File dir = new File(name); jjg@400: if (!dir.mkdirs()) jjg@400: throw new Exception("cannot create directories " + dir); jjg@400: for (String e: entries) { jjg@400: writeFile(new File(dir, e), e); jjg@400: } jjg@400: return dir; jjg@400: } jjg@400: jjg@400: File createJar(String name, String... entries) throws IOException { jjg@400: File jar = new File(name); jjg@400: OutputStream out = new FileOutputStream(jar); jjg@400: try { jjg@400: JarOutputStream jos = new JarOutputStream(out); jjg@400: for (String e: entries) { jjg@400: jos.putNextEntry(new ZipEntry(e)); jjg@400: jos.write(e.getBytes()); jjg@400: } jjg@400: jos.close(); jjg@400: } finally { jjg@400: out.close(); jjg@400: } jjg@400: return jar; jjg@400: } jjg@400: jjg@400: File findRtJar() throws Exception { jjg@400: File java_home = new File(System.getProperty("java.home")); jjg@400: if (java_home.getName().equals("jre")) jjg@400: java_home = java_home.getParentFile(); jjg@400: File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar"); jjg@400: if (!rt_jar.exists()) jjg@400: throw new Exception("can't find rt.jar"); jjg@400: return rt_jar; jjg@400: } jjg@400: jjg@400: byte[] read(InputStream in) throws IOException { jjg@400: byte[] data = new byte[1024]; jjg@400: int offset = 0; jjg@400: try { jjg@400: int n; jjg@400: while ((n = in.read(data, offset, data.length - offset)) != -1) { jjg@400: offset += n; jjg@400: if (offset == data.length) jjg@400: data = Arrays.copyOf(data, 2 * data.length); jjg@400: } jjg@400: } finally { jjg@400: in.close(); jjg@400: } jjg@400: return Arrays.copyOf(data, offset); jjg@400: } jjg@400: jjg@400: void writeFile(File f, String s) throws IOException { jjg@400: f.getParentFile().mkdirs(); jjg@400: FileWriter out = new FileWriter(f); jjg@400: try { jjg@400: out.write(s); jjg@400: } finally { jjg@400: out.close(); jjg@400: } jjg@400: } jjg@400: }