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