Skip to content

Commit 496951e

Browse files
committed
Simplify log4j-osgi-test
1 parent e25c308 commit 496951e

File tree

6 files changed

+36
-110
lines changed

6 files changed

+36
-110
lines changed

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

Lines changed: 18 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertTrue;
2121

22-
import java.io.PrintStream;
23-
import java.lang.reflect.Field;
2422
import java.lang.reflect.Method;
2523
import java.util.List;
2624
import java.util.stream.Collectors;
2725
import java.util.stream.Stream;
28-
import org.apache.logging.log4j.osgi.tests.junit.OsgiRule;
2926
import org.apache.logging.log4j.util.ServiceLoaderUtil;
3027
import org.junit.Assert;
3128
import org.junit.Before;
@@ -39,18 +36,19 @@
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 {

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,20 @@
3535
* This Configuration is the same as the DefaultConfiguration but shows how a
3636
* custom configuration can be built programmatically
3737
*/
38-
public class CustomConfiguration extends AbstractConfiguration {
39-
40-
/**
41-
* The name of the default configuration.
42-
*/
43-
public static final String CONFIG_NAME = "Custom";
38+
final class CustomConfiguration extends AbstractConfiguration {
4439

4540
private final ListAppender appender = new ListAppender();
4641

47-
public CustomConfiguration(final LoggerContext loggerContext) {
42+
CustomConfiguration(final LoggerContext loggerContext) {
4843
this(loggerContext, ConfigurationSource.NULL_SOURCE);
4944
}
5045

5146
/**
5247
* Constructor to create the default configuration.
5348
*/
54-
public CustomConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
49+
CustomConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) {
5550
super(loggerContext, source);
56-
setName(CONFIG_NAME);
51+
setName("Custom");
5752
appender.start();
5853
addAppender(appender);
5954
final LoggerConfig root = getRootLogger();
@@ -72,9 +67,9 @@ public void clearEvents() {
7267
appender.getEvents().clear();
7368
}
7469

75-
private static class ListAppender extends AbstractLifeCycle implements Appender {
70+
private static final class ListAppender extends AbstractLifeCycle implements Appender {
7671

77-
private final List<LogEvent> events = Collections.<LogEvent>synchronizedList(new ArrayList<>());
72+
private final List<LogEvent> events = Collections.synchronizedList(new ArrayList<>());
7873

7974
@Override
8075
public void append(final LogEvent event) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
@Plugin(name = "CustomConfigurationFactory", category = ConfigurationFactory.CATEGORY)
3131
@Order(50)
32-
public class CustomConfigurationFactory extends ConfigurationFactory {
32+
public final class CustomConfigurationFactory extends ConfigurationFactory {
3333

3434
/**
3535
* Valid file extensions for XML files.
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.osgi.tests.equinox;
17+
package org.apache.logging.log4j.osgi.tests;
1818

19-
import org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest;
2019
import org.eclipse.osgi.launch.EquinoxFactory;
21-
import org.osgi.framework.launch.FrameworkFactory;
2220

2321
/**
2422
* Tests loading the Core bundle into an Eclipse Equinox OSGi container.
2523
*/
2624
public class EquinoxLoadApiBundleTest extends AbstractLoadBundleTest {
2725

28-
@Override
29-
protected FrameworkFactory getFactory() {
30-
return new EquinoxFactory();
26+
public EquinoxLoadApiBundleTest() {
27+
super(new EquinoxFactory());
3128
}
3229
}
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.osgi.tests.felix;
17+
package org.apache.logging.log4j.osgi.tests;
1818

19-
import org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest;
20-
import org.osgi.framework.launch.FrameworkFactory;
19+
import org.apache.felix.framework.FrameworkFactory;
2120

2221
/**
2322
* Tests loading the Core bundle into an Apache Felix OSGi container.
2423
*/
2524
public class FelixLoadApiBundleTest extends AbstractLoadBundleTest {
2625

27-
@Override
28-
protected FrameworkFactory getFactory() {
29-
return new org.apache.felix.framework.FrameworkFactory();
26+
public FelixLoadApiBundleTest() {
27+
super(new FrameworkFactory());
3028
}
3129
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.osgi.tests.junit;
17+
package org.apache.logging.log4j.osgi.tests;
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
@@ -26,12 +26,13 @@
2626
/**
2727
* JUnit rule to initialize and shutdown an OSGi framework.
2828
*/
29-
public class OsgiRule extends ExternalResource {
29+
class OsgiRule extends ExternalResource {
3030

3131
private final FrameworkFactory factory;
32+
3233
private Framework framework;
3334

34-
public OsgiRule(final FrameworkFactory factory) {
35+
OsgiRule(final FrameworkFactory factory) {
3536
this.factory = factory;
3637
}
3738

0 commit comments

Comments
 (0)