1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  
15  package ch.qos.logback.classic.servlet;
16  
17  import static org.mockito.ArgumentMatchers.any;
18  import static org.mockito.Mockito.mock;
19  import static org.mockito.Mockito.times;
20  import static org.mockito.Mockito.verify;
21  import static org.mockito.Mockito.when;
22  
23  import jakarta.servlet.ServletContext;
24  import jakarta.servlet.ServletException;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  
29  import ch.qos.logback.core.CoreConstants;
30  import org.junit.jupiter.api.Test;
31  
32  public class LogbackServletContainerInitializerTest {
33  
34      LogbackServletContainerInitializer lsci = new LogbackServletContainerInitializer();
35  
36      @BeforeEach
37      public void setUp() throws Exception {
38      }
39  
40      @AfterEach
41      public void tearDown() throws Exception {
42      }
43  
44      @Test
45      public void testOnStartup() throws ServletException {
46          ServletContext mockedServletContext = mock(ServletContext.class);
47          lsci.onStartup(null, mockedServletContext);
48          verify(mockedServletContext).addListener(any(LogbackServletContextListener.class));
49      }
50  
51      @Test
52      public void noListenerShouldBeAddedWhenDisabled() throws ServletException {
53          ServletContext mockedServletContext = mock(ServletContext.class);
54          when(mockedServletContext.getInitParameter(CoreConstants.DISABLE_SERVLET_CONTAINER_INITIALIZER_KEY))
55                  .thenReturn("true");
56          lsci.onStartup(null, mockedServletContext);
57          verify(mockedServletContext, times(0)).addListener(any(LogbackServletContextListener.class));
58      }
59  
60  }