1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.InputStream;
import java.lang.module.Configuration;
import java.lang.module.ResolvedModule;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* Basic test of ClassLoader getResource and getResourceAsStream when
* invoked from code in named modules.
*/
public class Main {
static final String NAME = "myresource";
public static void main(String[] args) throws IOException {
// create resources in m1
createResource("m1", Paths.get("."), "m1");
createResource("m1", Paths.get("p1"), "m1/p1");
createResource("m1", Paths.get("p1", "impl"), "m1/p1.impl");
createResource("m1", Paths.get("p1", "resources"), "m1/p1.resources");
// create resources in m2
createResource("m2", Paths.get("."), "m2");
createResource("m2", Paths.get("p2"), "m2/p2");
createResource("m2", Paths.get("p2", "impl"), "m2/p2.impl");
createResource("m2", Paths.get("p2", "resources"), "m2/p2.resources");
// invoke ClassLoader getResource from the unnamed module
ClassLoader thisLoader = Main.class.getClassLoader();
URL url = thisLoader.getResource(NAME);
assertNotNull(url);
url = thisLoader.getResource("p1/" + NAME);
assertNull(url);
url = thisLoader.getResource("p1/impl/" + NAME);
assertNull(url);
url = thisLoader.getResource("p1/resources/" + NAME);
assertEquals(readAllAsString(url), "m1/p1.resources");
url = thisLoader.getResource("p2/" + NAME);
assertNull(url);
url = thisLoader.getResource("p2/impl/" + NAME);
assertNull(url);
url = thisLoader.getResource("p2/resources/" + NAME);
assertNull(url);
// invoke ClassLoader getResource from module m1
url = p1.Main.getResourceInClassLoader(NAME);
assertNotNull(url);
url = p1.Main.getResourceInClassLoader("p1/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p1/impl/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(url), "m1/p1.resources");
url = p1.Main.getResourceInClassLoader("p2/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p2/impl/" + NAME);
assertNull(url);
url = p1.Main.getResourceInClassLoader("p2/resources/" + NAME);
assertNull(url);
// invoke ClassLoader getResource from module m2
url = p2.Main.getResourceInClassLoader(NAME);
assertNotNull(url);
url = p2.Main.getResourceInClassLoader("p1/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p1/impl/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(url), "m1/p1.resources");
url = p2.Main.getResourceInClassLoader("p2/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p2/impl/" + NAME);
assertNull(url);
url = p2.Main.getResourceInClassLoader("p2/resources/" + NAME);
assertNull(url);
// invoke ClassLoader getResources from the unnamed module
Enumeration<URL> urls = thisLoader.getResources(NAME);
List<String> resources = readAllAsStrings(urls);
assertTrue(resources.size() == 2);
assertTrue(resources.contains("m1"));
assertTrue(resources.contains("m2"));
urls = thisLoader.getResources("p1/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p1/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p1/resources/" + NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 1);
assertTrue(resources.contains("m1/p1.resources"));
urls = thisLoader.getResources("p2/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p2/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = thisLoader.getResources("p2/resources/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
// invoke ClassLoader getResources from m1
urls = p1.Main.getResourcesInClassLoader(NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 2);
assertTrue(resources.contains("m1"));
assertTrue(resources.contains("m2"));
urls = p1.Main.getResourcesInClassLoader("p1/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p1/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p1/resources/" + NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 1);
assertTrue(resources.contains("m1/p1.resources"));
urls = p1.Main.getResourcesInClassLoader("p2/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p2/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p1.Main.getResourcesInClassLoader("p2/resources/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
// invoke ClassLoader getResources from m2
urls = p2.Main.getResourcesInClassLoader(NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 2);
assertTrue(resources.contains("m1"));
assertTrue(resources.contains("m2"));
urls = p2.Main.getResourcesInClassLoader("p1/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p1/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p1/resources/" + NAME);
resources = readAllAsStrings(urls);
assertTrue(resources.size() == 1);
assertTrue(resources.contains("m1/p1.resources"));
urls = p2.Main.getResourcesInClassLoader("p2/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p2/impl/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
urls = p2.Main.getResourcesInClassLoader("p2/resources/" + NAME);
assertTrue(readAllAsStrings(urls).isEmpty());
// invoke ClassLoader getResourceAsStream from the unnamed module
InputStream in = thisLoader.getResourceAsStream(NAME);
assertNotNull(in);
in = thisLoader.getResourceAsStream("p1/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p1/impl/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p1/resources/" + NAME);
assertEquals(readAllAsString(in), "m1/p1.resources");
in = thisLoader.getResourceAsStream("p2/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p2/impl/" + NAME);
assertNull(in);
in = thisLoader.getResourceAsStream("p2/resources/" + NAME);
assertNull(in);
// invoke ClassLoader getResource from modules m1
in = p1.Main.getResourceAsStreamInClassLoader(NAME);
assertNotNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p1/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p1/impl/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(in), "m1/p1.resources");
in = p1.Main.getResourceAsStreamInClassLoader("p2/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p2/impl/" + NAME);
assertNull(in);
in = p1.Main.getResourceAsStreamInClassLoader("p2/resources/" + NAME);
assertNull(in);
// invoke ClassLoader getResource from modules m2
in = p2.Main.getResourceAsStreamInClassLoader(NAME);
assertNotNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p1/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p1/impl/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p1/resources/" + NAME);
assertEquals(readAllAsString(in), "m1/p1.resources");
in = p2.Main.getResourceAsStreamInClassLoader("p2/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p2/impl/" + NAME);
assertNull(in);
in = p2.Main.getResourceAsStreamInClassLoader("p2/resources/" + NAME);
assertNull(in);
// SecurityManager case
System.setSecurityManager(new SecurityManager());
assertNull(Main.class.getClassLoader().getResource("/" + NAME));
assertNull(p1.Main.getResourceInClassLoader("/" + NAME));
assertNull(p2.Main.getResourceInClassLoader("/" + NAME));
assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));
assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));
assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));
System.out.println("Success!");
}
/**
* Create a resource in the sub-directory of the given exploded module
*/
static void createResource(String mn, Path subdir, String msg) throws IOException {
Path dir = directoryFor(mn).resolve(subdir);
Path file = dir.resolve(NAME);
Files.write(file, msg.getBytes("UTF-8"));
}
/**
* Returns the directory for the given module (by name).
*/
static Path directoryFor(String mn) {
Configuration cf = ModuleLayer.boot().configuration();
ResolvedModule resolvedModule = cf.findModule(mn).orElse(null);
if (resolvedModule == null)
throw new RuntimeException("not found: " + mn);
Path dir = Paths.get(resolvedModule.reference().location().get());
if (!Files.isDirectory(dir))
throw new RuntimeException("not a directory: " + dir);
return dir;
}
static String readAllAsString(InputStream in) throws IOException {
if (in == null)
return null;
try (in) {
return new String(in.readAllBytes(), "UTF-8");
}
}
static String readAllAsString(URL url) throws IOException {
if (url == null)
return null;
InputStream in = url.openStream();
return readAllAsString(url.openStream());
}
static List<String> readAllAsStrings(Enumeration<URL> urls) throws IOException {
List<String> result = new ArrayList<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
result.add(readAllAsString(url));
}
return result;
}
static void assertTrue(boolean condition) {
if (!condition) throw new RuntimeException();
}
static void assertFalse(boolean condition) {
if (condition) throw new RuntimeException();
}
static void assertNull(Object o) {
assertTrue(o == null);
}
static void assertNotNull(Object o) {
assertTrue(o != null);
}
static void assertEquals(Object actual, Object expected) {
if (expected == null) {
assertNull(actual);
} else {
assertTrue(expected.equals(actual));
}
}
static void assertNotEquals(Object actual, Object expected) {
if (expected == null) {
assertNotNull(actual);
} else {
assertTrue(!expected.equals(actual));
}
}
}
|