1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/T6877206.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,239 @@ 1.4 +/* 1.5 + * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6877206 1.30 + * @summary JavaFileObject.toUri returns bogus URI (win) 1.31 + */ 1.32 + 1.33 +import java.io.*; 1.34 +import java.net.*; 1.35 +import java.util.*; 1.36 +import java.util.jar.*; 1.37 +import java.util.zip.*; 1.38 +import javax.tools.*; 1.39 + 1.40 +import com.sun.tools.javac.file.JavacFileManager; 1.41 +import com.sun.tools.javac.util.Context; 1.42 +import com.sun.tools.javac.util.Options; 1.43 + 1.44 +// Test URIs returned from JavacFileManager and its support classes. 1.45 +// For a variety of file objects, verify the validity of FileObject.toUri() 1.46 +// by verifying the URI exists and points to the same contents as the file 1.47 +// object itself 1.48 + 1.49 +public class T6877206 { 1.50 + public static void main(String... args) throws Exception { 1.51 + new T6877206().run(); 1.52 + } 1.53 + 1.54 + Set<String> foundClasses = new TreeSet<String>(); 1.55 + Set<String> foundJars = new TreeSet<String>(); 1.56 + 1.57 + void run() throws Exception { 1.58 + File rt_jar = findRtJar(); 1.59 + 1.60 + // names for entries to be created in directories and jar files 1.61 + String[] entries = { "p/A.class", "p/resources/A-1.jpg" }; 1.62 + 1.63 + // test various combinations of directories and jar files, intended to 1.64 + // cover all sources of URIs within JavacFileManager's support classes 1.65 + 1.66 + test(createFileManager(), createDir("dir", entries), "p", entries.length); 1.67 + test(createFileManager(), createDir("a b/dir", entries), "p", entries.length); 1.68 + 1.69 + for (boolean useOptimizedZip: new boolean[] { false, true }) { 1.70 + test(createFileManager(useOptimizedZip), createJar("jar", entries), "p", entries.length); 1.71 + test(createFileManager(useOptimizedZip), createJar("jar jar", entries), "p", entries.length); 1.72 + 1.73 + for (boolean useSymbolFile: new boolean[] { false, true }) { 1.74 + test(createFileManager(useOptimizedZip, useSymbolFile), rt_jar, "java.lang.ref", -1); 1.75 + } 1.76 + } 1.77 + 1.78 + // Verify that we hit all the impl classes we intended 1.79 + checkCoverage("classes", foundClasses, 1.80 + "RegularFileObject", "SymbolFileObject", "ZipFileIndexFileObject", "ZipFileObject"); 1.81 + 1.82 + // Verify that we hit the jar files we intended, specifically ct.sym as well as rt.jar 1.83 + checkCoverage("jar files", foundJars, 1.84 + "ct.sym", "jar", "jar jar", "rt.jar"); 1.85 + } 1.86 + 1.87 + // use a new file manager for each test 1.88 + void test(StandardJavaFileManager fm, File f, String pkg, int expect) throws Exception { 1.89 + JarURLConnection c; 1.90 + System.err.println("Test " + f); 1.91 + try { 1.92 + fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f)); 1.93 + 1.94 + int count = 0; 1.95 + for (JavaFileObject fo: fm.list(StandardLocation.CLASS_PATH, 1.96 + pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) { 1.97 + System.err.println("checking " + fo); 1.98 + // record the file object class name for coverage checks later 1.99 + foundClasses.add(fo.getClass().getSimpleName()); 1.100 + testFileObject(fo); 1.101 + count++; 1.102 + } 1.103 + 1.104 + if (expect > 0 && count != expect) 1.105 + throw new Exception("wrong number of entries found: " 1.106 + + count + ", expected " + expect); 1.107 + } finally { 1.108 + fm.close(); 1.109 + } 1.110 + } 1.111 + 1.112 + void testFileObject(JavaFileObject fo) throws Exception { 1.113 + // test the validity of the result of toUri() by using URLConnection 1.114 + // and comparing the results of reading from the connection with the 1.115 + // result of reading from the file object directly. 1.116 + URI uri = fo.toUri(); 1.117 + System.err.println("uri: " + uri); 1.118 + 1.119 + URLConnection urlconn = uri.toURL().openConnection(); 1.120 + if (urlconn instanceof JarURLConnection) { 1.121 + JarURLConnection jarconn = (JarURLConnection) urlconn; 1.122 + File f = new File(jarconn.getJarFile().getName()); 1.123 + // record access to the jar file for coverage checks later 1.124 + foundJars.add(f.getName()); 1.125 + } 1.126 + 1.127 + try { 1.128 + byte[] uriData = read(urlconn.getInputStream()); 1.129 + byte[] foData = read(fo.openInputStream()); 1.130 + if (!Arrays.equals(uriData, foData)) { 1.131 + if (uriData.length != foData.length) 1.132 + throw new Exception("data size differs: uri data " 1.133 + + uriData.length + " bytes, fo data " + foData.length+ " bytes"); 1.134 + for (int i = 0; i < uriData.length; i++) { 1.135 + if (uriData[i] != foData[i]) 1.136 + throw new Exception("unexpected data returned at offset " + i 1.137 + + ", uri data " + uriData[i] + ", fo data " + foData[i]); 1.138 + } 1.139 + throw new AssertionError("cannot find difference"); 1.140 + } 1.141 + } finally { 1.142 + // In principle, simply closing the result of urlconn.getInputStream() 1.143 + // should have been sufficient. But the internal JarURLConnection 1.144 + // does not close the JarFile in an expeditious manner, thus preventing 1.145 + // jtreg from deleting the jar file before starting the next test. 1.146 + // Therefore we force access to the JarURLConnection to close the 1.147 + // JarFile when necessary. 1.148 + if (urlconn instanceof JarURLConnection) { 1.149 + JarURLConnection jarconn = (JarURLConnection) urlconn; 1.150 + jarconn.getJarFile().close(); 1.151 + } 1.152 + } 1.153 + } 1.154 + 1.155 + void checkCoverage(String label, Set<String> found, String... expect) throws Exception { 1.156 + Set<String> e = new TreeSet<String>(Arrays.asList(expect)); 1.157 + if (!found.equals(e)) { 1.158 + e.removeAll(found); 1.159 + throw new Exception("expected " + label + " not used: " + e); 1.160 + } 1.161 + } 1.162 + 1.163 + JavacFileManager createFileManager() { 1.164 + return createFileManager(false, false); 1.165 + } 1.166 + 1.167 + JavacFileManager createFileManager(boolean useOptimizedZip) { 1.168 + return createFileManager(useOptimizedZip, false); 1.169 + } 1.170 + 1.171 + JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) { 1.172 + Context ctx = new Context(); 1.173 + Options options = Options.instance(ctx); 1.174 + options.put("useOptimizedZip", Boolean.toString(useOptimizedZip)); 1.175 + if (!useSymbolFile) { 1.176 + options.put("ignore.symbol.file", "true"); 1.177 + } 1.178 + return new JavacFileManager(ctx, false, null); 1.179 + } 1.180 + 1.181 + File createDir(String name, String... entries) throws Exception { 1.182 + File dir = new File(name); 1.183 + if (!dir.mkdirs()) 1.184 + throw new Exception("cannot create directories " + dir); 1.185 + for (String e: entries) { 1.186 + writeFile(new File(dir, e), e); 1.187 + } 1.188 + return dir; 1.189 + } 1.190 + 1.191 + File createJar(String name, String... entries) throws IOException { 1.192 + File jar = new File(name); 1.193 + OutputStream out = new FileOutputStream(jar); 1.194 + try { 1.195 + JarOutputStream jos = new JarOutputStream(out); 1.196 + for (String e: entries) { 1.197 + jos.putNextEntry(new ZipEntry(e)); 1.198 + jos.write(e.getBytes()); 1.199 + } 1.200 + jos.close(); 1.201 + } finally { 1.202 + out.close(); 1.203 + } 1.204 + return jar; 1.205 + } 1.206 + 1.207 + File findRtJar() throws Exception { 1.208 + File java_home = new File(System.getProperty("java.home")); 1.209 + if (java_home.getName().equals("jre")) 1.210 + java_home = java_home.getParentFile(); 1.211 + File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar"); 1.212 + if (!rt_jar.exists()) 1.213 + throw new Exception("can't find rt.jar"); 1.214 + return rt_jar; 1.215 + } 1.216 + 1.217 + byte[] read(InputStream in) throws IOException { 1.218 + byte[] data = new byte[1024]; 1.219 + int offset = 0; 1.220 + try { 1.221 + int n; 1.222 + while ((n = in.read(data, offset, data.length - offset)) != -1) { 1.223 + offset += n; 1.224 + if (offset == data.length) 1.225 + data = Arrays.copyOf(data, 2 * data.length); 1.226 + } 1.227 + } finally { 1.228 + in.close(); 1.229 + } 1.230 + return Arrays.copyOf(data, offset); 1.231 + } 1.232 + 1.233 + void writeFile(File f, String s) throws IOException { 1.234 + f.getParentFile().mkdirs(); 1.235 + FileWriter out = new FileWriter(f); 1.236 + try { 1.237 + out.write(s); 1.238 + } finally { 1.239 + out.close(); 1.240 + } 1.241 + } 1.242 +}