T6877206.java revision 2721:f7ce2cfa4cdb
1/* 2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6877206 27 * @summary JavaFileObject.toUri returns bogus URI (win) 28 */ 29 30import java.io.*; 31import java.net.*; 32import java.util.*; 33import java.util.jar.*; 34import java.util.zip.*; 35import javax.tools.*; 36 37import com.sun.tools.javac.file.JavacFileManager; 38import com.sun.tools.javac.util.Context; 39import com.sun.tools.javac.util.Options; 40 41// Test URIs returned from JavacFileManager and its support classes. 42// For a variety of file objects, verify the validity of FileObject.toUri() 43// by verifying the URI exists and points to the same contents as the file 44// object itself 45 46public class T6877206 { 47 public static void main(String... args) throws Exception { 48 new T6877206().run(); 49 } 50 51 Set<String> foundClasses = new TreeSet<String>(); 52 Set<String> foundJars = new TreeSet<String>(); 53 54 void run() throws Exception { 55 // names for entries to be created in directories and jar files 56 String[] entries = { "p/A.class", "p/resources/A-1.jpg" }; 57 58 // test various combinations of directories and jar files, intended to 59 // cover all sources of URIs within JavacFileManager's support classes 60 61 test(createFileManager(), createDir("dir", entries), "p", entries.length); 62 test(createFileManager(), createDir("a b/dir", entries), "p", entries.length); 63 64 for (boolean useOptimizedZip: new boolean[] { false, true }) { 65 test(createFileManager(useOptimizedZip), createJar("jar", entries), "p", entries.length); 66 test(createFileManager(useOptimizedZip), createJar("jar jar", entries), "p", entries.length); 67 } 68 69 // Verify that we hit the files we intended 70 checkCoverage("classes", foundClasses, 71 "RegularFileObject", "ZipFileIndexFileObject", "ZipFileObject"); 72 73 // Verify that we hit the jar files we intended 74 checkCoverage("jar files", foundJars, "jar", "jar jar"); 75 } 76 77 // use a new file manager for each test 78 void test(StandardJavaFileManager fm, File f, String pkg, int expect) throws Exception { 79 JarURLConnection c; 80 System.err.println("Test " + f); 81 try { 82 fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f)); 83 84 int count = 0; 85 for (JavaFileObject fo: fm.list(StandardLocation.CLASS_PATH, 86 pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) { 87 System.err.println("checking " + fo); 88 // record the file object class name for coverage checks later 89 foundClasses.add(fo.getClass().getSimpleName()); 90 testFileObject(fo); 91 count++; 92 } 93 94 if (expect > 0 && count != expect) 95 throw new Exception("wrong number of entries found: " 96 + count + ", expected " + expect); 97 } finally { 98 fm.close(); 99 } 100 } 101 102 void testFileObject(JavaFileObject fo) throws Exception { 103 // test the validity of the result of toUri() by using URLConnection 104 // and comparing the results of reading from the connection with the 105 // result of reading from the file object directly. 106 URI uri = fo.toUri(); 107 System.err.println("uri: " + uri); 108 109 URLConnection urlconn = uri.toURL().openConnection(); 110 if (urlconn instanceof JarURLConnection) { 111 JarURLConnection jarconn = (JarURLConnection) urlconn; 112 File f = new File(jarconn.getJarFile().getName()); 113 // record access to the jar file for coverage checks later 114 foundJars.add(f.getName()); 115 } 116 117 try { 118 byte[] uriData = read(urlconn.getInputStream()); 119 byte[] foData = read(fo.openInputStream()); 120 if (!Arrays.equals(uriData, foData)) { 121 if (uriData.length != foData.length) 122 throw new Exception("data size differs: uri data " 123 + uriData.length + " bytes, fo data " + foData.length+ " bytes"); 124 for (int i = 0; i < uriData.length; i++) { 125 if (uriData[i] != foData[i]) 126 throw new Exception("unexpected data returned at offset " + i 127 + ", uri data " + uriData[i] + ", fo data " + foData[i]); 128 } 129 throw new AssertionError("cannot find difference"); 130 } 131 } finally { 132 // In principle, simply closing the result of urlconn.getInputStream() 133 // should have been sufficient. But the internal JarURLConnection 134 // does not close the JarFile in an expeditious manner, thus preventing 135 // jtreg from deleting the jar file before starting the next test. 136 // Therefore we force access to the JarURLConnection to close the 137 // JarFile when necessary. 138 if (urlconn instanceof JarURLConnection) { 139 JarURLConnection jarconn = (JarURLConnection) urlconn; 140 jarconn.getJarFile().close(); 141 } 142 } 143 } 144 145 void checkCoverage(String label, Set<String> found, String... expect) throws Exception { 146 Set<String> e = new TreeSet<String>(Arrays.asList(expect)); 147 if (!found.equals(e)) { 148 e.removeAll(found); 149 throw new Exception("expected " + label + " not used: " + e); 150 } 151 } 152 153 JavacFileManager createFileManager() { 154 return createFileManager(false, false); 155 } 156 157 JavacFileManager createFileManager(boolean useOptimizedZip) { 158 return createFileManager(useOptimizedZip, false); 159 } 160 161 JavacFileManager createFileManager(boolean useOptimizedZip, boolean useSymbolFile) { 162 Context ctx = new Context(); 163 Options options = Options.instance(ctx); 164 options.put("useOptimizedZip", Boolean.toString(useOptimizedZip)); 165 if (!useSymbolFile) { 166 options.put("ignore.symbol.file", "true"); 167 } 168 return new JavacFileManager(ctx, false, null); 169 } 170 171 File createDir(String name, String... entries) throws Exception { 172 File dir = new File(name); 173 if (!dir.mkdirs()) 174 throw new Exception("cannot create directories " + dir); 175 for (String e: entries) { 176 writeFile(new File(dir, e), e); 177 } 178 return dir; 179 } 180 181 File createJar(String name, String... entries) throws IOException { 182 File jar = new File(name); 183 OutputStream out = new FileOutputStream(jar); 184 try { 185 JarOutputStream jos = new JarOutputStream(out); 186 for (String e: entries) { 187 jos.putNextEntry(new ZipEntry(e)); 188 jos.write(e.getBytes()); 189 } 190 jos.close(); 191 } finally { 192 out.close(); 193 } 194 return jar; 195 } 196 197 byte[] read(InputStream in) throws IOException { 198 byte[] data = new byte[1024]; 199 int offset = 0; 200 try { 201 int n; 202 while ((n = in.read(data, offset, data.length - offset)) != -1) { 203 offset += n; 204 if (offset == data.length) 205 data = Arrays.copyOf(data, 2 * data.length); 206 } 207 } finally { 208 in.close(); 209 } 210 return Arrays.copyOf(data, offset); 211 } 212 213 void writeFile(File f, String s) throws IOException { 214 f.getParentFile().mkdirs(); 215 FileWriter out = new FileWriter(f); 216 try { 217 out.write(s); 218 } finally { 219 out.close(); 220 } 221 } 222} 223