View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.mina.transport.nio;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.fail;
24  import static org.mockito.Matchers.anyInt;
25  import static org.mockito.Matchers.eq;
26  import static org.mockito.Mockito.doThrow;
27  import static org.mockito.Mockito.verify;
28  import static org.mockito.Mockito.verifyNoMoreInteractions;
29  import static org.mockito.Mockito.when;
30  
31  import java.net.Socket;
32  import java.net.SocketException;
33  
34  import org.apache.mina.api.ConfigurationException;
35  import org.apache.mina.api.IdleStatus;
36  import org.apache.mina.transport.tcp.ProxyTcpSessionConfig;
37  import org.junit.Before;
38  import org.junit.Test;
39  import org.mockito.Mockito;
40  
41  /**
42   * Unit test for {@link ProxyTcpSessionConfig}
43   *
44   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
45   */
46  public class ProxySocketSessionConfigTest {
47  
48      private ProxyTcpSessionConfig config;
49  
50      private Socket socket;
51  
52      @Before
53      public void setup() throws Exception {
54          socket = Mockito.mock(Socket.class);
55          config = new ProxyTcpSessionConfig(socket);
56      }
57  
58      @Test
59      public void idle() {
60          assertEquals(-1, config.getIdleTimeInMillis(IdleStatus.READ_IDLE));
61          assertEquals(-1, config.getIdleTimeInMillis(IdleStatus.WRITE_IDLE));
62  
63          config.setIdleTimeInMillis(IdleStatus.READ_IDLE, 1);
64          assertEquals(1, config.getIdleTimeInMillis(IdleStatus.READ_IDLE));
65          assertEquals(-1, config.getIdleTimeInMillis(IdleStatus.WRITE_IDLE));
66  
67          assertEquals(1, config.getIdleTimeInMillis(IdleStatus.READ_IDLE));
68          assertEquals(-1, config.getIdleTimeInMillis(IdleStatus.WRITE_IDLE));
69  
70          config.setIdleTimeInMillis(IdleStatus.WRITE_IDLE, 3);
71          assertEquals(1, config.getIdleTimeInMillis(IdleStatus.READ_IDLE));
72          assertEquals(3, config.getIdleTimeInMillis(IdleStatus.WRITE_IDLE));
73      }
74  
75      @Test
76      public void tcpNoDelay() throws SocketException {
77          when(socket.getTcpNoDelay()).thenReturn(true);
78          assertTrue(config.isTcpNoDelay());
79          verify(socket).getTcpNoDelay();
80          verifyNoMoreInteractions(socket);
81  
82          config.setTcpNoDelay(true);
83          verify(socket).setTcpNoDelay(eq(true));
84          verifyNoMoreInteractions(socket);
85  
86          // handle error
87          when(socket.getTcpNoDelay()).thenThrow(new SocketException("test"));
88          try {
89              config.isTcpNoDelay();
90              fail();
91          } catch (ConfigurationException e) {
92              assertEquals("test", e.getCause().getMessage());
93          }
94  
95          // handle error
96          doThrow(new SocketException("test")).when(socket).setTcpNoDelay(eq(true));
97          try {
98              config.setTcpNoDelay(true);
99              fail();
100         } catch (ConfigurationException e) {
101             assertEquals("test", e.getCause().getMessage());
102         }
103 
104     }
105 
106     @Test
107     public void reuseAddress() throws SocketException {
108         when(socket.getReuseAddress()).thenReturn(true);
109         assertTrue(config.isReuseAddress());
110         verify(socket).getReuseAddress();
111         verifyNoMoreInteractions(socket);
112 
113         config.setReuseAddress(true);
114         verify(socket).setReuseAddress(eq(true));
115         verifyNoMoreInteractions(socket);
116 
117         // handle error
118         when(socket.getReuseAddress()).thenThrow(new SocketException("test"));
119         try {
120             config.isReuseAddress();
121             fail();
122         } catch (ConfigurationException e) {
123             assertEquals("test", e.getCause().getMessage());
124         }
125 
126         // handle error
127         doThrow(new SocketException("test")).when(socket).setReuseAddress(eq(true));
128         try {
129             config.setReuseAddress(true);
130             fail();
131         } catch (ConfigurationException e) {
132             assertEquals("test", e.getCause().getMessage());
133         }
134     }
135 
136     @Test
137     public void receiveBufferSize() throws SocketException {
138         when(socket.getReceiveBufferSize()).thenReturn(1234);
139         assertEquals(1234, config.getReadBufferSize().intValue());
140         verify(socket).getReceiveBufferSize();
141         verifyNoMoreInteractions(socket);
142 
143         config.setReadBufferSize(1234);
144         verify(socket).setReceiveBufferSize(eq(1234));
145         verifyNoMoreInteractions(socket);
146 
147         // handle error
148         when(socket.getReceiveBufferSize()).thenThrow(new SocketException("test"));
149         try {
150             config.getReadBufferSize();
151             fail();
152         } catch (ConfigurationException e) {
153             assertEquals("test", e.getCause().getMessage());
154         }
155 
156         // handle error
157         doThrow(new SocketException("test")).when(socket).setReceiveBufferSize(eq(1234));
158         try {
159             config.setReadBufferSize(1234);
160             fail();
161         } catch (ConfigurationException e) {
162             assertEquals("test", e.getCause().getMessage());
163         }
164 
165     }
166 
167     @Test
168     public void sendBufferSize() throws SocketException {
169         when(socket.getSendBufferSize()).thenReturn(1234);
170         assertEquals(1234, config.getSendBufferSize().intValue());
171         verify(socket).getSendBufferSize();
172         verifyNoMoreInteractions(socket);
173 
174         config.setSendBufferSize(1234);
175         verify(socket).setSendBufferSize(eq(1234));
176         verifyNoMoreInteractions(socket);
177 
178         // handle error
179         when(socket.getSendBufferSize()).thenThrow(new SocketException("test"));
180         try {
181             config.getSendBufferSize();
182             fail();
183         } catch (ConfigurationException e) {
184             assertEquals("test", e.getCause().getMessage());
185         }
186 
187         // handle error
188         doThrow(new SocketException("test")).when(socket).setSendBufferSize(eq(1234));
189         try {
190             config.setSendBufferSize(1234);
191             fail();
192         } catch (ConfigurationException e) {
193             assertEquals("test", e.getCause().getMessage());
194         }
195     }
196 
197     @Test
198     public void trafficClass() throws SocketException {
199         when(socket.getTrafficClass()).thenReturn(1234);
200         assertEquals(1234, config.getTrafficClass());
201         verify(socket).getTrafficClass();
202         verifyNoMoreInteractions(socket);
203 
204         config.setTrafficClass(1234);
205         verify(socket).setTrafficClass(eq(1234));
206         verifyNoMoreInteractions(socket);
207 
208         // handle error
209         when(socket.getTrafficClass()).thenThrow(new SocketException("test"));
210         try {
211             config.getTrafficClass();
212             fail();
213         } catch (ConfigurationException e) {
214             assertEquals("test", e.getCause().getMessage());
215         }
216 
217         // handle error
218         doThrow(new SocketException("test")).when(socket).setTrafficClass(eq(1234));
219         try {
220             config.setTrafficClass(1234);
221             fail();
222         } catch (ConfigurationException e) {
223             assertEquals("test", e.getCause().getMessage());
224         }
225     }
226 
227     @Test
228     public void keepAlive() throws SocketException {
229         when(socket.getKeepAlive()).thenReturn(true);
230         assertTrue(config.isKeepAlive());
231         verify(socket).getKeepAlive();
232         verifyNoMoreInteractions(socket);
233 
234         config.setKeepAlive(true);
235         verify(socket).setKeepAlive(eq(true));
236         verifyNoMoreInteractions(socket);
237 
238         // handle error
239         when(socket.getKeepAlive()).thenThrow(new SocketException("test"));
240         try {
241             config.isKeepAlive();
242             fail();
243         } catch (ConfigurationException e) {
244             assertEquals("test", e.getCause().getMessage());
245         }
246 
247         // handle error
248         doThrow(new SocketException("test")).when(socket).setKeepAlive(eq(true));
249         try {
250             config.setKeepAlive(true);
251             fail();
252         } catch (ConfigurationException e) {
253             assertEquals("test", e.getCause().getMessage());
254         }
255     }
256 
257     @Test
258     public void oobInline() throws SocketException {
259         when(socket.getOOBInline()).thenReturn(true);
260         assertTrue(config.isOobInline());
261         verify(socket).getOOBInline();
262         verifyNoMoreInteractions(socket);
263 
264         config.setOobInline(true);
265         verify(socket).setOOBInline(eq(true));
266         verifyNoMoreInteractions(socket);
267 
268         // handle error
269         when(socket.getOOBInline()).thenThrow(new SocketException("test"));
270         try {
271             config.isOobInline();
272             fail();
273         } catch (ConfigurationException e) {
274             assertEquals("test", e.getCause().getMessage());
275         }
276 
277         // handle error
278         doThrow(new SocketException("test")).when(socket).setOOBInline(eq(true));
279         try {
280             config.setOobInline(true);
281             fail();
282         } catch (ConfigurationException e) {
283             assertEquals("test", e.getCause().getMessage());
284         }
285     }
286 
287     @Test
288     public void soLinger() throws SocketException {
289         when(socket.getSoLinger()).thenReturn(1234);
290         assertEquals(1234, config.getSoLinger().intValue());
291         verify(socket).getSoLinger();
292         verifyNoMoreInteractions(socket);
293 
294         config.setSoLinger(1234);
295         verify(socket).setSoLinger(eq(true), eq(1234));
296         verifyNoMoreInteractions(socket);
297 
298         config.setSoLinger(-1234);
299         verify(socket).setSoLinger(eq(false), anyInt());
300         verifyNoMoreInteractions(socket);
301 
302         // handle error
303         when(socket.getSoLinger()).thenThrow(new SocketException("test"));
304         try {
305             config.getSoLinger();
306             fail();
307         } catch (ConfigurationException e) {
308             assertEquals("test", e.getCause().getMessage());
309         }
310 
311         // handle error
312         doThrow(new SocketException("test")).when(socket).setSoLinger(eq(true), eq(1234));
313         try {
314             config.setSoLinger(1234);
315             fail();
316         } catch (ConfigurationException e) {
317             assertEquals("test", e.getCause().getMessage());
318         }
319     }
320 
321 }