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