-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jpms] enable analysis of embedded jars in executable
Signed-off-by: Raymond Augé <raymond.auge@liferay.com>
- Loading branch information
Showing
7 changed files
with
320 additions
and
78 deletions.
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
biz.aQute.bndlib.tests/test/test/exporter/ExecutableExporterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package test.exporter; | ||
|
||
import static aQute.bnd.exporter.executable.ExecutableJarExporter.EXECUTABLE_JAR; | ||
import static aQute.bnd.osgi.Constants.JPMS_MODULE_INFO; | ||
import static aQute.bnd.osgi.Constants.MAIN_CLASS; | ||
import static aQute.bnd.osgi.Constants.MODULE_INFO_CLASS; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.File; | ||
import java.nio.ByteBuffer; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.jar.Attributes; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import aQute.bnd.build.Run; | ||
import aQute.bnd.classfile.ClassFile; | ||
import aQute.bnd.classfile.ModuleAttribute; | ||
import aQute.bnd.classfile.ModuleMainClassAttribute; | ||
import aQute.bnd.classfile.ModulePackagesAttribute; | ||
import aQute.bnd.osgi.Constants; | ||
import aQute.bnd.osgi.Jar; | ||
import aQute.bnd.osgi.JarResource; | ||
import aQute.bnd.osgi.Resource; | ||
import aQute.lib.io.ByteBufferDataInput; | ||
import aQute.lib.io.IO; | ||
|
||
public class ExecutableExporterTest { | ||
|
||
@Test | ||
public void testExecutableExportWithJPMS() throws Exception { | ||
File f = IO.getFile("testresources/export-executable/executable-1.bndrun"); | ||
assertThat(f).isFile(); | ||
|
||
try (Run run = Run.createRun(null, f)) { | ||
assertThat(run).isNotNull(); | ||
|
||
run.setProperty(JPMS_MODULE_INFO, "foo;version=3.9.8"); | ||
|
||
Map.Entry<String, Resource> export = run.export(EXECUTABLE_JAR, null); | ||
|
||
assertThat(export).isNotNull(); | ||
|
||
try (JarResource jarResource = (JarResource) export.getValue()) { | ||
Jar jar = jarResource.getJar(); | ||
|
||
Map<String, Resource> resources = jar.getResources(); | ||
|
||
assertThat(resources).containsKeys("jar/org.apache.felix.scr-2.1.12.jar", | ||
"jar/org.eclipse.osgi-3.17.100.jar", "jar/slf4j-api-1.7.25.jar", "jar/slf4j-simple-1.7.25.jar", | ||
"module-info.class"); | ||
|
||
Attributes mainAttributes = jar.getManifest() | ||
.getMainAttributes(); | ||
|
||
assertThat(mainAttributes.getValue(MAIN_CLASS)) | ||
.isEqualTo("aQute.launcher.pre.EmbeddedLauncher"); | ||
|
||
Resource moduleInfoResource = jar.getResource(MODULE_INFO_CLASS); | ||
|
||
assertThat(moduleInfoResource).isNotNull(); | ||
ClassFile module_info; | ||
ByteBuffer bb = moduleInfoResource.buffer(); | ||
if (bb != null) { | ||
module_info = ClassFile.parseClassFile(ByteBufferDataInput.wrap(bb)); | ||
} else { | ||
try (DataInputStream din = new DataInputStream(moduleInfoResource.openInputStream())) { | ||
module_info = ClassFile.parseClassFile(din); | ||
} | ||
} | ||
|
||
ModuleAttribute moduleAttribute = Arrays.stream(module_info.attributes) | ||
.filter(ModuleAttribute.class::isInstance) | ||
.map(ModuleAttribute.class::cast) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
assertThat(moduleAttribute.module_name).isEqualTo("foo"); | ||
assertThat(moduleAttribute.module_version).isEqualTo("3.9.8"); | ||
assertThat(moduleAttribute.module_flags).isEqualTo(32); | ||
|
||
assertThat( // | ||
Arrays.stream(moduleAttribute.requires) | ||
.map(e -> e.requires) | ||
.collect(toList()) // | ||
).containsExactly( // | ||
"java.base", "java.instrument", "java.logging", "java.management", "java.xml", "jdk.unsupported"); | ||
|
||
ModulePackagesAttribute modulePackagesAttribute = Arrays.stream(module_info.attributes) | ||
.filter(ModulePackagesAttribute.class::isInstance) | ||
.map(ModulePackagesAttribute.class::cast) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
assertThat(modulePackagesAttribute.packages).containsExactly("aQute/launcher/agent", | ||
"aQute/launcher/pre", "jar"); | ||
|
||
ModuleMainClassAttribute moduleMainClassAttribute = Arrays.stream(module_info.attributes) | ||
.filter(ModuleMainClassAttribute.class::isInstance) | ||
.map(ModuleMainClassAttribute.class::cast) | ||
.findFirst() | ||
.orElse(null); | ||
|
||
assertThat(moduleMainClassAttribute.main_class).isEqualTo("aQute/launcher/pre/EmbeddedLauncher"); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testExecutableExport() throws Exception { | ||
File f = IO.getFile("testresources/export-executable/executable-1.bndrun"); | ||
assertThat(f).isFile(); | ||
|
||
try (Run run = Run.createRun(null, f)) { | ||
assertThat(run).isNotNull(); | ||
|
||
Map.Entry<String, Resource> export = run.export(EXECUTABLE_JAR, null); | ||
|
||
assertThat(export).isNotNull(); | ||
|
||
try (JarResource jarResource = (JarResource) export.getValue()) { | ||
Jar jar = jarResource.getJar(); | ||
|
||
assertThat(jar.getResources()).containsKeys("jar/org.apache.felix.scr-2.1.12.jar", | ||
"jar/org.eclipse.osgi-3.17.100.jar", "jar/slf4j-api-1.7.25.jar", "jar/slf4j-simple-1.7.25.jar") | ||
.doesNotContainKeys("module-info.class"); | ||
|
||
Attributes mainAttributes = jar.getManifest() | ||
.getMainAttributes(); | ||
|
||
assertThat(mainAttributes.getValue(Constants.MAIN_CLASS)) | ||
.isEqualTo("aQute.launcher.pre.EmbeddedLauncher"); | ||
|
||
assertThat(jar.getModuleName()).isNull(); | ||
assertThat(jar.getModuleVersion()).isNull(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
biz.aQute.bndlib.tests/testresources/export-executable/central.mvn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
org.apache.felix:org.apache.felix.scr:2.1.12 | ||
|
||
org.eclipse.platform:org.eclipse.osgi:3.17.100 | ||
|
||
org.slf4j:slf4j-api:1.7.25 | ||
org.slf4j:slf4j-simple:1.7.25 |
22 changes: 22 additions & 0 deletions
22
biz.aQute.bndlib.tests/testresources/export-executable/executable-1.bndrun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-standalone: true | ||
|
||
mavencentral: https://repo.maven.apache.org/maven2 | ||
|
||
-plugin.maven.central:\ | ||
aQute.bnd.repository.maven.provider.MavenBndRepository;\ | ||
name="Maven Central";\ | ||
releaseUrl="${mavencentral}";\ | ||
index="${.}/central.mvn";\ | ||
readOnly=true | ||
|
||
-runfw: org.eclipse.osgi | ||
-runee: JavaSE-1.8 | ||
|
||
-resolve.effective: resolve, active | ||
|
||
-runpath: \ | ||
slf4j.api,\ | ||
slf4j.simple | ||
|
||
-runbundles: \ | ||
org.apache.felix.scr;version=latest |
Oops, something went wrong.