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.status;
15  
16  import java.util.Iterator;
17  
18  import junit.framework.TestCase;
19  
20  public class StatusBaseTest extends TestCase {
21  
22      public void testAddStatus() {
23          {
24              InfoStatus status = new InfoStatus("testing", this);
25              status.add(new ErrorStatus("error", this));
26              Iterator<Status> it = status.iterator();
27              assertTrue("No status was added", it.hasNext());
28              assertTrue("hasChilden method reported wrong result", status.hasChildren());
29          }
30          {
31              InfoStatus status = new InfoStatus("testing", this);
32              try {
33                  status.add(null);
34                  fail("method should have thrown an Exception");
35              } catch (NullPointerException ex) {
36              }
37          }
38      }
39  
40      public void testRemoveStatus() {
41          {
42              InfoStatus status = new InfoStatus("testing", this);
43              ErrorStatus error = new ErrorStatus("error", this);
44              status.add(error);
45              boolean result = status.remove(error);
46              Iterator<Status> it = status.iterator();
47              assertTrue("Remove failed", result);
48              assertFalse("No status was removed", it.hasNext());
49              assertFalse("hasChilden method reported wrong result", status.hasChildren());
50          }
51          {
52              InfoStatus status = new InfoStatus("testing", this);
53              ErrorStatus error = new ErrorStatus("error", this);
54              status.add(error);
55              boolean result = status.remove(null);
56              assertFalse("Remove result was not false", result);
57          }
58      }
59  
60      public void testEffectiveLevel() {
61          {
62              // effective level = 0 level deep
63              ErrorStatus status = new ErrorStatus("error", this);
64              WarnStatus warn = new WarnStatus("warning", this);
65              status.add(warn);
66              assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.ERROR);
67          }
68  
69          {
70              // effective level = 1 level deep
71              InfoStatus status = new InfoStatus("info", this);
72              WarnStatus warn = new WarnStatus("warning", this);
73              status.add(warn);
74              assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.WARN);
75          }
76  
77          {
78              // effective level = 2 levels deep
79              InfoStatus status = new InfoStatus("info", this);
80              WarnStatus warn = new WarnStatus("warning", this);
81              ErrorStatus error = new ErrorStatus("error", this);
82              status.add(warn);
83              warn.add(error);
84              assertEquals("effective level misevaluated", status.getEffectiveLevel(), Status.ERROR);
85          }
86      }
87  
88  }