001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.spi; 015 016import static org.junit.Assert.assertFalse; 017import static org.junit.Assert.assertNotNull; 018import static org.junit.Assert.assertNull; 019import static org.junit.Assert.assertTrue; 020 021import java.util.Iterator; 022 023import org.junit.After; 024import org.junit.Before; 025import org.junit.Test; 026 027import ch.qos.logback.core.Appender; 028import ch.qos.logback.core.helpers.NOPAppender; 029 030/** 031 * This test case verifies all the methods of AppenderAttableImpl work properly. 032 * 033 * @author Ralph Goers 034 */ 035public class AppenderAttachableImplTest { 036 037 private AppenderAttachableImpl<TestEvent> aai; 038 039 @Before 040 public void setUp() throws Exception { 041 aai = new AppenderAttachableImpl<TestEvent>(); 042 } 043 044 @After 045 public void tearDown() throws Exception { 046 aai = null; 047 } 048 049 @Test 050 public void testAddAppender() throws Exception { 051 TestEvent event = new TestEvent(); 052 NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>(); 053 ta.start(); 054 aai.addAppender(ta); 055 ta = new NOPAppender<TestEvent>(); 056 ta.setName("test"); 057 ta.start(); 058 aai.addAppender(ta); 059 int size = aai.appendLoopOnAppenders(event); 060 assertTrue("Incorrect number of appenders", size == 2); 061 } 062 063 @Test 064 public void testIteratorForAppenders() throws Exception { 065 NOPAppender<TestEvent> ta = new NOPAppender<TestEvent>(); 066 ta.start(); 067 aai.addAppender(ta); 068 NOPAppender<TestEvent> tab = new NOPAppender<TestEvent>(); 069 tab.setName("test"); 070 tab.start(); 071 aai.addAppender(tab); 072 Iterator<Appender<TestEvent>> iter = aai.iteratorForAppenders(); 073 int size = 0; 074 while (iter.hasNext()) { 075 ++size; 076 Appender<TestEvent> app = iter.next(); 077 assertTrue("Bad Appender", app == ta || app == tab); 078 } 079 assertTrue("Incorrect number of appenders", size == 2); 080 } 081 082 @Test 083 public void getGetAppender() throws Exception { 084 NOPAppender<TestEvent> test = new NOPAppender<TestEvent>(); 085 test.setName("test"); 086 test.start(); 087 aai.addAppender(test); 088 089 NOPAppender<TestEvent> testOther = new NOPAppender<TestEvent>(); 090 testOther.setName("testOther"); 091 testOther.start(); 092 aai.addAppender(testOther); 093 094 Appender<TestEvent> a = aai.getAppender("testOther"); 095 assertNotNull("Could not find appender", a); 096 assertTrue("Wrong appender", a == testOther); 097 098 a = aai.getAppender("test"); 099 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}