1 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 ; 15 import java.util.Set ; 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 { 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 (), 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 { 56 super.tearDown(); 57 } 58 59 62 public void testConnectionPolicyInteraction() throws Exception { 63 64 assertNull(wpaFactory.newWireProtocolAdaptorCalls.poll(0)); 65 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 assertNull(transportHandshakeMessageFactory.createSynAckCalls.poll(0)); 82 assertNull(transport.sendToConnectionCalls.poll(0)); 84 connectionPolicy.maxConnections = 13; 85 connectionPolicy.maxConnectionsExceeded = true; 86 sink.putMessage(syn); 88 assertSame(synAck, transport.sendToConnectionCalls.take()); 90 91 assertEquals(1, connectionPolicy.clientConnected); 93 MockTransportHandshakeMessageFactory.CallContext args = (CallContext) transportHandshakeMessageFactory.createSynAckCalls 95 .poll(0); 96 assertEquals(new Boolean (connectionPolicy.maxConnectionsExceeded), args.getIsMaxConnectionsExceeded()); 97 assertEquals(new Integer (connectionPolicy.maxConnections), args.getMaxConnections()); 98 99 assertEquals(0, connectionPolicy.clientDisconnected); 100 transport.connectionId = this.connId; 103 provider.notifyTransportClosed(transport); 104 assertEquals(1, connectionPolicy.clientConnected); 105 106 } 107 108 public void testRebuildStack() throws Exception { 109 ConnectionID connectionID1 = connectionIDProvider.nextConnectionId(); 110 ConnectionID connectionID2 = connectionIDProvider.nextConnectionId(); 111 Set rebuild = new HashSet (); 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 try { 123 provider.attachNewConnection(connectionID2, new MockTCConnection()); 124 fail("Expected StackNotFoundException"); 125 } catch (StackNotFoundException e) { 126 } 128 } 129 130 public void testNotifyTransportDisconnected() throws Exception { 131 TestTCConnection conn = new TestTCConnection(); 132 provider.attachNewConnection(ConnectionID.NULL_ID, conn); 133 134 MockMessageTransport transport = new MockMessageTransport(); 136 transport.connectionId = this.connId; 137 assertEquals(0, connectionPolicy.clientDisconnected); 138 provider.notifyTransportDisconnected(transport); 139 140 assertEquals(0, connectionPolicy.clientDisconnected); 142 143 provider.notifyTransportClosed(transport); 145 146 assertEquals(1, connectionPolicy.clientDisconnected); 148 149 } 150 151 public void testNotifyTransportClose() throws Exception { 152 TestTCConnection conn = new TestTCConnection(); 153 provider.attachNewConnection(ConnectionID.NULL_ID, conn); 154 155 provider.attachNewConnection(connId, conn); 157 158 MockMessageTransport transport = new MockMessageTransport(); 160 transport.connectionId = this.connId; 161 provider.notifyTransportClosed(transport); 162 163 try { 165 provider.attachNewConnection(this.connId, conn); 166 fail("Expected StackNotFoundException."); 167 } catch (StackNotFoundException e) { 168 } 170 } 171 172 175 public void testRemoveNetworkStack() throws Exception { 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 provider.attachNewConnection(this.connId, conn); 185 fail("Should have thrown an exception."); 186 } catch (StackNotFoundException e) { 187 } 189 190 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 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 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 } 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 |