View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 v1.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  package ch.qos.logback.core;
15  
16  import static ch.qos.logback.core.BasicStatusManager.MAX_HEADER_COUNT;
17  import static ch.qos.logback.core.BasicStatusManager.TAIL_SIZE;
18  import static org.junit.Assert.*;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import ch.qos.logback.core.status.OnConsoleStatusListener;
24  import ch.qos.logback.core.status.StatusListener;
25  
26  import org.junit.Test;
27  
28  import ch.qos.logback.core.status.ErrorStatus;
29  import ch.qos.logback.core.status.Status;
30  
31  public class BasicStatusManagerTest {
32  
33      BasicStatusManager bsm = new BasicStatusManager();
34  
35      @Test
36      public void smoke() {
37          bsm.add(new ErrorStatus("hello", this));
38          assertEquals(Status.ERROR, bsm.getLevel());
39  
40          List<Status> statusList = bsm.getCopyOfStatusList();
41          assertNotNull(statusList);
42          assertEquals(1, statusList.size());
43          assertEquals("hello", statusList.get(0).getMessage());
44      }
45  
46      @Test
47      public void many() {
48          int margin = 300;
49          int len = MAX_HEADER_COUNT + TAIL_SIZE + margin;
50          for (int i = 0; i < len; i++) {
51              bsm.add(new ErrorStatus("" + i, this));
52          }
53  
54          List<Status> statusList = bsm.getCopyOfStatusList();
55          assertNotNull(statusList);
56          assertEquals(MAX_HEADER_COUNT + TAIL_SIZE, statusList.size());
57          List<Status> witness = new ArrayList<Status>();
58          for (int i = 0; i < MAX_HEADER_COUNT; i++) {
59              witness.add(new ErrorStatus("" + i, this));
60          }
61          for (int i = 0; i < TAIL_SIZE; i++) {
62              witness.add(new ErrorStatus("" + (MAX_HEADER_COUNT + margin + i), this));
63          }
64          assertEquals(witness, statusList);
65      }
66  
67      @Test
68      public void duplicateInstallationsOfOnConsoleListener() {
69          OnConsoleStatusListener sl0 = new OnConsoleStatusListener();
70          sl0.start();
71          OnConsoleStatusListener sl1 = new OnConsoleStatusListener();
72          sl1.start();
73  
74          assertTrue(bsm.add(sl0));
75  
76          {
77              List<StatusListener> listeners = bsm.getCopyOfStatusListenerList();
78              assertEquals(1, listeners.size());
79          }
80  
81          assertFalse(bsm.add(sl1));
82          {
83              List<StatusListener> listeners = bsm.getCopyOfStatusListenerList();
84              assertEquals(1, listeners.size());
85          }
86      }
87  
88  }