Blob Blame History Raw
From 4c962ce4c1356e94b66a5599a2c32c30eb3369e3 Mon Sep 17 00:00:00 2001
From: Mikolaj Izdebski <mizdebsk@redhat.com>
Date: Wed, 15 Dec 2021 17:26:22 +0100
Subject: [PATCH 1/2] Add test case for JNDI disablement

---
 tests/build.xml                               | 14 +++++++-
 tests/input/JNDI0.properties                  |  3 ++
 tests/input/JNDI1.properties                  |  4 +++
 .../java/org/apache/log4j/JNDITestCase.java   | 34 +++++++++++++++++++
 4 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 tests/input/JNDI0.properties
 create mode 100644 tests/input/JNDI1.properties
 create mode 100644 tests/src/java/org/apache/log4j/JNDITestCase.java

diff --git a/tests/build.xml b/tests/build.xml
index 74a7139d..7cac1424 100644
--- a/tests/build.xml
+++ b/tests/build.xml
@@ -38,6 +38,8 @@
   <property name="activation.jar" location="lib/mail.jar"/>
   <property name="junit.version" value="3.8.1"/>
   <property name="junit.jar" location="lib/junit.jar"/>
+  <property name="geronimo-jms.jar"
+         value="lib/jms.jar"/>
 
 
   <!-- Read the system environment variables and stores them in properties, -->
@@ -71,6 +73,7 @@
     <pathelement location="${junit.jar}"/>
     <pathelement location="${javamail.jar}"/>
     <pathelement location="${activation.jar}"/>
+    <pathelement location="${geronimo-jms.jar}"/>
     <pathelement path="${java.class.path}"/>
   </path>
 
@@ -158,7 +161,7 @@
   <!-- ================================================================= -->
   <!-- Run all tests                                                     -->
   <!-- ================================================================= -->  
-  <target name="runAll" depends="regression, longUnit"/>
+  <target name="runAll" depends="regression, longUnit, JNDI"/>
 
   <!-- ================================================================= -->
   <!-- Tests multiple parts of log4j. These tests are much more fragile  -->
@@ -262,6 +265,15 @@
     </junit>
   </target>
 
+  <target name="JNDI" depends="build">
+    <junit printsummary="yes" fork="yes" 
+        haltonfailure="${haltonfailure}"  dir="${basedir}">
+      <classpath refid="tests.classpath"/>
+      <formatter type="plain" usefile="false"/>
+      <test name="org.apache.log4j.JNDITestCase" />
+    </junit>
+  </target>
+
   <target name="DefaultInit" depends="build">
     <!-- Delete possible cruft from previous runs. -->
     <delete file="${basedir}/classes/log4j.xml"/>
diff --git a/tests/input/JNDI0.properties b/tests/input/JNDI0.properties
new file mode 100644
index 00000000..1fd92627
--- /dev/null
+++ b/tests/input/JNDI0.properties
@@ -0,0 +1,3 @@
+log4j.rootLogger=DEBUG, testAppender
+log4j.appender.testAppender=org.apache.log4j.net.JMSAppender
+log4j.appender.testAppender.TopicConnectionFactoryBindingName=foo
diff --git a/tests/input/JNDI1.properties b/tests/input/JNDI1.properties
new file mode 100644
index 00000000..28202fed
--- /dev/null
+++ b/tests/input/JNDI1.properties
@@ -0,0 +1,4 @@
+log4j.rootLogger=DEBUG, testAppender
+log4j.appender.testAppender=org.apache.log4j.net.JMSAppender
+log4j.appender.testAppender.TopicConnectionFactoryBindingName=foo
+log4j.appender.testAppender.Enabled=true
diff --git a/tests/src/java/org/apache/log4j/JNDITestCase.java b/tests/src/java/org/apache/log4j/JNDITestCase.java
new file mode 100644
index 00000000..6ef8ec90
--- /dev/null
+++ b/tests/src/java/org/apache/log4j/JNDITestCase.java
@@ -0,0 +1,34 @@
+package org.apache.log4j;
+
+import junit.framework.TestCase;
+
+/**
+ * Test for JNDI disablement.
+ *
+ * @author Mikolaj Izdebski
+ */
+public class JNDITestCase extends TestCase {
+
+  public JNDITestCase(String name) {
+    super(name);
+  }
+
+  public void tearDown() {
+    LogManager.resetConfiguration();
+  }
+
+  public void testDisabled() throws Exception {
+    Logger logger = Logger.getLogger(JNDITestCase.class);
+    try {
+      PropertyConfigurator.configure("input/JNDI0.properties");
+      fail("IllegalStateException exception was expected");
+    } catch (IllegalStateException e) {
+      assertTrue(e.getMessage().startsWith("JMS appender is disabled"));
+    }
+  }
+
+  public void testEnabled() throws Exception {
+    Logger logger = Logger.getLogger(JNDITestCase.class);
+    PropertyConfigurator.configure("input/JNDI1.properties");
+  }
+}
-- 
2.33.1