KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > transport > ServerStackProviderTest


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.transport;
6
7 import com.tc.logging.TCLogging;
8 import com.tc.net.core.MockTCConnection;
9 import com.tc.net.core.TestTCConnection;
10 import com.tc.net.protocol.StackNotFoundException;
11 import com.tc.net.protocol.transport.MockTransportHandshakeMessageFactory.CallContext;
12 import com.tc.test.TCTestCase;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 public class ServerStackProviderTest extends TCTestCase {
18
19   private ServerStackProvider provider;
20   private MockStackHarnessFactory harnessFactory;
21   private MockNetworkStackHarness harness;
22   private MockTransportHandshakeMessageFactory transportHandshakeMessageFactory;
23   private ConnectionID connId;
24   private ConnectionIDFactory connectionIdFactory;
25   private TestConnectionPolicy connectionPolicy;
26   private TestWireProtocolAdaptorFactory wpaFactory;
27   private MockMessageTransportFactory transportFactory;
28   private DefaultConnectionIdFactory connectionIDProvider;
29
30   public ServerStackProviderTest() {
31     super();
32   }
33
34   protected void setUp() throws Exception JavaDoc {
35     super.setUp();
36
37     this.harness = new MockNetworkStackHarness();
38
39     this.harnessFactory = new MockStackHarnessFactory();
40     this.harnessFactory.harness = this.harness;
41     this.connectionIdFactory = new TestConnectionIDFactory();
42
43     transportFactory = new MockMessageTransportFactory();
44     transportHandshakeMessageFactory = new MockTransportHandshakeMessageFactory();
45     connectionPolicy = new TestConnectionPolicy();
46     wpaFactory = new TestWireProtocolAdaptorFactory();
47     this.provider = new ServerStackProvider(TCLogging.getLogger(ServerStackProvider.class), new HashSet JavaDoc(),
48                                             this.harnessFactory, null, transportFactory,
49                                             transportHandshakeMessageFactory, this.connectionIdFactory,
50                                             connectionPolicy, wpaFactory);
51     connectionIDProvider = new DefaultConnectionIdFactory();
52     this.connId = connectionIDProvider.nextConnectionId();
53   }
54
55   protected void tearDown() throws Exception JavaDoc {
56     super.tearDown();
57   }
58
59   /**
60    * Test to make sure that the connection accounting is done properly.
61    */

62   public void testConnectionPolicyInteraction() throws Exception JavaDoc {
63
64     assertNull(wpaFactory.newWireProtocolAdaptorCalls.poll(0));
65     // XXX: This is yucky. This has the effect of creating a new TCProtocolAdapter which creates a wire protocol
66
// message sink which is the thing we need to drop messages on.
67
provider.getInstance();
68
69     WireProtocolMessageSink sink = (WireProtocolMessageSink) wpaFactory.newWireProtocolAdaptorCalls.take();
70     TestSynMessage syn = new TestSynMessage();
71     TestSynAckMessage synAck = new TestSynAckMessage();
72     TestTCConnection connection = new TestTCConnection();
73
74     this.transportHandshakeMessageFactory.synAck = synAck;
75     syn.connection = connection;
76
77     MockMessageTransport transport = new MockMessageTransport();
78     transportFactory.transport = transport;
79
80     // make sure that createSynACk calls in the transport handshake message factory are clear
81
assertNull(transportHandshakeMessageFactory.createSynAckCalls.poll(0));
82     // make sure the send calls in the transport are clear
83
assertNull(transport.sendToConnectionCalls.poll(0));
84     connectionPolicy.maxConnections = 13;
85     connectionPolicy.maxConnectionsExceeded = true;
86     // Send SYN message
87
sink.putMessage(syn);
88     // the client should have sent the SYN_ACK message
89
assertSame(synAck, transport.sendToConnectionCalls.take());
90
91     // The connected client count should have been incremented
92
assertEquals(1, connectionPolicy.clientConnected);
93     // make sure that the transport message factory was called with the proper arguments
94
MockTransportHandshakeMessageFactory.CallContext args = (CallContext) transportHandshakeMessageFactory.createSynAckCalls
95         .poll(0);
96     assertEquals(new Boolean JavaDoc(connectionPolicy.maxConnectionsExceeded), args.getIsMaxConnectionsExceeded());
97     assertEquals(new Integer JavaDoc(connectionPolicy.maxConnections), args.getMaxConnections());
98
99     assertEquals(0, connectionPolicy.clientDisconnected);
100     // XXX: This is yucky. THis is the connection id that the stack provider assigns to the transport (via the
101
// connection id factory)
102
transport.connectionId = this.connId;
103     provider.notifyTransportClosed(transport);
104     assertEquals(1, connectionPolicy.clientConnected);
105
106   }
107
108   public void testRebuildStack() throws Exception JavaDoc {
109     ConnectionID connectionID1 = connectionIDProvider.nextConnectionId();
110     ConnectionID connectionID2 = connectionIDProvider.nextConnectionId();
111     Set JavaDoc rebuild = new HashSet JavaDoc();
112     rebuild.add(connectionID1);
113
114     provider = new ServerStackProvider(TCLogging.getLogger(ServerStackProvider.class), rebuild, this.harnessFactory,
115                                        null, transportFactory, null, this.connectionIdFactory, connectionPolicy,
116                                        new WireProtocolAdaptorFactoryImpl());
117
118     MockTCConnection conn = new MockTCConnection();
119     provider.attachNewConnection(connectionID1, conn);
120
121     // trying to attach a stack that wasn't rebuilt at startup should fail.
122
try {
123       provider.attachNewConnection(connectionID2, new MockTCConnection());
124       fail("Expected StackNotFoundException");
125     } catch (StackNotFoundException e) {
126       // expected.
127
}
128   }
129
130   public void testNotifyTransportDisconnected() throws Exception JavaDoc {
131     TestTCConnection conn = new TestTCConnection();
132     provider.attachNewConnection(ConnectionID.NULL_ID, conn);
133
134     // send a transport disconnected event
135
MockMessageTransport transport = new MockMessageTransport();
136     transport.connectionId = this.connId;
137     assertEquals(0, connectionPolicy.clientDisconnected);
138     provider.notifyTransportDisconnected(transport);
139
140     // transport disconnect event doesnt close client
141
assertEquals(0, connectionPolicy.clientDisconnected);
142     
143     //send transport close event
144
provider.notifyTransportClosed(transport);
145     
146     // make sure that the connection policy is decremented
147
assertEquals(1, connectionPolicy.clientDisconnected);
148
149   }
150
151   public void testNotifyTransportClose() throws Exception JavaDoc {
152     TestTCConnection conn = new TestTCConnection();
153     provider.attachNewConnection(ConnectionID.NULL_ID, conn);
154
155     // try looking it up again. Make sure it found what it was looking for.
156
provider.attachNewConnection(connId, conn);
157
158     // send it a transport closed event.
159
MockMessageTransport transport = new MockMessageTransport();
160     transport.connectionId = this.connId;
161     provider.notifyTransportClosed(transport);
162
163     // make sure that a future lookup throws a StackNotFoundException
164
try {
165       provider.attachNewConnection(this.connId, conn);
166       fail("Expected StackNotFoundException.");
167     } catch (StackNotFoundException e) {
168       // expected
169
}
170   }
171
172   /**
173    * Makes sure that removeNetworkStack(String) removes the the expected stack.
174    */

175   public void testRemoveNetworkStack() throws Exception JavaDoc {
176     MockTCConnection conn = new MockTCConnection();
177     provider.attachNewConnection(ConnectionID.NULL_ID, conn);
178
179     assertEquals(harness, provider.removeNetworkStack(this.connId));
180     assertTrue(provider.removeNetworkStack(this.connId) == null);
181
182     try {
183       // try looking it up again. Make sure it throws an exception
184
provider.attachNewConnection(this.connId, conn);
185       fail("Should have thrown an exception.");
186     } catch (StackNotFoundException e) {
187       // expected
188
}
189
190     // trying to remove it again should return null.
191
assertTrue(provider.removeNetworkStack(this.connId) == null);
192   }
193
194   public void testAttachNewConnection() {
195     assertFalse(harness.wasAttachNewConnectionCalled);
196     assertFalse(harness.wasFinalizeStackCalled);
197
198     MockTCConnection conn = new MockTCConnection();
199     try {
200       provider.attachNewConnection(ConnectionID.NULL_ID, conn);
201     } catch (StackNotFoundException e) {
202       fail("was virgin, should not throw exception");
203     }
204
205     assertFalse(harness.wasAttachNewConnectionCalled);
206     assertTrue(harness.wasFinalizeStackCalled);
207
208     // test look up of existing connection ID
209
harness.wasAttachNewConnectionCalled = false;
210     harness.wasFinalizeStackCalled = false;
211
212     try {
213       provider.attachNewConnection(this.connId, conn);
214     } catch (StackNotFoundException e) {
215       fail("was virgin, should not throw exception");
216     }
217
218     assertTrue(harness.wasAttachNewConnectionCalled);
219     assertFalse(harness.wasFinalizeStackCalled);
220
221     // cause lookup failure
222
ConnectionID differentConnId = connectionIDProvider.nextConnectionId();
223     harness.wasAttachNewConnectionCalled = false;
224     harness.wasFinalizeStackCalled = false;
225
226     try {
227       provider.attachNewConnection(differentConnId, conn);
228       fail("was not virgin and had connId, but should not exist in provider");
229     } catch (StackNotFoundException e) {
230       // expected
231
}
232   }
233   
234   private class TestConnectionIDFactory extends DefaultConnectionIdFactory {
235
236     public synchronized ConnectionID nextConnectionId() {
237       connId = super.nextConnectionId();
238       return connId;
239     }
240   }
241
242 }
243
Popular Tags