author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 42338 | a60f280f803c |
permissions | -rw-r--r-- |
36511 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, 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 |
import java.lang.module.Configuration; |
|
25 |
import java.lang.module.ResolvedModule; |
|
26 |
import java.io.IOException; |
|
27 |
import java.io.InputStream; |
|
28 |
import java.net.URL; |
|
29 |
import java.nio.file.Files; |
|
30 |
import java.nio.file.Path; |
|
31 |
import java.nio.file.Paths; |
|
32 |
||
33 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
34 |
* Basic test of Class getResource and getResourceAsStream to locate/read |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
35 |
* resources in named modules. |
36511 | 36 |
*/ |
37 |
||
38 |
public class Main { |
|
39 |
||
40 |
static final String NAME = "myresource"; |
|
41 |
||
42 |
public static void main(String[] args) throws IOException { |
|
43 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
44 |
// create resources in m1 |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
45 |
createResource("m1", Paths.get("."), "m1"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
46 |
createResource("m1", Paths.get("p1"), "m1/p1"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
47 |
createResource("m1", Paths.get("p1", "impl"), "m1/p1.impl"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
48 |
createResource("m1", Paths.get("p1", "resources"), "m1/p1.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
49 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
50 |
// create resources in m2 |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
51 |
createResource("m2", Paths.get("."), "m2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
52 |
createResource("m2", Paths.get("p2"), "m2/p2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
53 |
createResource("m2", Paths.get("p2", "impl"), "m2/p2.impl"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
54 |
createResource("m2", Paths.get("p2", "resources"), "m2/p2.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
55 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
56 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
57 |
// invoke Class getResource from an unnamed module. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
58 |
URL url = Main.class.getResource("/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
59 |
assertNotNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
60 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
61 |
url = Main.class.getResource("/p1/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
62 |
assertNull(url); |
36511 | 63 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
64 |
url = Main.class.getResource("/p1/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
65 |
assertNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
66 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
67 |
url = Main.class.getResource("/p1/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
68 |
assertEquals(readAllAsString(url), "m1/p1.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
69 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
70 |
url = Main.class.getResource("/p2/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
71 |
assertNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
72 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
73 |
url = Main.class.getResource("/p2/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
74 |
assertNull(url); |
36511 | 75 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
76 |
// m1: exports private p1.resources |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
77 |
url = Main.class.getResource("/p2/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
78 |
assertNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
79 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
80 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
81 |
// invoke Class getResource from module m1 |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
82 |
url = p1.Main.getResource("/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
83 |
assertEquals(readAllAsString(url), "m1"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
84 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
85 |
url = p1.Main.getResource("/p1/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
86 |
assertEquals(readAllAsString(url), "m1/p1"); |
36511 | 87 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
88 |
url = p1.Main.getResource("/p1/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
89 |
assertEquals(readAllAsString(url), "m1/p1.impl"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
90 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
91 |
url = p1.Main.getResource("/p1/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
92 |
assertEquals(readAllAsString(url), "m1/p1.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
93 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
94 |
// m2: not a named package |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
95 |
url = p1.Main.getResource(p2.Main.class, "/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
96 |
assertEquals(readAllAsString(url), "m2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
97 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
98 |
url = p1.Main.getResource(p2.Main.class, "/p2/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
99 |
assertNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
100 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
101 |
// m2: exports p2.impl to m1 |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
102 |
url = p1.Main.getResource(p2.Main.class, "/p2/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
103 |
assertNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
104 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
105 |
// m2: exports private p2.resources to m1; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
106 |
url = p1.Main.getResource(p2.Main.class, "/p2/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
107 |
assertEquals(readAllAsString(url), "m2/p2.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
108 |
|
36511 | 109 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
110 |
// invoke Class getResource from module m2 |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
111 |
url = p2.Main.getResource("/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
112 |
assertEquals(readAllAsString(url), "m2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
113 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
114 |
url = p2.Main.getResource("/p2/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
115 |
assertEquals(readAllAsString(url), "m2/p2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
116 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
117 |
url = p2.Main.getResource("/p2/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
118 |
assertEquals(readAllAsString(url), "m2/p2.impl"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
119 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
120 |
url = p2.Main.getResource("/p2/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
121 |
assertEquals(readAllAsString(url), "m2/p2.resources"); |
36511 | 122 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
123 |
// m1: not a named package |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
124 |
url = p2.Main.getResource(p1.Main.class, "/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
125 |
assertEquals(readAllAsString(url), "m1"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
126 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
127 |
url = p2.Main.getResource(p1.Main.class, "/p1/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
128 |
assertNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
129 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
130 |
url = p2.Main.getResource(p1.Main.class, "/p1/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
131 |
assertNull(url); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
132 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
133 |
// m1: exports private p1.resources; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
134 |
url = p2.Main.getResource(p1.Main.class, "/p1/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
135 |
assertEquals(readAllAsString(url), "m1/p1.resources"); |
36511 | 136 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
137 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
138 |
// invoke Class getResourceAsStream from an unnamed module. |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
139 |
InputStream in = Main.class.getResourceAsStream("/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
140 |
assertNotNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
141 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
142 |
in = Main.class.getResourceAsStream("/p1/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
143 |
assertNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
144 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
145 |
in = Main.class.getResourceAsStream("/p1/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
146 |
assertNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
147 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
148 |
in = Main.class.getResourceAsStream("/p1/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
149 |
assertEquals(readAllAsString(in), "m1/p1.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
150 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
151 |
in = Main.class.getResourceAsStream("/p2/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
152 |
assertNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
153 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
154 |
in = Main.class.getResourceAsStream("/p2/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
155 |
assertNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
156 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
157 |
in = Main.class.getResourceAsStream("/p2/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
158 |
assertNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
159 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
160 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
161 |
// invoke Class getResourceAsStream from module m1 |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
162 |
in = p1.Main.getResourceAsStream("/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
163 |
assertEquals(readAllAsString(in), "m1"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
164 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
165 |
in = p1.Main.getResourceAsStream("/p1/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
166 |
assertEquals(readAllAsString(in), "m1/p1"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
167 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
168 |
in = p1.Main.getResourceAsStream("/p1/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
169 |
assertEquals(readAllAsString(in), "m1/p1.impl"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
170 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
171 |
in = p1.Main.getResourceAsStream("/p1/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
172 |
assertEquals(readAllAsString(in), "m1/p1.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
173 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
174 |
in = p1.Main.getResourceAsStream(p2.Main.class, "/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
175 |
assertEquals(readAllAsString(in), "m2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
176 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
177 |
in = p1.Main.getResourceAsStream(p2.Main.class, "/p2/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
178 |
assertNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
179 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
180 |
in = p1.Main.getResourceAsStream(p2.Main.class, "/p2/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
181 |
assertNull(in); |
36511 | 182 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
183 |
in = p1.Main.getResourceAsStream(p2.Main.class, "/p2/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
184 |
assertEquals(readAllAsString(in), "m2/p2.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
185 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
186 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
187 |
// invoke Class getResourceAsStream from module m2 |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
188 |
in = p2.Main.getResourceAsStream("/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
189 |
assertEquals(readAllAsString(in), "m2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
190 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
191 |
in = p2.Main.getResourceAsStream("/p2/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
192 |
assertEquals(readAllAsString(in), "m2/p2"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
193 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
194 |
in = p2.Main.getResourceAsStream("/p2/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
195 |
assertEquals(readAllAsString(in), "m2/p2.impl"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
196 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
197 |
in = p2.Main.getResourceAsStream("/p2/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
198 |
assertEquals(readAllAsString(in), "m2/p2.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
199 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
200 |
in = p2.Main.getResourceAsStream(p1.Main.class, "/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
201 |
assertEquals(readAllAsString(in), "m1"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
202 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
203 |
in = p2.Main.getResourceAsStream(p1.Main.class, "/p1/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
204 |
assertNull(in); |
36511 | 205 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
206 |
in = p2.Main.getResourceAsStream(p1.Main.class, "/p1/impl/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
207 |
assertNull(in); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
208 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
209 |
in = p2.Main.getResourceAsStream(p1.Main.class, "/p1/resources/" + NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
210 |
assertEquals(readAllAsString(in), "m1/p1.resources"); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
211 |
|
36511 | 212 |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
213 |
// Nulls |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
214 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
215 |
Main.class.getResource(null); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
216 |
assertTrue(false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
217 |
} catch (NullPointerException expected) { } |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
218 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
219 |
Main.class.getResourceAsStream(null); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
220 |
assertTrue(false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
221 |
} catch (NullPointerException expected) { } |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
222 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
223 |
p1.Main.class.getResource(null); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
224 |
assertTrue(false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
225 |
} catch (NullPointerException expected) { } |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
226 |
try { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
227 |
p1.Main.class.getResourceAsStream(null); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
228 |
assertTrue(false); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
229 |
} catch (NullPointerException expected) { } |
36511 | 230 |
|
231 |
// SecurityManager case |
|
232 |
System.setSecurityManager(new SecurityManager()); |
|
233 |
||
234 |
assertNull(Main.class.getResource("/" + NAME)); |
|
235 |
assertNull(p1.Main.getResource("/" + NAME)); |
|
236 |
assertNull(p2.Main.getResource("/" + NAME)); |
|
237 |
||
238 |
assertNull(Main.class.getResourceAsStream("/" + NAME)); |
|
239 |
assertNull(p1.Main.getResourceAsStream("/" + NAME)); |
|
240 |
assertNull(p2.Main.getResourceAsStream("/" + NAME)); |
|
241 |
||
242 |
System.out.println("Success!"); |
|
243 |
} |
|
244 |
||
245 |
/** |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
246 |
* Create a resource in the sub-directory of the given exploded module |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
247 |
*/ |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
248 |
static void createResource(String mn, Path subdir, String msg) throws IOException { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
249 |
Path dir = directoryFor(mn).resolve(subdir); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
250 |
Path file = dir.resolve(NAME); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
251 |
Files.write(file, msg.getBytes("UTF-8")); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
252 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
253 |
|
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
254 |
/** |
36511 | 255 |
* Returns the directory for the given module (by name). |
256 |
*/ |
|
257 |
static Path directoryFor(String name) { |
|
44545
83b611b88ac8
8177530: Module system implementation refresh (4/2017)
alanb
parents:
42338
diff
changeset
|
258 |
Configuration cf = ModuleLayer.boot().configuration(); |
36511 | 259 |
ResolvedModule resolvedModule = cf.findModule(name).orElse(null); |
260 |
if (resolvedModule == null) |
|
261 |
throw new RuntimeException("not found: " + name); |
|
262 |
Path dir = Paths.get(resolvedModule.reference().location().get()); |
|
263 |
if (!Files.isDirectory(dir)) |
|
264 |
throw new RuntimeException("not a directory: " + dir); |
|
265 |
return dir; |
|
266 |
} |
|
267 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
268 |
static String readAllAsString(InputStream in) throws IOException { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
269 |
if (in == null) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
270 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
271 |
try (in) { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
272 |
return new String(in.readAllBytes(), "UTF-8"); |
36511 | 273 |
} |
274 |
} |
|
275 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
276 |
static String readAllAsString(URL url) throws IOException { |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
277 |
if (url == null) |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
278 |
return null; |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
279 |
InputStream in = url.openStream(); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
280 |
return readAllAsString(url.openStream()); |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
281 |
} |
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
36511
diff
changeset
|
282 |
|
36511 | 283 |
static void assertTrue(boolean condition) { |
284 |
if (!condition) throw new RuntimeException(); |
|
285 |
} |
|
286 |
||
287 |
static void assertNull(Object o) { |
|
288 |
assertTrue(o == null); |
|
289 |
} |
|
290 |
||
291 |
static void assertNotNull(Object o) { |
|
292 |
assertTrue(o != null); |
|
293 |
} |
|
294 |
||
295 |
static void assertEquals(Object actual, Object expected) { |
|
296 |
if (expected == null) { |
|
297 |
assertNull(actual); |
|
298 |
} else { |
|
299 |
assertTrue(expected.equals(actual)); |
|
300 |
} |
|
301 |
} |
|
302 |
||
303 |
static void assertNotEquals(Object actual, Object expected) { |
|
304 |
if (expected == null) { |
|
305 |
assertNotNull(actual); |
|
306 |
} else { |
|
307 |
assertTrue(!expected.equals(actual)); |
|
308 |
} |
|
309 |
} |
|
310 |
} |
|
311 |