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  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import ch.qos.logback.core.status.OnConsoleStatusListener;
28  import ch.qos.logback.core.status.StatusBase;
29  import ch.qos.logback.core.status.StatusListener;
30  
31  import ch.qos.logback.core.status.StatusUtil;
32  import org.junit.jupiter.api.Test;
33  
34  import ch.qos.logback.core.status.ErrorStatus;
35  import ch.qos.logback.core.status.Status;
36  
37  public class BasicStatusManagerTest {
38  
39      BasicStatusManager bsm = new BasicStatusManager();
40  
41      @Test
42      public void smoke() {
43          bsm.add(new ErrorStatus("hello", this));
44          assertEquals(Status.ERROR, bsm.getLevel());
45  
46          List<Status> statusList = bsm.getCopyOfStatusList();
47          assertNotNull(statusList);
48          assertEquals(1, statusList.size());
49          assertEquals("hello", statusList.get(0).getMessage());
50      }
51  
52      @Test
53      public void many() {
54          int margin = 300;
55          int len = MAX_HEADER_COUNT + TAIL_SIZE + margin;
56          List<Status> witness = new ArrayList<Status>();
57          for (int i = 0; i < len; i++) {
58              Status s = new ErrorStatus("" + i, this);
59              bsm.add(s);
60              if(i < MAX_HEADER_COUNT) {
61                  witness.add(s);
62              }
63              if(i >= MAX_HEADER_COUNT + margin) {
64                  witness.add(s);
65              }
66          }
67  
68          List<Status> statusList = bsm.getCopyOfStatusList();
69          assertNotNull(statusList);
70          assertEquals(MAX_HEADER_COUNT + TAIL_SIZE, statusList.size());
71  
72          arrayDiff(witness, statusList);
73      }
74  
75      private void arrayDiff(List<Status> witness, List<Status> otherList) {
76          int witnessSize = witness.size();
77          int otherSize = otherList.size();
78          boolean diff = false;
79          for(int i = 0; i < witness.size(); i++) {
80  
81              Status w = witness.get(i);
82              Status o = otherList.get(i);
83              if(!w.equals(o)) {
84                  System.out.println("at "+i + " differs w.message=" + w.getMessage() + " and o.message=" +o.getMessage());
85                  String diffMsg = StatusUtil.diff(w, o);
86                  System.out.println(diffMsg);
87                  diff = true;
88              }
89          }
90  
91  
92          assertEquals(witnessSize, otherSize, "witnessSize="+witnessSize+" does not match resultSize="+otherSize);
93          assertFalse(diff, "diff detected");
94      }
95  
96      @Test
97      public void duplicateInstallationsOfOnConsoleListener() {
98          OnConsoleStatusListener sl0 = new OnConsoleStatusListener();
99          sl0.start();
100         OnConsoleStatusListener sl1 = new OnConsoleStatusListener();
101         sl1.start();
102 
103         assertTrue(bsm.add(sl0));
104 
105         {
106             List<StatusListener> listeners = bsm.getCopyOfStatusListenerList();
107             assertEquals(1, listeners.size());
108         }
109 
110         assertFalse(bsm.add(sl1));
111         {
112             List<StatusListener> listeners = bsm.getCopyOfStatusListenerList();
113             assertEquals(1, listeners.size());
114         }
115     }
116 
117 }