jjg@698: /* jjg@698: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. jjg@698: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@698: * jjg@698: * This code is free software; you can redistribute it and/or modify it jjg@698: * under the terms of the GNU General Public License version 2 only, as jjg@698: * published by the Free Software Foundation. jjg@698: * jjg@698: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@698: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@698: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@698: * version 2 for more details (a copy is included in the LICENSE file that jjg@698: * accompanied this code). jjg@698: * jjg@698: * You should have received a copy of the GNU General Public License version jjg@698: * 2 along with this work; if not, write to the Free Software Foundation, jjg@698: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@698: * jjg@698: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@698: * or visit www.oracle.com if you need additional information or have any jjg@698: * questions. jjg@698: */ jjg@698: jjg@698: /* jjg@698: * @test jjg@698: * @bug 6502392 jjg@698: * @summary Invalid relative names for Filer.createResource and Filer.getResource darcy@1466: * @library /tools/javac/lib darcy@699: * @build JavacTestingAbstractProcessor jjg@698: * @compile TestInvalidRelativeNames.java jjg@698: * @compile/process -processor TestInvalidRelativeNames java.lang.Object jjg@698: */ jjg@698: jjg@698: import java.io.*; jjg@698: import java.util.*; jjg@698: import javax.annotation.processing.*; jjg@698: import javax.lang.model.*; jjg@698: import javax.lang.model.element.*; jjg@698: import javax.tools.Diagnostic; jjg@698: import javax.tools.StandardLocation; jjg@698: darcy@699: public class TestInvalidRelativeNames extends JavacTestingAbstractProcessor { jjg@698: enum Kind { CREATE_WRITER, GET_READER, CREATE_OUTPUT_STREAM, GET_INPUT_STREAM }; jjg@698: jjg@698: static final String[] invalidRelativeNames = { jjg@698: "/boo", "goo/../hoo", "./ioo", "" jjg@698: }; jjg@698: jjg@698: public boolean process(Set annotations, RoundEnvironment roundEnv) { jjg@698: if (roundEnv.processingOver()) { jjg@698: for (String relative: invalidRelativeNames) { jjg@698: for (Kind kind: Kind.values()) { jjg@698: test(relative, kind); jjg@698: } jjg@698: } jjg@698: } jjg@698: return true; jjg@698: } jjg@698: jjg@698: void test(String relative, Kind kind) { jjg@698: System.out.println("test relative path: " + relative + ", kind: " + kind); jjg@698: try { jjg@698: switch (kind) { jjg@698: case CREATE_WRITER: jjg@698: Writer writer = filer.createResource( jjg@698: StandardLocation.SOURCE_OUTPUT, "", relative).openWriter(); jjg@698: writer.close(); jjg@698: break; jjg@698: jjg@698: case GET_READER: jjg@698: Reader reader = filer.getResource( jjg@698: StandardLocation.SOURCE_OUTPUT, "", relative).openReader(true); jjg@698: reader.close(); jjg@698: break; jjg@698: jjg@698: case CREATE_OUTPUT_STREAM: jjg@698: OutputStream out = filer.createResource( jjg@698: StandardLocation.SOURCE_OUTPUT, "", relative).openOutputStream(); jjg@698: out.close(); jjg@698: break; jjg@698: jjg@698: case GET_INPUT_STREAM: jjg@698: InputStream in = filer.createResource( jjg@698: StandardLocation.SOURCE_OUTPUT, "", relative).openInputStream(); jjg@698: in.close(); jjg@698: break; jjg@698: } jjg@698: } catch (IllegalArgumentException expected) { jjg@698: System.out.println("expected exception thrown: " + expected); jjg@698: return; jjg@698: } catch (Exception e) { jjg@698: messager.printMessage(Diagnostic.Kind.ERROR, jjg@698: "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e); jjg@698: return; jjg@698: } jjg@698: messager.printMessage(Diagnostic.Kind.ERROR, jjg@698: "relative path: " + relative + ", kind: " + kind + ", no exception thrown"); jjg@698: } jjg@698: } jjg@698: