|
16 | 16 | */
|
17 | 17 | package org.apache.logging.log4j.core;
|
18 | 18 |
|
19 |
| -import static org.junit.jupiter.api.Assertions.assertFalse; |
20 |
| -import static org.junit.jupiter.api.Assertions.assertNull; |
| 19 | +import static org.apache.logging.log4j.core.util.ReflectionUtil.getFieldValue; |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.mockito.Mockito.CALLS_REAL_METHODS; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | +import static org.mockito.Mockito.withSettings; |
21 | 25 |
|
22 | 26 | import java.lang.reflect.Field;
|
| 27 | +import org.apache.logging.log4j.core.config.AbstractConfiguration; |
23 | 28 | import org.apache.logging.log4j.core.config.Configuration;
|
| 29 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
24 | 30 | import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
|
25 |
| -import org.apache.logging.log4j.core.util.ReflectionUtil; |
26 | 31 | import org.apache.logging.log4j.test.junit.SetTestProperty;
|
27 | 32 | import org.junit.jupiter.api.Test;
|
| 33 | +import org.junit.jupiter.api.TestInfo; |
28 | 34 |
|
29 | 35 | @SetTestProperty(key = "log4j2.isWebapp", value = "false")
|
30 |
| -@LoggerContextSource("log4j-test3.xml") |
31 | 36 | class ShutdownDisabledTest {
|
32 | 37 |
|
| 38 | + private static final Field shutdownCallbackField; |
| 39 | + |
| 40 | + static { |
| 41 | + try { |
| 42 | + shutdownCallbackField = LoggerContext.class.getDeclaredField("shutdownCallback"); |
| 43 | + } catch (NoSuchFieldException e) { |
| 44 | + throw new RuntimeException(e); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + @LoggerContextSource("log4j-test3.xml") |
| 50 | + void testShutdownFlag(final Configuration config, final LoggerContext ctx) { |
| 51 | + assertThat(config.isShutdownHookEnabled()) |
| 52 | + .as("Shutdown hook is enabled") |
| 53 | + .isFalse(); |
| 54 | + assertThat(getFieldValue(shutdownCallbackField, ctx)) |
| 55 | + .as("Shutdown callback") |
| 56 | + .isNull(); |
| 57 | + } |
| 58 | + |
33 | 59 | @Test
|
34 |
| - void testShutdownFlag(final Configuration config, final LoggerContext ctx) throws NoSuchFieldException { |
35 |
| - Field shutdownCallback = LoggerContext.class.getDeclaredField("shutdownCallback"); |
36 |
| - Object fieldValue = ReflectionUtil.getFieldValue(shutdownCallback, ctx); |
37 |
| - assertFalse(config.isShutdownHookEnabled(), "Shutdown hook is enabled"); |
38 |
| - assertNull(fieldValue, "Shutdown callback"); |
| 60 | + void whenLoggerContextInitialized_respectsShutdownDisabled(TestInfo testInfo) { |
| 61 | + Configuration configuration = mockConfiguration(); |
| 62 | + when(configuration.isShutdownHookEnabled()).thenReturn(false); |
| 63 | + try (final LoggerContext ctx = new LoggerContext(testInfo.getDisplayName())) { |
| 64 | + ctx.start(configuration); |
| 65 | + assertThat(ctx.isStarted()).isTrue(); |
| 66 | + assertThat(ctx.getConfiguration()).isSameAs(configuration); |
| 67 | + assertThat(getFieldValue(shutdownCallbackField, ctx)) |
| 68 | + .as("Shutdown callback") |
| 69 | + .isNull(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void whenLoggerContextStarted_ignoresShutdownDisabled(TestInfo testInfo) { |
| 75 | + // Traditional behavior: during reconfiguration, the shutdown hook is not removed. |
| 76 | + Configuration initialConfiguration = mockConfiguration(); |
| 77 | + when(initialConfiguration.isShutdownHookEnabled()).thenReturn(true); |
| 78 | + Configuration configuration = mockConfiguration(); |
| 79 | + when(configuration.isShutdownHookEnabled()).thenReturn(false); |
| 80 | + try (final LoggerContext ctx = new LoggerContext(testInfo.getDisplayName())) { |
| 81 | + ctx.start(initialConfiguration); |
| 82 | + assertThat(ctx.isStarted()).isTrue(); |
| 83 | + Object shutdownCallback = getFieldValue(shutdownCallbackField, ctx); |
| 84 | + assertThat(shutdownCallback).as("Shutdown callback").isNotNull(); |
| 85 | + ctx.start(configuration); |
| 86 | + assertThat(getFieldValue(shutdownCallbackField, ctx)) |
| 87 | + .as("Shutdown callback") |
| 88 | + .isSameAs(shutdownCallback); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static Configuration mockConfiguration() { |
| 93 | + return mock( |
| 94 | + AbstractConfiguration.class, |
| 95 | + withSettings() |
| 96 | + .useConstructor(null, ConfigurationSource.NULL_SOURCE) |
| 97 | + .defaultAnswer(CALLS_REAL_METHODS)); |
39 | 98 | }
|
40 | 99 | }
|
0 commit comments