View Javadoc

1   // Copyright (C) 2007 - 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.console.client;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertNull;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  import static org.mockito.Matchers.isA;
30  import static org.mockito.Mockito.verify;
31  
32  import java.io.InputStream;
33  import java.io.ObjectInputStream;
34  import java.util.concurrent.ScheduledExecutorService;
35  import java.util.concurrent.TimeUnit;
36  
37  import net.grinder.communication.CommunicationException;
38  import net.grinder.communication.ConnectionType;
39  import net.grinder.communication.KeepAliveMessage;
40  import net.grinder.communication.SocketAcceptorThread;
41  
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.mockito.ArgumentCaptor;
45  import org.mockito.Captor;
46  import org.mockito.Mock;
47  import org.mockito.MockitoAnnotations;
48  
49  
50  /**
51   * Unit tests for {@link ConsoleConnectionFactory}.
52  *
53   * @author Philip Aston
54   */
55  public class TestConsoleConnectionFactory {
56  
57    @Mock private ScheduledExecutorService m_scheduler;
58    @Captor private ArgumentCaptor<Runnable> m_runnableCaptor;
59  
60    @Before public void setUp() {
61      MockitoAnnotations.initMocks(this);
62    }
63  
64    @Test public void testConnect() throws Exception {
65      final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create();
66  
67      final ConsoleConnection consoleConnection =
68        new ConsoleConnectionFactory().connect(socketAcceptor.getHostName(),
69                                               socketAcceptor.getPort());
70  
71      assertNotNull(consoleConnection);
72  
73      socketAcceptor.close();
74  
75      final InputStream socketInput =
76        socketAcceptor.getAcceptedSocket().getInputStream();
77  
78      final ObjectInputStream objectInputStream =
79        new ObjectInputStream(socketInput);
80  
81      assertEquals(ConnectionType.CONSOLE_CLIENT, objectInputStream.readObject());
82    }
83  
84    @Test(expected = ConsoleConnectionException.class)
85    public void testConnectBadAcceptor() throws Exception {
86  
87      final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create();
88  
89      final ConsoleConnectionFactory consoleConnectionFactory =
90          new ConsoleConnectionFactory();
91  
92      final ConsoleConnection consoleConnection =
93        consoleConnectionFactory.connect(
94          socketAcceptor.getHostName(), socketAcceptor.getPort());
95  
96      assertNotNull(consoleConnection);
97  
98      socketAcceptor.close();
99  
100     consoleConnectionFactory.connect(socketAcceptor.getHostName(),
101                                      socketAcceptor.getPort());
102   }
103 
104   @Test public void testKeepAlive() throws Exception {
105     final ConsoleConnectionFactory consoleConnection =
106         new ConsoleConnectionFactory(m_scheduler);
107 
108     final SocketAcceptorThread socketAcceptor = SocketAcceptorThread.create();
109 
110     consoleConnection.connect(socketAcceptor.getHostName(),
111                               socketAcceptor.getPort());
112     socketAcceptor.close();
113 
114     verify(m_scheduler).scheduleWithFixedDelay(m_runnableCaptor.capture(),
115                                                isA(Long.class),
116                                                isA(Long.class),
117                                                isA(TimeUnit.class));
118 
119     final InputStream socketInput =
120       socketAcceptor.getAcceptedSocket().getInputStream();
121 
122     final ObjectInputStream objectInputStream =
123       new ObjectInputStream(socketInput);
124 
125     assertEquals(ConnectionType.CONSOLE_CLIENT, objectInputStream.readObject());
126     assertNull(objectInputStream.readObject());
127     assertEquals(0, socketInput.available());
128 
129     final Runnable keepAlive = m_runnableCaptor.getValue();
130 
131     keepAlive.run();
132 
133     final ObjectInputStream objectInputStream2 =
134         new ObjectInputStream(socketInput);
135 
136     assertTrue(objectInputStream2.readObject() instanceof KeepAliveMessage);
137 
138     socketAcceptor.getAcceptedSocket().close();
139 
140     try {
141       // Not sure why, but we don't find out until the second message we send.
142       keepAlive.run();
143       keepAlive.run();
144       fail("Expected RuntimeException");
145     }
146     catch (RuntimeException e) {
147       assertTrue(e.getCause() instanceof CommunicationException);
148     }
149   }
150 }