Skip to content

Commit a157901

Browse files
authored
Merge pull request #3219 from neighbourhoodie/log4j-osgi-test-migration-junit5
Migrate `log4j-osgi-test` to JUnit 5
2 parents 5df0a57 + c8ef055 commit a157901

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
*/
1717
package org.apache.logging.log4j.osgi.tests;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2122

2223
import java.lang.reflect.Method;
2324
import java.util.List;
2425
import java.util.stream.Collectors;
2526
import java.util.stream.Stream;
2627
import org.apache.logging.log4j.util.ServiceLoaderUtil;
27-
import org.junit.Assert;
28-
import org.junit.Before;
29-
import org.junit.Rule;
30-
import org.junit.Test;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.RegisterExtension;
3131
import org.junit.jupiter.api.function.ThrowingConsumer;
3232
import org.osgi.framework.Bundle;
3333
import org.osgi.framework.BundleContext;
@@ -41,14 +41,14 @@ abstract class AbstractLoadBundleTest {
4141

4242
private BundleContext bundleContext;
4343

44-
@Rule
45-
public final OsgiRule osgi;
44+
@RegisterExtension
45+
public final OsgiExt osgi;
4646

4747
AbstractLoadBundleTest(final FrameworkFactory frameworkFactory) {
48-
this.osgi = new OsgiRule(frameworkFactory);
48+
this.osgi = new OsgiExt(frameworkFactory);
4949
}
5050

51-
@Before
51+
@BeforeEach
5252
public void before() {
5353
bundleContext = osgi.getFramework().getBundleContext();
5454
}
@@ -83,9 +83,8 @@ public void testApiCoreStartStopStartStop() throws BundleException {
8383

8484
final Bundle api = getApiBundle();
8585
final Bundle core = getCoreBundle();
86-
87-
Assert.assertEquals("api is not in INSTALLED state", Bundle.INSTALLED, api.getState());
88-
Assert.assertEquals("core is not in INSTALLED state", Bundle.INSTALLED, core.getState());
86+
assertEquals(Bundle.INSTALLED, api.getState(), "api is not in INSTALLED state");
87+
assertEquals(Bundle.INSTALLED, core.getState(), "core is not in INSTALLED state");
8988

9089
// 1st start-stop
9190
doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core);
@@ -125,7 +124,7 @@ public void testClassNotFoundErrorLogger() throws BundleException {
125124
throw error0;
126125
}
127126
}
128-
assertEquals(String.format("`%s` bundle state mismatch", core), Bundle.ACTIVE, core.getState());
127+
assertEquals(Bundle.ACTIVE, core.getState(), String.format("`%s` bundle state mismatch", core));
129128

130129
doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api);
131130
doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, core, api);
@@ -148,14 +147,14 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx
148147
final Class<?> levelClassFrom12API = core.loadClass("org.apache.log4j.Level");
149148
final Class<?> levelClassFromAPI = core.loadClass("org.apache.logging.log4j.Level");
150149

151-
Assert.assertEquals(
152-
"expected 1.2 API Level to have the same class loader as Core",
150+
assertEquals(
153151
levelClassFrom12API.getClassLoader(),
154-
coreClassFromCore.getClassLoader());
155-
Assert.assertNotEquals(
156-
"expected 1.2 API Level NOT to have the same class loader as API Level",
152+
coreClassFromCore.getClassLoader(),
153+
"expected 1.2 API Level to have the same class loader as Core");
154+
assertNotEquals(
157155
levelClassFrom12API.getClassLoader(),
158-
levelClassFromAPI.getClassLoader());
156+
levelClassFromAPI.getClassLoader(),
157+
"expected 1.2 API Level NOT to have the same class loader as API Level");
159158

160159
doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api);
161160
doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, compat, core, api);
@@ -171,8 +170,7 @@ public void testServiceLoader() throws BundleException, ReflectiveOperationExcep
171170
final Bundle apiTests = getApiTestsBundle();
172171

173172
final Class<?> osgiServiceLocator = api.loadClass("org.apache.logging.log4j.util.OsgiServiceLocator");
174-
assertTrue("OsgiServiceLocator is active", (boolean)
175-
osgiServiceLocator.getMethod("isAvailable").invoke(null));
173+
assertTrue((boolean) osgiServiceLocator.getMethod("isAvailable").invoke(null), "OsgiServiceLocator is active");
176174

177175
doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core, apiTests);
178176

@@ -202,7 +200,7 @@ private static void doOnBundlesAndVerifyState(
202200
final String message = String.format("operation failure for bundle `%s`", bundle);
203201
throw new RuntimeException(message, error);
204202
}
205-
assertEquals(String.format("`%s` bundle state mismatch", bundle), expectedState, bundle.getState());
203+
assertEquals(expectedState, bundle.getState(), String.format("`%s` bundle state mismatch", bundle));
206204
}
207205
}
208206
}

log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiRule.java renamed to log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiExt.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@
2121
import java.util.Map;
2222
import java.util.Properties;
2323
import java.util.stream.Collectors;
24-
import org.junit.rules.ExternalResource;
24+
import org.junit.jupiter.api.extension.AfterEachCallback;
25+
import org.junit.jupiter.api.extension.BeforeEachCallback;
26+
import org.junit.jupiter.api.extension.ExtensionContext;
2527
import org.osgi.framework.BundleException;
2628
import org.osgi.framework.launch.Framework;
2729
import org.osgi.framework.launch.FrameworkFactory;
2830

2931
/**
3032
* JUnit rule to initialize and shutdown an OSGi framework.
3133
*/
32-
class OsgiRule extends ExternalResource {
33-
34+
class OsgiExt implements AfterEachCallback, BeforeEachCallback {
3435
private final FrameworkFactory factory;
3536
private Framework framework;
3637

37-
OsgiRule(final FrameworkFactory factory) {
38+
OsgiExt(final FrameworkFactory factory) {
3839
this.factory = factory;
3940
}
4041

4142
@Override
42-
protected void after() {
43+
public void afterEach(ExtensionContext context) {
4344
if (framework != null) {
4445
try {
4546
framework.stop();
@@ -52,8 +53,8 @@ protected void after() {
5253
}
5354

5455
@Override
55-
protected void before() throws Throwable {
56-
try (final InputStream is = OsgiRule.class.getResourceAsStream("/osgi.properties")) {
56+
public void beforeEach(ExtensionContext context) throws Exception {
57+
try (final InputStream is = OsgiExt.class.getResourceAsStream("/osgi.properties")) {
5758
final Properties props = new Properties();
5859
props.load(is);
5960
final Map<String, String> configMap = props.entrySet().stream()
@@ -74,6 +75,6 @@ public Framework getFramework() {
7475

7576
@Override
7677
public String toString() {
77-
return "OsgiRule [factory=" + factory + ", framework=" + framework + "]";
78+
return "OsgiExt [factory=" + factory + ", framework=" + framework + "]";
7879
}
7980
}

0 commit comments

Comments
 (0)