test/tools/javac/api/T6877206.java

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 1013
8eb952f43b11
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

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

mercurial