1 /*
2  * Copyright (c) 2006, 2015, 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 /*
25  * @test
26  * @bug 6380018 6449798
27  * @summary Test Filer.getResource
28  * @author  Joseph D. Darcy
29  * @library /tools/javac/lib
30  * @modules java.compiler
31  *          jdk.compiler
32  * @build  JavacTestingAbstractProcessor TestGetResource
33  * @compile -processor TestGetResource -proc:only -Aphase=write TestGetResource.java
34  * @compile -processor TestGetResource -proc:only -Aphase=read  TestGetResource.java
35  */
36 
37 import java.util.Set;
38 import java.util.Map;
39 import javax.annotation.processing.*;
40 import javax.lang.model.SourceVersion;
41 import static javax.lang.model.SourceVersion.*;
42 import javax.lang.model.element.*;
43 import javax.lang.model.util.*;
44 import static javax.lang.model.util.ElementFilter.*;
45 import static javax.tools.Diagnostic.Kind.*;
46 import static javax.tools.StandardLocation.*;
47 import java.io.IOException;
48 import java.io.PrintWriter;
49 
50 /**
51  * Test basic functionality of the Filer.getResource method.  On the
52  * first run of the annotation processor, write out a resource file
53  * and on the second run read it in.
54  */
55 @SupportedOptions("phase")
56 public class TestGetResource extends JavacTestingAbstractProcessor {
57     private static String CONTENTS = "Hello World.";
58     private static String PKG = "";
59     private static String RESOURCE_NAME = "Resource1";
60 
process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)61     public boolean process(Set<? extends TypeElement> annotations,
62                            RoundEnvironment roundEnv) {
63         try {
64             if (!roundEnv.processingOver()) {
65                 String phase = options.get("phase");
66 
67                 if (phase.equals("write")) {
68                     PrintWriter pw =
69                         new PrintWriter(filer.createResource(CLASS_OUTPUT, PKG, RESOURCE_NAME).openWriter());
70                     pw.print(CONTENTS);
71                     pw.close();
72                 } else if (phase.equals("read")) {
73                     String contents = filer.getResource(CLASS_OUTPUT,
74                                                        PKG,
75                                                        RESOURCE_NAME).getCharContent(false).toString();
76                     if (!contents.equals(CONTENTS))
77                         throw new RuntimeException("Expected \n\t" + CONTENTS +
78                                                    "\nbut instead got \n\t" +
79                                                    contents);
80                     // Now try to open the file for writing
81                     filer.createResource(CLASS_OUTPUT,
82                                          PKG,
83                                          RESOURCE_NAME);
84                 } else {
85                     throw new RuntimeException("Unexpected phase: " + phase);
86                 }
87             }
88         } catch(IOException ioe) {
89             throw new RuntimeException(ioe);
90         }
91         return false;
92     }
93 }
94