Skip to content

Commit

Permalink
[jpms] enable analysis of embedded jars in executable
Browse files Browse the repository at this point in the history
Signed-off-by: Raymond Augé <raymond.auge@liferay.com>
  • Loading branch information
rotty3000 committed May 2, 2022
1 parent 733bc78 commit dd2f0c5
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 78 deletions.
143 changes: 143 additions & 0 deletions biz.aQute.bndlib.tests/test/test/exporter/ExecutableExporterTest.java
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();
}
}
}

}
34 changes: 17 additions & 17 deletions biz.aQute.bndlib.tests/test/test/jpms/JPMSModuleInfoPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void moduleWithOptionsIgnore() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/j"));
Expand Down Expand Up @@ -204,7 +204,7 @@ public void moduleWithOptionsStatic() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/j"));
Expand Down Expand Up @@ -265,7 +265,7 @@ public void moduleWithOptionsMapModuleNameStatic() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/j"));
Expand Down Expand Up @@ -326,7 +326,7 @@ public void moduleWithOptionsMapModuleName() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/j"));
Expand Down Expand Up @@ -387,7 +387,7 @@ public void moduleWithAllDynamicImportsOrOptionalIsStatic() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/j"));
Expand Down Expand Up @@ -451,7 +451,7 @@ public void moduleWithAllDynamicImportsIsStatic_2() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/i"));
Expand Down Expand Up @@ -516,7 +516,7 @@ public void moduleWithAllDynamicImportsIsStatic() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/i"));
Expand Down Expand Up @@ -580,7 +580,7 @@ public void moduleWithAllResolutionOptionalImportsIsStatic() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/i"));
Expand Down Expand Up @@ -643,7 +643,7 @@ public void moduleWithNoImportsIsStatic() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/i"));
Expand Down Expand Up @@ -707,7 +707,7 @@ public void moduleManualConfiguration() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo.module");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7-module");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/i"));
Expand Down Expand Up @@ -785,7 +785,7 @@ public void moduleRequiresModuleB() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/i"));
Expand Down Expand Up @@ -861,7 +861,7 @@ public void moduleRequiresModuleA() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/i"));
Expand Down Expand Up @@ -923,7 +923,7 @@ public void moduleRequiresA() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(1)
.anyMatch(e -> e.exports.equals("test/jpms/h"));
Expand Down Expand Up @@ -1102,7 +1102,7 @@ public void moduleOpenPackage() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(2)
.anyMatch(e -> e.opens.equals("test/jpms/e/other"))
.anyMatch(e -> e.opens.equals("test/jpms/e"));
Expand Down Expand Up @@ -1225,7 +1225,7 @@ public void moduleWithMainClass() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(0);
assertThat(moduleAttribute.provides).hasSize(0);
Expand Down Expand Up @@ -1281,7 +1281,7 @@ public void moduleWithExports() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(2)
.anyMatch(e -> e.exports.equals("test/jpms/b/other"))
Expand Down Expand Up @@ -1350,7 +1350,7 @@ public void simpleModule() throws Exception {

assertThat(moduleAttribute.module_name).isEqualTo("foo");
assertThat(moduleAttribute.module_version).isEqualTo("1.2.7");
assertThat(moduleAttribute.module_flags).isEqualTo(0);
assertThat(moduleAttribute.module_flags).isEqualTo(32);
assertThat(moduleAttribute.opens).hasSize(0);
assertThat(moduleAttribute.exports).hasSize(0);
assertThat(moduleAttribute.provides).hasSize(0);
Expand Down
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
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
Loading

0 comments on commit dd2f0c5

Please sign in to comment.