|
| 1 | +/* |
| 2 | + * Copyright 2012-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.logging.logback; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import ch.qos.logback.core.spi.LifeCycle; |
| 23 | +import ch.qos.logback.core.status.ErrorStatus; |
| 24 | +import ch.qos.logback.core.status.InfoStatus; |
| 25 | +import ch.qos.logback.core.status.Status; |
| 26 | +import ch.qos.logback.core.status.StatusListener; |
| 27 | +import ch.qos.logback.core.status.WarnStatus; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link FilteringStatusListener}. |
| 34 | + * |
| 35 | + * @author Dmytro Nosan |
| 36 | + */ |
| 37 | +class FilteringStatusListenerTests { |
| 38 | + |
| 39 | + private final DelegateStatusListener delegate = new DelegateStatusListener(); |
| 40 | + |
| 41 | + @Test |
| 42 | + void shouldFilterOutInfoStatus() { |
| 43 | + FilteringStatusListener listener = createListener(Status.WARN); |
| 44 | + InfoStatus info = new InfoStatus("info", getClass()); |
| 45 | + WarnStatus warn = new WarnStatus("warn", getClass()); |
| 46 | + ErrorStatus error = new ErrorStatus("error", getClass()); |
| 47 | + listener.addStatusEvent(info); |
| 48 | + listener.addStatusEvent(warn); |
| 49 | + listener.addStatusEvent(error); |
| 50 | + assertThat(this.delegate.getStatuses()).containsExactly(warn, error); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void shouldStartUnderlyingStatusListener() { |
| 55 | + FilteringStatusListener listener = createListener(Status.INFO); |
| 56 | + assertThat(this.delegate.isStarted()).isFalse(); |
| 57 | + listener.start(); |
| 58 | + assertThat(this.delegate.isStarted()).isTrue(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void shouldStopUnderlyingStatusListener() { |
| 63 | + FilteringStatusListener listener = createListener(); |
| 64 | + this.delegate.start(); |
| 65 | + assertThat(this.delegate.isStarted()).isTrue(); |
| 66 | + listener.stop(); |
| 67 | + assertThat(this.delegate.isStarted()).isFalse(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void shouldUseResetResistantValueFromUnderlyingStatusListener() { |
| 72 | + FilteringStatusListener listener = createListener(); |
| 73 | + assertThat(listener.isResetResistant()).isEqualTo(this.delegate.isResetResistant()); |
| 74 | + } |
| 75 | + |
| 76 | + private FilteringStatusListener createListener() { |
| 77 | + return new FilteringStatusListener(this.delegate, Status.INFO); |
| 78 | + } |
| 79 | + |
| 80 | + private FilteringStatusListener createListener(int levelThreshold) { |
| 81 | + return new FilteringStatusListener(this.delegate, levelThreshold); |
| 82 | + } |
| 83 | + |
| 84 | + private static final class DelegateStatusListener implements StatusListener, LifeCycle { |
| 85 | + |
| 86 | + private final List<Status> statuses = new ArrayList<>(); |
| 87 | + |
| 88 | + private boolean started = false; |
| 89 | + |
| 90 | + @Override |
| 91 | + public void addStatusEvent(Status status) { |
| 92 | + this.statuses.add(status); |
| 93 | + } |
| 94 | + |
| 95 | + List<Status> getStatuses() { |
| 96 | + return this.statuses; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean isResetResistant() { |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void start() { |
| 106 | + this.started = true; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void stop() { |
| 111 | + this.started = false; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean isStarted() { |
| 116 | + return this.started; |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments