|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.status; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +import org.apache.logging.log4j.Level; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +class StatusLoggerLevelTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + void effective_level_should_be_the_least_specific_one() { |
| 30 | + |
| 31 | + // Verify the initial level |
| 32 | + final StatusLogger logger = StatusLogger.getLogger(); |
| 33 | + final Level fallbackListenerLevel = Level.ERROR; |
| 34 | + assertThat(logger.getLevel()).isEqualTo(fallbackListenerLevel); |
| 35 | + |
| 36 | + // Register a less specific listener |
| 37 | + final StatusListener listener1 = mock(StatusListener.class); |
| 38 | + Level listener1Level = Level.WARN; |
| 39 | + when(listener1.getStatusLevel()).thenReturn(listener1Level); |
| 40 | + logger.registerListener(listener1); |
| 41 | + assertThat(listener1Level).isNotEqualTo(fallbackListenerLevel); // Verify that the level is distinct |
| 42 | + assertThat(logger.getLevel()).isEqualTo(listener1Level); // Verify that the logger level is changed |
| 43 | + |
| 44 | + // Register a less specific listener |
| 45 | + final StatusListener listener2 = mock(StatusListener.class); |
| 46 | + final Level listener2Level = Level.INFO; |
| 47 | + when(listener2.getStatusLevel()).thenReturn(listener2Level); |
| 48 | + logger.registerListener(listener2); |
| 49 | + assertThat(listener2Level) |
| 50 | + .isNotEqualTo(fallbackListenerLevel) |
| 51 | + .isNotEqualTo(listener1Level); // Verify that the level is distinct |
| 52 | + assertThat(logger.getLevel()).isEqualTo(listener2Level); // Verify that the logger level is changed |
| 53 | + |
| 54 | + // Register a more specific listener |
| 55 | + final StatusListener listener3 = mock(StatusListener.class); |
| 56 | + final Level listener3Level = Level.ERROR; |
| 57 | + when(listener3.getStatusLevel()).thenReturn(listener3Level); |
| 58 | + logger.registerListener(listener3); |
| 59 | + assertThat(listener3Level) |
| 60 | + .isNotEqualTo(listener1Level) |
| 61 | + .isNotEqualTo(listener2Level); // Verify that the level is distinct |
| 62 | + assertThat(logger.getLevel()).isEqualTo(listener2Level); // Verify that the logger level is not changed |
| 63 | + |
| 64 | + // Update a registered listener level |
| 65 | + listener1Level = Level.DEBUG; |
| 66 | + when(listener1.getStatusLevel()).thenReturn(listener1Level); |
| 67 | + assertThat(listener1Level) // Verify that the level is distinct |
| 68 | + .isNotEqualTo(fallbackListenerLevel) |
| 69 | + .isNotEqualTo(listener2Level) |
| 70 | + .isNotEqualTo(listener3Level); |
| 71 | + assertThat(logger.getLevel()).isEqualTo(listener1Level); // Verify that the logger level is changed |
| 72 | + |
| 73 | + // Remove the least specific listener |
| 74 | + logger.removeListener(listener2); |
| 75 | + assertThat(logger.getLevel()).isEqualTo(listener1Level); // Verify that the level is changed |
| 76 | + |
| 77 | + // Remove the most specific listener |
| 78 | + logger.removeListener(listener3); |
| 79 | + assertThat(logger.getLevel()).isEqualTo(listener1Level); // Verify that the level is not changed |
| 80 | + |
| 81 | + // Remove the last listener |
| 82 | + logger.removeListener(listener1); |
| 83 | + assertThat(logger.getLevel()).isEqualTo(fallbackListenerLevel); // Verify that the level is changed |
| 84 | + } |
| 85 | +} |
0 commit comments