TestGetResource2.java 6.3 KB
Newer Older
1
/*
2
 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/* @test
 * @bug 6929404
 * @summary Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
27
 * @library ../../lib
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 */

import java.io.*;
import java.security.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.*;
import javax.lang.model.element.*;
import javax.tools.*;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaCompiler.CompilationTask;

public class TestGetResource2 {

    public static void main(String[] args) throws Exception {
        new TestGetResource2().run();
    }

    void run() throws Exception {
        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
        CodeSource cs = javac.getClass().getProtectionDomain().getCodeSource();
        if (cs == null) {
            System.err.println("got compiler from " +
                ClassLoader.getSystemResource(javac.getClass().getName().replace(".", "/")+".class"));
        } else {
            System.err.println("got compiler from " + cs.getLocation());
        }

        testSingleSourceDir(javac);
        testCompositeSourcePath(javac);
        testMissingResource(javac);
    }

    private void testSingleSourceDir(JavaCompiler javac) throws Exception {
        System.err.println("testSingleSourceDir");
        File tmpdir = new File("testSingleSourceDir");
        File srcdir = new File(tmpdir, "src");
        File destdir = new File(tmpdir, "dest");
        write(srcdir, "pkg/X.java", "package pkg; class X {}");
        write(srcdir, "resources/file.txt", "hello");
        destdir.mkdirs();

        CompilationTask task = javac.getTask(null, null, null,
                Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
                Collections.singleton("pkg.X"), null);
        task.setProcessors(Collections.singleton(new AnnoProc()));
        boolean result = task.call();
        System.err.println("javac result with single source dir: " + result);
        expect(result, true);
    }

    private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
        System.err.println("testCompositeSearchPath");
        File tmpdir = new File("testCompositeSourcePath");
        File srcdir = new File(tmpdir, "src");
        File rsrcdir = new File(tmpdir, "rsrc");
        File destdir = new File(tmpdir, "dest");
        write(srcdir, "pkg/X.java", "package pkg; class X {}");
        write(rsrcdir, "resources/file.txt", "hello");
        destdir.mkdirs();

        CompilationTask task = javac.getTask(null, null, null,
                Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
                Collections.singleton("pkg.X"), null);
        task.setProcessors(Collections.singleton(new AnnoProc()));
        boolean result = task.call();
        System.err.println("javac result with composite source path: " + result);
        expect(result, true);
    }

    private void testMissingResource(JavaCompiler javac) throws Exception {
        System.err.println("testMissingResource");
        File tmpdir = new File("testMissingResource");
        File srcdir = new File(tmpdir, "src");
        File destdir = new File(tmpdir, "dest");
        write(srcdir, "pkg/X.java", "package pkg; class X {}");
        destdir.mkdirs();

        CompilationTask task = javac.getTask(null, null, null,
                Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
                Collections.singleton("pkg.X"), null);
        task.setProcessors(Collections.singleton(new AnnoProc()));
        boolean result = task.call();
        System.err.println("javac result when missing resource: " + result);
        expect(result, false);

        if (errors > 0)
            throw new Exception(errors + " errors occurred");
    }

118
    static class AnnoProc extends JavacTestingAbstractProcessor {
119 120 121 122 123 124 125

        public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
            if (roundEnv.processingOver()) {
                return false;
            }

            try {
126
                FileObject resource = filer.getResource(StandardLocation.SOURCE_PATH, "resources", "file.txt");
127 128
                try {
                    resource.openInputStream().close();
129
                    messager.printMessage(Kind.NOTE, "found: " + resource.toUri());
130 131
                    return true;
                } catch (IOException x) {
132
                    messager.printMessage(Kind.ERROR, "could not read: " + resource.toUri());
133 134 135
                    x.printStackTrace();
                }
            } catch (IOException x) {
136
                messager.printMessage(Kind.ERROR, "did not find resource");
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                x.printStackTrace();
            }

            return false;
        }

    }

    private File write(File dir, String path, String contents) throws IOException {
        File f = new File(dir, path);
        f.getParentFile().mkdirs();
        Writer w = new FileWriter(f);
        try {
            w.write(contents);
        } finally {
            w.close();
        }
        return f;
    }

    void expect(boolean val, boolean expect) {
        if (val != expect)
            error("Unexpected value: " + val + "; expected: " + expect);
    }

    void error(String msg) {
        System.err.println(msg);
        errors++;
    }

    int errors = 0;
}