View Javadoc

1   // Copyright (C) 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.communication;
23  
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.IOException;
29  import java.net.InetAddress;
30  import java.net.Socket;
31  
32  import net.grinder.util.StandardTimeAuthority;
33  import net.grinder.util.TimeAuthority;
34  
35  import org.junit.AfterClass;
36  import org.junit.Before;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.mockito.Mock;
40  import org.mockito.MockitoAnnotations;
41  
42  
43  /**
44   *  Unit tests for {@link IdleAwareSocketWrapper}.
45   *
46   * @author Philip Aston
47   */
48  public class TestIdleAwareSocketWrapper {
49  
50    private static Acceptor s_acceptor;
51    private Socket m_socket;
52    @Mock private TimeAuthority m_timeAuthority;
53  
54    @BeforeClass public static void setUpAcceptor() throws Exception {
55      s_acceptor = new Acceptor("localhost", 0, 1, new StandardTimeAuthority());
56    }
57  
58    @AfterClass public static void shutDownAcceptor() throws Exception {
59      s_acceptor.shutdown();
60    }
61  
62    @Before public void createSocket() throws Exception {
63      MockitoAnnotations.initMocks(this);
64  
65      m_socket = new Socket(InetAddress.getByName(null), s_acceptor.getPort());
66    }
67  
68    @Test(expected=CommunicationException.class)
69    public void testConstructionWithBadSocket() throws Exception {
70      m_socket.close();
71      new IdleAwareSocketWrapper(m_socket, m_timeAuthority);
72    }
73  
74    @Test public void testHasDataNoData() throws Exception {
75      final IdleAwareSocketWrapper socketWrapper =
76          new IdleAwareSocketWrapper(m_socket, m_timeAuthority);
77  
78      assertFalse(socketWrapper.hasData(99));
79    }
80  
81    @Test(expected = IOException.class)
82    public void testHasDataSocketClosed() throws Exception {
83      final IdleAwareSocketWrapper socketWrapper =
84          new IdleAwareSocketWrapper(m_socket, m_timeAuthority);
85      socketWrapper.close();
86  
87      socketWrapper.hasData(99);
88    }
89  
90    @Test public void testHasDataTimeOut() throws Exception {
91  
92      final IdleAwareSocketWrapper socketWrapper =
93          new IdleAwareSocketWrapper(m_socket,
94                                     m_timeAuthority);
95  
96      when(m_timeAuthority.getTimeInMilliseconds())
97        .thenReturn(1000L)
98        .thenReturn(2000L);
99  
100     assertFalse(socketWrapper.hasData(123));
101     assertFalse(m_socket.isClosed());
102 
103     assertFalse(socketWrapper.hasData(123));
104     assertTrue(m_socket.isClosed());
105   }
106 }