1 /* 2 * Copyright (c) 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 6502392 27 * @summary Invalid relative names for Filer.createResource and Filer.getResource 28 * @library ../../lib 29 * @build JavacTestingAbstractProcessor 30 * @compile TestInvalidRelativeNames.java 31 * @compile/process -processor TestInvalidRelativeNames java.lang.Object 32 */ 33 34 import java.io.*; 35 import java.util.*; 36 import javax.annotation.processing.*; 37 import javax.lang.model.*; 38 import javax.lang.model.element.*; 39 import javax.tools.Diagnostic; 40 import javax.tools.StandardLocation; 41 42 public class TestInvalidRelativeNames extends JavacTestingAbstractProcessor { 43 enum Kind { CREATE_WRITER, GET_READER, CREATE_OUTPUT_STREAM, GET_INPUT_STREAM }; 44 45 static final String[] invalidRelativeNames = { 46 "/boo", "goo/../hoo", "./ioo", "" 47 }; 48 49 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 50 if (roundEnv.processingOver()) { 51 for (String relative: invalidRelativeNames) { 52 for (Kind kind: Kind.values()) { 53 test(relative, kind); 54 } 55 } 56 } 57 return true; 58 } 59 60 void test(String relative, Kind kind) { 61 System.out.println("test relative path: " + relative + ", kind: " + kind); 62 try { 63 switch (kind) { 64 case CREATE_WRITER: 65 Writer writer = filer.createResource( 66 StandardLocation.SOURCE_OUTPUT, "", relative).openWriter(); 67 writer.close(); 68 break; 69 70 case GET_READER: 71 Reader reader = filer.getResource( 72 StandardLocation.SOURCE_OUTPUT, "", relative).openReader(true); 73 reader.close(); 74 break; 75 76 case CREATE_OUTPUT_STREAM: 77 OutputStream out = filer.createResource( 78 StandardLocation.SOURCE_OUTPUT, "", relative).openOutputStream(); 79 out.close(); 80 break; 81 82 case GET_INPUT_STREAM: 83 InputStream in = filer.createResource( 84 StandardLocation.SOURCE_OUTPUT, "", relative).openInputStream(); 85 in.close(); 86 break; 87 } 88 } catch (IllegalArgumentException expected) { 89 System.out.println("expected exception thrown: " + expected); 90 return; 91 } catch (Exception e) { 92 messager.printMessage(Diagnostic.Kind.ERROR, 93 "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e); 94 return; 95 } 96 messager.printMessage(Diagnostic.Kind.ERROR, 97 "relative path: " + relative + ", kind: " + kind + ", no exception thrown"); 98 } 99 } 100