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