test/tools/javac/processing/filer/TestValidRelativeNames.java

Fri, 21 Dec 2012 08:45:43 -0800

author
darcy
date
Fri, 21 Dec 2012 08:45:43 -0800
changeset 1466
b52a38d4536c
parent 802
0141f508b98d
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8005282: Use @library tag with non-relative path for javac tests
Reviewed-by: jjg

jjg@802 1 /*
jjg@802 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
jjg@802 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@802 4 *
jjg@802 5 * This code is free software; you can redistribute it and/or modify it
jjg@802 6 * under the terms of the GNU General Public License version 2 only, as
jjg@802 7 * published by the Free Software Foundation.
jjg@802 8 *
jjg@802 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@802 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@802 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@802 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@802 13 * accompanied this code).
jjg@802 14 *
jjg@802 15 * You should have received a copy of the GNU General Public License version
jjg@802 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@802 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@802 18 *
jjg@802 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@802 20 * or visit www.oracle.com if you need additional information or have any
jjg@802 21 * questions.
jjg@802 22 */
jjg@802 23
jjg@802 24 /*
jjg@802 25 * @test
jjg@802 26 * @bug 6999891
jjg@802 27 * @summary Test valid relative names for Filer.createResource and Filer.getResource
darcy@1466 28 * @library /tools/javac/lib
jjg@802 29 * @build JavacTestingAbstractProcessor
jjg@802 30 * @compile TestValidRelativeNames.java
jjg@802 31 * @compile/process -processor TestValidRelativeNames -Amode=create java.lang.Object
jjg@802 32 * @compile/process -processor TestValidRelativeNames -Amode=get java.lang.Object
jjg@802 33 */
jjg@802 34
jjg@802 35 import java.io.*;
jjg@802 36 import java.util.*;
jjg@802 37 import javax.annotation.processing.*;
jjg@802 38 import javax.lang.model.*;
jjg@802 39 import javax.lang.model.element.*;
jjg@802 40 import javax.tools.Diagnostic;
jjg@802 41 import javax.tools.StandardLocation;
jjg@802 42
jjg@802 43 @SupportedOptions("mode")
jjg@802 44 public class TestValidRelativeNames extends JavacTestingAbstractProcessor {
jjg@802 45 enum Kind { READER_WRITER, INPUT_OUTPUT_STREAM };
jjg@802 46
jjg@802 47 static final String[] validRelativeNames = {
jjg@802 48 "foo", "foo.bar", ".foo", ".foo.bar", "foodir/bar", "foodir/.bar"
jjg@802 49 };
jjg@802 50
jjg@802 51 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
jjg@802 52 if (roundEnv.processingOver()) {
jjg@802 53 String mode = options.get("mode");
jjg@802 54 for (String relativeBase: validRelativeNames) {
jjg@802 55 for (Kind kind: Kind.values()) {
jjg@802 56 if (mode.equals("create"))
jjg@802 57 testCreate(relativeBase, kind);
jjg@802 58 else
jjg@802 59 testGet(relativeBase, kind);
jjg@802 60 }
jjg@802 61 }
jjg@802 62 }
jjg@802 63
jjg@802 64 return true;
jjg@802 65 }
jjg@802 66
jjg@802 67 void testCreate(String relativeBase, Kind kind) {
jjg@802 68 String relative = getRelative(relativeBase, kind);
jjg@802 69 System.out.println("test create relative path: " + relative + ", kind: " + kind);
jjg@802 70 try {
jjg@802 71 switch (kind) {
jjg@802 72 case READER_WRITER:
jjg@802 73 try (Writer writer = filer.createResource(
jjg@802 74 StandardLocation.CLASS_OUTPUT, "", relative).openWriter()) {
jjg@802 75 writer.write(relative);
jjg@802 76 }
jjg@802 77 break;
jjg@802 78
jjg@802 79 case INPUT_OUTPUT_STREAM:
jjg@802 80 try (OutputStream out = filer.createResource(
jjg@802 81 StandardLocation.CLASS_OUTPUT, "", relative).openOutputStream()) {
jjg@802 82 out.write(relative.getBytes());
jjg@802 83 }
jjg@802 84 break;
jjg@802 85 }
jjg@802 86 } catch (Exception e) {
jjg@802 87 messager.printMessage(Diagnostic.Kind.ERROR,
jjg@802 88 "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
jjg@802 89 }
jjg@802 90 }
jjg@802 91
jjg@802 92 void testGet(String relativeBase, Kind kind) {
jjg@802 93 String relative = getRelative(relativeBase, kind);
jjg@802 94 System.out.println("test get relative path: " + relative + ", kind: " + kind);
jjg@802 95 try {
jjg@802 96 switch (kind) {
jjg@802 97 case READER_WRITER:
jjg@802 98 try (Reader reader = new BufferedReader(filer.getResource(
jjg@802 99 StandardLocation.CLASS_OUTPUT, "", relative).openReader(true))) {
jjg@802 100 StringBuilder sb = new StringBuilder();
jjg@802 101 char[] buf = new char[1024];
jjg@802 102 int n;
jjg@802 103 while ((n = reader.read(buf, 0, buf.length)) > 0)
jjg@802 104 sb.append(new String(buf, 0, n));
jjg@802 105 if (!sb.toString().equals(relative)) {
jjg@802 106 messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb);
jjg@802 107 }
jjg@802 108 }
jjg@802 109 break;
jjg@802 110
jjg@802 111 case INPUT_OUTPUT_STREAM:
jjg@802 112 try (InputStream in = new DataInputStream(filer.getResource(
jjg@802 113 StandardLocation.CLASS_OUTPUT, "", relative).openInputStream())) {
jjg@802 114 StringBuilder sb = new StringBuilder();
jjg@802 115 byte[] buf = new byte[1024];
jjg@802 116 int n;
jjg@802 117 while ((n = in.read(buf, 0, buf.length)) > 0)
jjg@802 118 sb.append(new String(buf, 0, n));
jjg@802 119 if (!sb.toString().equals(relative)) {
jjg@802 120 messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb);
jjg@802 121 }
jjg@802 122 }
jjg@802 123 break;
jjg@802 124 }
jjg@802 125 } catch (Exception e) {
jjg@802 126 messager.printMessage(Diagnostic.Kind.ERROR,
jjg@802 127 "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
jjg@802 128 }
jjg@802 129 }
jjg@802 130
jjg@802 131 String getRelative(String relativeBase, Kind kind) {
jjg@802 132 String suffix = (kind == Kind.READER_WRITER ? "RW" : "IOS");
jjg@802 133 return relativeBase.replace("foo", "foo" + suffix);
jjg@802 134 }
jjg@802 135 }
jjg@802 136

mercurial