author | alanb |
Fri, 07 Apr 2017 08:05:54 +0000 | |
changeset 44545 | 83b611b88ac8 |
parent 43712 | 5dfd0950317c |
child 43894 | c316ff1ea2c7 |
permissions | -rw-r--r-- |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
1 |
/* |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
4 |
* |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
8 |
* |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
14 |
* |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
18 |
* |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
21 |
* questions. |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
22 |
*/ |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
23 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
24 |
/** |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
25 |
* @test |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
26 |
* @library /lib/testlibrary |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
27 |
* @modules java.base/jdk.internal.module |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
28 |
* @build MultiReleaseJarTest JarUtils |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
29 |
* @run testng MultiReleaseJarTest |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
30 |
* @run testng/othervm -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarTest |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
31 |
* @summary Basic test of modular JARs as multi-release JARs |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
32 |
*/ |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
33 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
34 |
import java.io.File; |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
35 |
import java.io.InputStream; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
36 |
import java.io.OutputStream; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
37 |
import java.lang.module.ModuleDescriptor; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
38 |
import java.lang.module.ModuleFinder; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
39 |
import java.lang.module.ModuleReader; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
40 |
import java.lang.module.ModuleReference; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
41 |
import java.net.URI; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
42 |
import java.net.URLConnection; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
43 |
import java.nio.ByteBuffer; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
44 |
import java.nio.file.Files; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
45 |
import java.nio.file.Path; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
46 |
import java.nio.file.Paths; |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
47 |
import java.util.ArrayList; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
48 |
import java.util.HashMap; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
49 |
import java.util.HashSet; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
50 |
import java.util.List; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
51 |
import java.util.Map; |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
52 |
import java.util.Optional; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
53 |
import java.util.Set; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
54 |
import java.util.jar.Attributes; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
55 |
import java.util.jar.Manifest; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
56 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
57 |
import jdk.internal.module.ModuleInfoWriter; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
58 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
59 |
import org.testng.annotations.Test; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
60 |
import static org.testng.Assert.*; |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
61 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
62 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
63 |
@Test |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
64 |
public class MultiReleaseJarTest { |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
65 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
66 |
private static final String MODULE_INFO = "module-info.class"; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
67 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
68 |
private static final int VERSION = Runtime.version().major(); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
69 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
70 |
// are multi-release JARs enabled? |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
71 |
private static final boolean MULTI_RELEASE; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
72 |
static { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
73 |
String s = System.getProperty("jdk.util.jar.enableMultiRelease"); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
74 |
MULTI_RELEASE = (s == null || Boolean.parseBoolean(s)); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
75 |
} |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
76 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
77 |
/** |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
78 |
* Basic test of a multi-release JAR. |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
79 |
*/ |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
80 |
public void testBasic() throws Exception { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
81 |
String name = "m1"; |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
82 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
83 |
ModuleDescriptor descriptor = ModuleDescriptor.newModule(name) |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
84 |
.requires("java.base") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
85 |
.build(); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
86 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
87 |
Path jar = new JarBuilder(name) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
88 |
.moduleInfo("module-info.class", descriptor) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
89 |
.resource("p/Main.class") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
90 |
.resource("p/Helper.class") |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
91 |
.resource("META-INF/versions/" + VERSION + "/p/Helper.class") |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
92 |
.resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class") |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
93 |
.build(); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
94 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
95 |
// find the module |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
96 |
ModuleFinder finder = ModuleFinder.of(jar); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
97 |
Optional<ModuleReference> omref = finder.find(name); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
98 |
assertTrue((omref.isPresent())); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
99 |
ModuleReference mref = omref.get(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
100 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
101 |
// check module packages |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
102 |
descriptor = mref.descriptor(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
103 |
Set<String> packages = descriptor.packages(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
104 |
assertTrue(packages.contains("p")); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
105 |
if (MULTI_RELEASE) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
106 |
assertTrue(packages.size() == 2); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
107 |
assertTrue(packages.contains("p.internal")); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
108 |
} else { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
109 |
assertTrue(packages.size() == 1); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
110 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
111 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
112 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
113 |
/** |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
114 |
* Test a multi-release JAR with a module-info.class in the versioned |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
115 |
* section of the JAR. |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
116 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
117 |
public void testModuleInfoInVersionedSection() throws Exception { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
118 |
String name = "m1"; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
119 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
120 |
ModuleDescriptor descriptor1 = ModuleDescriptor.newModule(name) |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
121 |
.requires("java.base") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
122 |
.build(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
123 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
124 |
// module descriptor for versioned section |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
125 |
ModuleDescriptor descriptor2 = ModuleDescriptor.newModule(name) |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
126 |
.requires("java.base") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
127 |
.requires("jdk.unsupported") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
128 |
.build(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
129 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
130 |
Path jar = new JarBuilder(name) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
131 |
.moduleInfo(MODULE_INFO, descriptor1) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
132 |
.resource("p/Main.class") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
133 |
.resource("p/Helper.class") |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
134 |
.moduleInfo("META-INF/versions/" + VERSION + "/" + MODULE_INFO, descriptor2) |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
135 |
.resource("META-INF/versions/" + VERSION + "/p/Helper.class") |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
136 |
.resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class") |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
137 |
.build(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
138 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
139 |
// find the module |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
140 |
ModuleFinder finder = ModuleFinder.of(jar); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
141 |
Optional<ModuleReference> omref = finder.find(name); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
142 |
assertTrue((omref.isPresent())); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
143 |
ModuleReference mref = omref.get(); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
144 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
145 |
// ensure that the right module-info.class is loaded |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
146 |
ModuleDescriptor descriptor = mref.descriptor(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
147 |
assertEquals(descriptor.name(), name); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
148 |
if (MULTI_RELEASE) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
149 |
assertEquals(descriptor.requires(), descriptor2.requires()); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
150 |
} else { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
151 |
assertEquals(descriptor.requires(), descriptor1.requires()); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
152 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
153 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
154 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
155 |
/** |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
156 |
* Test multi-release JAR as an automatic module. |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
157 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
158 |
public void testAutomaticModule() throws Exception { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
159 |
String name = "m"; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
160 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
161 |
Path jar = new JarBuilder(name) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
162 |
.resource("p/Main.class") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
163 |
.resource("p/Helper.class") |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
164 |
.resource("META-INF/versions/" + VERSION + "/p/Helper.class") |
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
165 |
.resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class") |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
166 |
.build(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
167 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
168 |
// find the module |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
169 |
ModuleFinder finder = ModuleFinder.of(jar); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
170 |
Optional<ModuleReference> omref = finder.find(name); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
171 |
assertTrue((omref.isPresent())); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
172 |
ModuleReference mref = omref.get(); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
173 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
174 |
// check module packages |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
175 |
ModuleDescriptor descriptor = mref.descriptor(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
176 |
Set<String> packages = descriptor.packages(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
177 |
if (MULTI_RELEASE) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
178 |
assertTrue(packages.size() == 2); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
179 |
assertTrue(packages.contains("p.internal")); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
180 |
} else { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
181 |
assertTrue(packages.size() == 1); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
182 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
183 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
184 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
185 |
/** |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
186 |
* Exercise ModuleReader on a multi-release JAR |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
187 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
188 |
public void testModuleReader() throws Exception { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
189 |
String name = "m1"; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
190 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
191 |
ModuleDescriptor descriptor1 = ModuleDescriptor.newModule(name) |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
192 |
.requires("java.base") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
193 |
.build(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
194 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
195 |
// module descriptor for versioned section |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
196 |
ModuleDescriptor descriptor2 = ModuleDescriptor.newModule(name) |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
197 |
.requires("java.base") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
198 |
.requires("jdk.unsupported") |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
199 |
.build(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
200 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
201 |
Path jar = new JarBuilder(name) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
202 |
.moduleInfo(MODULE_INFO, descriptor1) |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
203 |
.moduleInfo("META-INF/versions/" + VERSION + "/" + MODULE_INFO, descriptor2) |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
204 |
.build(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
205 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
206 |
// find the module |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
207 |
ModuleFinder finder = ModuleFinder.of(jar); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
208 |
Optional<ModuleReference> omref = finder.find(name); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
209 |
assertTrue((omref.isPresent())); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
210 |
ModuleReference mref = omref.get(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
211 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
212 |
ModuleDescriptor expected; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
213 |
if (MULTI_RELEASE) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
214 |
expected = descriptor2; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
215 |
} else { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
216 |
expected = descriptor1; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
217 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
218 |
|
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
219 |
// test ModuleReader by reading module-info.class resource |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
220 |
try (ModuleReader reader = mref.open()) { |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
221 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
222 |
// open resource |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
223 |
Optional<InputStream> oin = reader.open(MODULE_INFO); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
224 |
assertTrue(oin.isPresent()); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
225 |
try (InputStream in = oin.get()) { |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
226 |
checkRequires(ModuleDescriptor.read(in), expected); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
227 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
228 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
229 |
// read resource |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
230 |
Optional<ByteBuffer> obb = reader.read(MODULE_INFO); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
231 |
assertTrue(obb.isPresent()); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
232 |
ByteBuffer bb = obb.get(); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
233 |
try { |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
234 |
checkRequires(ModuleDescriptor.read(bb), expected); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
235 |
} finally { |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
236 |
reader.release(bb); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
237 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
238 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
239 |
// find resource |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
240 |
Optional<URI> ouri = reader.find(MODULE_INFO); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
241 |
assertTrue(ouri.isPresent()); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
242 |
URI uri = ouri.get(); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
243 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
244 |
String expectedTail = "!/"; |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
245 |
if (MULTI_RELEASE) |
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
246 |
expectedTail += "META-INF/versions/" + VERSION + "/"; |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
247 |
expectedTail += MODULE_INFO; |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
248 |
assertTrue(uri.toString().endsWith(expectedTail)); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
249 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
250 |
URLConnection uc = uri.toURL().openConnection(); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
251 |
uc.setUseCaches(false); |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
252 |
try (InputStream in = uc.getInputStream()) { |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
253 |
checkRequires(ModuleDescriptor.read(in), expected); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
254 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
255 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
256 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
257 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
258 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
259 |
/** |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
260 |
* Check that two ModuleDescriptor have the same requires |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
261 |
*/ |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
262 |
static void checkRequires(ModuleDescriptor md1, ModuleDescriptor md2) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
263 |
assertEquals(md1.requires(), md2.requires()); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
264 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
265 |
|
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
266 |
/** |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
267 |
* A builder of multi-release JAR files. |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
268 |
*/ |
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
269 |
static class JarBuilder { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
270 |
private String name; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
271 |
private Set<String> resources = new HashSet<>(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
272 |
private Map<String, ModuleDescriptor> descriptors = new HashMap<>(); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
273 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
274 |
JarBuilder(String name) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
275 |
this.name = name; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
276 |
} |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
277 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
278 |
/** |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
279 |
* Adds a module-info.class to the JAR file. |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
280 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
281 |
JarBuilder moduleInfo(String name, ModuleDescriptor descriptor) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
282 |
descriptors.put(name, descriptor); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
283 |
return this; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
284 |
} |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
285 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
286 |
/** |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
287 |
* Adds a dummy resource to the JAR file. |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
288 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
289 |
JarBuilder resource(String name) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
290 |
resources.add(name); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
291 |
return this; |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
292 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
293 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
294 |
/** |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
295 |
* Create the multi-release JAR, returning its file path. |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
296 |
*/ |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
297 |
Path build() throws Exception { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
298 |
Path dir = Files.createTempDirectory(Paths.get(""), "jar"); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
299 |
List<Path> files = new ArrayList<>(); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
300 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
301 |
// write the module-info.class |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
302 |
for (Map.Entry<String, ModuleDescriptor> e : descriptors.entrySet()) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
303 |
String name = e.getKey(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
304 |
ModuleDescriptor descriptor = e.getValue(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
305 |
Path mi = Paths.get(name.replace('/', File.separatorChar)); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
306 |
Path parent = dir.resolve(mi).getParent(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
307 |
if (parent != null) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
308 |
Files.createDirectories(parent); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
309 |
try (OutputStream out = Files.newOutputStream(dir.resolve(mi))) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
310 |
ModuleInfoWriter.write(descriptor, out); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
311 |
} |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
312 |
files.add(mi); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
313 |
} |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
314 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
315 |
// write the dummy resources |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
316 |
for (String name : resources) { |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
317 |
Path file = Paths.get(name.replace('/', File.separatorChar)); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
318 |
// create dummy resource |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
319 |
Path parent = dir.resolve(file).getParent(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
320 |
if (parent != null) |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
321 |
Files.createDirectories(parent); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
322 |
Files.createFile(dir.resolve(file)); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
323 |
files.add(file); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
324 |
} |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
325 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
326 |
Manifest man = new Manifest(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
327 |
Attributes attrs = man.getMainAttributes(); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
328 |
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
329 |
attrs.put(Attributes.Name.MULTI_RELEASE, "true"); |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
330 |
|
41817
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
331 |
Path jarfile = Paths.get(name + ".jar"); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
332 |
JarUtils.createJarFile(jarfile, man, dir, files.toArray(new Path[0])); |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
333 |
return jarfile; |
b90ad1de93ea
8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents:
38476
diff
changeset
|
334 |
} |
38476
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
335 |
} |
c491c24d34a9
8157598: ModuleReader find returns incorrect URI when modular JAR is a multi-release JAR
alanb
parents:
diff
changeset
|
336 |
} |