jjg@802: /* jjg@802: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. jjg@802: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@802: * jjg@802: * This code is free software; you can redistribute it and/or modify it jjg@802: * under the terms of the GNU General Public License version 2 only, as jjg@802: * published by the Free Software Foundation. jjg@802: * jjg@802: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@802: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@802: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@802: * version 2 for more details (a copy is included in the LICENSE file that jjg@802: * accompanied this code). jjg@802: * jjg@802: * You should have received a copy of the GNU General Public License version jjg@802: * 2 along with this work; if not, write to the Free Software Foundation, jjg@802: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@802: * jjg@802: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@802: * or visit www.oracle.com if you need additional information or have any jjg@802: * questions. jjg@802: */ jjg@802: jjg@802: /* jjg@802: * @test jjg@802: * @bug 6999891 jjg@802: * @summary Test valid relative names for Filer.createResource and Filer.getResource darcy@1466: * @library /tools/javac/lib jjg@802: * @build JavacTestingAbstractProcessor jjg@802: * @compile TestValidRelativeNames.java jjg@802: * @compile/process -processor TestValidRelativeNames -Amode=create java.lang.Object jjg@802: * @compile/process -processor TestValidRelativeNames -Amode=get java.lang.Object jjg@802: */ jjg@802: jjg@802: import java.io.*; jjg@802: import java.util.*; jjg@802: import javax.annotation.processing.*; jjg@802: import javax.lang.model.*; jjg@802: import javax.lang.model.element.*; jjg@802: import javax.tools.Diagnostic; jjg@802: import javax.tools.StandardLocation; jjg@802: jjg@802: @SupportedOptions("mode") jjg@802: public class TestValidRelativeNames extends JavacTestingAbstractProcessor { jjg@802: enum Kind { READER_WRITER, INPUT_OUTPUT_STREAM }; jjg@802: jjg@802: static final String[] validRelativeNames = { jjg@802: "foo", "foo.bar", ".foo", ".foo.bar", "foodir/bar", "foodir/.bar" jjg@802: }; jjg@802: jjg@802: public boolean process(Set annotations, RoundEnvironment roundEnv) { jjg@802: if (roundEnv.processingOver()) { jjg@802: String mode = options.get("mode"); jjg@802: for (String relativeBase: validRelativeNames) { jjg@802: for (Kind kind: Kind.values()) { jjg@802: if (mode.equals("create")) jjg@802: testCreate(relativeBase, kind); jjg@802: else jjg@802: testGet(relativeBase, kind); jjg@802: } jjg@802: } jjg@802: } jjg@802: jjg@802: return true; jjg@802: } jjg@802: jjg@802: void testCreate(String relativeBase, Kind kind) { jjg@802: String relative = getRelative(relativeBase, kind); jjg@802: System.out.println("test create relative path: " + relative + ", kind: " + kind); jjg@802: try { jjg@802: switch (kind) { jjg@802: case READER_WRITER: jjg@802: try (Writer writer = filer.createResource( jjg@802: StandardLocation.CLASS_OUTPUT, "", relative).openWriter()) { jjg@802: writer.write(relative); jjg@802: } jjg@802: break; jjg@802: jjg@802: case INPUT_OUTPUT_STREAM: jjg@802: try (OutputStream out = filer.createResource( jjg@802: StandardLocation.CLASS_OUTPUT, "", relative).openOutputStream()) { jjg@802: out.write(relative.getBytes()); jjg@802: } jjg@802: break; jjg@802: } jjg@802: } catch (Exception e) { jjg@802: messager.printMessage(Diagnostic.Kind.ERROR, jjg@802: "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e); jjg@802: } jjg@802: } jjg@802: jjg@802: void testGet(String relativeBase, Kind kind) { jjg@802: String relative = getRelative(relativeBase, kind); jjg@802: System.out.println("test get relative path: " + relative + ", kind: " + kind); jjg@802: try { jjg@802: switch (kind) { jjg@802: case READER_WRITER: jjg@802: try (Reader reader = new BufferedReader(filer.getResource( jjg@802: StandardLocation.CLASS_OUTPUT, "", relative).openReader(true))) { jjg@802: StringBuilder sb = new StringBuilder(); jjg@802: char[] buf = new char[1024]; jjg@802: int n; jjg@802: while ((n = reader.read(buf, 0, buf.length)) > 0) jjg@802: sb.append(new String(buf, 0, n)); jjg@802: if (!sb.toString().equals(relative)) { jjg@802: messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb); jjg@802: } jjg@802: } jjg@802: break; jjg@802: jjg@802: case INPUT_OUTPUT_STREAM: jjg@802: try (InputStream in = new DataInputStream(filer.getResource( jjg@802: StandardLocation.CLASS_OUTPUT, "", relative).openInputStream())) { jjg@802: StringBuilder sb = new StringBuilder(); jjg@802: byte[] buf = new byte[1024]; jjg@802: int n; jjg@802: while ((n = in.read(buf, 0, buf.length)) > 0) jjg@802: sb.append(new String(buf, 0, n)); jjg@802: if (!sb.toString().equals(relative)) { jjg@802: messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb); jjg@802: } jjg@802: } jjg@802: break; jjg@802: } jjg@802: } catch (Exception e) { jjg@802: messager.printMessage(Diagnostic.Kind.ERROR, jjg@802: "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e); jjg@802: } jjg@802: } jjg@802: jjg@802: String getRelative(String relativeBase, Kind kind) { jjg@802: String suffix = (kind == Kind.READER_WRITER ? "RW" : "IOS"); jjg@802: return relativeBase.replace("foo", "foo" + suffix); jjg@802: } jjg@802: } jjg@802: