01: package org.codehaus.spice.netserve.connection.impl;
02:
03: import java.net.ServerSocket;
04: import java.io.IOException;
05:
06: import com.mockobjects.dynamic.C;
07: import com.mockobjects.dynamic.Mock;
08: import junit.framework.TestCase;
09: import org.codehaus.dna.Logger;
10:
11: public class DNAAcceptorMonitorTestCase extends TestCase {
12: public void testDNAAcceptorMonitor() throws Exception {
13: final Mock mockLogger = new Mock(Logger.class);
14:
15: final String name = "MyName";
16: final ServerSocket serverSocket = new ServerSocket(2023);
17: final IOException ioe = new IOException();
18:
19: final String acceptorCreatedMessage = "Creating Acceptor "
20: + name + " on "
21: + serverSocket.getInetAddress().getHostAddress() + ":"
22: + serverSocket.getLocalPort() + ".";
23:
24: final String acceptorClosingMessage = "Closing Acceptor "
25: + name + ".";
26:
27: final String serverSocketListening = "About to call accept() on ServerSocket '"
28: + name + "'.";
29:
30: final String errorAcceptingConnectionMessage = "Error Accepting connection on "
31: + name;
32:
33: final String errorClosingServerSocketMessage = "Error Closing Server Socket "
34: + name;
35:
36: mockLogger.expect("info", C.args(C.eq(acceptorCreatedMessage)));
37: mockLogger.expect("info", C.args(C.eq(acceptorClosingMessage)));
38: mockLogger.expectAndReturn("isDebugEnabled", C.NO_ARGS, false);
39: mockLogger.expectAndReturn("isDebugEnabled", C.NO_ARGS, true);
40: mockLogger.expect("debug", C.args(C.eq(serverSocketListening)));
41: mockLogger.expect("warn", C.args(C
42: .eq(errorAcceptingConnectionMessage), C.eq(ioe)));
43: mockLogger.expect("warn", C.args(C
44: .eq(errorClosingServerSocketMessage), C.eq(ioe)));
45: final Logger logger = (Logger) mockLogger.proxy();
46:
47: final AcceptorMonitor monitor = new DNAAcceptorMonitor(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 testDNAAcceptorMonitorPassedNullIntoCtor()
59: throws Exception {
60: try {
61: new DNAAcceptorMonitor(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: }
|