36511
|
1 |
/*
|
|
2 |
* Copyright (c) 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 |
* @summary Test a pool containing external files.
|
|
27 |
* @author Andrei Eremeev
|
|
28 |
* @modules jdk.jlink/jdk.tools.jlink.internal
|
|
29 |
* @run build ImageFilePoolTest
|
|
30 |
* @run main ImageFilePoolTest
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.ByteArrayInputStream;
|
38320
|
34 |
import java.util.Optional;
|
|
35 |
import java.util.function.Function;
|
|
36 |
import jdk.tools.jlink.internal.ModuleEntryImpl;
|
|
37 |
import jdk.tools.jlink.internal.ModulePoolImpl;
|
|
38 |
import jdk.tools.jlink.plugin.ModuleEntry;
|
|
39 |
import jdk.tools.jlink.plugin.ModulePool;
|
36511
|
40 |
|
|
41 |
public class ImageFilePoolTest {
|
|
42 |
public static void main(String[] args) throws Exception {
|
|
43 |
new ImageFilePoolTest().test();
|
|
44 |
}
|
|
45 |
|
|
46 |
public void test() throws Exception {
|
|
47 |
checkNegative();
|
|
48 |
checkVisitor();
|
|
49 |
}
|
|
50 |
|
|
51 |
private static final String SUFFIX = "END";
|
|
52 |
|
|
53 |
private void checkVisitor() throws Exception {
|
38320
|
54 |
ModulePool input = new ModulePoolImpl();
|
36511
|
55 |
for (int i = 0; i < 1000; ++i) {
|
|
56 |
String module = "module" + (i / 100);
|
|
57 |
input.add(new InMemoryImageFile(module, "/" + module + "/java/class" + i,
|
38320
|
58 |
ModuleEntry.Type.CONFIG, "class" + i));
|
36511
|
59 |
}
|
38320
|
60 |
if (input.getEntryCount() != 1000) {
|
36511
|
61 |
throw new AssertionError();
|
|
62 |
}
|
38320
|
63 |
ModulePool output = new ModulePoolImpl();
|
36511
|
64 |
ResourceVisitor visitor = new ResourceVisitor();
|
38320
|
65 |
input.transformAndCopy(visitor, output);
|
36511
|
66 |
if (visitor.getAmountBefore() == 0) {
|
|
67 |
throw new AssertionError("Resources not found");
|
|
68 |
}
|
38320
|
69 |
if (visitor.getAmountBefore() != input.getEntryCount()) {
|
36511
|
70 |
throw new AssertionError("Number of visited resources. Expected: " +
|
38320
|
71 |
visitor.getAmountBefore() + ", got: " + input.getEntryCount());
|
36511
|
72 |
}
|
38320
|
73 |
if (visitor.getAmountAfter() != output.getEntryCount()) {
|
36511
|
74 |
throw new AssertionError("Number of added resources. Expected: " +
|
38320
|
75 |
visitor.getAmountAfter() + ", got: " + output.getEntryCount());
|
36511
|
76 |
}
|
38320
|
77 |
output.entries().forEach(outFile -> {
|
36511
|
78 |
String path = outFile.getPath().replaceAll(SUFFIX + "$", "");
|
38320
|
79 |
Optional<ModuleEntry> inFile = input.findEntry(path);
|
|
80 |
if (!inFile.isPresent()) {
|
36511
|
81 |
throw new AssertionError("Unknown resource: " + path);
|
|
82 |
}
|
38320
|
83 |
});
|
36511
|
84 |
}
|
|
85 |
|
38320
|
86 |
private static class ResourceVisitor implements Function<ModuleEntry, ModuleEntry> {
|
36511
|
87 |
|
|
88 |
private int amountBefore;
|
|
89 |
private int amountAfter;
|
|
90 |
|
|
91 |
@Override
|
38320
|
92 |
public ModuleEntry apply(ModuleEntry file) {
|
36511
|
93 |
int index = ++amountBefore % 3;
|
|
94 |
switch (index) {
|
|
95 |
case 0:
|
|
96 |
++amountAfter;
|
|
97 |
return new InMemoryImageFile(file.getModule(), file.getPath() + SUFFIX,
|
|
98 |
file.getType(), file.getPath());
|
|
99 |
case 1:
|
|
100 |
++amountAfter;
|
|
101 |
return new InMemoryImageFile(file.getModule(), file.getPath(),
|
|
102 |
file.getType(), file.getPath());
|
|
103 |
}
|
|
104 |
return null;
|
|
105 |
}
|
|
106 |
|
|
107 |
public int getAmountAfter() {
|
|
108 |
return amountAfter;
|
|
109 |
}
|
|
110 |
|
|
111 |
public int getAmountBefore() {
|
|
112 |
return amountBefore;
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
private void checkNegative() throws Exception {
|
38320
|
117 |
ModulePoolImpl input = new ModulePoolImpl();
|
36511
|
118 |
try {
|
|
119 |
input.add(null);
|
|
120 |
throw new AssertionError("NullPointerException is not thrown");
|
|
121 |
} catch (NullPointerException e) {
|
|
122 |
// expected
|
|
123 |
}
|
|
124 |
try {
|
|
125 |
input.contains(null);
|
|
126 |
throw new AssertionError("NullPointerException is not thrown");
|
|
127 |
} catch (NullPointerException e) {
|
|
128 |
// expected
|
|
129 |
}
|
38320
|
130 |
if (input.findEntry("unknown").isPresent()) {
|
|
131 |
throw new AssertionError("ImageFileModulePool does not return null for unknown file");
|
36511
|
132 |
}
|
38320
|
133 |
if (input.contains(new InMemoryImageFile("", "unknown", ModuleEntry.Type.CONFIG, "unknown"))) {
|
36511
|
134 |
throw new AssertionError("'contain' returns true for unknown file");
|
|
135 |
}
|
38320
|
136 |
input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleEntry.Type.CONFIG, ""));
|
36511
|
137 |
try {
|
38320
|
138 |
input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleEntry.Type.CONFIG, ""));
|
36511
|
139 |
throw new AssertionError("Exception expected");
|
|
140 |
} catch (Exception e) {
|
|
141 |
// expected
|
|
142 |
}
|
|
143 |
input.setReadOnly();
|
|
144 |
try {
|
38320
|
145 |
input.add(new InMemoryImageFile("", "/aaa/ccc", ModuleEntry.Type.CONFIG, ""));
|
36511
|
146 |
throw new AssertionError("Exception expected");
|
|
147 |
} catch (Exception e) {
|
|
148 |
// expected
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
38320
|
152 |
private static class InMemoryImageFile extends ModuleEntryImpl {
|
|
153 |
public InMemoryImageFile(String module, String path, ModuleEntry.Type type, String content) {
|
36511
|
154 |
super(module, path, type, new ByteArrayInputStream(content.getBytes()), content.getBytes().length);
|
|
155 |
}
|
|
156 |
}
|
|
157 |
}
|