KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > core > TCConnectionManagerTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.core;
5
6 import com.tc.net.TCSocketAddress;
7 import com.tc.net.protocol.NullProtocolAdaptor;
8 import com.tc.net.protocol.ProtocolAdaptorFactory;
9 import com.tc.net.protocol.TCProtocolAdaptor;
10
11 import java.util.Arrays JavaDoc;
12
13 import junit.framework.TestCase;
14
15 /**
16  * TODO Jan 13, 2005: comment describing what this class is for.
17  */

18 public class TCConnectionManagerTest extends TestCase {
19
20   private TCConnectionManager clientConnMgr;
21   private TCConnectionManager serverConnMgr;
22   private TCListener lsnr;
23
24   protected void setUp() throws Exception JavaDoc {
25     super.setUp();
26     this.clientConnMgr = new TCConnectionManagerFactory().getInstance();
27     this.serverConnMgr = new TCConnectionManagerFactory().getInstance();
28     this.lsnr = this.serverConnMgr.createListener(new TCSocketAddress(0), new ProtocolAdaptorFactory() {
29       public TCProtocolAdaptor getInstance() {
30         return new NullProtocolAdaptor();
31       }
32     });
33   }
34
35   protected void tearDown() throws Exception JavaDoc {
36     clientConnMgr.shutdown();
37     serverConnMgr.shutdown();
38   }
39
40   public void testCreateConnection() throws Exception JavaDoc {
41     assertEquals(0, clientConnMgr.getAllConnections().length);
42
43     TCConnection conn1 = clientConnMgr.createConnection(new NullProtocolAdaptor());
44     TCConnection conn2 = clientConnMgr.createConnection(new NullProtocolAdaptor());
45     TCConnection conn3 = clientConnMgr.createConnection(new NullProtocolAdaptor());
46
47     TCConnection conns[] = clientConnMgr.getAllConnections();
48     assertEquals(3, conns.length);
49
50     assertTrue(Arrays.asList(conns).containsAll(Arrays.asList(new Object JavaDoc[] { conn1, conn2, conn3 })));
51
52     clientConnMgr.closeAllConnections(5000);
53
54     assertEquals(0, clientConnMgr.getAllConnections().length);
55
56     conn1 = clientConnMgr.createConnection(new NullProtocolAdaptor());
57     assertEquals(1, clientConnMgr.getAllConnections().length);
58
59     conn1.close(5000);
60     assertEquals(0, clientConnMgr.getAllConnections().length);
61
62     conn1 = clientConnMgr.createConnection(new NullProtocolAdaptor());
63     assertEquals(1, clientConnMgr.getAllConnections().length);
64     conn1.connect(lsnr.getBindSocketAddress(), 3000);
65     assertEquals(1, clientConnMgr.getAllConnections().length);
66     conn1.close(5000);
67     assertEquals(0, clientConnMgr.getAllConnections().length);
68   }
69
70   public void testShutdown() {
71     assertEquals(1, serverConnMgr.getAllListeners().length);
72     assertEquals(0, clientConnMgr.getAllConnections().length);
73
74     clientConnMgr.createConnection(new NullProtocolAdaptor());
75
76     assertEquals(1, serverConnMgr.getAllListeners().length);
77     assertEquals(1, clientConnMgr.getAllConnections().length);
78
79     serverConnMgr.shutdown();
80     clientConnMgr.shutdown();
81
82     assertEquals(0, serverConnMgr.getAllListeners().length);
83     assertEquals(0, clientConnMgr.getAllConnections().length);
84
85     // double shutdown call
86
serverConnMgr.shutdown();
87     clientConnMgr.shutdown();
88   }
89
90   public void testCreateListenerTCSocketAddressProtocolAdaptorFactory() {
91     assertEquals(1, serverConnMgr.getAllListeners().length);
92     this.lsnr.stop();
93     assertEquals(0, serverConnMgr.getAllListeners().length);
94   }
95
96 }
Popular Tags