| /* |
| * ========================================================================= |
| * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved. |
| * This product is protected by U.S. and international copyright |
| * and intellectual property laws. Pivotal products are covered by |
| * more patents listed at http://www.pivotal.io/patents. |
| * ========================================================================= |
| */ |
| |
| package com.gemstone.gemfire.internal.net; |
| |
| import static org.junit.Assert.*; |
| |
| import java.io.IOException; |
| import java.net.ServerSocket; |
| import java.net.Socket; |
| |
| import org.jmock.Expectations; |
| import org.jmock.Mockery; |
| import org.jmock.lib.legacy.ClassImposteriser; |
| import org.junit.After; |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.experimental.categories.Category; |
| |
| import com.gemstone.gemfire.test.junit.categories.UnitTest; |
| |
| /** |
| * The SocketUtilsJUnitTest class is a test suite of test cases testing the contract and functionality of the SocketUtils |
| * utility class. |
| * <p/> |
| * @author John Blum |
| * @see com.gemstone.gemfire.internal.net.SocketUtils |
| * @see org.jmock.Expectations |
| * @see org.jmock.Mockery |
| * @see org.junit.Assert |
| * @see org.junit.Test |
| * @since 7.0 |
| */ |
| @Category(UnitTest.class) |
| public class SocketUtilsJUnitTest { |
| |
| private Mockery mockContext; |
| |
| @Before |
| public void setup() { |
| mockContext = new Mockery() {{ |
| setImposteriser(ClassImposteriser.INSTANCE); |
| }}; |
| } |
| |
| @After |
| public void tearDown() { |
| mockContext.assertIsSatisfied(); |
| } |
| |
| @Test |
| public void testCloseSocket() throws IOException { |
| final Socket mockSocket = mockContext.mock(Socket.class, "closeSocketTest"); |
| |
| mockContext.checking(new Expectations() {{ |
| oneOf(mockSocket).close(); |
| }}); |
| |
| assertTrue(SocketUtils.close(mockSocket)); |
| } |
| |
| @Test |
| public void testCloseSocketThrowsIOException() throws IOException { |
| final Socket mockSocket = mockContext.mock(Socket.class, "closeSocketThrowsIOExceptionTest"); |
| |
| mockContext.checking(new Expectations() {{ |
| oneOf(mockSocket).close(); |
| will(throwException(new IOException("test"))); |
| }}); |
| |
| try { |
| assertFalse(SocketUtils.close(mockSocket)); |
| } |
| catch (Throwable t) { |
| fail("Calling close on a Socket using SocketUtils threw an unexpected Throwable (" + t + ")!"); |
| } |
| } |
| |
| @Test |
| public void testCloseSocketWithNull() { |
| assertTrue(SocketUtils.close((Socket) null)); |
| } |
| |
| @Test |
| public void testCloseServerSocket() throws IOException { |
| final ServerSocket mockServerSocket = mockContext.mock(ServerSocket.class, "closeServerSocketTest"); |
| |
| mockContext.checking(new Expectations() {{ |
| oneOf(mockServerSocket).close(); |
| }}); |
| |
| assertTrue(SocketUtils.close(mockServerSocket)); |
| } |
| |
| @Test |
| public void testCloseServerSocketThrowsIOException() throws IOException { |
| final ServerSocket mockServerSocket = mockContext.mock(ServerSocket.class, "closeServerSocketThrowsIOExceptionTest"); |
| |
| mockContext.checking(new Expectations() {{ |
| oneOf(mockServerSocket).close(); |
| will(throwException(new IOException("test"))); |
| }}); |
| |
| try { |
| assertFalse(SocketUtils.close(mockServerSocket)); |
| } |
| catch (Throwable t) { |
| fail("Calling close on a ServerSocket using SocketUtils threw an unexpected Throwable (" + t + ")!"); |
| } |
| } |
| |
| @Test |
| public void testCloseServerSocketWithNull() { |
| assertTrue(SocketUtils.close((ServerSocket) null)); |
| } |
| |
| } |