1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.spi;
11  
12  
13  import static org.junit.Assert.*;
14  import ch.qos.logback.core.appender.NOPAppender;
15  import ch.qos.logback.core.Appender;
16  import ch.qos.logback.core.layout.NopLayout;
17  
18  import java.util.Iterator;
19  
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  
24  /**
25   * This test case verifies all the methods of AppenderAttableImpl work properly.
26   *
27   * @author Ralph Goers
28   */
29  public class AppenderAttachableImplTest {
30  
31    
32    private AppenderAttachableImpl<TestEvent> aai;
33  
34    @Before
35    public void setUp() throws Exception {
36      aai = new AppenderAttachableImpl<TestEvent>();
37    }
38  
39    @After
40    public void tearDown() throws Exception {
41      aai = null;
42    }
43  
44    @Test
45    public void testAddAppender() throws Exception {
46      TestEvent event = new TestEvent();
47      NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
48      ta.setLayout(new NopLayout<TestEvent>());
49      ta.start();
50      aai.addAppender(ta);
51      ta = new NOPAppender<TestEvent>();
52      ta.setName("test");
53      ta.setLayout(new NopLayout<TestEvent>());
54      ta.start();
55      aai.addAppender(ta);
56      int size = aai.appendLoopOnAppenders(event);
57      assertTrue("Incorrect number of appenders", size == 2);
58    }
59  
60    @Test
61    public void testIteratorForAppenders() throws Exception {
62      NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
63      ta.setLayout(new NopLayout<TestEvent>());
64      ta.start();
65      aai.addAppender(ta);
66      NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
67      tab.setName("test");
68      tab.setLayout(new NopLayout<TestEvent>());
69      tab.start();
70      aai.addAppender(tab);
71      Iterator<Appender<TestEvent>> iter = aai.iteratorForAppenders();
72      int size = 0;
73      while (iter.hasNext()) {
74        ++size;
75        Appender<TestEvent> app = iter.next();
76        assertTrue("Bad Appender", app == ta || app == tab);
77      }
78      assertTrue("Incorrect number of appenders", size == 2);
79    }
80  
81    @Test
82    public void getGetAppender() throws Exception {
83      NOPAppender<TestEvent> test = new NOPAppender<TestEvent>();
84      test.setLayout(new NopLayout<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.setLayout(new NopLayout<TestEvent>());
92      testOther.start();
93      aai.addAppender(testOther);
94      
95      Appender a = aai.getAppender("testOther");
96      assertNotNull("Could not find appender", a);
97      assertTrue("Wrong appender", a == testOther);
98      
99      a = aai.getAppender("test");
100     assertNotNull("Could not find appender", a);
101     assertTrue("Wrong appender", a == test);
102     a = aai.getAppender("NotThere");
103     assertNull("Appender was returned", a);
104   }
105 
106   @Test
107   public void testIsAttached() throws Exception {
108     NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
109     ta.setLayout(new NopLayout<TestEvent>());
110     ta.start();
111     aai.addAppender(ta);
112     NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
113     tab.setName("test");
114     tab.setLayout(new NopLayout<TestEvent>());
115     tab.start();
116     aai.addAppender(tab);
117     assertTrue("Appender is not attached", aai.isAttached(ta));
118     assertTrue("Appender is not attached", aai.isAttached(tab));
119   }
120 
121   @Test
122   public void testDetachAndStopAllAppenders() throws Exception {
123     NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
124     ta.setLayout(new NopLayout<TestEvent>());
125     ta.start();
126     aai.addAppender(ta);
127     NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
128     tab.setName("test");
129     tab.setLayout(new NopLayout<TestEvent>());
130     tab.start();
131     aai.addAppender(tab);
132     assertTrue("Appender was not started", tab.isStarted());
133     aai.detachAndStopAllAppenders();
134     assertNull("Appender was not removed", aai.getAppender("test"));
135     assertFalse("Appender was not stopped", tab.isStarted());
136   }
137 
138   @Test
139   public void testDetachAppender() throws Exception {
140     NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
141     ta.setLayout(new NopLayout<TestEvent>());
142     ta.start();
143     aai.addAppender(ta);
144     NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
145     tab.setName("test");
146     tab.setLayout(new NopLayout<TestEvent>());
147     tab.start();
148     aai.addAppender(tab);
149     assertTrue("Appender not detached", aai.detachAppender(tab));
150     assertNull("Appender was not removed", aai.getAppender("test"));
151     assertFalse("Appender detach error", aai.detachAppender(tab));
152   }
153 
154   @Test
155   public void testDetachAppenderByName() throws Exception {
156     NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>();
157     ta.setName("test1");
158     ta.setLayout(new NopLayout<TestEvent>());
159     ta.start();
160     aai.addAppender(ta);
161     NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>();
162     tab.setName("test");
163     tab.setLayout(new NopLayout<TestEvent>());
164     tab.start();
165     aai.addAppender(tab);
166    
167     assertTrue(aai.detachAppender("test"));
168     assertTrue(aai.detachAppender("test1"));
169     assertFalse( aai.detachAppender("test1"));
170   }
171 
172   private static class TestEvent {
173 
174   }
175 
176 }
177