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