View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    * <p>
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    * <p>
9    * or (per the licensee's choosing)
10   * <p>
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 org.junit.jupiter.api.Assertions;
19  import org.junit.jupiter.api.Test;
20  
21  public class StatusBaseTest {
22  
23  
24      @Test
25      public void testAddStatus() {
26          {
27              InfoStatus status = new InfoStatus("testing", this);
28              status.add(new ErrorStatus("error", this));
29              Iterator<Status> it = status.iterator();
30              Assertions.assertTrue(it.hasNext(), "No status was added");
31              Assertions.assertTrue(status.hasChildren(), "hasChilden method reported wrong result");
32          }
33          {
34              InfoStatus status = new InfoStatus("testing", this);
35              try {
36                  status.add(null);
37                  Assertions.fail("method should have thrown an Exception");
38              } catch (NullPointerException ex) {
39              }
40          }
41      }
42  
43      @Test
44      public void testRemoveStatus() {
45          {
46              InfoStatus status = new InfoStatus("testing", this);
47              ErrorStatus error = new ErrorStatus("error", this);
48              status.add(error);
49              boolean result = status.remove(error);
50              Iterator<Status> it = status.iterator();
51              Assertions.assertTrue(result, "Remove failed");
52              Assertions.assertFalse(it.hasNext(), "No status was removed");
53              Assertions.assertFalse(status.hasChildren(), "hasChilden method reported wrong result");
54          }
55          {
56              InfoStatus status = new InfoStatus("testing", this);
57              ErrorStatus error = new ErrorStatus("error", this);
58              status.add(error);
59              boolean result = status.remove(null);
60              Assertions.assertFalse(result, "Remove result was not false");
61          }
62      }
63  
64      public void testEffectiveLevel() {
65          {
66              // effective level = 0 level deep
67              ErrorStatus status = new ErrorStatus("error", this);
68              WarnStatus warn = new WarnStatus("warning", this);
69              status.add(warn);
70              Assertions.assertEquals(status.getEffectiveLevel(), Status.ERROR, "effective level misevaluated");
71          }
72  
73          {
74              // effective level = 1 level deep
75              InfoStatus status = new InfoStatus("info", this);
76              WarnStatus warn = new WarnStatus("warning", this);
77              status.add(warn);
78              Assertions.assertEquals(status.getEffectiveLevel(), Status.WARN, "effective level misevaluated");
79          }
80  
81          {
82              // effective level = 2 levels deep
83              InfoStatus status = new InfoStatus("info", this);
84              WarnStatus warn = new WarnStatus("warning", this);
85              ErrorStatus error = new ErrorStatus("error", this);
86              status.add(warn);
87              warn.add(error);
88              Assertions.assertEquals(status.getEffectiveLevel(), Status.ERROR, "effective level misevaluated");
89          }
90      }
91  
92  }