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.classic.net.server; 015 016import static org.junit.Assert.assertEquals; 017import static org.junit.Assert.assertNull; 018 019import java.io.ByteArrayInputStream; 020import java.io.ByteArrayOutputStream; 021import java.io.ObjectOutputStream; 022 023import org.junit.Before; 024import org.junit.Test; 025import org.slf4j.LoggerFactory; 026 027import ch.qos.logback.classic.Level; 028import ch.qos.logback.classic.Logger; 029import ch.qos.logback.classic.LoggerContext; 030import ch.qos.logback.classic.net.mock.MockAppender; 031import ch.qos.logback.classic.spi.ILoggingEvent; 032import ch.qos.logback.classic.spi.LoggingEvent; 033import ch.qos.logback.classic.spi.LoggingEventVO; 034 035/** 036 * Unit tests for {@link RemoteAppenderStreamClient}. 037 * 038 * @author Carl Harris 039 */ 040public class RemoteAppenderStreamClientTest { 041 042 private MockAppender appender; 043 private Logger logger; 044 private LoggingEvent event; 045 private RemoteAppenderStreamClient client; 046 047 @Before 048 public void setUp() throws Exception { 049 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); 050 051 appender = new MockAppender(); 052 appender.start(); 053 054 logger = lc.getLogger(getClass()); 055 logger.addAppender(appender); 056 057 event = new LoggingEvent(logger.getName(), logger, Level.DEBUG, "test message", null, new Object[0]); 058 059 LoggingEventVO eventVO = LoggingEventVO.build(event); 060 061 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 062 ObjectOutputStream oos = new ObjectOutputStream(bos); 063 oos.writeObject(eventVO); 064 oos.close(); 065 066 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 067 client = new RemoteAppenderStreamClient("some client ID", bis); 068 client.setLoggerContext(lc); 069 } 070 071 @Test 072 public void testWithEnabledLevel() throws Exception { 073 logger.setLevel(Level.DEBUG); 074 client.run(); 075 client.close(); 076 077 ILoggingEvent rcvdEvent = appender.getLastEvent(); 078 assertEquals(event.getLoggerName(), rcvdEvent.getLoggerName()); 079 assertEquals(event.getLevel(), rcvdEvent.getLevel()); 080 assertEquals(event.getMessage(), rcvdEvent.getMessage()); 081 } 082 083 @Test 084 public void testWithDisabledLevel() throws Exception { 085 logger.setLevel(Level.INFO); 086 client.run(); 087 client.close(); 088 assertNull(appender.getLastEvent()); 089 } 090 091}