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