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

Thu, 25 Aug 2011 17:18:25 -0700

author
schien
date
Thu, 25 Aug 2011 17:18:25 -0700
changeset 1067
f497fac86cf9
parent 802
0141f508b98d
child 1466
b52a38d4536c
permissions
-rw-r--r--

Added tag jdk8-b02 for changeset b3c059de2a61

     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  */
    24 /*
    25  * @test
    26  * @bug 6999891
    27  * @summary Test valid relative names for Filer.createResource and Filer.getResource
    28  * @library ../../lib
    29  * @build   JavacTestingAbstractProcessor
    30  * @compile TestValidRelativeNames.java
    31  * @compile/process -processor TestValidRelativeNames -Amode=create java.lang.Object
    32  * @compile/process -processor TestValidRelativeNames -Amode=get    java.lang.Object
    33  */
    35 import java.io.*;
    36 import java.util.*;
    37 import javax.annotation.processing.*;
    38 import javax.lang.model.*;
    39 import javax.lang.model.element.*;
    40 import javax.tools.Diagnostic;
    41 import javax.tools.StandardLocation;
    43 @SupportedOptions("mode")
    44 public class TestValidRelativeNames extends JavacTestingAbstractProcessor {
    45     enum Kind { READER_WRITER, INPUT_OUTPUT_STREAM };
    47     static final String[] validRelativeNames = {
    48             "foo", "foo.bar", ".foo", ".foo.bar", "foodir/bar", "foodir/.bar"
    49     };
    51     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    52         if (roundEnv.processingOver()) {
    53             String mode = options.get("mode");
    54             for (String relativeBase: validRelativeNames) {
    55                 for (Kind kind: Kind.values()) {
    56                     if (mode.equals("create"))
    57                         testCreate(relativeBase, kind);
    58                     else
    59                         testGet(relativeBase, kind);
    60                 }
    61             }
    62         }
    64         return true;
    65     }
    67     void testCreate(String relativeBase, Kind kind) {
    68         String relative = getRelative(relativeBase, kind);
    69         System.out.println("test create relative path: " + relative + ", kind: " + kind);
    70         try {
    71             switch (kind) {
    72                 case READER_WRITER:
    73                     try (Writer writer = filer.createResource(
    74                             StandardLocation.CLASS_OUTPUT, "", relative).openWriter()) {
    75                         writer.write(relative);
    76                     }
    77                     break;
    79                 case INPUT_OUTPUT_STREAM:
    80                     try (OutputStream out = filer.createResource(
    81                             StandardLocation.CLASS_OUTPUT, "", relative).openOutputStream()) {
    82                         out.write(relative.getBytes());
    83                     }
    84                     break;
    85             }
    86         } catch (Exception e) {
    87             messager.printMessage(Diagnostic.Kind.ERROR,
    88                     "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
    89         }
    90     }
    92     void testGet(String relativeBase, Kind kind) {
    93         String relative = getRelative(relativeBase, kind);
    94         System.out.println("test get relative path: " + relative + ", kind: " + kind);
    95         try {
    96             switch (kind) {
    97                 case READER_WRITER:
    98                     try (Reader reader = new BufferedReader(filer.getResource(
    99                             StandardLocation.CLASS_OUTPUT, "", relative).openReader(true))) {
   100                         StringBuilder sb = new StringBuilder();
   101                         char[] buf = new char[1024];
   102                         int n;
   103                         while ((n = reader.read(buf, 0, buf.length)) > 0)
   104                             sb.append(new String(buf, 0, n));
   105                         if (!sb.toString().equals(relative)) {
   106                             messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb);
   107                         }
   108                     }
   109                     break;
   111                 case INPUT_OUTPUT_STREAM:
   112                     try (InputStream in = new DataInputStream(filer.getResource(
   113                             StandardLocation.CLASS_OUTPUT, "", relative).openInputStream())) {
   114                         StringBuilder sb = new StringBuilder();
   115                         byte[] buf = new byte[1024];
   116                         int n;
   117                         while ((n = in.read(buf, 0, buf.length)) > 0)
   118                             sb.append(new String(buf, 0, n));
   119                         if (!sb.toString().equals(relative)) {
   120                             messager.printMessage(Diagnostic.Kind.ERROR, "unexpected content: " + sb);
   121                         }
   122                     }
   123                     break;
   124             }
   125         } catch (Exception e) {
   126             messager.printMessage(Diagnostic.Kind.ERROR,
   127                     "relative path: " + relative + ", kind: " + kind + ", unexpected exception: " + e);
   128         }
   129     }
   131     String getRelative(String relativeBase, Kind kind) {
   132         String suffix = (kind == Kind.READER_WRITER ? "RW" : "IOS");
   133         return relativeBase.replace("foo", "foo" + suffix);
   134     }
   135 }

mercurial