test/tools/javac/api/T6877206.java

changeset 400
35e29f51a7c3
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/api/T6877206.java	Tue Sep 08 11:12:13 2009 -0700
     1.3 @@ -0,0 +1,263 @@
     1.4 +/*
     1.5 + * Copyright 2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any 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 useJavaUtilZip: new boolean[] { false, true }) {
    1.70 +            test(createFileManager(useJavaUtilZip), createJar("jar", entries), "p", entries.length);
    1.71 +            test(createFileManager(useJavaUtilZip), createJar("jar jar", entries), "p", entries.length);
    1.72 +
    1.73 +            for (boolean useSymbolFile: new boolean[] { false, true }) {
    1.74 +                test(createFileManager(useJavaUtilZip, 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 useJavaUtilZip) {
   1.168 +        return createFileManager(useJavaUtilZip, false);
   1.169 +    }
   1.170 +
   1.171 +    JavacFileManager createFileManager(boolean useJavaUtilZip, boolean useSymbolFile) {
   1.172 +        // javac should really not be using system properties like this
   1.173 +        // -- it should really be using (hidden) options -- but until then
   1.174 +        // take care to leave system properties as we find them, so as not
   1.175 +        // to adversely affect other tests that might follow.
   1.176 +        String prev = System.getProperty("useJavaUtilZip");
   1.177 +        boolean resetProperties = false;
   1.178 +        try {
   1.179 +            if (useJavaUtilZip) {
   1.180 +                System.setProperty("useJavaUtilZip", "true");
   1.181 +                resetProperties = true;
   1.182 +            } else if (System.getProperty("useJavaUtilZip") != null) {
   1.183 +                System.getProperties().remove("useJavaUtilZip");
   1.184 +                resetProperties = true;
   1.185 +            }
   1.186 +
   1.187 +            Context c = new Context();
   1.188 +            if (!useSymbolFile) {
   1.189 +                Options options = Options.instance(c);
   1.190 +                options.put("ignore.symbol.file", "true");
   1.191 +            }
   1.192 +
   1.193 +            return new JavacFileManager(c, false, null);
   1.194 +        } finally {
   1.195 +            if (resetProperties) {
   1.196 +                if (prev == null) {
   1.197 +                    System.getProperties().remove("useJavaUtilZip");
   1.198 +                } else {
   1.199 +                    System.setProperty("useJavaUtilZip", prev);
   1.200 +                }
   1.201 +            }
   1.202 +        }
   1.203 +    }
   1.204 +
   1.205 +    File createDir(String name, String... entries) throws Exception {
   1.206 +        File dir = new File(name);
   1.207 +        if (!dir.mkdirs())
   1.208 +            throw new Exception("cannot create directories " + dir);
   1.209 +        for (String e: entries) {
   1.210 +            writeFile(new File(dir, e), e);
   1.211 +        }
   1.212 +        return dir;
   1.213 +    }
   1.214 +
   1.215 +    File createJar(String name, String... entries) throws IOException {
   1.216 +        File jar = new File(name);
   1.217 +        OutputStream out = new FileOutputStream(jar);
   1.218 +        try {
   1.219 +            JarOutputStream jos = new JarOutputStream(out);
   1.220 +            for (String e: entries) {
   1.221 +                jos.putNextEntry(new ZipEntry(e));
   1.222 +                jos.write(e.getBytes());
   1.223 +            }
   1.224 +            jos.close();
   1.225 +        } finally {
   1.226 +            out.close();
   1.227 +        }
   1.228 +        return jar;
   1.229 +    }
   1.230 +
   1.231 +    File findRtJar() throws Exception {
   1.232 +        File java_home = new File(System.getProperty("java.home"));
   1.233 +        if (java_home.getName().equals("jre"))
   1.234 +            java_home = java_home.getParentFile();
   1.235 +        File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar");
   1.236 +        if (!rt_jar.exists())
   1.237 +            throw new Exception("can't find rt.jar");
   1.238 +        return rt_jar;
   1.239 +    }
   1.240 +
   1.241 +    byte[] read(InputStream in) throws IOException {
   1.242 +        byte[] data = new byte[1024];
   1.243 +        int offset = 0;
   1.244 +        try {
   1.245 +            int n;
   1.246 +            while ((n = in.read(data, offset, data.length - offset)) != -1) {
   1.247 +                offset += n;
   1.248 +                if (offset == data.length)
   1.249 +                    data = Arrays.copyOf(data, 2 * data.length);
   1.250 +            }
   1.251 +        } finally {
   1.252 +            in.close();
   1.253 +        }
   1.254 +        return Arrays.copyOf(data, offset);
   1.255 +    }
   1.256 +
   1.257 +    void writeFile(File f, String s) throws IOException {
   1.258 +        f.getParentFile().mkdirs();
   1.259 +        FileWriter out = new FileWriter(f);
   1.260 +        try {
   1.261 +            out.write(s);
   1.262 +        } finally {
   1.263 +            out.close();
   1.264 +        }
   1.265 +    }
   1.266 +}

mercurial