KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > NetworkListenerTest


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

5 package com.tc.net.protocol.tcm;
6
7 import com.tc.net.TCSocketAddress;
8 import com.tc.net.protocol.PlainNetworkStackHarnessFactory;
9 import com.tc.net.protocol.transport.ConnectionIDFactory;
10 import com.tc.net.protocol.transport.DefaultConnectionIdFactory;
11 import com.tc.net.protocol.transport.NullConnectionPolicy;
12 import com.tc.object.session.NullSessionManager;
13 import com.tc.object.session.SessionProvider;
14 import com.tc.util.TCTimeoutException;
15
16 import java.io.IOException JavaDoc;
17 import java.net.InetAddress JavaDoc;
18 import java.net.UnknownHostException JavaDoc;
19 import java.util.Collections JavaDoc;
20
21 import junit.framework.TestCase;
22
23 /**
24  * TODO: Document me
25  *
26  * @author teck
27  */

28 public class NetworkListenerTest extends TestCase {
29
30   CommunicationsManager commsMgr;
31
32   public void setUp() throws Exception JavaDoc {
33     super.setUp();
34     commsMgr = new CommunicationsManagerImpl(new NullMessageMonitor(), new PlainNetworkStackHarnessFactory(),
35                                              new NullConnectionPolicy());
36   }
37
38   public void tearDown() throws Exception JavaDoc {
39     super.tearDown();
40
41     if (commsMgr != null) {
42       commsMgr.shutdown();
43     }
44   }
45
46   public void testBindException() throws Exception JavaDoc {
47     assertTrue(commsMgr.getAllListeners().length == 0);
48
49     ConnectionIDFactory cidf = new DefaultConnectionIdFactory();
50     SessionProvider sessionProvider = new NullSessionManager();
51     NetworkListener lsnr = commsMgr.createListener(sessionProvider, new TCSocketAddress(0), true, cidf, false);
52
53     try {
54       lsnr.start(Collections.EMPTY_SET);
55     } catch (IOException JavaDoc ioe) {
56       fail(ioe.getMessage());
57     }
58
59     NetworkListener lsnr2 = commsMgr.createListener(sessionProvider, new TCSocketAddress(lsnr.getBindPort()), true,
60                                                     cidf, false);
61     try {
62       lsnr2.start(Collections.EMPTY_SET);
63       fail();
64     } catch (IOException JavaDoc ioe) {
65       // expect a bind exception
66
}
67
68     assertTrue(commsMgr.getAllListeners().length == 1);
69
70     lsnr.stop(5000);
71
72     assertTrue(commsMgr.getAllListeners().length == 0);
73   }
74
75   public void testMany() throws UnknownHostException JavaDoc, TCTimeoutException {
76     assertTrue(commsMgr.getAllListeners().length == 0);
77
78     final int cnt = 20;
79     NetworkListener[] listeners = new NetworkListener[cnt];
80
81     for (int i = 0; i < cnt; i++) {
82       NetworkListener lsnr = commsMgr.createListener(new NullSessionManager(), new TCSocketAddress(InetAddress
83           .getByName("127.0.0.1"), 0), true, new DefaultConnectionIdFactory());
84
85       try {
86         lsnr.start(Collections.EMPTY_SET);
87         listeners[i] = lsnr;
88       } catch (IOException JavaDoc ioe) {
89         fail(ioe.getMessage());
90       }
91     }
92
93     assertTrue(commsMgr.getAllListeners().length == cnt);
94
95     for (int i = 0; i < cnt; i++) {
96       // try stop() twice, shouldn't fail
97

98       listeners[i].stop(5000);
99       listeners[i].stop(5000);
100     }
101
102     assertTrue(commsMgr.getAllListeners().length == 0);
103   }
104
105 }
106
Popular Tags