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.ServerSocket;
011:
012: import junit.framework.TestCase;
013:
014: /**
015: *
016: * @author Peter Donald
017: * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
018: */
019: public class ConnectionAcceptorTestCase extends TestCase {
020: public void testNullConfigInCtor() throws Exception {
021: try {
022: new ConnectionAcceptor(null, new NullAcceptorMonitor());
023: } catch (final NullPointerException npe) {
024: assertEquals("npe.message", "config", npe.getMessage());
025: return;
026: }
027: fail("Expected to fail due to NPE for config");
028: }
029:
030: public void testNullMonitorInCtor() throws Exception {
031: try {
032: new ConnectionAcceptor(new AcceptorConfig("name",
033: new ServerSocket(),
034: new MockSocketConnectionHandler()), null);
035: } catch (NullPointerException npe) {
036: assertEquals("npe.message", "monitor", npe.getMessage());
037: return;
038: }
039: fail("Expected to fail due to NPE for monitor");
040: }
041:
042: public void testShutdownServerSocketCausesError() throws Exception {
043: final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
044: final ConnectionAcceptor acceptor = new ConnectionAcceptor(
045: new AcceptorConfig("name",
046: new ExceptOnCloseServerSocket(),
047: new MockSocketConnectionHandler()), monitor);
048: assertEquals(
049: "errorClosingServerSocket pre-shutdownServerSocket()",
050: null, monitor.getErrorClosingServerSocket());
051: acceptor.shutdownServerSocket();
052: assertEquals(
053: "errorClosingServerSocket post-shutdownServerSocket()",
054: ExceptOnCloseServerSocket.EXCEPTION, monitor
055: .getErrorClosingServerSocket());
056: }
057:
058: public void testShutdownServerSocket() throws Exception {
059: final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
060: final ConnectionAcceptor acceptor = new ConnectionAcceptor(
061: new AcceptorConfig("name", new ServerSocket(),
062: new MockSocketConnectionHandler()), monitor);
063: assertEquals(
064: "errorClosingServerSocket pre-shutdownServerSocket()",
065: null, monitor.getErrorClosingServerSocket());
066: acceptor.shutdownServerSocket();
067: assertEquals(
068: "errorClosingServerSocket post-shutdownServerSocket()",
069: null, monitor.getErrorClosingServerSocket());
070: }
071:
072: public void testExceptionOnAccept() throws Exception {
073: final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
074: final ConnectionAcceptor acceptor = new ConnectionAcceptor(
075: new AcceptorConfig("name",
076: new ExceptOnAcceptServerSocket(false),
077: new MockSocketConnectionHandler()), monitor);
078: assertEquals(
079: "getErrorAcceptingConnection pre-shutdownServerSocket()",
080: null, monitor.getErrorAcceptingConnection());
081: final Thread thread = startAcceptor(acceptor);
082: waitUntilStarted(acceptor);
083: waitUntilListening(monitor);
084:
085: assertEquals(
086: "getErrorAcceptingConnection post-shutdownServerSocket()",
087: ExceptOnAcceptServerSocket.ERROR_EXCEPTION, monitor
088: .getErrorAcceptingConnection());
089:
090: acceptor.close(0);
091: thread.join();
092: }
093:
094: public void testInteruptOnAccept() throws Exception {
095: final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
096: final ConnectionAcceptor acceptor = new ConnectionAcceptor(
097: new AcceptorConfig("name",
098: new ExceptOnAcceptServerSocket(true),
099: new MockSocketConnectionHandler()), monitor);
100: final Thread thread = startAcceptor(acceptor);
101: waitUntilStarted(acceptor);
102: waitUntilListening(monitor);
103:
104: try {
105: Thread.sleep(30);
106: } catch (final InterruptedException e) {
107: }
108: assertTrue("1 < monitor.getListenCount", 1 < monitor
109: .getListenCount());
110:
111: acceptor.close(0);
112: thread.join();
113: }
114:
115: public void _testNormalHandlerAccept() throws Exception {
116: final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
117: final BlockingServerSocket serverSocket = new BlockingServerSocket();
118: final MockSocketConnectionHandler handler = new MockSocketConnectionHandler();
119: final ConnectionAcceptor acceptor = new ConnectionAcceptor(
120: new AcceptorConfig("name", serverSocket, handler),
121: monitor);
122: final Thread thread = startAcceptor(acceptor);
123: waitUntilStarted(acceptor);
124: waitUntilListening(monitor);
125: serverSocket.unlock();
126: try {
127: Thread.sleep(30);
128: } catch (final InterruptedException e) {
129: }
130: assertEquals("handler.getSocket()",
131: BlockingServerSocket.SOCKET, handler.getSocket());
132: acceptor.close(50);
133: serverSocket.unlock();
134: thread.join();
135: }
136:
137: public void testAcceptAfterClose() throws Exception {
138: final RecordingAcceptorMonitor monitor = new RecordingAcceptorMonitor();
139: final BlockingServerSocket serverSocket = new BlockingServerSocket();
140: final MockSocketConnectionHandler handler = new MockSocketConnectionHandler();
141: final ConnectionAcceptor acceptor = new ConnectionAcceptor(
142: new AcceptorConfig("name", serverSocket, handler),
143: monitor);
144: final Thread thread = startAcceptor(acceptor);
145: waitUntilStarted(acceptor);
146: waitUntilListening(monitor);
147: acceptor.close(50);
148: serverSocket.unlock();
149: waitUntilListening(monitor);
150: assertEquals("handler.getSocket()", null, handler.getSocket());
151: serverSocket.unlock();
152: thread.join();
153: }
154:
155: private void waitUntilListening(
156: final RecordingAcceptorMonitor monitor) {
157: while (0 == monitor.getListenCount()) {
158: try {
159: Thread.sleep(30);
160: } catch (final InterruptedException e) {
161: }
162: }
163: }
164:
165: private Thread startAcceptor(final ConnectionAcceptor acceptor) {
166: final Thread thread = new Thread(acceptor);
167: thread.start();
168: return thread;
169: }
170:
171: private void waitUntilStarted(final ConnectionAcceptor acceptor) {
172: while (!acceptor.isRunning()) {
173: try {
174: Thread.sleep(30);
175: } catch (final InterruptedException e) {
176: }
177: }
178: }
179: }
|