001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.net.server; 015 016import static org.junit.Assert.assertEquals; 017import static org.junit.Assert.assertFalse; 018import static org.junit.Assert.assertNotNull; 019import static org.junit.Assert.assertSame; 020import static org.junit.Assert.assertTrue; 021 022import java.io.IOException; 023import java.net.ServerSocket; 024 025import org.junit.After; 026import org.junit.Before; 027import org.junit.Test; 028 029import ch.qos.logback.core.net.mock.MockContext; 030import ch.qos.logback.core.net.server.test.MockServerListener; 031import ch.qos.logback.core.net.server.test.MockServerRunner; 032import ch.qos.logback.core.net.server.test.ServerSocketUtil; 033import ch.qos.logback.core.status.ErrorStatus; 034import ch.qos.logback.core.status.Status; 035 036/** 037 * Unit tests for {@link ServerSocketReceiver}. 038 * 039 * @author Carl Harris 040 */ 041public class ServerSocketReceiverTest { 042 043 private MockContext context = new MockContext(); 044 045 private MockServerRunner<RemoteAppenderClient> runner = new MockServerRunner<RemoteAppenderClient>(); 046 047 private MockServerListener<RemoteAppenderClient> listener = new MockServerListener<RemoteAppenderClient>(); 048 049 private ServerSocket serverSocket; 050 private InstrumentedServerSocketReceiver receiver; 051 052 @Before 053 public void setUp() throws Exception { 054 serverSocket = ServerSocketUtil.createServerSocket(); 055 receiver = new InstrumentedServerSocketReceiver(serverSocket, listener, runner); 056 receiver.setContext(context); 057 } 058 059 @After 060 public void tearDown() throws Exception { 061 serverSocket.close(); 062 } 063 064 @Test 065 public void testStartStop() throws Exception { 066 receiver.start(); 067 assertTrue(runner.isContextInjected()); 068 assertTrue(runner.isRunning()); 069 assertSame(listener, receiver.getLastListener()); 070 071 receiver.stop(); 072 assertFalse(runner.isRunning()); 073 } 074 075 @Test 076 public void testStartWhenAlreadyStarted() throws Exception { 077 receiver.start(); 078 receiver.start(); 079 assertEquals(1, runner.getStartCount()); 080 } 081 082 @Test 083 public void testStopThrowsException() throws Exception { 084 receiver.start(); 085 assertTrue(receiver.isStarted()); 086 IOException ex = new IOException("test exception"); 087 runner.setStopException(ex); 088 receiver.stop(); 089 090 Status status = context.getLastStatus(); 091 assertNotNull(status); 092 assertTrue(status instanceof ErrorStatus); 093 assertTrue(status.getMessage().contains(ex.getMessage())); 094 assertSame(ex, status.getThrowable()); 095 } 096 097 @Test 098 public void testStopWhenNotStarted() throws Exception { 099 receiver.stop(); 100 assertEquals(0, runner.getStartCount()); 101 } 102 103}