29 * @run build ImageFilePoolTest |
29 * @run build ImageFilePoolTest |
30 * @run main ImageFilePoolTest |
30 * @run main ImageFilePoolTest |
31 */ |
31 */ |
32 |
32 |
33 import java.io.ByteArrayInputStream; |
33 import java.io.ByteArrayInputStream; |
34 import jdk.tools.jlink.internal.PoolImpl; |
34 import java.util.Optional; |
35 import jdk.tools.jlink.plugin.Pool; |
35 import java.util.function.Function; |
36 import jdk.tools.jlink.plugin.Pool.ModuleData; |
36 import jdk.tools.jlink.internal.ModuleEntryImpl; |
37 import jdk.tools.jlink.plugin.Pool.ModuleDataType; |
37 import jdk.tools.jlink.internal.ModulePoolImpl; |
38 import jdk.tools.jlink.plugin.Pool.Visitor; |
38 import jdk.tools.jlink.plugin.ModuleEntry; |
|
39 import jdk.tools.jlink.plugin.ModulePool; |
39 |
40 |
40 public class ImageFilePoolTest { |
41 public class ImageFilePoolTest { |
41 public static void main(String[] args) throws Exception { |
42 public static void main(String[] args) throws Exception { |
42 new ImageFilePoolTest().test(); |
43 new ImageFilePoolTest().test(); |
43 } |
44 } |
48 } |
49 } |
49 |
50 |
50 private static final String SUFFIX = "END"; |
51 private static final String SUFFIX = "END"; |
51 |
52 |
52 private void checkVisitor() throws Exception { |
53 private void checkVisitor() throws Exception { |
53 Pool input = new PoolImpl(); |
54 ModulePool input = new ModulePoolImpl(); |
54 for (int i = 0; i < 1000; ++i) { |
55 for (int i = 0; i < 1000; ++i) { |
55 String module = "module" + (i / 100); |
56 String module = "module" + (i / 100); |
56 input.add(new InMemoryImageFile(module, "/" + module + "/java/class" + i, |
57 input.add(new InMemoryImageFile(module, "/" + module + "/java/class" + i, |
57 ModuleDataType.CONFIG, "class" + i)); |
58 ModuleEntry.Type.CONFIG, "class" + i)); |
58 } |
59 } |
59 if (input.getContent().size() != 1000) { |
60 if (input.getEntryCount() != 1000) { |
60 throw new AssertionError(); |
61 throw new AssertionError(); |
61 } |
62 } |
62 Pool output = new PoolImpl(); |
63 ModulePool output = new ModulePoolImpl(); |
63 ResourceVisitor visitor = new ResourceVisitor(); |
64 ResourceVisitor visitor = new ResourceVisitor(); |
64 input.visit(visitor, output); |
65 input.transformAndCopy(visitor, output); |
65 if (visitor.getAmountBefore() == 0) { |
66 if (visitor.getAmountBefore() == 0) { |
66 throw new AssertionError("Resources not found"); |
67 throw new AssertionError("Resources not found"); |
67 } |
68 } |
68 if (visitor.getAmountBefore() != input.getContent().size()) { |
69 if (visitor.getAmountBefore() != input.getEntryCount()) { |
69 throw new AssertionError("Number of visited resources. Expected: " + |
70 throw new AssertionError("Number of visited resources. Expected: " + |
70 visitor.getAmountBefore() + ", got: " + input.getContent().size()); |
71 visitor.getAmountBefore() + ", got: " + input.getEntryCount()); |
71 } |
72 } |
72 if (visitor.getAmountAfter() != output.getContent().size()) { |
73 if (visitor.getAmountAfter() != output.getEntryCount()) { |
73 throw new AssertionError("Number of added resources. Expected: " + |
74 throw new AssertionError("Number of added resources. Expected: " + |
74 visitor.getAmountAfter() + ", got: " + output.getContent().size()); |
75 visitor.getAmountAfter() + ", got: " + output.getEntryCount()); |
75 } |
76 } |
76 for (ModuleData outFile : output.getContent()) { |
77 output.entries().forEach(outFile -> { |
77 String path = outFile.getPath().replaceAll(SUFFIX + "$", ""); |
78 String path = outFile.getPath().replaceAll(SUFFIX + "$", ""); |
78 ModuleData inFile = input.get(path); |
79 Optional<ModuleEntry> inFile = input.findEntry(path); |
79 if (inFile == null) { |
80 if (!inFile.isPresent()) { |
80 throw new AssertionError("Unknown resource: " + path); |
81 throw new AssertionError("Unknown resource: " + path); |
81 } |
82 } |
82 } |
83 }); |
83 } |
84 } |
84 |
85 |
85 private static class ResourceVisitor implements Visitor { |
86 private static class ResourceVisitor implements Function<ModuleEntry, ModuleEntry> { |
86 |
87 |
87 private int amountBefore; |
88 private int amountBefore; |
88 private int amountAfter; |
89 private int amountAfter; |
89 |
90 |
90 @Override |
91 @Override |
91 public ModuleData visit(ModuleData file) { |
92 public ModuleEntry apply(ModuleEntry file) { |
92 int index = ++amountBefore % 3; |
93 int index = ++amountBefore % 3; |
93 switch (index) { |
94 switch (index) { |
94 case 0: |
95 case 0: |
95 ++amountAfter; |
96 ++amountAfter; |
96 return new InMemoryImageFile(file.getModule(), file.getPath() + SUFFIX, |
97 return new InMemoryImageFile(file.getModule(), file.getPath() + SUFFIX, |
124 input.contains(null); |
125 input.contains(null); |
125 throw new AssertionError("NullPointerException is not thrown"); |
126 throw new AssertionError("NullPointerException is not thrown"); |
126 } catch (NullPointerException e) { |
127 } catch (NullPointerException e) { |
127 // expected |
128 // expected |
128 } |
129 } |
129 if (input.get("unknown") != null) { |
130 if (input.findEntry("unknown").isPresent()) { |
130 throw new AssertionError("ImageFilePool does not return null for unknown file"); |
131 throw new AssertionError("ImageFileModulePool does not return null for unknown file"); |
131 } |
132 } |
132 if (input.contains(new InMemoryImageFile("", "unknown", ModuleDataType.CONFIG, "unknown"))) { |
133 if (input.contains(new InMemoryImageFile("", "unknown", ModuleEntry.Type.CONFIG, "unknown"))) { |
133 throw new AssertionError("'contain' returns true for unknown file"); |
134 throw new AssertionError("'contain' returns true for unknown file"); |
134 } |
135 } |
135 input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleDataType.CONFIG, "")); |
136 input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleEntry.Type.CONFIG, "")); |
136 try { |
137 try { |
137 input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleDataType.CONFIG, "")); |
138 input.add(new InMemoryImageFile("", "/aaa/bbb", ModuleEntry.Type.CONFIG, "")); |
138 throw new AssertionError("Exception expected"); |
139 throw new AssertionError("Exception expected"); |
139 } catch (Exception e) { |
140 } catch (Exception e) { |
140 // expected |
141 // expected |
141 } |
142 } |
142 input.setReadOnly(); |
143 input.setReadOnly(); |
143 try { |
144 try { |
144 input.add(new InMemoryImageFile("", "/aaa/ccc", ModuleDataType.CONFIG, "")); |
145 input.add(new InMemoryImageFile("", "/aaa/ccc", ModuleEntry.Type.CONFIG, "")); |
145 throw new AssertionError("Exception expected"); |
146 throw new AssertionError("Exception expected"); |
146 } catch (Exception e) { |
147 } catch (Exception e) { |
147 // expected |
148 // expected |
148 } |
149 } |
149 } |
150 } |
150 |
151 |
151 private static class InMemoryImageFile extends ModuleData { |
152 private static class InMemoryImageFile extends ModuleEntryImpl { |
152 public InMemoryImageFile(String module, String path, ModuleDataType type, String content) { |
153 public InMemoryImageFile(String module, String path, ModuleEntry.Type type, String content) { |
153 super(module, path, type, new ByteArrayInputStream(content.getBytes()), content.getBytes().length); |
154 super(module, path, type, new ByteArrayInputStream(content.getBytes()), content.getBytes().length); |
154 } |
155 } |
155 } |
156 } |
156 } |
157 } |