Skip to content

Commit e3a2559

Browse files
committed
Improve state check in AbstractLoadBundleTest
1 parent 125f505 commit e3a2559

File tree

1 file changed

+31
-54
lines changed

1 file changed

+31
-54
lines changed

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

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.Before;
2929
import org.junit.Rule;
3030
import org.junit.Test;
31+
import org.junit.jupiter.api.function.ThrowingConsumer;
3132
import org.osgi.framework.Bundle;
3233
import org.osgi.framework.BundleContext;
3334
import org.osgi.framework.BundleException;
@@ -74,12 +75,6 @@ private Bundle getApiTestsBundle() throws BundleException {
7475
return installBundle("org.apache.logging.log4j.api.test");
7576
}
7677

77-
private void uninstall(final Bundle api, final Bundle core, final Bundle dummy) throws BundleException {
78-
dummy.uninstall();
79-
core.uninstall();
80-
api.uninstall();
81-
}
82-
8378
/**
8479
* Tests starting, then stopping, then restarting, then stopping, and finally uninstalling the API and Core bundles
8580
*/
@@ -92,35 +87,15 @@ public void testApiCoreStartStopStartStop() throws BundleException {
9287
Assert.assertEquals("api is not in INSTALLED state", Bundle.INSTALLED, api.getState());
9388
Assert.assertEquals("core is not in INSTALLED state", Bundle.INSTALLED, core.getState());
9489

95-
api.start();
96-
core.start();
97-
98-
Assert.assertEquals("api is not in ACTIVE state", Bundle.ACTIVE, api.getState());
99-
Assert.assertEquals("core is not in ACTIVE state", Bundle.ACTIVE, core.getState());
100-
101-
core.stop();
102-
api.stop();
103-
104-
Assert.assertEquals("api is not in RESOLVED state", Bundle.RESOLVED, api.getState());
105-
Assert.assertEquals("core is not in RESOLVED state", Bundle.RESOLVED, core.getState());
106-
107-
api.start();
108-
core.start();
109-
110-
Assert.assertEquals("api is not in ACTIVE state", Bundle.ACTIVE, api.getState());
111-
Assert.assertEquals("core is not in ACTIVE state", Bundle.ACTIVE, core.getState());
112-
113-
core.stop();
114-
api.stop();
90+
// 1st start-stop
91+
doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core);
92+
doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api);
11593

116-
Assert.assertEquals("api is not in RESOLVED state", Bundle.RESOLVED, api.getState());
117-
Assert.assertEquals("core is not in RESOLVED state", Bundle.RESOLVED, core.getState());
94+
// 2nd start-stop
95+
doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core);
96+
doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api);
11897

119-
core.uninstall();
120-
api.uninstall();
121-
122-
Assert.assertEquals("api is not in UNINSTALLED state", Bundle.UNINSTALLED, api.getState());
123-
Assert.assertEquals("core is not in UNINSTALLED state", Bundle.UNINSTALLED, core.getState());
98+
doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, core, api);
12499
}
125100

126101
/**
@@ -132,7 +107,7 @@ public void testClassNotFoundErrorLogger() throws BundleException {
132107
final Bundle api = getApiBundle();
133108
final Bundle core = getCoreBundle();
134109

135-
api.start();
110+
doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api);
136111
// fails if LOG4J2-1637 is not fixed
137112
try {
138113
core.start();
@@ -150,12 +125,10 @@ public void testClassNotFoundErrorLogger() throws BundleException {
150125
throw error0;
151126
}
152127
}
128+
assertEquals(String.format("`%s` bundle state mismatch", core), Bundle.ACTIVE, core.getState());
153129

154-
core.stop();
155-
api.stop();
156-
157-
core.uninstall();
158-
api.uninstall();
130+
doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api);
131+
doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, core, api);
159132
}
160133

161134
/**
@@ -169,8 +142,7 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx
169142
final Bundle core = getCoreBundle();
170143
final Bundle compat = get12ApiBundle();
171144

172-
api.start();
173-
core.start();
145+
doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core);
174146

175147
final Class<?> coreClassFromCore = core.loadClass("org.apache.logging.log4j.core.Core");
176148
final Class<?> levelClassFrom12API = core.loadClass("org.apache.log4j.Level");
@@ -185,10 +157,8 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx
185157
levelClassFrom12API.getClassLoader(),
186158
levelClassFromAPI.getClassLoader());
187159

188-
core.stop();
189-
api.stop();
190-
191-
uninstall(api, core, compat);
160+
doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, core, api);
161+
doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, compat, core, api);
192162
}
193163

194164
/**
@@ -204,9 +174,7 @@ public void testServiceLoader() throws BundleException, ReflectiveOperationExcep
204174
assertTrue("OsgiServiceLocator is active", (boolean)
205175
osgiServiceLocator.getMethod("isAvailable").invoke(null));
206176

207-
core.start();
208-
apiTests.start();
209-
assertEquals("api-tests is not in ACTIVE state", Bundle.ACTIVE, apiTests.getState());
177+
doOnBundlesAndVerifyState(Bundle::start, Bundle.ACTIVE, api, core, apiTests);
210178

211179
final Class<?> osgiServiceLocatorTest =
212180
apiTests.loadClass("org.apache.logging.log4j.test.util.OsgiServiceLocatorTest");
@@ -221,11 +189,20 @@ public void testServiceLoader() throws BundleException, ReflectiveOperationExcep
221189
"org.apache.logging.log4j.core.impl.Log4jProvider",
222190
services.get(0).getClass().getName());
223191

224-
apiTests.stop();
225-
core.stop();
226-
api.stop();
227-
assertEquals("api-tests is not in ACTIVE state", Bundle.RESOLVED, apiTests.getState());
228-
uninstall(apiTests, api, core);
229-
assertEquals("api-tests is not in ACTIVE state", Bundle.UNINSTALLED, apiTests.getState());
192+
doOnBundlesAndVerifyState(Bundle::stop, Bundle.RESOLVED, apiTests, core, api);
193+
doOnBundlesAndVerifyState(Bundle::uninstall, Bundle.UNINSTALLED, apiTests, core, api);
194+
}
195+
196+
private static void doOnBundlesAndVerifyState(
197+
final ThrowingConsumer<Bundle> operation, final int expectedState, final Bundle... bundles) {
198+
for (final Bundle bundle : bundles) {
199+
try {
200+
operation.accept(bundle);
201+
} catch (final Throwable error) {
202+
final String message = String.format("operation failure for bundle `%s`", bundle);
203+
throw new RuntimeException(message, error);
204+
}
205+
assertEquals(String.format("`%s` bundle state mismatch", bundle), expectedState, bundle.getState());
206+
}
230207
}
231208
}

0 commit comments

Comments
 (0)