001: /*
002: * Copyright (C) The Spice Group. All rights reserved.
003: *
004: * This software is published under the terms of the Spice
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.spice.netserve.connection.impl;
009:
010: import java.net.Socket;
011: import java.net.InetAddress;
012: import java.net.InetSocketAddress;
013: import java.net.ServerSocket;
014: import java.nio.channels.ServerSocketChannel;
015: import java.util.Random;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: *
021: * @author Peter Donald
022: * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
023: */
024: public class DefaultAcceptorManagerTestCase extends TestCase {
025: public void testConnectAndDisconnectWithTimeoutNotSet()
026: throws Exception {
027: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
028: manager.setMonitor(NullAcceptorMonitor.MONITOR);
029: manager.setSoTimeout(10);
030: manager.setShutdownTimeout(0);
031: final String name = "name";
032: assertEquals("isConnected pre connect", false, manager
033: .isConnected(name));
034: final ExceptOnAcceptServerSocket socket = new ExceptOnAcceptServerSocket(
035: true);
036: assertEquals("socket.getSoTimeout pre connect", 0, socket
037: .getSoTimeout());
038: manager
039: .connect(name, socket,
040: new MockSocketConnectionHandler());
041: assertEquals("socket.getSoTimeout post connect", 10, socket
042: .getSoTimeout());
043: assertEquals("isConnected pre disconnect", true, manager
044: .isConnected(name));
045: manager.disconnect(name);
046: assertEquals("isConnected post disconnect", false, manager
047: .isConnected(name));
048: }
049:
050: public void testShutdownAcceptors() throws Exception {
051: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
052: manager.setMonitor(NullAcceptorMonitor.MONITOR);
053: manager.setSoTimeout(10);
054: final String name = "name";
055: assertEquals("isConnected pre connect", false, manager
056: .isConnected(name));
057: manager.connect(name, new ExceptOnAcceptServerSocket(true),
058: new MockSocketConnectionHandler());
059: assertEquals("isConnected pre shutdownAcceptors", true, manager
060: .isConnected(name));
061: manager.shutdownAcceptors();
062: assertEquals("isConnected post shutdownAcceptors", false,
063: manager.isConnected(name));
064: }
065:
066: public void testConnectAndDisconnectWithTimeoutSet()
067: throws Exception {
068: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
069: manager.setMonitor(NullAcceptorMonitor.MONITOR);
070: manager.setSoTimeout(10);
071: final String name = "name";
072: assertEquals("isConnected pre connect", false, manager
073: .isConnected(name));
074: final ExceptOnAcceptServerSocket socket = new ExceptOnAcceptServerSocket(
075: true);
076: socket.setSoTimeout(123);
077: assertEquals("socket.getSoTimeout pre connect", 123, socket
078: .getSoTimeout());
079: manager
080: .connect(name, socket,
081: new MockSocketConnectionHandler());
082: assertEquals("socket.getSoTimeout post connect", 123, socket
083: .getSoTimeout());
084: assertEquals("isConnected pre disconnect", true, manager
085: .isConnected(name));
086: manager.disconnect(name);
087: assertEquals("isConnected post disconnect", false, manager
088: .isConnected(name));
089: }
090:
091: public void testConnectAndAcceptAndDisconnectWithTimeoutSet()
092: throws Exception {
093: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
094: final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
095: manager.setMonitor(monitor);
096: manager.setSoTimeout(10);
097: final String name = "name";
098: assertEquals("isConnected pre connect", false, manager
099: .isConnected(name));
100: final ServerSocketChannel channel = ServerSocketChannel.open();
101: final ServerSocket serverSocket = channel.socket();
102: serverSocket.setReuseAddress(true);
103: final InetAddress localAddress = InetAddress.getLocalHost();
104: final Random random = new Random();
105: final int port = Math.abs(random.nextInt() % 5000) + 1024;
106: final InetSocketAddress address = new InetSocketAddress(
107: localAddress, port);
108: serverSocket.bind(address);
109: while (!serverSocket.isBound()) {
110: System.out.print(".");
111: Thread.yield();
112: }
113:
114: final MockSocketConnectionHandler handler = new MockSocketConnectionHandler();
115: manager.connect(name, serverSocket, handler);
116:
117: final Socket clientSocket = new Socket(localAddress, port);
118: System.out.print("Socket Connecting");
119: while (!clientSocket.isConnected()) {
120: System.out.print(".");
121: Thread.yield();
122: }
123: System.out.println(" - Connected");
124: clientSocket.close();
125:
126: //Sleep to make sure accept goes through
127: Thread.sleep(200);
128:
129: assertNotNull("handler.socket", handler.getSocket());
130:
131: assertEquals("isConnected pre disconnect", true, manager
132: .isConnected(name));
133: manager.disconnect(name);
134: assertEquals("isConnected post disconnect", false, manager
135: .isConnected(name));
136: }
137:
138: public void testDuplicateConnect() throws Exception {
139: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
140: final String name = "name";
141: assertEquals("isConnected pre connect", false, manager
142: .isConnected(name));
143: final ExceptOnAcceptServerSocket socket = new ExceptOnAcceptServerSocket(
144: true);
145: manager
146: .connect(name, socket,
147: new MockSocketConnectionHandler());
148: assertEquals("isConnected pre disconnect", true, manager
149: .isConnected(name));
150: try {
151: manager.connect(name, new ExceptOnAcceptServerSocket(true),
152: new MockSocketConnectionHandler());
153: } catch (final IllegalArgumentException iae) {
154: return;
155: } finally {
156: socket.close();
157: manager.shutdownAcceptors();
158: assertEquals("isConnected post disconnect", false, manager
159: .isConnected(name));
160: }
161: fail("Expected to fail due to duplicate connect");
162: }
163:
164: public void testDisconnectNonExistent() throws Exception {
165: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
166: try {
167: manager.disconnect("NonExistent");
168: } catch (IllegalArgumentException iae) {
169: return;
170: } finally {
171: manager.shutdownAcceptors();
172: }
173: fail("Expected to fail to disconnect non existent acceptor");
174: }
175:
176: public void testConnectWithNullName() throws Exception {
177: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
178: try {
179: manager.connect(null, new ExceptOnAcceptServerSocket(true),
180: new MockSocketConnectionHandler());
181: } catch (final NullPointerException npe) {
182: assertEquals("npe.message", "name", npe.getMessage());
183: return;
184: } finally {
185: manager.shutdownAcceptors();
186: }
187: fail("expected NPE due to null name in connect");
188: }
189:
190: public void testConnectWithNullSocket() throws Exception {
191: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
192: try {
193: manager.connect("name", null,
194: new MockSocketConnectionHandler());
195: } catch (final NullPointerException npe) {
196: assertEquals("npe.message", "socket", npe.getMessage());
197: return;
198: } finally {
199: manager.shutdownAcceptors();
200: }
201: fail("expected NPE due to null socket in connect");
202: }
203:
204: public void testConnectWithNullHandler() throws Exception {
205: final DefaultAcceptorManager manager = new DefaultAcceptorManager();
206: try {
207: manager.connect("name",
208: new ExceptOnAcceptServerSocket(true), null);
209: } catch (final NullPointerException npe) {
210: assertEquals("npe.message", "handler", npe.getMessage());
211: return;
212: } finally {
213: manager.shutdownAcceptors();
214: }
215: fail("expected NPE due to null handler in connect");
216: }
217:
218: protected void setUp() throws Exception {
219: System.out.print("Test: " + getName());
220: }
221: }
|