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.handlers;
009:
010: import junit.framework.TestCase;
011: import java.net.Socket;
012:
013: /**
014: *
015: * @author Peter Donald
016: * @version $Revision: 1.2 $ $Date: 2004/03/21 23:42:59 $
017: */
018: public class RequestHandlerTestCase extends TestCase {
019: public void testEndConnectionWithError() throws Exception {
020: final MockRequestHandler handler = new MockRequestHandler();
021: final ExceptionOnCloseSocket socket = new ExceptionOnCloseSocket();
022: handler.endConnection(socket);
023: assertEquals("getErrorClosingConnectionSocket", socket, handler
024: .getErrorClosingConnectionSocket());
025: assertEquals("getErrorClosingConnectionThrowable",
026: ExceptionOnCloseSocket.IO_EXCEPTION, handler
027: .getErrorClosingConnectionThrowable());
028: }
029:
030: public void testCreateRunnable() throws Exception {
031: final MockRequestHandler handler = new MockRequestHandler();
032: final Socket socket = new Socket();
033: final Runnable runnable = handler.createRunnable(socket);
034: runnable.run();
035: assertEquals("getPerformRequestSocket", socket, handler
036: .getPerformRequestSocket());
037: }
038:
039: public void testPerformRequestWithError() throws Exception {
040: final ExceptingRequestHandler handler = new ExceptingRequestHandler();
041: final Socket socket = new Socket();
042: handler.performRequest(socket);
043: assertEquals("getPerformRequestSocket", socket, handler
044: .getPerformRequestSocket());
045: assertEquals("getErrorHandlingConnectionSocket", socket,
046: handler.getErrorHandlingConnectionSocket());
047: assertEquals("getErrorClosingConnectionThrowable",
048: ExceptingRequestHandler.EXCEPTION, handler
049: .getErrorHandlingConnectionThrowable());
050: }
051:
052: public void testShutdownWhileThreadStillGoingButInteruptible()
053: throws Exception {
054: final DelayingRequestHandler handler = new DelayingRequestHandler(
055: 2000, true);
056: final Socket socket = new Socket();
057: final Runnable runnable = new Runnable() {
058: public void run() {
059: handler.handleConnection(socket);
060: }
061: };
062: final Thread thread = new Thread(runnable);
063: thread.start();
064: Thread.sleep(50);
065:
066: handler.shutdown(50);
067: assertEquals("isShutdown", true, handler.isShutdown());
068: assertEquals("isExited", true, handler.isExited());
069: assertEquals("isExitDueToInterrupt", true, handler
070: .isExitDueToInterrupt());
071: }
072:
073: public void testShutdownWhileThreadStillGoingAndNotInteruptible()
074: throws Exception {
075: final DelayingRequestHandler handler = new DelayingRequestHandler(
076: 2000, false);
077: final Socket socket = new Socket();
078: final Runnable runnable = new Runnable() {
079: public void run() {
080: handler.handleConnection(socket);
081: }
082: };
083: final Thread thread = new Thread(runnable);
084: thread.start();
085: Thread.sleep(50);
086:
087: handler.shutdown(50);
088: assertEquals("isShutdown", true, handler.isShutdown());
089: assertEquals("isExited", false, handler.isExited());
090: assertEquals("isExitDueToInterrupt", false, handler
091: .isExitDueToInterrupt());
092: }
093:
094: public void testShutdownWhileThreadStillGoingAndWaitIndefinetly()
095: throws Exception {
096: final DelayingRequestHandler handler = new DelayingRequestHandler(
097: 200, false);
098: final Socket socket = new Socket();
099: final Runnable runnable = new Runnable() {
100: public void run() {
101: handler.handleConnection(socket);
102: }
103: };
104: final Thread thread = new Thread(runnable);
105: thread.start();
106: Thread.sleep(50);
107:
108: handler.shutdown(0);
109: assertEquals("isShutdown", true, handler.isShutdown());
110: assertEquals("isExited", true, handler.isExited());
111: assertEquals("isExitDueToInterrupt", false, handler
112: .isExitDueToInterrupt());
113: }
114: }
|