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