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