ramanathan1504 commented on code in PR #4070:
URL: https://github.com/apache/logging-log4j2/pull/4070#discussion_r3330202372


##########
log4j-jakarta-web/src/test/java/org/apache/logging/log4j/web/WebLookupTest.java:
##########
@@ -94,5 +112,63 @@ public class WebLookupTest {
     //        initializer.stop();
     //        ContextAnchor.THREAD_CONTEXT.remove();
     //    }
+    /**
+     * Regression test for GitHub issue #2351:
+     * "Missing servlet context in web lookup when using composite 
configuration".
+     *
+     * When log4jConfiguration contains a comma-separated list of config files,
+     * the resulting composite LoggerContext must still expose the 
ServletContext
+     * via WebLoggerContextUtils.getServletContext() so that ${web:*} lookups 
resolve.
+     */
+    @Test
+    void testCompositeConfigurationServletContextName() throws Exception {
+        ContextAnchor.THREAD_CONTEXT.remove();
+
+        final String expectedServletContextName = "CompositeTest";
+
+        // Use Mockito to create a minimal ServletContext (no Spring 
dependency needed)
+        final ServletContext servletContext = mock(ServletContext.class);
+        
when(servletContext.getServletContextName()).thenReturn(expectedServletContextName);
+        when(servletContext.getContextPath()).thenReturn("/composite-test");
+        // Composite configuration: two comma-separated config files
+        
when(servletContext.getInitParameter(Log4jWebSupport.LOG4J_CONFIG_LOCATION))
+                .thenReturn("log4j2-combined.xml,log4j2-override.xml");
+        // Let the initializer resolve each file via the servlet context 
resource lookup
+        when(servletContext.getResource("log4j2-combined.xml"))
+                .thenReturn(getClass().getResource("/log4j2-combined.xml"));
+        when(servletContext.getResource("log4j2-override.xml"))
+                .thenReturn(getClass().getResource("/log4j2-override.xml"));
+
+        final Log4jWebLifeCycle initializer = 
WebLoggerContextUtils.getWebLifeCycle(servletContext);
+        try {
+            initializer.start();
+            initializer.setLoggerContext();
+
+            final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
+            assertNotNull(ctx, "No LoggerContext");
+
+            // The servlet context MUST be reachable via the web lookup for 
composite config.
+            // Before the fix this returns null, breaking all ${web:*} lookups.
+            assertNotNull(
+                    WebLoggerContextUtils.getServletContext(),
+                    "ServletContext is null in composite configuration - "
+                            + "${web:*} lookups will not resolve (issue 
#2351)");
+
+            final Configuration config = ctx.getConfiguration();
+            assertNotNull(config, "No Configuration");
+
+            final StrSubstitutor substitutor = config.getStrSubstitutor();
+            assertNotNull(substitutor, "No StrSubstitutor");

Review Comment:
   ```java 
   assertInstanceOf(CompositeConfiguration.class,config,"Expected 
CompositeConfiguration for comma-separated log4jConfiguration");
   ```
   Updated        



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to