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.lang.reflect.Layer;
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.nio.file.Files;
|
|
29 |
import java.nio.file.Path;
|
|
30 |
import java.nio.file.Paths;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Basic test of ClassLoader getResource and getResourceAsStream when
|
|
34 |
* invoked from code in named modules.
|
|
35 |
*/
|
|
36 |
|
|
37 |
public class Main {
|
|
38 |
|
|
39 |
static final String NAME = "myresource";
|
|
40 |
|
|
41 |
public static void main(String[] args) throws IOException {
|
|
42 |
|
|
43 |
// create m1/myresource containing "m1"
|
|
44 |
Path file = directoryFor("m1").resolve(NAME);
|
|
45 |
Files.write(file, "m1".getBytes("UTF-8"));
|
|
46 |
|
|
47 |
// create m2/myresource containing "m2"
|
|
48 |
file = directoryFor("m2").resolve(NAME);
|
|
49 |
Files.write(file, "m2".getBytes("UTF-8"));
|
|
50 |
|
|
51 |
// check that m3/myresource does not exist
|
|
52 |
assertTrue(Files.notExists(directoryFor("m3").resolve(NAME)));
|
|
53 |
|
|
54 |
// invoke ClassLoader getResource from the unnamed module
|
|
55 |
assertNull(Main.class.getClassLoader().getResource("/" + NAME));
|
|
56 |
|
|
57 |
// invoke ClassLoader getResource from modules m1-m3
|
|
58 |
// Resources in a named module are private to that module.
|
|
59 |
// ClassLoader.getResource should not find resource in named modules.
|
|
60 |
assertNull(p1.Main.getResourceInClassLoader("/" + NAME));
|
|
61 |
assertNull(p2.Main.getResourceInClassLoader("/" + NAME));
|
|
62 |
assertNull(p3.Main.getResourceInClassLoader("/" + NAME));
|
|
63 |
|
|
64 |
// invoke ClassLoader getResourceAsStream from the unnamed module
|
|
65 |
assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));
|
|
66 |
|
|
67 |
// invoke ClassLoader getResourceAsStream from modules m1-m3
|
|
68 |
// Resources in a named module are private to that module.
|
|
69 |
// ClassLoader.getResourceAsStream should not find resource in named modules.
|
|
70 |
assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));
|
|
71 |
assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));
|
|
72 |
assertNull(p3.Main.getResourceAsStreamInClassLoader("/" + NAME));
|
|
73 |
|
|
74 |
// SecurityManager case
|
|
75 |
System.setSecurityManager(new SecurityManager());
|
|
76 |
|
|
77 |
assertNull(Main.class.getClassLoader().getResource("/" + NAME));
|
|
78 |
assertNull(p1.Main.getResourceInClassLoader("/" + NAME));
|
|
79 |
assertNull(p2.Main.getResourceInClassLoader("/" + NAME));
|
|
80 |
assertNull(p3.Main.getResourceInClassLoader("/" + NAME));
|
|
81 |
|
|
82 |
assertNull(Main.class.getClassLoader().getResourceAsStream("/" + NAME));
|
|
83 |
assertNull(p1.Main.getResourceAsStreamInClassLoader("/" + NAME));
|
|
84 |
assertNull(p2.Main.getResourceAsStreamInClassLoader("/" + NAME));
|
|
85 |
assertNull(p3.Main.getResourceAsStreamInClassLoader("/" + NAME));
|
|
86 |
|
|
87 |
System.out.println("Success!");
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Returns the directory for the given module (by name).
|
|
92 |
*/
|
|
93 |
static Path directoryFor(String mn) {
|
|
94 |
Configuration cf = Layer.boot().configuration();
|
|
95 |
ResolvedModule resolvedModule = cf.findModule(mn).orElse(null);
|
|
96 |
if (resolvedModule == null)
|
|
97 |
throw new RuntimeException("not found: " + mn);
|
|
98 |
Path dir = Paths.get(resolvedModule.reference().location().get());
|
|
99 |
if (!Files.isDirectory(dir))
|
|
100 |
throw new RuntimeException("not a directory: " + dir);
|
|
101 |
return dir;
|
|
102 |
}
|
|
103 |
|
|
104 |
static void assertTrue(boolean condition) {
|
|
105 |
if (!condition) throw new RuntimeException();
|
|
106 |
}
|
|
107 |
|
|
108 |
static void assertNull(Object o) {
|
|
109 |
assertTrue(o == null);
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|