test/tools/javac/processing/filer/TestGetResource2.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/filer/TestGetResource2.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,168 @@
     1.4 +/*
     1.5 + * Copyright (c) 2006, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/* @test
    1.28 + * @bug 6929404
    1.29 + * @summary Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
    1.30 + * @library /tools/javac/lib
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.security.*;
    1.35 +import java.util.*;
    1.36 +import javax.annotation.processing.*;
    1.37 +import javax.lang.model.*;
    1.38 +import javax.lang.model.element.*;
    1.39 +import javax.tools.*;
    1.40 +import javax.tools.Diagnostic.Kind;
    1.41 +import javax.tools.JavaCompiler.CompilationTask;
    1.42 +
    1.43 +public class TestGetResource2 {
    1.44 +
    1.45 +    public static void main(String[] args) throws Exception {
    1.46 +        new TestGetResource2().run();
    1.47 +    }
    1.48 +
    1.49 +    void run() throws Exception {
    1.50 +        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
    1.51 +        CodeSource cs = javac.getClass().getProtectionDomain().getCodeSource();
    1.52 +        if (cs == null) {
    1.53 +            System.err.println("got compiler from " +
    1.54 +                ClassLoader.getSystemResource(javac.getClass().getName().replace(".", "/")+".class"));
    1.55 +        } else {
    1.56 +            System.err.println("got compiler from " + cs.getLocation());
    1.57 +        }
    1.58 +
    1.59 +        testSingleSourceDir(javac);
    1.60 +        testCompositeSourcePath(javac);
    1.61 +        testMissingResource(javac);
    1.62 +    }
    1.63 +
    1.64 +    private void testSingleSourceDir(JavaCompiler javac) throws Exception {
    1.65 +        System.err.println("testSingleSourceDir");
    1.66 +        File tmpdir = new File("testSingleSourceDir");
    1.67 +        File srcdir = new File(tmpdir, "src");
    1.68 +        File destdir = new File(tmpdir, "dest");
    1.69 +        write(srcdir, "pkg/X.java", "package pkg; class X {}");
    1.70 +        write(srcdir, "resources/file.txt", "hello");
    1.71 +        destdir.mkdirs();
    1.72 +
    1.73 +        CompilationTask task = javac.getTask(null, null, null,
    1.74 +                Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
    1.75 +                Collections.singleton("pkg.X"), null);
    1.76 +        task.setProcessors(Collections.singleton(new AnnoProc()));
    1.77 +        boolean result = task.call();
    1.78 +        System.err.println("javac result with single source dir: " + result);
    1.79 +        expect(result, true);
    1.80 +    }
    1.81 +
    1.82 +    private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
    1.83 +        System.err.println("testCompositeSearchPath");
    1.84 +        File tmpdir = new File("testCompositeSourcePath");
    1.85 +        File srcdir = new File(tmpdir, "src");
    1.86 +        File rsrcdir = new File(tmpdir, "rsrc");
    1.87 +        File destdir = new File(tmpdir, "dest");
    1.88 +        write(srcdir, "pkg/X.java", "package pkg; class X {}");
    1.89 +        write(rsrcdir, "resources/file.txt", "hello");
    1.90 +        destdir.mkdirs();
    1.91 +
    1.92 +        CompilationTask task = javac.getTask(null, null, null,
    1.93 +                Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
    1.94 +                Collections.singleton("pkg.X"), null);
    1.95 +        task.setProcessors(Collections.singleton(new AnnoProc()));
    1.96 +        boolean result = task.call();
    1.97 +        System.err.println("javac result with composite source path: " + result);
    1.98 +        expect(result, true);
    1.99 +    }
   1.100 +
   1.101 +    private void testMissingResource(JavaCompiler javac) throws Exception {
   1.102 +        System.err.println("testMissingResource");
   1.103 +        File tmpdir = new File("testMissingResource");
   1.104 +        File srcdir = new File(tmpdir, "src");
   1.105 +        File destdir = new File(tmpdir, "dest");
   1.106 +        write(srcdir, "pkg/X.java", "package pkg; class X {}");
   1.107 +        destdir.mkdirs();
   1.108 +
   1.109 +        CompilationTask task = javac.getTask(null, null, null,
   1.110 +                Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
   1.111 +                Collections.singleton("pkg.X"), null);
   1.112 +        task.setProcessors(Collections.singleton(new AnnoProc()));
   1.113 +        boolean result = task.call();
   1.114 +        System.err.println("javac result when missing resource: " + result);
   1.115 +        expect(result, false);
   1.116 +
   1.117 +        if (errors > 0)
   1.118 +            throw new Exception(errors + " errors occurred");
   1.119 +    }
   1.120 +
   1.121 +    static class AnnoProc extends JavacTestingAbstractProcessor {
   1.122 +
   1.123 +        public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   1.124 +            if (roundEnv.processingOver()) {
   1.125 +                return false;
   1.126 +            }
   1.127 +
   1.128 +            try {
   1.129 +                FileObject resource = filer.getResource(StandardLocation.SOURCE_PATH, "resources", "file.txt");
   1.130 +                try {
   1.131 +                    resource.openInputStream().close();
   1.132 +                    messager.printMessage(Kind.NOTE, "found: " + resource.toUri());
   1.133 +                    return true;
   1.134 +                } catch (IOException x) {
   1.135 +                    messager.printMessage(Kind.ERROR, "could not read: " + resource.toUri());
   1.136 +                    x.printStackTrace();
   1.137 +                }
   1.138 +            } catch (IOException x) {
   1.139 +                messager.printMessage(Kind.ERROR, "did not find resource");
   1.140 +                x.printStackTrace();
   1.141 +            }
   1.142 +
   1.143 +            return false;
   1.144 +        }
   1.145 +
   1.146 +    }
   1.147 +
   1.148 +    private File write(File dir, String path, String contents) throws IOException {
   1.149 +        File f = new File(dir, path);
   1.150 +        f.getParentFile().mkdirs();
   1.151 +        Writer w = new FileWriter(f);
   1.152 +        try {
   1.153 +            w.write(contents);
   1.154 +        } finally {
   1.155 +            w.close();
   1.156 +        }
   1.157 +        return f;
   1.158 +    }
   1.159 +
   1.160 +    void expect(boolean val, boolean expect) {
   1.161 +        if (val != expect)
   1.162 +            error("Unexpected value: " + val + "; expected: " + expect);
   1.163 +    }
   1.164 +
   1.165 +    void error(String msg) {
   1.166 +        System.err.println(msg);
   1.167 +        errors++;
   1.168 +    }
   1.169 +
   1.170 +    int errors = 0;
   1.171 +}

mercurial