test/tools/javac/api/T6877206.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial