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 static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertSame;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.IOException;
23  import java.net.ServerSocket;
24  
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import ch.qos.logback.core.net.mock.MockContext;
30  import ch.qos.logback.core.net.server.test.MockServerListener;
31  import ch.qos.logback.core.net.server.test.MockServerRunner;
32  import ch.qos.logback.core.net.server.test.ServerSocketUtil;
33  import ch.qos.logback.core.status.ErrorStatus;
34  import ch.qos.logback.core.status.Status;
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      @Before
53      public void setUp() throws Exception {
54          serverSocket = ServerSocketUtil.createServerSocket();
55          receiver = new InstrumentedServerSocketReceiver(serverSocket, listener, runner);
56          receiver.setContext(context);
57      }
58  
59      @After
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 }