1
2
3
4
5
6
7
8
9
10
11
12
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
37
38
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 }