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.spi;
15  
16  import java.util.Iterator;
17  
18  import org.junit.jupiter.api.AfterEach;
19  import org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.BeforeEach;
21  
22  import ch.qos.logback.core.Appender;
23  import ch.qos.logback.core.helpers.NOPAppender;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * This test case verifies all the methods of AppenderAttableImpl work properly.
28   *
29   * @author Ralph Goers
30   */
31  public class AppenderAttachableImplTest {
32  
33      private AppenderAttachableImpl<TestEvent> aai;
34  
35      @BeforeEach
36      public void setUp() throws Exception {
37          aai = new AppenderAttachableImpl<TestEvent>();
38      }
39  
40      @AfterEach
41      public void tearDown() throws Exception {
42          aai = null;
43      }
44  
45      @Test
46      public void testAddAppender() throws Exception {
47          TestEvent event = new TestEvent();
48          NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
49          ta.start();
50          aai.addAppender(ta);
51          ta = new NOPAppender<TestEvent>();
52          ta.setName("test");
53          ta.start();
54          aai.addAppender(ta);
55          int size = aai.appendLoopOnAppenders(event);
56          Assertions.assertTrue(size == 2, "Incorrect number of appenders");
57      }
58  
59      @Test
60      public void testIteratorForAppenders() throws Exception {
61          NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
62          ta.start();
63          aai.addAppender(ta);
64          NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
65          tab.setName("test");
66          tab.start();
67          aai.addAppender(tab);
68          Iterator<Appender<TestEvent>> iter = aai.iteratorForAppenders();
69          int size = 0;
70          while (iter.hasNext()) {
71              ++size;
72              Appender<TestEvent> app = iter.next();
73              Assertions.assertTrue(app == ta || app == tab, "Bad Appender");
74          }
75          Assertions.assertTrue(size == 2, "Incorrect number of appenders");
76      }
77  
78      @Test
79      public void getGetAppender() throws Exception {
80          NOPAppender<TestEvent> test = new NOPAppender<TestEvent>();
81          test.setName("test");
82          test.start();
83          aai.addAppender(test);
84  
85          NOPAppender<TestEvent> testOther = new NOPAppender<TestEvent>();
86          testOther.setName("testOther");
87          testOther.start();
88          aai.addAppender(testOther);
89  
90          Appender<TestEvent> a = aai.getAppender("testOther");
91          Assertions.assertNotNull(a, "Could not find appender");
92          Assertions.assertTrue(a == testOther, "Wrong appender");
93  
94          a = aai.getAppender("test");
95          Assertions.assertNotNull(a, "Could not find appender");
96          Assertions.assertTrue(a == test, "Wrong appender");
97          a = aai.getAppender("NotThere");
98          Assertions.assertNull(a, "Appender was returned");
99      }
100 
101     @Test
102     public void testIsAttached() throws Exception {
103         NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
104         ta.start();
105         aai.addAppender(ta);
106         NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
107         tab.setName("test");
108         tab.start();
109         aai.addAppender(tab);
110         Assertions.assertTrue(aai.isAttached(ta), "Appender is not attached");
111         Assertions.assertTrue(aai.isAttached(tab), "Appender is not attached");
112     }
113 
114     @Test
115     public void testDetachAndStopAllAppenders() throws Exception {
116         NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
117         ta.start();
118         aai.addAppender(ta);
119         NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
120         tab.setName("test");
121         tab.start();
122         aai.addAppender(tab);
123         Assertions.assertTrue(tab.isStarted(), "Appender was not started");
124         aai.detachAndStopAllAppenders();
125         Assertions.assertNull(aai.getAppender("test"), "Appender was not removed");
126         Assertions.assertFalse(tab.isStarted(), "Appender was not stopped");
127     }
128 
129     @Test
130     public void testDetachAppender() throws Exception {
131         NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
132         ta.start();
133         aai.addAppender(ta);
134         NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
135         tab.setName("test");
136         tab.start();
137         aai.addAppender(tab);
138         Assertions.assertTrue(aai.detachAppender(tab),"Appender not detached");
139         Assertions.assertNull(aai.getAppender("test"), "Appender was not removed");
140         Assertions.assertFalse(aai.detachAppender(tab), "Appender detach error");
141     }
142 
143     @Test
144     public void testDetachAppenderByName() throws Exception {
145         NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
146         ta.setName("test1");
147         ta.start();
148         aai.addAppender(ta);
149         NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
150         tab.setName("test");
151         tab.start();
152         aai.addAppender(tab);
153 
154         Assertions.assertTrue(aai.detachAppender("test"));
155         Assertions.assertTrue(aai.detachAppender("test1"));
156         Assertions.assertFalse(aai.detachAppender("test1"));
157     }
158 
159     private static class TestEvent {
160 
161     }
162 
163 }