Skip to content

Commit

Permalink
Merge pull request #3219 from neighbourhoodie/log4j-osgi-test-migrati…
Browse files Browse the repository at this point in the history
…on-junit5

Migrate `log4j-osgi-test` to JUnit 5
  • Loading branch information
jvz authored Dec 3, 2024
2 parents 5df0a57 + c8ef055 commit a157901
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,26 @@
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;

/**
* 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();
Expand All @@ -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<String, String> configMap = props.entrySet().stream()
Expand All @@ -74,6 +75,6 @@ public Framework getFramework() {

@Override
public String toString() {
return "OsgiRule [factory=" + factory + ", framework=" + framework + "]";
return "OsgiExt [factory=" + factory + ", framework=" + framework + "]";
}
}

0 comments on commit a157901

Please sign in to comment.