-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Mark micrometer OSGi imports as optional and add bundle resolution test #1982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
64a8cd8
Mark micrometer OSGi imports as optional and add bundle resolution test
rozza 9c48f66
Merge branch 'main' into JAVA-6215
rozza ff012ea
PR review updates
rozza 85002ea
Update testing/osgi-test/src/test/java/com/mongodb/osgi/OsgiBundleRes…
rozza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,65 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| plugins { | ||
| id("project.base") | ||
| id("checkstyle") | ||
| id("conventions.testing-base") | ||
| } | ||
|
|
||
| java { | ||
| toolchain { languageVersion = JavaLanguageVersion.of(17) } | ||
| } | ||
|
|
||
| dependencies { | ||
| testImplementation(platform(libs.junit.bom)) | ||
| testImplementation(libs.junit.jupiter) | ||
| testImplementation(libs.junit.jupiter.platform.launcher) | ||
| // AssertJ used here for infrastructure assertions (isDirectory, hasSize, containsExactly) | ||
| // which are significantly more readable than JUnit 5 equivalents for this test. | ||
| testImplementation(libs.assertj) | ||
| testImplementation(libs.felix.framework) | ||
|
|
||
| // These JARs are scanned by buildSystemPackagesFromClasspath() to export packages | ||
| // from the Felix system bundle, satisfying non-optional imports from bundles under test. | ||
| testImplementation(libs.reactive.streams) | ||
| testImplementation(platform(libs.project.reactor.bom)) | ||
| testImplementation(libs.project.reactor.core) | ||
| testImplementation(platform(libs.kotlin.bom)) | ||
| testImplementation(libs.kotlin.stdlib.jdk8) | ||
| testImplementation(libs.kotlin.reflect) | ||
| testImplementation(platform(libs.kotlinx.coroutines.bom)) | ||
| testImplementation(libs.kotlinx.coroutines.core) | ||
| testImplementation(libs.kotlinx.coroutines.reactive) | ||
| testImplementation(libs.findbugs.jsr) | ||
| testImplementation(libs.jna) | ||
| } | ||
|
|
||
| tasks.test { | ||
| dependsOn( | ||
| ":bson:jar", | ||
| ":bson-record-codec:jar", | ||
| ":mongodb-crypt:jar", | ||
| ":driver-core:jar", | ||
| ":bson-scala:jar", | ||
| ":driver-sync:jar", | ||
| ":driver-reactive-streams:jar", | ||
| ":driver-scala:jar", | ||
| ":driver-kotlin-sync:jar", | ||
| ":driver-kotlin-coroutine:jar", | ||
| ":driver-kotlin-extensions:jar" | ||
| ) | ||
| systemProperty("projectRoot", rootProject.projectDir.absolutePath) | ||
| } | ||
267 changes: 267 additions & 0 deletions
267
testing/osgi-test/src/test/java/com/mongodb/osgi/OsgiBundleResolutionTest.java
This file contains hidden or 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,267 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.mongodb.osgi; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.fail; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.UncheckedIOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.Enumeration; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.jar.JarEntry; | ||
| import java.util.jar.JarFile; | ||
| import java.util.jar.Manifest; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import org.apache.felix.framework.FrameworkFactory; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
| import org.osgi.framework.Bundle; | ||
| import org.osgi.framework.BundleContext; | ||
| import org.osgi.framework.BundleException; | ||
| import org.osgi.framework.FrameworkEvent; | ||
| import org.osgi.framework.launch.Framework; | ||
|
|
||
| class OsgiBundleResolutionTest { | ||
|
|
||
| private static final Path PROJECT_ROOT = Paths.get(System.getProperty("projectRoot", "../..")); | ||
|
|
||
|
rozza marked this conversation as resolved.
|
||
| // Listed in dependency order (leaves last) so that the first bundle.start() failure | ||
| // identifies the root cause rather than a cascading downstream resolution error. | ||
| private static final String[] BUNDLE_MODULES = { | ||
| "bson", | ||
| "bson-record-codec", | ||
| "mongodb-crypt", | ||
| "driver-core", | ||
| "bson-scala", | ||
| "driver-sync", | ||
| "driver-reactive-streams", | ||
| "driver-scala", | ||
| "driver-kotlin-sync", | ||
| "driver-kotlin-coroutine", | ||
| "driver-kotlin-extensions" | ||
| }; | ||
|
|
||
| // JARs on the test classpath whose packages are exported from the Felix system bundle, | ||
| // satisfying non-optional imports from the bundles under test. | ||
| private static final String[] SYSTEM_PACKAGE_JAR_PREFIXES = { | ||
| "reactive-streams", | ||
| "reactor-core", | ||
| "kotlin-stdlib", | ||
| "kotlin-reflect", | ||
| "kotlinx-coroutines-core", | ||
| "kotlinx-coroutines-reactive", | ||
| "jsr305", | ||
| "jna" | ||
| }; | ||
|
|
||
| // Eagerly computed — the classpath is fixed for the lifetime of the test JVM. | ||
| private static final String SYSTEM_PACKAGES = buildSystemPackagesFromClasspath(); | ||
|
|
||
| @TempDir | ||
| private Path cacheDir; | ||
|
|
||
| private Framework framework; | ||
|
|
||
| @BeforeEach | ||
| void startFramework() throws BundleException { | ||
| Map<String, String> config = new HashMap<>(); | ||
| config.put("org.osgi.framework.storage", cacheDir.toString()); | ||
| config.put("org.osgi.framework.storage.clean", "onFirstInit"); | ||
| config.put("felix.log.level", "1"); | ||
| if (!SYSTEM_PACKAGES.isEmpty()) { | ||
| config.put("org.osgi.framework.system.packages.extra", SYSTEM_PACKAGES); | ||
| } | ||
|
|
||
| framework = new FrameworkFactory().newFramework(config); | ||
| framework.start(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void stopFramework() throws BundleException, InterruptedException { | ||
| if (framework != null) { | ||
| framework.stop(); | ||
| FrameworkEvent event = framework.waitForStop(10_000); | ||
| if (event.getType() == FrameworkEvent.WAIT_TIMEDOUT) { | ||
| throw new IllegalStateException("OSGi framework did not stop within 10 seconds"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void bundlesResolveWithoutOptionalDependencies() throws Exception { | ||
| List<Bundle> installed = installAllBundles(framework.getBundleContext()); | ||
|
|
||
| for (Bundle bundle : installed) { | ||
| try { | ||
| bundle.start(); | ||
| } catch (BundleException e) { | ||
| // Fail immediately on the first resolution error. Bundles are wired by | ||
| // Import-Package, so an unresolved bundle (e.g. driver-core missing a | ||
| // required import) leaves its exported packages unsatisfied for all | ||
| // downstream bundles. Collecting further failures would only add | ||
| // cascading noise — the first message identifies the root cause. | ||
| fail(formatBundleFailure(bundle, e)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void bundlesReportCorrectSymbolicNames() throws Exception { | ||
|
rozza marked this conversation as resolved.
|
||
| List<Bundle> installed = installAllBundles(framework.getBundleContext()); | ||
|
|
||
| List<String> symbolicNames = installed.stream() | ||
| .map(Bundle::getSymbolicName) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| assertThat(symbolicNames).containsExactly( | ||
| "org.mongodb.bson", | ||
| "org.mongodb.bson-record-codec", | ||
| "com.mongodb.crypt.capi", | ||
| "org.mongodb.driver-core", | ||
| "org.mongodb.scala.mongo-scala-bson", | ||
| "org.mongodb.driver-sync", | ||
| "org.mongodb.driver-reactivestreams", | ||
| "org.mongodb.scala.mongo-scala-driver", | ||
| "org.mongodb.mongodb-driver-kotlin-sync", | ||
| "org.mongodb.mongodb-driver-kotlin-coroutine", | ||
| "org.mongodb.mongodb-driver-kotlin-extensions"); | ||
| } | ||
|
|
||
| private List<Bundle> installAllBundles(final BundleContext ctx) throws Exception { | ||
| List<Bundle> installed = new ArrayList<>(); | ||
| for (String module : BUNDLE_MODULES) { | ||
| File jar = findBundleJar(module); | ||
| try (InputStream is = Files.newInputStream(jar.toPath())) { | ||
| Bundle bundle = ctx.installBundle("file:" + jar.getAbsolutePath(), is); | ||
| installed.add(bundle); | ||
| } | ||
| } | ||
| return installed; | ||
| } | ||
|
|
||
| // Parses Felix's error message format to extract the missing package name. | ||
| private static String formatBundleFailure(final Bundle bundle, final BundleException e) { | ||
| String msg = e.getMessage(); | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("\n\n====================================================================\n"); | ||
| sb.append("BUNDLE RESOLUTION FAILURE: ").append(bundle.getSymbolicName()).append("\n"); | ||
| sb.append("====================================================================\n"); | ||
|
|
||
| if (msg != null && msg.contains("missing requirement")) { | ||
| int pkgStart = msg.indexOf("osgi.wiring.package="); | ||
| if (pkgStart >= 0) { | ||
| String remainder = msg.substring(pkgStart + "osgi.wiring.package=".length()); | ||
| int pkgEnd = remainder.indexOf(')'); | ||
| String missingPackage = pkgEnd >= 0 ? remainder.substring(0, pkgEnd) : remainder; | ||
| sb.append("Missing required package: ").append(missingPackage).append("\n\n"); | ||
| sb.append("FIX: Add '").append(missingPackage).append(".*;resolution:=optional' to the\n"); | ||
| sb.append(" Import-Package list in the module's build.gradle.kts\n"); | ||
| } | ||
| } | ||
|
|
||
| sb.append("\nFull error: ").append(msg); | ||
| sb.append("\n====================================================================\n"); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| private static String buildSystemPackagesFromClasspath() { | ||
| Set<String> packages = new LinkedHashSet<>(); | ||
| String classpath = System.getProperty("java.class.path", ""); | ||
|
|
||
| for (String entry : classpath.split(File.pathSeparator)) { | ||
| File file = new File(entry); | ||
| String name = file.getName(); | ||
| if (!matchesAnyPrefix(name)) { | ||
| continue; | ||
| } | ||
| if (!file.isFile() || !name.endsWith(".jar")) { | ||
| continue; | ||
| } | ||
| try (JarFile jar = new JarFile(file)) { | ||
| Manifest manifest = jar.getManifest(); | ||
| if (manifest == null) { | ||
| continue; | ||
| } | ||
| String version = manifest.getMainAttributes().getValue("Bundle-Version"); | ||
| if (version == null) { | ||
| version = "0.0.0"; | ||
| } | ||
|
Comment on lines
+208
to
+216
|
||
| Enumeration<JarEntry> entries = jar.entries(); | ||
| while (entries.hasMoreElements()) { | ||
| JarEntry jarEntry = entries.nextElement(); | ||
| String entryName = jarEntry.getName(); | ||
| if (entryName.endsWith(".class") && entryName.contains("/")) { | ||
| String pkg = entryName.substring(0, entryName.lastIndexOf('/')).replace('/', '.'); | ||
| packages.add(pkg + ";version=\"" + version + "\""); | ||
| } | ||
| } | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to read classpath JAR: " + file, e); | ||
| } | ||
| } | ||
|
|
||
| return String.join(",", packages); | ||
| } | ||
|
|
||
| private static boolean matchesAnyPrefix(final String fileName) { | ||
| for (String prefix : SYSTEM_PACKAGE_JAR_PREFIXES) { | ||
| if (fileName.startsWith(prefix)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static File findBundleJar(final String module) { | ||
| Path libsDir = PROJECT_ROOT.resolve(module).resolve("build").resolve("libs"); | ||
| assertThat(libsDir) | ||
| .as("Build output directory for module '%s' must exist. Run ./gradlew jar first.", module) | ||
| .isDirectory(); | ||
|
|
||
| try (Stream<Path> files = Files.list(libsDir)) { | ||
| List<File> candidates = files | ||
| .filter(p -> p.getFileName().toString().endsWith(".jar")) | ||
| .filter(p -> !p.getFileName().toString().contains("-test")) | ||
| .filter(p -> !p.getFileName().toString().contains("-sources")) | ||
| .filter(p -> !p.getFileName().toString().contains("-javadoc")) | ||
| .map(Path::toFile) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| assertThat(candidates) | ||
| .as("Expected exactly one main JAR in %s", libsDir) | ||
| .hasSize(1); | ||
|
|
||
| return candidates.get(0); | ||
| } catch (IOException e) { | ||
| return fail("Failed to list JARs in " + libsDir + ": " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.