001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.net.core;
005:
006: import com.tc.net.TCSocketAddress;
007: import com.tc.net.protocol.NullProtocolAdaptor;
008: import com.tc.net.protocol.ProtocolAdaptorFactory;
009: import com.tc.net.protocol.TCProtocolAdaptor;
010:
011: import java.util.Arrays;
012:
013: import junit.framework.TestCase;
014:
015: /**
016: * TODO Jan 13, 2005: comment describing what this class is for.
017: */
018: public class TCConnectionManagerTest extends TestCase {
019:
020: private TCConnectionManager clientConnMgr;
021: private TCConnectionManager serverConnMgr;
022: private TCListener lsnr;
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: this .clientConnMgr = new TCConnectionManagerJDK14();
027: this .serverConnMgr = new TCConnectionManagerJDK14();
028: this .lsnr = this .serverConnMgr.createListener(
029: new TCSocketAddress(0), new ProtocolAdaptorFactory() {
030: public TCProtocolAdaptor getInstance() {
031: return new NullProtocolAdaptor();
032: }
033: });
034: }
035:
036: protected void tearDown() throws Exception {
037: clientConnMgr.shutdown();
038: serverConnMgr.shutdown();
039: }
040:
041: public void testCreateConnection() throws Exception {
042: assertEquals(0, clientConnMgr.getAllConnections().length);
043:
044: TCConnection conn1 = clientConnMgr
045: .createConnection(new NullProtocolAdaptor());
046: TCConnection conn2 = clientConnMgr
047: .createConnection(new NullProtocolAdaptor());
048: TCConnection conn3 = clientConnMgr
049: .createConnection(new NullProtocolAdaptor());
050:
051: TCConnection conns[] = clientConnMgr.getAllConnections();
052: assertEquals(3, conns.length);
053:
054: assertTrue(Arrays.asList(conns).containsAll(
055: Arrays.asList(new Object[] { conn1, conn2, conn3 })));
056:
057: clientConnMgr.closeAllConnections(5000);
058:
059: assertEquals(0, clientConnMgr.getAllConnections().length);
060:
061: conn1 = clientConnMgr
062: .createConnection(new NullProtocolAdaptor());
063: assertEquals(1, clientConnMgr.getAllConnections().length);
064:
065: conn1.close(5000);
066: assertEquals(0, clientConnMgr.getAllConnections().length);
067:
068: conn1 = clientConnMgr
069: .createConnection(new NullProtocolAdaptor());
070: assertEquals(1, clientConnMgr.getAllConnections().length);
071: conn1.connect(lsnr.getBindSocketAddress(), 3000);
072: assertEquals(1, clientConnMgr.getAllConnections().length);
073: conn1.close(5000);
074: assertEquals(0, clientConnMgr.getAllConnections().length);
075: }
076:
077: public void testShutdown() {
078: assertEquals(1, serverConnMgr.getAllListeners().length);
079: assertEquals(0, clientConnMgr.getAllConnections().length);
080:
081: clientConnMgr.createConnection(new NullProtocolAdaptor());
082:
083: assertEquals(1, serverConnMgr.getAllListeners().length);
084: assertEquals(1, clientConnMgr.getAllConnections().length);
085:
086: serverConnMgr.shutdown();
087: clientConnMgr.shutdown();
088:
089: assertEquals(0, serverConnMgr.getAllListeners().length);
090: assertEquals(0, clientConnMgr.getAllConnections().length);
091:
092: // double shutdown call
093: serverConnMgr.shutdown();
094: clientConnMgr.shutdown();
095: }
096:
097: public void testCreateListenerTCSocketAddressProtocolAdaptorFactory() {
098: assertEquals(1, serverConnMgr.getAllListeners().length);
099: this .lsnr.stop();
100: assertEquals(0, serverConnMgr.getAllListeners().length);
101: }
102:
103: }
|