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.classic.net.server;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.ByteArrayOutputStream;
18  import java.io.ObjectOutputStream;
19  
20  import org.junit.jupiter.api.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.slf4j.LoggerFactory;
23  
24  import ch.qos.logback.classic.Level;
25  import ch.qos.logback.classic.Logger;
26  import ch.qos.logback.classic.LoggerContext;
27  import ch.qos.logback.classic.net.mock.MockAppender;
28  import ch.qos.logback.classic.spi.ILoggingEvent;
29  import ch.qos.logback.classic.spi.LoggingEvent;
30  import ch.qos.logback.classic.spi.LoggingEventVO;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertNull;
34  
35  /**
36   * Unit tests for {@link RemoteAppenderStreamClient}.
37   *
38   * @author Carl Harris
39   */
40  public class RemoteAppenderStreamClientTest {
41  
42      private MockAppender appender;
43      private Logger logger;
44      private LoggingEvent event;
45      private RemoteAppenderStreamClient client;
46  
47      @BeforeEach
48      public void setUp() throws Exception {
49          LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
50  
51          appender = new MockAppender();
52          appender.start();
53  
54          logger = lc.getLogger(getClass());
55          logger.addAppender(appender);
56  
57          event = new LoggingEvent(logger.getName(), logger, Level.DEBUG, "test message", null, new Object[0]);
58  
59          LoggingEventVO eventVO = LoggingEventVO.build(event);
60  
61          ByteArrayOutputStream bos = new ByteArrayOutputStream();
62          ObjectOutputStream oos = new ObjectOutputStream(bos);
63          oos.writeObject(eventVO);
64          oos.close();
65  
66          ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
67          client = new RemoteAppenderStreamClient("some client ID", bis);
68          client.setLoggerContext(lc);
69      }
70  
71      @Test
72      public void testWithEnabledLevel() throws Exception {
73          logger.setLevel(Level.DEBUG);
74          client.run();
75          client.close();
76  
77          ILoggingEvent rcvdEvent = appender.getLastEvent();
78          assertEquals(event.getLoggerName(), rcvdEvent.getLoggerName());
79          assertEquals(event.getLevel(), rcvdEvent.getLevel());
80          assertEquals(event.getMessage(), rcvdEvent.getMessage());
81      }
82  
83      @Test
84      public void testWithDisabledLevel() throws Exception {
85          logger.setLevel(Level.INFO);
86          client.run();
87          client.close();
88          assertNull(appender.getLastEvent());
89      }
90  
91  }