jjg@655: /* jjg@655: * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. jjg@655: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@655: * jjg@655: * This code is free software; you can redistribute it and/or modify it jjg@655: * under the terms of the GNU General Public License version 2 only, as jjg@655: * published by the Free Software Foundation. jjg@655: * jjg@655: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@655: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@655: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@655: * version 2 for more details (a copy is included in the LICENSE file that jjg@655: * accompanied this code). jjg@655: * jjg@655: * You should have received a copy of the GNU General Public License version jjg@655: * 2 along with this work; if not, write to the Free Software Foundation, jjg@655: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@655: * jjg@655: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@655: * or visit www.oracle.com if you need additional information or have any jjg@655: * questions. jjg@655: */ jjg@655: jjg@655: /* @test jjg@655: * @bug 6929404 jjg@655: * @summary Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry jjg@655: */ jjg@655: jjg@655: import java.io.*; jjg@655: import java.security.*; jjg@655: import java.util.*; jjg@655: import javax.annotation.processing.*; jjg@655: import javax.lang.model.*; jjg@655: import javax.lang.model.element.*; jjg@655: import javax.tools.*; jjg@655: import javax.tools.Diagnostic.Kind; jjg@655: import javax.tools.JavaCompiler.CompilationTask; jjg@655: jjg@655: public class TestGetResource2 { jjg@655: jjg@655: public static void main(String[] args) throws Exception { jjg@655: new TestGetResource2().run(); jjg@655: } jjg@655: jjg@655: void run() throws Exception { jjg@655: JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); jjg@655: CodeSource cs = javac.getClass().getProtectionDomain().getCodeSource(); jjg@655: if (cs == null) { jjg@655: System.err.println("got compiler from " + jjg@655: ClassLoader.getSystemResource(javac.getClass().getName().replace(".", "/")+".class")); jjg@655: } else { jjg@655: System.err.println("got compiler from " + cs.getLocation()); jjg@655: } jjg@655: jjg@655: testSingleSourceDir(javac); jjg@655: testCompositeSourcePath(javac); jjg@655: testMissingResource(javac); jjg@655: } jjg@655: jjg@655: private void testSingleSourceDir(JavaCompiler javac) throws Exception { jjg@655: System.err.println("testSingleSourceDir"); jjg@655: File tmpdir = new File("testSingleSourceDir"); jjg@655: File srcdir = new File(tmpdir, "src"); jjg@655: File destdir = new File(tmpdir, "dest"); jjg@655: write(srcdir, "pkg/X.java", "package pkg; class X {}"); jjg@655: write(srcdir, "resources/file.txt", "hello"); jjg@655: destdir.mkdirs(); jjg@655: jjg@655: CompilationTask task = javac.getTask(null, null, null, jjg@655: Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()), jjg@655: Collections.singleton("pkg.X"), null); jjg@655: task.setProcessors(Collections.singleton(new AnnoProc())); jjg@655: boolean result = task.call(); jjg@655: System.err.println("javac result with single source dir: " + result); jjg@655: expect(result, true); jjg@655: } jjg@655: jjg@655: private void testCompositeSourcePath(JavaCompiler javac) throws Exception { jjg@655: System.err.println("testCompositeSearchPath"); jjg@655: File tmpdir = new File("testCompositeSourcePath"); jjg@655: File srcdir = new File(tmpdir, "src"); jjg@655: File rsrcdir = new File(tmpdir, "rsrc"); jjg@655: File destdir = new File(tmpdir, "dest"); jjg@655: write(srcdir, "pkg/X.java", "package pkg; class X {}"); jjg@655: write(rsrcdir, "resources/file.txt", "hello"); jjg@655: destdir.mkdirs(); jjg@655: jjg@655: CompilationTask task = javac.getTask(null, null, null, jjg@655: Arrays.asList("-sourcepath", srcdir + File.pathSeparator + rsrcdir, "-d", destdir.toString()), jjg@655: Collections.singleton("pkg.X"), null); jjg@655: task.setProcessors(Collections.singleton(new AnnoProc())); jjg@655: boolean result = task.call(); jjg@655: System.err.println("javac result with composite source path: " + result); jjg@655: expect(result, true); jjg@655: } jjg@655: jjg@655: private void testMissingResource(JavaCompiler javac) throws Exception { jjg@655: System.err.println("testMissingResource"); jjg@655: File tmpdir = new File("testMissingResource"); jjg@655: File srcdir = new File(tmpdir, "src"); jjg@655: File destdir = new File(tmpdir, "dest"); jjg@655: write(srcdir, "pkg/X.java", "package pkg; class X {}"); jjg@655: destdir.mkdirs(); jjg@655: jjg@655: CompilationTask task = javac.getTask(null, null, null, jjg@655: Arrays.asList("-sourcepath", srcdir.toString(), "-d", destdir.toString()), jjg@655: Collections.singleton("pkg.X"), null); jjg@655: task.setProcessors(Collections.singleton(new AnnoProc())); jjg@655: boolean result = task.call(); jjg@655: System.err.println("javac result when missing resource: " + result); jjg@655: expect(result, false); jjg@655: jjg@655: if (errors > 0) jjg@655: throw new Exception(errors + " errors occurred"); jjg@655: } jjg@655: jjg@655: @SupportedAnnotationTypes("*") jjg@655: static class AnnoProc extends AbstractProcessor { jjg@655: jjg@655: public @Override boolean process(Set annotations, RoundEnvironment roundEnv) { jjg@655: if (roundEnv.processingOver()) { jjg@655: return false; jjg@655: } jjg@655: jjg@655: try { jjg@655: FileObject resource = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "resources", "file.txt"); jjg@655: try { jjg@655: resource.openInputStream().close(); jjg@655: processingEnv.getMessager().printMessage(Kind.NOTE, "found: " + resource.toUri()); jjg@655: return true; jjg@655: } catch (IOException x) { jjg@655: processingEnv.getMessager().printMessage(Kind.ERROR, "could not read: " + resource.toUri()); jjg@655: x.printStackTrace(); jjg@655: } jjg@655: } catch (IOException x) { jjg@655: processingEnv.getMessager().printMessage(Kind.ERROR, "did not find resource"); jjg@655: x.printStackTrace(); jjg@655: } jjg@655: jjg@655: return false; jjg@655: } jjg@655: jjg@655: @Override jjg@655: public SourceVersion getSupportedSourceVersion() { jjg@655: return SourceVersion.latest(); jjg@655: } jjg@655: } jjg@655: jjg@655: private File write(File dir, String path, String contents) throws IOException { jjg@655: File f = new File(dir, path); jjg@655: f.getParentFile().mkdirs(); jjg@655: Writer w = new FileWriter(f); jjg@655: try { jjg@655: w.write(contents); jjg@655: } finally { jjg@655: w.close(); jjg@655: } jjg@655: return f; jjg@655: } jjg@655: jjg@655: void expect(boolean val, boolean expect) { jjg@655: if (val != expect) jjg@655: error("Unexpected value: " + val + "; expected: " + expect); jjg@655: } jjg@655: jjg@655: void error(String msg) { jjg@655: System.err.println(msg); jjg@655: errors++; jjg@655: } jjg@655: jjg@655: int errors = 0; jjg@655: }