1/*
2 * Copyright (c) 2006, 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/* @test
25 * @bug 6929404
26 * @summary Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
27 * @library /tools/javac/lib
28 * @modules java.compiler
29 *          jdk.compiler
30 */
31
32import java.io.*;
33import java.security.*;
34import java.util.*;
35import javax.annotation.processing.*;
36import javax.lang.model.*;
37import javax.lang.model.element.*;
38import javax.tools.*;
39import javax.tools.Diagnostic.Kind;
40import javax.tools.JavaCompiler.CompilationTask;
41
42public class TestGetResource2 {
43
44    public static void main(String[] args) throws Exception {
45        new TestGetResource2().run();
46    }
47
48    void run() throws Exception {
49        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
50        CodeSource cs = javac.getClass().getProtectionDomain().getCodeSource();
51        if (cs == null) {
52            System.err.println("got compiler from " +
53                ClassLoader.getSystemResource(javac.getClass().getName().replace(".", "/")+".class"));
54        } else {
55            System.err.println("got compiler from " + cs.getLocation());
56        }
57
58        testSingleSourceDir(javac);
59        testCompositeSourcePath(javac);
60        testMissingResource(javac);
61    }
62
63    private void testSingleSourceDir(JavaCompiler javac) throws Exception {
64        System.err.println("testSingleSourceDir");
65        File tmpdir = new File("testSingleSourceDir");
66        File srcdir = new File(tmpdir, "src");
67        File destdir = new File(tmpdir, "dest");
68        write(srcdir, "pkg/X.java", "package pkg; class X {}");
69        write(srcdir, "resources/file.txt", "hello");
70        destdir.mkdirs();
71
72        CompilationTask task = javac.getTask(null, null, null,
73                Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
74                Collections.singleton("pkg.X"), null);
75        task.setProcessors(Collections.singleton(new AnnoProc()));
76        boolean result = task.call();
77        System.err.println("javac result with single source dir: " + result);
78        expect(result, true);
79    }
80
81    private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
82        System.err.println("testCompositeSearchPath");
83        File tmpdir = new File("testCompositeSourcePath");
84        File srcdir = new File(tmpdir, "src");
85        File rsrcdir = new File(tmpdir, "rsrc");
86        File destdir = new File(tmpdir, "dest");
87        write(srcdir, "pkg/X.java", "package pkg; class X {}");
88        write(rsrcdir, "resources/file.txt", "hello");
89        destdir.mkdirs();
90
91        CompilationTask task = javac.getTask(null, null, null,
92                Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
93                Collections.singleton("pkg.X"), null);
94        task.setProcessors(Collections.singleton(new AnnoProc()));
95        boolean result = task.call();
96        System.err.println("javac result with composite source path: " + result);
97        expect(result, true);
98    }
99
100    private void testMissingResource(JavaCompiler javac) throws Exception {
101        System.err.println("testMissingResource");
102        File tmpdir = new File("testMissingResource");
103        File srcdir = new File(tmpdir, "src");
104        File destdir = new File(tmpdir, "dest");
105        write(srcdir, "pkg/X.java", "package pkg; class X {}");
106        destdir.mkdirs();
107
108        CompilationTask task = javac.getTask(null, null, null,
109                Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
110                Collections.singleton("pkg.X"), null);
111        task.setProcessors(Collections.singleton(new AnnoProc()));
112        boolean result = task.call();
113        System.err.println("javac result when missing resource: " + result);
114        expect(result, false);
115
116        if (errors > 0)
117            throw new Exception(errors + " errors occurred");
118    }
119
120    static class AnnoProc extends JavacTestingAbstractProcessor {
121
122        public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
123            if (roundEnv.processingOver()) {
124                return false;
125            }
126
127            try {
128                FileObject resource = filer.getResource(StandardLocation.SOURCE_PATH, "resources", "file.txt");
129                try {
130                    resource.openInputStream().close();
131                    messager.printMessage(Kind.NOTE, "found: " + resource.toUri());
132                    return true;
133                } catch (IOException x) {
134                    messager.printMessage(Kind.ERROR, "could not read: " + resource.toUri());
135                    x.printStackTrace();
136                }
137            } catch (IOException x) {
138                messager.printMessage(Kind.ERROR, "did not find resource");
139                x.printStackTrace();
140            }
141
142            return false;
143        }
144
145    }
146
147    private File write(File dir, String path, String contents) throws IOException {
148        File f = new File(dir, path);
149        f.getParentFile().mkdirs();
150        Writer w = new FileWriter(f);
151        try {
152            w.write(contents);
153        } finally {
154            w.close();
155        }
156        return f;
157    }
158
159    void expect(boolean val, boolean expect) {
160        if (val != expect)
161            error("Unexpected value: " + val + "; expected: " + expect);
162    }
163
164    void error(String msg) {
165        System.err.println(msg);
166        errors++;
167    }
168
169    int errors = 0;
170}
171