1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: NetworkConnectorTest.java,v 1.1 2005/09/03 23:13:29 mat007 Exp $
29 */
30
31 package jtge.engine.net;
32
33 import java.io.InputStream;
34 import java.io.OutputStream;
35 import java.net.ServerSocket;
36 import jtge.engine.io.IConnectionObserver;
37 import jtge.engine.io.IInput;
38 import jtge.engine.io.IInputOutputFactory;
39 import jtge.engine.io.IOutput;
40 import jtge.engine.net.NetworkConnector;
41 import jtge.util.EasyMockTestCase;
42 import org.easymock.MockControl;
43
44 /***
45 * NetworkConnector test case.
46 *
47 * @author Mathieu Champlon
48 * @version $Revision: 1.1 $ $Date: 2005/09/03 23:13:29 $
49 */
50 public class NetworkConnectorTest extends EasyMockTestCase
51 {
52 /***
53 * Tested object.
54 */
55 private NetworkConnector connector;
56 /***
57 * Mock objects.
58 */
59 private IInputOutputFactory mockFactory;
60 private IConnectionObserver mockObserver;
61 private IInput mockInput;
62 private IOutput mockOutput;
63 /***
64 * Dummy objects.
65 */
66 private static final int PORT_NUMBER = 7777;
67 private static final int WRONG_PORT_NUMBER = 7778;
68 private ServerSocket server;
69
70 protected void setUp() throws Exception
71 {
72 mockFactory = (IInputOutputFactory)createMock( IInputOutputFactory.class );
73 mockObserver = (IConnectionObserver)createMock( IConnectionObserver.class );
74 mockInput = (IInput)createMock( IInput.class );
75 mockOutput = (IOutput)createMock( IOutput.class );
76 connector = new NetworkConnector( mockFactory, mockObserver, "localhost", PORT_NUMBER );
77 server = new ServerSocket( PORT_NUMBER );
78 }
79
80 protected void tearDown() throws Exception
81 {
82 server.close();
83 }
84
85 public void testCreateInvalidPort()
86 {
87 try
88 {
89 new NetworkConnector( mockFactory, mockObserver, "localhost", -1 );
90 }
91 catch( Exception e )
92 {
93 return;
94 }
95 fail();
96 }
97
98 public void testOpenTimeouts()
99 {
100 replay();
101 connector = new NetworkConnector( mockFactory, mockObserver, "localhost", WRONG_PORT_NUMBER );
102 try
103 {
104 connector.open();
105 }
106 catch( Exception e )
107 {
108 return;
109 }
110 fail( "should throw an exception" );
111 }
112
113 public void testOpenSuccessfull()
114 {
115 mockFactory.create( (InputStream)null );
116 control( mockFactory ).setMatcher( MockControl.ALWAYS_MATCHER );
117 control( mockFactory ).setDefaultReturnValue( mockInput );
118 mockFactory.create( (OutputStream)null );
119 control( mockFactory ).setMatcher( MockControl.ALWAYS_MATCHER );
120 control( mockFactory ).setDefaultReturnValue( mockOutput );
121 mockObserver.opened( mockInput, mockOutput );
122 replay();
123 connector.open();
124 }
125 }