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.core.util;
16  
17  import java.util.List;
18  
19  import org.junit.jupiter.api.AfterEach;
20  import org.junit.jupiter.api.BeforeEach;
21  
22  import ch.qos.logback.core.Context;
23  import ch.qos.logback.core.ContextBase;
24  import ch.qos.logback.core.status.OnConsoleStatusListener;
25  import ch.qos.logback.core.status.StatusListener;
26  import ch.qos.logback.core.status.StatusManager;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  public class StatusListenerConfigHelperTest {
34  
35      Context context = new ContextBase();
36      StatusManager sm = context.getStatusManager();
37  
38      @BeforeEach
39      public void setUp() throws Exception {
40      }
41  
42      @AfterEach
43      public void tearDown() throws Exception {
44      }
45  
46      @Test
47      public void addOnConsoleListenerInstanceShouldNotStartSecondListener() {
48          OnConsoleStatusListener ocl0 = new OnConsoleStatusListener();
49          OnConsoleStatusListener ocl1 = new OnConsoleStatusListener();
50  
51          StatusListenerConfigHelper.addOnConsoleListenerInstance(context, ocl0);
52          {
53              List<StatusListener> listeners = sm.getCopyOfStatusListenerList();
54              assertEquals(1, listeners.size());
55              assertTrue(ocl0.isStarted());
56          }
57  
58          // second listener should not have been started
59          StatusListenerConfigHelper.addOnConsoleListenerInstance(context, ocl1);
60          {
61              List<StatusListener> listeners = sm.getCopyOfStatusListenerList();
62              assertEquals(1, listeners.size());
63              assertFalse(ocl1.isStarted());
64          }
65      }
66  
67  }