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.core.net.server;
15  
16  
17  import java.io.IOException;
18  import java.net.ServerSocket;
19  
20  import org.junit.jupiter.api.AfterEach;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  import ch.qos.logback.core.net.mock.MockContext;
26  import ch.qos.logback.core.net.server.test.MockServerListener;
27  import ch.qos.logback.core.net.server.test.MockServerRunner;
28  import ch.qos.logback.core.net.server.test.ServerSocketUtil;
29  import ch.qos.logback.core.status.ErrorStatus;
30  import ch.qos.logback.core.status.Status;
31  
32  /**
33   * Unit tests for {@link AbstractServerSocketAppender}.
34   *
35   * @author Carl Harris
36   */
37  public class AbstractServerSocketAppenderTest {
38  
39      private MockContext context = new MockContext();
40  
41      private MockServerRunner<RemoteReceiverClient> runner = new MockServerRunner<RemoteReceiverClient>();
42  
43      private MockServerListener<RemoteReceiverClient> listener = new MockServerListener<RemoteReceiverClient>();
44  
45      private ServerSocket serverSocket;
46      private InstrumentedServerSocketAppenderBase appender;
47  
48      @BeforeEach
49      public void setUp() throws Exception {
50          serverSocket = ServerSocketUtil.createServerSocket();
51          appender = new InstrumentedServerSocketAppenderBase(serverSocket, listener, runner);
52          appender.setContext(context);
53      }
54  
55      @AfterEach
56      public void tearDown() throws Exception {
57          serverSocket.close();
58      }
59  
60      @Test
61      public void testStartStop() throws Exception {
62          appender.start();
63          Assertions.assertTrue(runner.isContextInjected());
64          Assertions.assertTrue(runner.isRunning());
65          Assertions.assertSame(listener, appender.getLastListener());
66  
67          appender.stop();
68          Assertions.assertFalse(runner.isRunning());
69      }
70  
71      @Test
72      public void testStartWhenAlreadyStarted() throws Exception {
73          appender.start();
74          appender.start();
75          Assertions.assertEquals(1, runner.getStartCount());
76      }
77  
78      @Test
79      public void testStopThrowsException() throws Exception {
80          appender.start();
81          Assertions.assertTrue(appender.isStarted());
82          IOException ex = new IOException("test exception");
83          runner.setStopException(ex);
84          appender.stop();
85  
86          Status status = context.getLastStatus();
87          Assertions.assertNotNull(status);
88          Assertions.assertTrue(status instanceof ErrorStatus);
89          Assertions.assertTrue(status.getMessage().contains(ex.getMessage()));
90          Assertions.assertSame(ex, status.getThrowable());
91      }
92  
93      @Test
94      public void testStopWhenNotStarted() throws Exception {
95          appender.stop();
96          Assertions.assertEquals(0, runner.getStartCount());
97      }
98  
99  }