-
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
Open
rozza
wants to merge
1
commit into
mongodb:main
Choose a base branch
from
rozza:JAVA-6215
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,41 @@ | ||
| /* | ||
| * 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("java-library") | ||
| 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) | ||
| testImplementation(libs.assertj) | ||
| testImplementation(libs.felix.framework) | ||
| testImplementation(libs.reactive.streams) | ||
| testImplementation(platform(libs.project.reactor.bom)) | ||
| testImplementation(libs.project.reactor.core) | ||
| } | ||
|
|
||
| tasks.test { | ||
| dependsOn(":bson:jar", ":driver-core:jar", ":driver-sync:jar", ":driver-reactive-streams:jar") | ||
| systemProperty("projectRoot", rootProject.projectDir.absolutePath) | ||
| } | ||
224 changes: 224 additions & 0 deletions
224
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,224 @@ | ||
| /* | ||
| * 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.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.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.launch.Framework; | ||
|
|
||
| class OsgiBundleResolutionTest { | ||
|
|
||
| private static final Path PROJECT_ROOT = Paths.get(System.getProperty("projectRoot", "../..")); | ||
|
|
||
| private static final String[] BUNDLE_MODULES = { | ||
| "bson", | ||
| "driver-core", | ||
| "driver-sync", | ||
| "driver-reactive-streams" | ||
|
Comment on lines
+55
to
+57
|
||
| }; | ||
|
|
||
| @TempDir | ||
| private Path cacheDir; | ||
|
|
||
| private Framework framework; | ||
|
|
||
| @BeforeEach | ||
| void startFramework() throws BundleException, IOException { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| 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"); | ||
| // Export required (non-optional) third-party packages from the system bundle. | ||
| // In a real OSGi container these would be provided by separately installed bundles. | ||
| // We scan the test classpath for reactive-streams and reactor-core JARs and export | ||
| // their packages so versions stay in sync with the version catalog automatically. | ||
| String extraPackages = buildSystemPackagesFromClasspath(); | ||
| if (!extraPackages.isEmpty()) { | ||
| config.put("org.osgi.framework.system.packages.extra", extraPackages); | ||
| } | ||
|
|
||
| framework = new FrameworkFactory().newFramework(config); | ||
| framework.start(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void stopFramework() throws BundleException, InterruptedException { | ||
| if (framework != null) { | ||
| framework.stop(); | ||
| framework.waitForStop(10_000); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void bundlesResolveWithoutOptionalDependencies() throws Exception { | ||
| BundleContext ctx = framework.getBundleContext(); | ||
| 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); | ||
| } | ||
| } | ||
|
|
||
| for (Bundle bundle : installed) { | ||
| try { | ||
| bundle.start(); | ||
| } catch (BundleException e) { | ||
| fail(formatBundleFailure(bundle, e)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void bundlesReportCorrectSymbolicNames() throws Exception { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can reduce duplication in these tests method by extracting a helper private List<Bundle> installAllBundles() throws IOException, BundleException {
BundleContext ctx = framework.getBundleContext();
List<Bundle> installed = new ArrayList<>();
for (String module : BUNDLE_MODULES) {
File jar = findBundleJar(module);
try (InputStream is = Files.newInputStream(jar.toPath())) {
installed.add(ctx.installBundle("file:" + jar.getAbsolutePath(), is));
}
}
return installed;
}
@Test
void bundlesResolveWithoutOptionalDependencies() throws Exception {
for (Bundle bundle : installAllBundles()) {
try {
bundle.start();
} catch (BundleException e) {
fail(formatBundleFailure(bundle, e));
}
}
}
@Test
void bundlesReportCorrectSymbolicNames() throws Exception {
assertThat(installAllBundles())
.extracting(Bundle::getSymbolicName)
.containsExactly(
"org.mongodb.bson",
"org.mongodb.driver-core",
"org.mongodb.driver-sync",
"org.mongodb.driver-reactivestreams");
} |
||
| BundleContext ctx = framework.getBundleContext(); | ||
| List<String> symbolicNames = 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); | ||
| symbolicNames.add(bundle.getSymbolicName()); | ||
| } | ||
| } | ||
|
|
||
| assertThat(symbolicNames).containsExactly( | ||
| "org.mongodb.bson", | ||
| "org.mongodb.driver-core", | ||
| "org.mongodb.driver-sync", | ||
| "org.mongodb.driver-reactivestreams"); | ||
| } | ||
|
|
||
| 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 systemPackagesCache; | ||
|
|
||
| private static String buildSystemPackagesFromClasspath() { | ||
| if (systemPackagesCache != null) { | ||
| return systemPackagesCache; | ||
| } | ||
| 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 (!name.startsWith("reactive-streams") && !name.startsWith("reactor-core")) { | ||
| continue; | ||
| } | ||
| if (!file.isFile() || !name.endsWith(".jar")) { | ||
| continue; | ||
| } | ||
| try (JarFile jar = new JarFile(file)) { | ||
| String version = jar.getManifest().getMainAttributes().getValue("Bundle-Version"); | ||
| if (version == null) { | ||
| version = "0.0.0"; | ||
| } | ||
|
Comment on lines
+177
to
+181
|
||
| 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) { | ||
| // Skip JARs that can't be read | ||
| } | ||
| } | ||
|
|
||
| systemPackagesCache = String.join(",", packages); | ||
| return systemPackagesCache; | ||
| } | ||
|
|
||
| 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()); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disabled this deps (
libs.reactive.streams) and the test still passes?