19
19
import static org .junit .Assert .assertEquals ;
20
20
import static org .junit .Assert .assertTrue ;
21
21
22
- import java .io .PrintStream ;
23
- import java .lang .reflect .Field ;
24
22
import java .lang .reflect .Method ;
25
23
import java .util .List ;
26
24
import java .util .stream .Collectors ;
27
25
import java .util .stream .Stream ;
28
- import org .apache .logging .log4j .osgi .tests .junit .OsgiRule ;
29
26
import org .apache .logging .log4j .util .ServiceLoaderUtil ;
30
27
import org .junit .Assert ;
31
28
import org .junit .Before ;
39
36
/**
40
37
* Tests a basic Log4J 'setup' in an OSGi container.
41
38
*/
42
- public abstract class AbstractLoadBundleTest {
39
+ abstract class AbstractLoadBundleTest {
43
40
44
41
private BundleContext bundleContext ;
45
42
46
43
@ Rule
47
- public OsgiRule osgi = new OsgiRule (getFactory ());
44
+ public final OsgiRule osgi ;
45
+
46
+ AbstractLoadBundleTest (final FrameworkFactory frameworkFactory ) {
47
+ this .osgi = new OsgiRule (frameworkFactory );
48
+ }
48
49
49
- /**
50
- * Called before each @Test.
51
- */
52
50
@ Before
53
- public void before () throws BundleException {
51
+ public void before () {
54
52
bundleContext = osgi .getFramework ().getBundleContext ();
55
53
}
56
54
@@ -76,59 +74,6 @@ private Bundle getApiTestsBundle() throws BundleException {
76
74
return installBundle ("org.apache.logging.log4j.api.test" );
77
75
}
78
76
79
- protected abstract FrameworkFactory getFactory ();
80
-
81
- private void log (final Bundle dummy ) throws ReflectiveOperationException {
82
- // use reflection to log in the context of the dummy bundle
83
-
84
- final Class <?> logManagerClass = dummy .loadClass ("org.apache.logging.log4j.LogManager" );
85
- final Method getLoggerMethod = logManagerClass .getMethod ("getLogger" , Class .class );
86
-
87
- final Class <?> loggerClass = dummy .loadClass ("org.apache.logging.log4j.configuration.CustomConfiguration" );
88
-
89
- final Object logger = getLoggerMethod .invoke (null , loggerClass );
90
- final Method errorMethod = logger .getClass ().getMethod ("error" , Object .class );
91
-
92
- errorMethod .invoke (logger , "Test OK" );
93
- }
94
-
95
- private PrintStream setupStream (final Bundle api , final PrintStream newStream ) throws ReflectiveOperationException {
96
- // use reflection to access the classes internals and in the context of the api bundle
97
-
98
- final Class <?> statusLoggerClass = api .loadClass ("org.apache.logging.log4j.status.StatusLogger" );
99
-
100
- final Field statusLoggerField = statusLoggerClass .getDeclaredField ("STATUS_LOGGER" );
101
- statusLoggerField .setAccessible (true );
102
- final Object statusLoggerFieldValue = statusLoggerField .get (null );
103
-
104
- final Field loggerField = statusLoggerClass .getDeclaredField ("logger" );
105
- loggerField .setAccessible (true );
106
- final Object loggerFieldValue = loggerField .get (statusLoggerFieldValue );
107
-
108
- final Class <?> simpleLoggerClass = api .loadClass ("org.apache.logging.log4j.simple.SimpleLogger" );
109
-
110
- final Field streamField = simpleLoggerClass .getDeclaredField ("stream" );
111
- streamField .setAccessible (true );
112
-
113
- final PrintStream oldStream = (PrintStream ) streamField .get (loggerFieldValue );
114
-
115
- streamField .set (loggerFieldValue , newStream );
116
-
117
- return oldStream ;
118
- }
119
-
120
- private void start (final Bundle api , final Bundle core , final Bundle dummy ) throws BundleException {
121
- api .start ();
122
- core .start ();
123
- dummy .start ();
124
- }
125
-
126
- private void stop (final Bundle api , final Bundle core , final Bundle dummy ) throws BundleException {
127
- dummy .stop ();
128
- core .stop ();
129
- api .stop ();
130
- }
131
-
132
77
private void uninstall (final Bundle api , final Bundle core , final Bundle dummy ) throws BundleException {
133
78
dummy .uninstall ();
134
79
core .uninstall ();
@@ -139,7 +84,7 @@ private void uninstall(final Bundle api, final Bundle core, final Bundle dummy)
139
84
* Tests starting, then stopping, then restarting, then stopping, and finally uninstalling the API and Core bundles
140
85
*/
141
86
@ Test
142
- public void testApiCoreStartStopStartStop () throws BundleException , ReflectiveOperationException {
87
+ public void testApiCoreStartStopStartStop () throws BundleException {
143
88
144
89
final Bundle api = getApiBundle ();
145
90
final Bundle core = getCoreBundle ();
@@ -191,25 +136,18 @@ public void testClassNotFoundErrorLogger() throws BundleException {
191
136
// fails if LOG4J2-1637 is not fixed
192
137
try {
193
138
core .start ();
194
- } catch (final BundleException ex ) {
195
- boolean shouldRethrow = true ;
196
- final Throwable t = ex .getCause ();
197
- if (t != null ) {
198
- final Throwable t2 = t .getCause ();
199
- if (t2 != null ) {
200
- final String cause = t2 .toString ();
201
- final boolean result =
202
- cause .equals ("java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger" ) // Equinox
203
- || cause .equals (
204
- "java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger not found by org.apache.logging.log4j.core [2]" ); // Felix
205
- Assert .assertFalse (
206
- "org.apache.logging.log4j package is not properly imported in org.apache.logging.log4j.core bundle, check that the package is exported from api and is not split between api and core" ,
207
- result );
208
- shouldRethrow = !result ;
139
+ } catch (final BundleException error0 ) {
140
+ boolean log4jClassNotFound = false ;
141
+ final Throwable error1 = error0 .getCause ();
142
+ if (error1 != null ) {
143
+ final Throwable error2 = error1 .getCause ();
144
+ if (error2 != null ) {
145
+ log4jClassNotFound = error2 .toString ()
146
+ .startsWith ("java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger" );
209
147
}
210
148
}
211
- if (shouldRethrow ) {
212
- throw ex ; // rethrow if the cause of the exception is something else
149
+ if (! log4jClassNotFound ) {
150
+ throw error0 ;
213
151
}
214
152
}
215
153
@@ -255,9 +193,6 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx
255
193
256
194
/**
257
195
* Tests whether the {@link ServiceLoaderUtil} finds services in other bundles.
258
- *
259
- * @throws BundleException
260
- * @throws ReflectiveOperationException
261
196
*/
262
197
@ Test
263
198
public void testServiceLoader () throws BundleException , ReflectiveOperationException {
0 commit comments