1919import static org .junit .Assert .assertEquals ;
2020import static org .junit .Assert .assertTrue ;
2121
22- import java .io .PrintStream ;
23- import java .lang .reflect .Field ;
2422import java .lang .reflect .Method ;
2523import java .util .List ;
2624import java .util .stream .Collectors ;
2725import java .util .stream .Stream ;
28- import org .apache .logging .log4j .osgi .tests .junit .OsgiRule ;
2926import org .apache .logging .log4j .util .ServiceLoaderUtil ;
3027import org .junit .Assert ;
3128import org .junit .Before ;
3936/**
4037 * Tests a basic Log4J 'setup' in an OSGi container.
4138 */
42- public abstract class AbstractLoadBundleTest {
39+ abstract class AbstractLoadBundleTest {
4340
4441 private BundleContext bundleContext ;
4542
4643 @ 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+ }
4849
49- /**
50- * Called before each @Test.
51- */
5250 @ Before
53- public void before () throws BundleException {
51+ public void before () {
5452 bundleContext = osgi .getFramework ().getBundleContext ();
5553 }
5654
@@ -76,59 +74,6 @@ private Bundle getApiTestsBundle() throws BundleException {
7674 return installBundle ("org.apache.logging.log4j.api.test" );
7775 }
7876
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-
13277 private void uninstall (final Bundle api , final Bundle core , final Bundle dummy ) throws BundleException {
13378 dummy .uninstall ();
13479 core .uninstall ();
@@ -139,7 +84,7 @@ private void uninstall(final Bundle api, final Bundle core, final Bundle dummy)
13984 * Tests starting, then stopping, then restarting, then stopping, and finally uninstalling the API and Core bundles
14085 */
14186 @ Test
142- public void testApiCoreStartStopStartStop () throws BundleException , ReflectiveOperationException {
87+ public void testApiCoreStartStopStartStop () throws BundleException {
14388
14489 final Bundle api = getApiBundle ();
14590 final Bundle core = getCoreBundle ();
@@ -191,25 +136,18 @@ public void testClassNotFoundErrorLogger() throws BundleException {
191136 // fails if LOG4J2-1637 is not fixed
192137 try {
193138 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" );
209147 }
210148 }
211- if (shouldRethrow ) {
212- throw ex ; // rethrow if the cause of the exception is something else
149+ if (! log4jClassNotFound ) {
150+ throw error0 ;
213151 }
214152 }
215153
@@ -255,9 +193,6 @@ public void testLog4J12Fragement() throws BundleException, ReflectiveOperationEx
255193
256194 /**
257195 * Tests whether the {@link ServiceLoaderUtil} finds services in other bundles.
258- *
259- * @throws BundleException
260- * @throws ReflectiveOperationException
261196 */
262197 @ Test
263198 public void testServiceLoader () throws BundleException , ReflectiveOperationException {
0 commit comments