T6877206.java revision 3155:30e288cb2d22
1/*
2 * Copyright (c) 2009, 2015, 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 * @modules jdk.compiler/com.sun.tools.javac.file
29 *          jdk.compiler/com.sun.tools.javac.util
30 */
31
32import java.io.*;
33import java.net.*;
34import java.util.*;
35import java.util.jar.*;
36import java.util.zip.*;
37import javax.tools.*;
38
39import com.sun.tools.javac.file.JavacFileManager;
40import com.sun.tools.javac.util.Context;
41import com.sun.tools.javac.util.Options;
42
43// Test URIs returned from JavacFileManager and its support classes.
44// For a variety of file objects, verify the validity of FileObject.toUri()
45// by verifying the URI exists and points to the same contents as the file
46// object itself
47
48public class T6877206 {
49    public static void main(String... args) throws Exception {
50        new T6877206().run();
51    }
52
53    Set<String> foundClasses = new TreeSet<String>();
54    Set<String> foundJars = new TreeSet<String>();
55
56    void run() throws Exception {
57        // names for entries to be created in directories and jar files
58        String[] entries = { "p/A.class", "p/resources/A-1.jpg" };
59
60        // test various combinations of directories and jar files, intended to
61        // cover all sources of URIs within JavacFileManager's support classes
62
63        test(createFileManager(), createDir("dir", entries), "p", entries.length);
64        test(createFileManager(), createDir("a b/dir", entries), "p", entries.length);
65        test(createFileManager(), createJar("jar", entries), "p", entries.length);
66        test(createFileManager(), createJar("jar jar", entries), "p", entries.length);
67
68        // Verify that we hit the files we intended
69        checkCoverage("classes", foundClasses,
70                "DirectoryFileObject", "JarFileObject");
71
72        // Verify that we hit the jar files we intended
73        checkCoverage("jar files", foundJars, "jar", "jar jar");
74    }
75
76    // use a new file manager for each test
77    void test(StandardJavaFileManager fm, File f, String pkg, int expect) throws Exception {
78        JarURLConnection c;
79        System.err.println("Test " + f);
80        try {
81            fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f));
82
83            int count = 0;
84            for (JavaFileObject fo: fm.list(StandardLocation.CLASS_PATH,
85                    pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) {
86                System.err.println("checking " + fo);
87                // record the file object class name for coverage checks later
88                foundClasses.add(fo.getClass().getSimpleName());
89                testFileObject(fo);
90                count++;
91            }
92
93            if (expect > 0 && count != expect)
94                throw new Exception("wrong number of entries found: "
95                        + count + ", expected " + expect);
96        } finally {
97            fm.close();
98        }
99    }
100
101    void testFileObject(JavaFileObject fo) throws Exception {
102        // test the validity of the result of toUri() by using URLConnection
103        // and comparing the results of reading from the connection with the
104        // result of reading from the file object directly.
105        URI uri = fo.toUri();
106        System.err.println("uri: " + uri);
107
108        URLConnection urlconn = uri.toURL().openConnection();
109        if (urlconn instanceof JarURLConnection) {
110            JarURLConnection jarconn = (JarURLConnection) urlconn;
111            File f = new File(jarconn.getJarFile().getName());
112            // record access to the jar file for coverage checks later
113            foundJars.add(f.getName());
114        }
115
116        try {
117            byte[] uriData = read(urlconn.getInputStream());
118            byte[] foData = read(fo.openInputStream());
119            if (!Arrays.equals(uriData, foData)) {
120                if (uriData.length != foData.length)
121                    throw new Exception("data size differs: uri data "
122                            + uriData.length + " bytes, fo data " + foData.length+ " bytes");
123                for (int i = 0; i < uriData.length; i++) {
124                    if (uriData[i] != foData[i])
125                    throw new Exception("unexpected data returned at offset " + i
126                            + ", uri data " + uriData[i] + ", fo data " + foData[i]);
127                }
128                throw new AssertionError("cannot find difference");
129            }
130        } finally {
131            // In principle, simply closing the result of urlconn.getInputStream()
132            // should have been sufficient. But the internal JarURLConnection
133            // does not close the JarFile in an expeditious manner, thus preventing
134            // jtreg from deleting the jar file before starting the next test.
135            // Therefore we force access to the JarURLConnection to close the
136            // JarFile when necessary.
137            if (urlconn instanceof JarURLConnection) {
138                JarURLConnection jarconn = (JarURLConnection) urlconn;
139                jarconn.getJarFile().close();
140            }
141        }
142    }
143
144    void checkCoverage(String label, Set<String> found, String... expect) throws Exception {
145        Set<String> e = new TreeSet<String>(Arrays.asList(expect));
146        if (!found.equals(e)) {
147            e.removeAll(found);
148            throw new Exception("expected " + label + " not used: " + e);
149        }
150    }
151
152    JavacFileManager createFileManager() {
153        return createFileManager(false);
154    }
155
156    JavacFileManager createFileManager(boolean useSymbolFile) {
157        Context ctx = new Context();
158        Options options = Options.instance(ctx);
159        if (!useSymbolFile) {
160            options.put("ignore.symbol.file", "true");
161        }
162        return new JavacFileManager(ctx, false, null);
163    }
164
165    File createDir(String name, String... entries) throws Exception {
166        File dir = new File(name);
167        if (!dir.mkdirs())
168            throw new Exception("cannot create directories " + dir);
169        for (String e: entries) {
170            writeFile(new File(dir, e), e);
171        }
172        return dir;
173    }
174
175    File createJar(String name, String... entries) throws IOException {
176        File jar = new File(name);
177        OutputStream out = new FileOutputStream(jar);
178        try {
179            JarOutputStream jos = new JarOutputStream(out);
180            for (String e: entries) {
181                jos.putNextEntry(new ZipEntry(e));
182                jos.write(e.getBytes());
183            }
184            jos.close();
185        } finally {
186            out.close();
187        }
188        return jar;
189    }
190
191    byte[] read(InputStream in) throws IOException {
192        byte[] data = new byte[1024];
193        int offset = 0;
194        try {
195            int n;
196            while ((n = in.read(data, offset, data.length - offset)) != -1) {
197                offset += n;
198                if (offset == data.length)
199                    data = Arrays.copyOf(data, 2 * data.length);
200            }
201        } finally {
202            in.close();
203        }
204        return Arrays.copyOf(data, offset);
205    }
206
207    void writeFile(File f, String s) throws IOException {
208        f.getParentFile().mkdirs();
209        FileWriter out = new FileWriter(f);
210        try {
211            out.write(s);
212        } finally {
213            out.close();
214        }
215    }
216}
217