This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/2.x by this push:
new 496951ef68 Simplify `log4j-osgi-test`
496951ef68 is described below
commit 496951ef6847c1b22d89c71c6c9ea3ac9bffdcdc
Author: Volkan Yazıcı <[email protected]>
AuthorDate: Fri Jan 12 11:23:13 2024 +0100
Simplify `log4j-osgi-test`
---
.../log4j/osgi/tests/AbstractLoadBundleTest.java | 101 ++++-----------------
.../log4j/osgi/tests/CustomConfiguration.java | 17 ++--
.../osgi/tests/CustomConfigurationFactory.java | 2 +-
.../{equinox => }/EquinoxLoadApiBundleTest.java | 9 +-
.../tests/{felix => }/FelixLoadApiBundleTest.java | 10 +-
.../log4j/osgi/tests/{junit => }/OsgiRule.java | 7 +-
6 files changed, 36 insertions(+), 110 deletions(-)
diff --git
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java
index 6a1879e533..b032f433d6 100644
---
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java
+++
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/AbstractLoadBundleTest.java
@@ -19,13 +19,10 @@ package org.apache.logging.log4j.osgi.tests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-import java.io.PrintStream;
-import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-import org.apache.logging.log4j.osgi.tests.junit.OsgiRule;
import org.apache.logging.log4j.util.ServiceLoaderUtil;
import org.junit.Assert;
import org.junit.Before;
@@ -39,18 +36,19 @@ import org.osgi.framework.launch.FrameworkFactory;
/**
* Tests a basic Log4J 'setup' in an OSGi container.
*/
-public abstract class AbstractLoadBundleTest {
+abstract class AbstractLoadBundleTest {
private BundleContext bundleContext;
@Rule
- public OsgiRule osgi = new OsgiRule(getFactory());
+ public final OsgiRule osgi;
+
+ AbstractLoadBundleTest(final FrameworkFactory frameworkFactory) {
+ this.osgi = new OsgiRule(frameworkFactory);
+ }
- /**
- * Called before each @Test.
- */
@Before
- public void before() throws BundleException {
+ public void before() {
bundleContext = osgi.getFramework().getBundleContext();
}
@@ -76,59 +74,6 @@ public abstract class AbstractLoadBundleTest {
return installBundle("org.apache.logging.log4j.api.test");
}
- protected abstract FrameworkFactory getFactory();
-
- private void log(final Bundle dummy) throws ReflectiveOperationException {
- // use reflection to log in the context of the dummy bundle
-
- final Class<?> logManagerClass =
dummy.loadClass("org.apache.logging.log4j.LogManager");
- final Method getLoggerMethod = logManagerClass.getMethod("getLogger",
Class.class);
-
- final Class<?> loggerClass =
dummy.loadClass("org.apache.logging.log4j.configuration.CustomConfiguration");
-
- final Object logger = getLoggerMethod.invoke(null, loggerClass);
- final Method errorMethod = logger.getClass().getMethod("error",
Object.class);
-
- errorMethod.invoke(logger, "Test OK");
- }
-
- private PrintStream setupStream(final Bundle api, final PrintStream
newStream) throws ReflectiveOperationException {
- // use reflection to access the classes internals and in the context
of the api bundle
-
- final Class<?> statusLoggerClass =
api.loadClass("org.apache.logging.log4j.status.StatusLogger");
-
- final Field statusLoggerField =
statusLoggerClass.getDeclaredField("STATUS_LOGGER");
- statusLoggerField.setAccessible(true);
- final Object statusLoggerFieldValue = statusLoggerField.get(null);
-
- final Field loggerField = statusLoggerClass.getDeclaredField("logger");
- loggerField.setAccessible(true);
- final Object loggerFieldValue =
loggerField.get(statusLoggerFieldValue);
-
- final Class<?> simpleLoggerClass =
api.loadClass("org.apache.logging.log4j.simple.SimpleLogger");
-
- final Field streamField = simpleLoggerClass.getDeclaredField("stream");
- streamField.setAccessible(true);
-
- final PrintStream oldStream = (PrintStream)
streamField.get(loggerFieldValue);
-
- streamField.set(loggerFieldValue, newStream);
-
- return oldStream;
- }
-
- private void start(final Bundle api, final Bundle core, final Bundle
dummy) throws BundleException {
- api.start();
- core.start();
- dummy.start();
- }
-
- private void stop(final Bundle api, final Bundle core, final Bundle dummy)
throws BundleException {
- dummy.stop();
- core.stop();
- api.stop();
- }
-
private void uninstall(final Bundle api, final Bundle core, final Bundle
dummy) throws BundleException {
dummy.uninstall();
core.uninstall();
@@ -139,7 +84,7 @@ public abstract class AbstractLoadBundleTest {
* Tests starting, then stopping, then restarting, then stopping, and
finally uninstalling the API and Core bundles
*/
@Test
- public void testApiCoreStartStopStartStop() throws BundleException,
ReflectiveOperationException {
+ public void testApiCoreStartStopStartStop() throws BundleException {
final Bundle api = getApiBundle();
final Bundle core = getCoreBundle();
@@ -191,25 +136,18 @@ public abstract class AbstractLoadBundleTest {
// fails if LOG4J2-1637 is not fixed
try {
core.start();
- } catch (final BundleException ex) {
- boolean shouldRethrow = true;
- final Throwable t = ex.getCause();
- if (t != null) {
- final Throwable t2 = t.getCause();
- if (t2 != null) {
- final String cause = t2.toString();
- final boolean result =
- cause.equals("java.lang.ClassNotFoundException:
org.apache.logging.log4j.Logger") // Equinox
- || cause.equals(
- "java.lang.ClassNotFoundException:
org.apache.logging.log4j.Logger not found by org.apache.logging.log4j.core
[2]"); // Felix
- Assert.assertFalse(
- "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",
- result);
- shouldRethrow = !result;
+ } catch (final BundleException error0) {
+ boolean log4jClassNotFound = false;
+ final Throwable error1 = error0.getCause();
+ if (error1 != null) {
+ final Throwable error2 = error1.getCause();
+ if (error2 != null) {
+ log4jClassNotFound = error2.toString()
+ .startsWith("java.lang.ClassNotFoundException:
org.apache.logging.log4j.Logger");
}
}
- if (shouldRethrow) {
- throw ex; // rethrow if the cause of the exception is
something else
+ if (!log4jClassNotFound) {
+ throw error0;
}
}
@@ -255,9 +193,6 @@ public abstract class AbstractLoadBundleTest {
/**
* Tests whether the {@link ServiceLoaderUtil} finds services in other
bundles.
- *
- * @throws BundleException
- * @throws ReflectiveOperationException
*/
@Test
public void testServiceLoader() throws BundleException,
ReflectiveOperationException {
diff --git
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfiguration.java
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfiguration.java
index 81a97d9611..8948b001fe 100644
---
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfiguration.java
+++
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfiguration.java
@@ -35,25 +35,20 @@ import org.apache.logging.log4j.core.config.LoggerConfig;
* This Configuration is the same as the DefaultConfiguration but shows how a
* custom configuration can be built programmatically
*/
-public class CustomConfiguration extends AbstractConfiguration {
-
- /**
- * The name of the default configuration.
- */
- public static final String CONFIG_NAME = "Custom";
+final class CustomConfiguration extends AbstractConfiguration {
private final ListAppender appender = new ListAppender();
- public CustomConfiguration(final LoggerContext loggerContext) {
+ CustomConfiguration(final LoggerContext loggerContext) {
this(loggerContext, ConfigurationSource.NULL_SOURCE);
}
/**
* Constructor to create the default configuration.
*/
- public CustomConfiguration(final LoggerContext loggerContext, final
ConfigurationSource source) {
+ CustomConfiguration(final LoggerContext loggerContext, final
ConfigurationSource source) {
super(loggerContext, source);
- setName(CONFIG_NAME);
+ setName("Custom");
appender.start();
addAppender(appender);
final LoggerConfig root = getRootLogger();
@@ -72,9 +67,9 @@ public class CustomConfiguration extends
AbstractConfiguration {
appender.getEvents().clear();
}
- private static class ListAppender extends AbstractLifeCycle implements
Appender {
+ private static final class ListAppender extends AbstractLifeCycle
implements Appender {
- private final List<LogEvent> events =
Collections.<LogEvent>synchronizedList(new ArrayList<>());
+ private final List<LogEvent> events = Collections.synchronizedList(new
ArrayList<>());
@Override
public void append(final LogEvent event) {
diff --git
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfigurationFactory.java
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfigurationFactory.java
index 695a105045..ddbfe68ddc 100644
---
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfigurationFactory.java
+++
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CustomConfigurationFactory.java
@@ -29,7 +29,7 @@ import org.apache.logging.log4j.core.config.plugins.Plugin;
*/
@Plugin(name = "CustomConfigurationFactory", category =
ConfigurationFactory.CATEGORY)
@Order(50)
-public class CustomConfigurationFactory extends ConfigurationFactory {
+public final class CustomConfigurationFactory extends ConfigurationFactory {
/**
* Valid file extensions for XML files.
diff --git
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/equinox/EquinoxLoadApiBundleTest.java
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/EquinoxLoadApiBundleTest.java
similarity index 79%
rename from
log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/equinox/EquinoxLoadApiBundleTest.java
rename to
log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/EquinoxLoadApiBundleTest.java
index b373650a03..c4060353a8 100644
---
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/equinox/EquinoxLoadApiBundleTest.java
+++
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/EquinoxLoadApiBundleTest.java
@@ -14,19 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.osgi.tests.equinox;
+package org.apache.logging.log4j.osgi.tests;
-import org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest;
import org.eclipse.osgi.launch.EquinoxFactory;
-import org.osgi.framework.launch.FrameworkFactory;
/**
* Tests loading the Core bundle into an Eclipse Equinox OSGi container.
*/
public class EquinoxLoadApiBundleTest extends AbstractLoadBundleTest {
- @Override
- protected FrameworkFactory getFactory() {
- return new EquinoxFactory();
+ public EquinoxLoadApiBundleTest() {
+ super(new EquinoxFactory());
}
}
diff --git
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/felix/FelixLoadApiBundleTest.java
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/FelixLoadApiBundleTest.java
similarity index 76%
rename from
log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/felix/FelixLoadApiBundleTest.java
rename to
log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/FelixLoadApiBundleTest.java
index 59f29302ea..e82cebb094 100644
---
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/felix/FelixLoadApiBundleTest.java
+++
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/FelixLoadApiBundleTest.java
@@ -14,18 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.osgi.tests.felix;
+package org.apache.logging.log4j.osgi.tests;
-import org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest;
-import org.osgi.framework.launch.FrameworkFactory;
+import org.apache.felix.framework.FrameworkFactory;
/**
* Tests loading the Core bundle into an Apache Felix OSGi container.
*/
public class FelixLoadApiBundleTest extends AbstractLoadBundleTest {
- @Override
- protected FrameworkFactory getFactory() {
- return new org.apache.felix.framework.FrameworkFactory();
+ public FelixLoadApiBundleTest() {
+ super(new FrameworkFactory());
}
}
diff --git
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/junit/OsgiRule.java
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiRule.java
similarity index 94%
rename from
log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/junit/OsgiRule.java
rename to
log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiRule.java
index b965992fe3..7fa2cd6a7a 100644
---
a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/junit/OsgiRule.java
+++
b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/OsgiRule.java
@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.logging.log4j.osgi.tests.junit;
+package org.apache.logging.log4j.osgi.tests;
import java.util.HashMap;
import java.util.Map;
@@ -26,12 +26,13 @@ import org.osgi.framework.launch.FrameworkFactory;
/**
* JUnit rule to initialize and shutdown an OSGi framework.
*/
-public class OsgiRule extends ExternalResource {
+class OsgiRule extends ExternalResource {
private final FrameworkFactory factory;
+
private Framework framework;
- public OsgiRule(final FrameworkFactory factory) {
+ OsgiRule(final FrameworkFactory factory) {
this.factory = factory;
}