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

Wed, 29 Sep 2010 23:27:57 -0700

author
darcy
date
Wed, 29 Sep 2010 23:27:57 -0700
changeset 699
d2aaaec153e8
parent 655
f3323b1c65ee
child 1466
b52a38d4536c
permissions
-rw-r--r--

6983738: Use a JavacTestingAbstractProcessor
Reviewed-by: jjg

jjg@655 1 /*
darcy@699 2 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
jjg@655 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@655 4 *
jjg@655 5 * This code is free software; you can redistribute it and/or modify it
jjg@655 6 * under the terms of the GNU General Public License version 2 only, as
jjg@655 7 * published by the Free Software Foundation.
jjg@655 8 *
jjg@655 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@655 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@655 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@655 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@655 13 * accompanied this code).
jjg@655 14 *
jjg@655 15 * You should have received a copy of the GNU General Public License version
jjg@655 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@655 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@655 18 *
jjg@655 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@655 20 * or visit www.oracle.com if you need additional information or have any
jjg@655 21 * questions.
jjg@655 22 */
jjg@655 23
jjg@655 24 /* @test
jjg@655 25 * @bug 6929404
jjg@655 26 * @summary Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
darcy@699 27 * @library ../../lib
jjg@655 28 */
jjg@655 29
jjg@655 30 import java.io.*;
jjg@655 31 import java.security.*;
jjg@655 32 import java.util.*;
jjg@655 33 import javax.annotation.processing.*;
jjg@655 34 import javax.lang.model.*;
jjg@655 35 import javax.lang.model.element.*;
jjg@655 36 import javax.tools.*;
jjg@655 37 import javax.tools.Diagnostic.Kind;
jjg@655 38 import javax.tools.JavaCompiler.CompilationTask;
jjg@655 39
jjg@655 40 public class TestGetResource2 {
jjg@655 41
jjg@655 42 public static void main(String[] args) throws Exception {
jjg@655 43 new TestGetResource2().run();
jjg@655 44 }
jjg@655 45
jjg@655 46 void run() throws Exception {
jjg@655 47 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
jjg@655 48 CodeSource cs = javac.getClass().getProtectionDomain().getCodeSource();
jjg@655 49 if (cs == null) {
jjg@655 50 System.err.println("got compiler from " +
jjg@655 51 ClassLoader.getSystemResource(javac.getClass().getName().replace(".", "/")+".class"));
jjg@655 52 } else {
jjg@655 53 System.err.println("got compiler from " + cs.getLocation());
jjg@655 54 }
jjg@655 55
jjg@655 56 testSingleSourceDir(javac);
jjg@655 57 testCompositeSourcePath(javac);
jjg@655 58 testMissingResource(javac);
jjg@655 59 }
jjg@655 60
jjg@655 61 private void testSingleSourceDir(JavaCompiler javac) throws Exception {
jjg@655 62 System.err.println("testSingleSourceDir");
jjg@655 63 File tmpdir = new File("testSingleSourceDir");
jjg@655 64 File srcdir = new File(tmpdir, "src");
jjg@655 65 File destdir = new File(tmpdir, "dest");
jjg@655 66 write(srcdir, "pkg/X.java", "package pkg; class X {}");
jjg@655 67 write(srcdir, "resources/file.txt", "hello");
jjg@655 68 destdir.mkdirs();
jjg@655 69
jjg@655 70 CompilationTask task = javac.getTask(null, null, null,
jjg@655 71 Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
jjg@655 72 Collections.singleton("pkg.X"), null);
jjg@655 73 task.setProcessors(Collections.singleton(new AnnoProc()));
jjg@655 74 boolean result = task.call();
jjg@655 75 System.err.println("javac result with single source dir: " + result);
jjg@655 76 expect(result, true);
jjg@655 77 }
jjg@655 78
jjg@655 79 private void testCompositeSourcePath(JavaCompiler javac) throws Exception {
jjg@655 80 System.err.println("testCompositeSearchPath");
jjg@655 81 File tmpdir = new File("testCompositeSourcePath");
jjg@655 82 File srcdir = new File(tmpdir, "src");
jjg@655 83 File rsrcdir = new File(tmpdir, "rsrc");
jjg@655 84 File destdir = new File(tmpdir, "dest");
jjg@655 85 write(srcdir, "pkg/X.java", "package pkg; class X {}");
jjg@655 86 write(rsrcdir, "resources/file.txt", "hello");
jjg@655 87 destdir.mkdirs();
jjg@655 88
jjg@655 89 CompilationTask task = javac.getTask(null, null, null,
jjg@655 90 Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()),
jjg@655 91 Collections.singleton("pkg.X"), null);
jjg@655 92 task.setProcessors(Collections.singleton(new AnnoProc()));
jjg@655 93 boolean result = task.call();
jjg@655 94 System.err.println("javac result with composite source path: " + result);
jjg@655 95 expect(result, true);
jjg@655 96 }
jjg@655 97
jjg@655 98 private void testMissingResource(JavaCompiler javac) throws Exception {
jjg@655 99 System.err.println("testMissingResource");
jjg@655 100 File tmpdir = new File("testMissingResource");
jjg@655 101 File srcdir = new File(tmpdir, "src");
jjg@655 102 File destdir = new File(tmpdir, "dest");
jjg@655 103 write(srcdir, "pkg/X.java", "package pkg; class X {}");
jjg@655 104 destdir.mkdirs();
jjg@655 105
jjg@655 106 CompilationTask task = javac.getTask(null, null, null,
jjg@655 107 Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()),
jjg@655 108 Collections.singleton("pkg.X"), null);
jjg@655 109 task.setProcessors(Collections.singleton(new AnnoProc()));
jjg@655 110 boolean result = task.call();
jjg@655 111 System.err.println("javac result when missing resource: " + result);
jjg@655 112 expect(result, false);
jjg@655 113
jjg@655 114 if (errors > 0)
jjg@655 115 throw new Exception(errors + " errors occurred");
jjg@655 116 }
jjg@655 117
darcy@699 118 static class AnnoProc extends JavacTestingAbstractProcessor {
jjg@655 119
jjg@655 120 public @Override boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@655 121 if (roundEnv.processingOver()) {
jjg@655 122 return false;
jjg@655 123 }
jjg@655 124
jjg@655 125 try {
darcy@699 126 FileObject resource = filer.getResource(StandardLocation.SOURCE_PATH, "resources", "file.txt");
jjg@655 127 try {
jjg@655 128 resource.openInputStream().close();
darcy@699 129 messager.printMessage(Kind.NOTE, "found: " + resource.toUri());
jjg@655 130 return true;
jjg@655 131 } catch (IOException x) {
darcy@699 132 messager.printMessage(Kind.ERROR, "could not read: " + resource.toUri());
jjg@655 133 x.printStackTrace();
jjg@655 134 }
jjg@655 135 } catch (IOException x) {
darcy@699 136 messager.printMessage(Kind.ERROR, "did not find resource");
jjg@655 137 x.printStackTrace();
jjg@655 138 }
jjg@655 139
jjg@655 140 return false;
jjg@655 141 }
jjg@655 142
jjg@655 143 }
jjg@655 144
jjg@655 145 private File write(File dir, String path, String contents) throws IOException {
jjg@655 146 File f = new File(dir, path);
jjg@655 147 f.getParentFile().mkdirs();
jjg@655 148 Writer w = new FileWriter(f);
jjg@655 149 try {
jjg@655 150 w.write(contents);
jjg@655 151 } finally {
jjg@655 152 w.close();
jjg@655 153 }
jjg@655 154 return f;
jjg@655 155 }
jjg@655 156
jjg@655 157 void expect(boolean val, boolean expect) {
jjg@655 158 if (val != expect)
jjg@655 159 error("Unexpected value: " + val + "; expected: " + expect);
jjg@655 160 }
jjg@655 161
jjg@655 162 void error(String msg) {
jjg@655 163 System.err.println(msg);
jjg@655 164 errors++;
jjg@655 165 }
jjg@655 166
jjg@655 167 int errors = 0;
jjg@655 168 }

mercurial