16
16
*/
17
17
package org .apache .logging .log4j .web ;
18
18
19
+ import java .util .concurrent .atomic .AtomicReference ;
20
+
19
21
import jakarta .servlet .ServletContext ;
20
22
import jakarta .servlet .ServletContextEvent ;
21
23
22
24
import org .apache .logging .log4j .util .Strings ;
23
25
import org .junit .jupiter .api .BeforeEach ;
24
26
import org .junit .jupiter .api .Test ;
25
27
import org .junit .jupiter .api .extension .ExtendWith ;
28
+ import org .junit .jupiter .params .ParameterizedTest ;
29
+ import org .junit .jupiter .params .provider .ValueSource ;
26
30
import org .mockito .Mock ;
31
+ import org .mockito .Mock .Strictness ;
27
32
import org .mockito .junit .jupiter .MockitoExtension ;
28
33
29
- import static org .junit .jupiter .api .Assertions .*;
30
- import static org .mockito .BDDMockito .eq ;
34
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
35
+ import static org .mockito .AdditionalAnswers .answerVoid ;
36
+ import static org .mockito .ArgumentMatchers .any ;
37
+ import static org .mockito .ArgumentMatchers .eq ;
31
38
import static org .mockito .BDDMockito .given ;
32
39
import static org .mockito .BDDMockito .then ;
33
40
import static org .mockito .BDDMockito .willThrow ;
41
+ import static org .mockito .Mockito .doAnswer ;
34
42
35
43
@ ExtendWith (MockitoExtension .class )
36
44
public class Log4jServletContextListenerTest {
37
45
/* event and servletContext are marked lenient because they aren't used in the
38
46
* testDestroyWithNoInit but are only accessed during initialization
39
47
*/
40
- @ Mock (lenient = true )
48
+ @ Mock (strictness = Strictness . LENIENT )
41
49
private ServletContextEvent event ;
42
- @ Mock (lenient = true )
50
+ @ Mock (strictness = Strictness . LENIENT )
43
51
private ServletContext servletContext ;
44
52
@ Mock
45
53
private Log4jWebLifeCycle initializer ;
46
54
47
- private Log4jServletContextListener listener ;
55
+ private final AtomicReference < Object > count = new AtomicReference <>() ;
48
56
49
57
@ BeforeEach
50
58
public void setUp () {
51
- this .listener = new Log4jServletContextListener ();
52
59
given (event .getServletContext ()).willReturn (servletContext );
53
60
given (servletContext .getAttribute (Log4jWebSupport .SUPPORT_ATTRIBUTE )).willReturn (initializer );
54
- }
55
61
56
- @ Test
57
- public void testInitAndDestroy () throws Exception {
58
- this .listener .contextInitialized (this .event );
62
+ doAnswer (answerVoid ((k , v ) -> count .set (v )))
63
+ .when (servletContext )
64
+ .setAttribute (eq (Log4jServletContextListener .START_COUNT_ATTR ), any ());
65
+ doAnswer (__ -> count .get ())
66
+ .when (servletContext )
67
+ .getAttribute (Log4jServletContextListener .START_COUNT_ATTR );
68
+ }
59
69
60
- then (initializer ).should ().start ();
61
- then (initializer ).should ().setLoggerContext ();
70
+ @ ParameterizedTest
71
+ @ ValueSource (ints = { 1 , 2 , 3 })
72
+ public void testInitAndDestroy (final int listenerCount ) throws Exception {
73
+ final Log4jServletContextListener [] listeners = new Log4jServletContextListener [listenerCount ];
74
+ for (int idx = 0 ; idx < listenerCount ; idx ++) {
75
+ final Log4jServletContextListener listener = new Log4jServletContextListener ();
76
+ listeners [idx ] = listener ;
77
+
78
+ listener .contextInitialized (event );
79
+ if (idx == 0 ) {
80
+ then (initializer ).should ().start ();
81
+ then (initializer ).should ().setLoggerContext ();
82
+ } else {
83
+ then (initializer ).shouldHaveNoMoreInteractions ();
84
+ }
85
+ }
62
86
63
- this .listener .contextDestroyed (this .event );
87
+ for (int idx = listenerCount - 1 ; idx >= 0 ; idx --) {
88
+ final Log4jServletContextListener listener = listeners [idx ];
64
89
65
- then (initializer ).should ().clearLoggerContext ();
66
- then (initializer ).should ().stop ();
90
+ listener .contextDestroyed (event );
91
+ if (idx == 0 ) {
92
+ then (initializer ).should ().clearLoggerContext ();
93
+ then (initializer ).should ().stop ();
94
+ } else {
95
+ then (initializer ).shouldHaveNoMoreInteractions ();
96
+ }
97
+ }
67
98
}
68
99
69
100
@ Test
70
101
public void testInitFailure () throws Exception {
71
102
willThrow (new IllegalStateException (Strings .EMPTY )).given (initializer ).start ();
103
+ final Log4jServletContextListener listener = new Log4jServletContextListener ();
72
104
73
- try {
74
- this .listener .contextInitialized (this .event );
75
- fail ("Expected a RuntimeException." );
76
- } catch (final RuntimeException e ) {
77
- assertEquals ("Failed to initialize Log4j properly." , e .getMessage (), "The message is not correct." );
78
- }
105
+ assertThrows (RuntimeException .class , () -> listener .contextInitialized (this .event ),
106
+ "Failed to initialize Log4j properly." );
79
107
}
80
108
81
109
@ Test
@@ -93,17 +121,12 @@ public void initializingLog4jServletContextListenerShouldFaileWhenAutoShutdownIs
93
121
}
94
122
95
123
private void ensureInitializingFailsWhenAuthShutdownIsEnabled () {
96
- try {
97
- this .listener .contextInitialized (this .event );
98
- fail ("Expected a RuntimeException." );
99
- } catch (final RuntimeException e ) {
100
- final String expectedMessage =
101
- "Do not use " + Log4jServletContextListener .class .getSimpleName () + " when "
102
- + Log4jWebSupport .IS_LOG4J_AUTO_SHUTDOWN_DISABLED + " is true. Please use "
103
- + Log4jShutdownOnContextDestroyedListener .class .getSimpleName () + " instead of "
104
- + Log4jServletContextListener .class .getSimpleName () + "." ;
105
-
106
- assertEquals (expectedMessage , e .getMessage (), "The message is not correct" );
107
- }
124
+ final Log4jServletContextListener listener = new Log4jServletContextListener ();
125
+ final String message = "Do not use " + Log4jServletContextListener .class .getSimpleName () + " when "
126
+ + Log4jWebSupport .IS_LOG4J_AUTO_SHUTDOWN_DISABLED + " is true. Please use "
127
+ + Log4jShutdownOnContextDestroyedListener .class .getSimpleName () + " instead of "
128
+ + Log4jServletContextListener .class .getSimpleName () + "." ;
129
+
130
+ assertThrows (RuntimeException .class , () -> listener .contextInitialized (event ), message );
108
131
}
109
132
}
0 commit comments