From c8ef0556394725b7cc255c4af2dd11f98085aca5 Mon Sep 17 00:00:00 2001 From: Alba Herrerias Date: Tue, 19 Nov 2024 14:02:08 +0000 Subject: [PATCH] Migrate `log4j-osgi-test` to JUnit 5 --- .../osgi/tests/AbstractLoadBundleTest.java | 44 +++++++++---------- .../tests/{OsgiRule.java => OsgiExt.java} | 17 +++---- 2 files changed, 30 insertions(+), 31 deletions(-) rename log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/{OsgiRule.java => OsgiExt.java} (79%) diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java index 15ac4e928aa..06bb63611d2 100644 --- a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java +++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java @@ -16,18 +16,18 @@ */ package org.apache.logging.log4j.osgi.tests; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.logging.log4j.util.ServiceLoaderUtil; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.function.ThrowingConsumer; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -41,14 +41,14 @@ abstract class AbstractLoadBundleTest { private BundleContext bundleContext; - @Rule - public final OsgiRule osgi; + @RegisterExtension + public final OsgiExt osgi; AbstractLoadBundleTest(final FrameworkFactory frameworkFactory) { - this.osgi = new OsgiRule(frameworkFactory); + this.osgi = new OsgiExt(frameworkFactory); } - @Before + @BeforeEach public void before() { bundleContext = osgi.getFramework().getBundleContext(); } @@ -83,9 +83,8 @@ public void testApiCoreStartStopStartStop() throws BundleException { final Bundle api = getApiBundle(); final Bundle core = getCoreBundle(); - - Assert.assertEquals("api is not in INSTALLED state", Bundle.INSTALLED, api.getState()); - Assert.assertEquals("core is not in INSTALLED state", Bundle.INSTALLED, core.getState()); + assertEquals(Bundle.INSTALLED, api.getState(), "api is not in INSTALLED state"); + assertEquals(Bundle.INSTALLED, core.getState(), "core is not in INSTALLED state"); // 1st start-stop doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core); @@ -125,7 +124,7 @@ public void testClassNotFoundErrorLogger() throws BundleException { throw error0; } } - assertEquals(String.format("`%s` bundle state mismatch", core), Bundle.ACTIVE, core.getState()); + assertEquals(Bundle.ACTIVE, core.getState(), String.format("`%s` bundle state mismatch", core)); doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api); doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, core, api); @@ -148,14 +147,14 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx final Class levelClassFrom12API = core.loadClass("org.apache.log4j.Level"); final Class levelClassFromAPI = core.loadClass("org.apache.logging.log4j.Level"); - Assert.assertEquals( - "expected 1.2 API Level to have the same class loader as Core", + assertEquals( levelClassFrom12API.getClassLoader(), - coreClassFromCore.getClassLoader()); - Assert.assertNotEquals( - "expected 1.2 API Level NOT to have the same class loader as API Level", + coreClassFromCore.getClassLoader(), + "expected 1.2 API Level to have the same class loader as Core"); + assertNotEquals( levelClassFrom12API.getClassLoader(), - levelClassFromAPI.getClassLoader()); + levelClassFromAPI.getClassLoader(), + "expected 1.2 API Level NOT to have the same class loader as API Level"); doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api); doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, compat, core, api); @@ -171,8 +170,7 @@ public void testServiceLoader() throws BundleException, ReflectiveOperationExcep final Bundle apiTests = getApiTestsBundle(); final Class osgiServiceLocator = api.loadClass("org.apache.logging.log4j.util.OsgiServiceLocator"); - assertTrue("OsgiServiceLocator is active", (boolean) - osgiServiceLocator.getMethod("isAvailable").invoke(null)); + assertTrue((boolean) osgiServiceLocator.getMethod("isAvailable").invoke(null), "OsgiServiceLocator is active"); doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core, apiTests); @@ -202,7 +200,7 @@ private static void doOnBundlesAndVerifyState( final String message = String.format("operation failure for bundle `%s`", bundle); throw new RuntimeException(message, error); } - assertEquals(String.format("`%s` bundle state mismatch", bundle), expectedState, bundle.getState()); + assertEquals(expectedState, bundle.getState(), String.format("`%s` bundle state mismatch", bundle)); } } } diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiRule.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiExt.java similarity index 79% rename from log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiRule.java rename to log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiExt.java index 4db53904eca..1caebff73da 100644 --- a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiRule.java +++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiExt.java @@ -21,7 +21,9 @@ import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; -import org.junit.rules.ExternalResource; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; import org.osgi.framework.BundleException; import org.osgi.framework.launch.Framework; import org.osgi.framework.launch.FrameworkFactory; @@ -29,17 +31,16 @@ /** * JUnit rule to initialize and shutdown an OSGi framework. */ -class OsgiRule extends ExternalResource { - +class OsgiExt implements AfterEachCallback, BeforeEachCallback { private final FrameworkFactory factory; private Framework framework; - OsgiRule(final FrameworkFactory factory) { + OsgiExt(final FrameworkFactory factory) { this.factory = factory; } @Override - protected void after() { + public void afterEach(ExtensionContext context) { if (framework != null) { try { framework.stop(); @@ -52,8 +53,8 @@ protected void after() { } @Override - protected void before() throws Throwable { - try (final InputStream is = OsgiRule.class.getResourceAsStream("/osgi.properties")) { + public void beforeEach(ExtensionContext context) throws Exception { + try (final InputStream is = OsgiExt.class.getResourceAsStream("/osgi.properties")) { final Properties props = new Properties(); props.load(is); final Map configMap = props.entrySet().stream() @@ -74,6 +75,6 @@ public Framework getFramework() { @Override public String toString() { - return "OsgiRule [factory=" + factory + ", framework=" + framework + "]"; + return "OsgiExt [factory=" + factory + ", framework=" + framework + "]"; } }