01: package org.codehaus.spice.netserve.connection.impl;
02:
03: import java.io.IOException;
04: import java.net.ServerSocket;
05: import junit.framework.TestCase;
06: import org.apache.avalon.framework.logger.Logger;
07: import com.mockobjects.dynamic.C;
08: import com.mockobjects.dynamic.Mock;
09:
10: public class AvalonAcceptorMonitorTestCase extends TestCase {
11: public void testAvalonAcceptorMonitor() throws Exception {
12: final Mock mockLogger = new Mock(Logger.class);
13:
14: final String name = "MyName";
15: final ServerSocket serverSocket = new ServerSocket(2023);
16: final IOException ioe = new IOException();
17:
18: final String acceptorCreatedMessage = "Creating Acceptor "
19: + name + " on "
20: + serverSocket.getInetAddress().getHostAddress() + ":"
21: + serverSocket.getLocalPort() + ".";
22:
23: final String acceptorClosingMessage = "Closing Acceptor "
24: + name + ".";
25:
26: final String serverSocketListening = "About to call accept() on ServerSocket '"
27: + name + "'.";
28:
29: final String errorAcceptingConnectionMessage = "Error Accepting connection on "
30: + name;
31:
32: final String errorClosingServerSocketMessage = "Error Closing Server Socket "
33: + name;
34:
35: mockLogger.expect("info", C.args(C.eq(acceptorCreatedMessage)));
36: mockLogger.expect("info", C.args(C.eq(acceptorClosingMessage)));
37: mockLogger.expectAndReturn("isDebugEnabled", C.NO_ARGS, false);
38: mockLogger.expectAndReturn("isDebugEnabled", C.NO_ARGS, true);
39: mockLogger.expect("debug", C.args(C.eq(serverSocketListening)));
40: mockLogger.expect("warn", C.args(C
41: .eq(errorAcceptingConnectionMessage), C.eq(ioe)));
42: mockLogger.expect("warn", C.args(C
43: .eq(errorClosingServerSocketMessage), C.eq(ioe)));
44: final Logger logger = (Logger) mockLogger.proxy();
45:
46: final AcceptorMonitor monitor = new AvalonAcceptorMonitor(
47: logger);
48: monitor.acceptorCreated(name, serverSocket);
49: monitor.acceptorClosing(name, serverSocket);
50: monitor.serverSocketListening(name, serverSocket);
51: monitor.serverSocketListening(name, serverSocket);
52: monitor.errorAcceptingConnection(name, ioe);
53: monitor.errorClosingServerSocket(name, ioe);
54:
55: mockLogger.verify();
56: }
57:
58: public void testAvalonAcceptorMonitorPassedNullIntoCtor()
59: throws Exception {
60: try {
61: new AvalonAcceptorMonitor(null);
62: } catch (final NullPointerException npe) {
63: assertEquals("npe.getMessage()", "logger", npe.getMessage());
64: return;
65: }
66: fail("Expected to fail due to npe in ctor");
67: }
68: }
|