28
28
import org .junit .Before ;
29
29
import org .junit .Rule ;
30
30
import org .junit .Test ;
31
+ import org .junit .jupiter .api .function .ThrowingConsumer ;
31
32
import org .osgi .framework .Bundle ;
32
33
import org .osgi .framework .BundleContext ;
33
34
import org .osgi .framework .BundleException ;
@@ -74,12 +75,6 @@ private Bundle getApiTestsBundle() throws BundleException {
74
75
return installBundle ("org.apache.logging.log4j.api.test" );
75
76
}
76
77
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
-
83
78
/**
84
79
* Tests starting, then stopping, then restarting, then stopping, and finally uninstalling the API and Core bundles
85
80
*/
@@ -92,35 +87,15 @@ public void testApiCoreStartStopStartStop() throws BundleException {
92
87
Assert .assertEquals ("api is not in INSTALLED state" , Bundle .INSTALLED , api .getState ());
93
88
Assert .assertEquals ("core is not in INSTALLED state" , Bundle .INSTALLED , core .getState ());
94
89
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 );
115
93
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 );
118
97
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 );
124
99
}
125
100
126
101
/**
@@ -132,7 +107,7 @@ public void testClassNotFoundErrorLogger() throws BundleException {
132
107
final Bundle api = getApiBundle ();
133
108
final Bundle core = getCoreBundle ();
134
109
135
- api . start ( );
110
+ doOnBundlesAndVerifyState ( Bundle :: start , Bundle . ACTIVE , api );
136
111
// fails if LOG4J2-1637 is not fixed
137
112
try {
138
113
core .start ();
@@ -150,12 +125,10 @@ public void testClassNotFoundErrorLogger() throws BundleException {
150
125
throw error0 ;
151
126
}
152
127
}
128
+ assertEquals (String .format ("`%s` bundle state mismatch" , core ), Bundle .ACTIVE , core .getState ());
153
129
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 );
159
132
}
160
133
161
134
/**
@@ -169,8 +142,7 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx
169
142
final Bundle core = getCoreBundle ();
170
143
final Bundle compat = get12ApiBundle ();
171
144
172
- api .start ();
173
- core .start ();
145
+ doOnBundlesAndVerifyState (Bundle ::start , Bundle .ACTIVE , api , core );
174
146
175
147
final Class <?> coreClassFromCore = core .loadClass ("org.apache.logging.log4j.core.Core" );
176
148
final Class <?> levelClassFrom12API = core .loadClass ("org.apache.log4j.Level" );
@@ -185,10 +157,8 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx
185
157
levelClassFrom12API .getClassLoader (),
186
158
levelClassFromAPI .getClassLoader ());
187
159
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 );
192
162
}
193
163
194
164
/**
@@ -204,9 +174,7 @@ public void testServiceLoader() throws BundleException, ReflectiveOperationExcep
204
174
assertTrue ("OsgiServiceLocator is active" , (boolean )
205
175
osgiServiceLocator .getMethod ("isAvailable" ).invoke (null ));
206
176
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 );
210
178
211
179
final Class <?> osgiServiceLocatorTest =
212
180
apiTests .loadClass ("org.apache.logging.log4j.test.util.OsgiServiceLocatorTest" );
@@ -221,11 +189,20 @@ public void testServiceLoader() throws BundleException, ReflectiveOperationExcep
221
189
"org.apache.logging.log4j.core.impl.Log4jProvider" ,
222
190
services .get (0 ).getClass ().getName ());
223
191
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
+ }
230
207
}
231
208
}
0 commit comments