1
2
3
4 package org.neo.swarm.services.tcp;
5
6 import java.lang.reflect.Method;
7 import java.net.InetAddress;
8
9 import junit.framework.TestCase;
10
11 import org.aopalliance.intercept.MethodInvocation;
12 import org.neo.swarm.core.aop.silc.comp.Invocation;
13 import org.neo.swarm.interceptor.tcp.TcpClientInterceptor;
14 import org.neo.swarm.util.network.tcp.client.TcpSender;
15 import org.neo.swarm.util.network.tcp.client.TcpSenderFactoryI;
16 import org.neo.swarm.util.serialize.Serializer;
17
18 import com.mockobjects.dynamic.C;
19 import com.mockobjects.dynamic.Mock;
20
21 /***
22 * This object does.....
23 *
24 * @author neil.avery
25 */
26 public class TcpClientInterceptorTest extends TestCase {
27
28 public void testTcpClientInterceptor() throws Exception, Throwable {
29 Mock serializer = new Mock(Serializer.class);
30 Mock tcpSenderFactory = new Mock(TcpSenderFactoryI.class);
31 Mock invocation = new Mock(MethodInvocation.class);
32 Mock tcpSender = new Mock(TcpSender.class);
33
34 Method method = MethodInvocation.class.getMethod("proceed", new Class[0]);
35
36 tcpSenderFactory.expectAndReturn("getTcpSender", C.ANY_ARGS, tcpSender.proxy());
37 serializer.expectAndReturn("serialize", C.ANY_ARGS, "blah".getBytes());
38 tcpSender.expectAndReturn("sendAndReceiveMessage", C.ANY_ARGS, "result".getBytes());
39 serializer.expectAndReturn("deserialize", C.ANY_ARGS, "results".getBytes());
40 invocation.expectAndReturn("getMethod", method);
41
42 TcpClientInterceptor tcpClientInterceptor = new TcpClientInterceptor(InetAddress.getLocalHost(), 10, (TcpSenderFactoryI) tcpSenderFactory.proxy(), (Serializer) serializer.proxy());
43 Object o = tcpClientInterceptor.invoke((MethodInvocation) invocation.proxy());
44
45 assertEquals("results", new String((byte []) o));
46 tcpSenderFactory.verify();
47 serializer.verify();
48 invocation.verify();
49 }
50
51 /***
52 * Ensure that exceptions coming off the wire are rethrown properly.
53 * @throws Exception
54 */
55 public void testTcpClientExceptionPropogation() throws Exception {
56
57 Method method = Invocation.class.getMethod("proceed", new Class[0]);
58
59 Mock tcpSenderFactory = new Mock(TcpSenderFactoryI.class);
60 Mock invocation = new Mock(Invocation.class);
61 Mock serializer = new Mock(Serializer.class);
62 Mock tcpSender = new Mock(TcpSender.class);
63
64 tcpSenderFactory.expectAndReturn("getTcpSender", C.ANY_ARGS, tcpSender.proxy());
65 serializer.expectAndReturn("serialize", C.ANY_ARGS, "blah".getBytes());
66 tcpSender.expectAndReturn("sendAndReceiveMessage", C.ANY_ARGS, "result".getBytes());
67 serializer.expectAndReturn("deserialize", C.ANY_ARGS, new MyException());
68 invocation.expectAndReturn("getMethod", method);
69
70 TcpClientInterceptor tcpClientInterceptor = new TcpClientInterceptor(InetAddress.getLocalHost(), 10, (TcpSenderFactoryI) tcpSenderFactory.proxy(), (Serializer) serializer.proxy());
71
72 boolean exceptionThrown = false;
73 try {
74 tcpClientInterceptor.invoke((MethodInvocation) invocation.proxy());
75 } catch (MyException ex) {
76 exceptionThrown = true;
77
78 }
79 assertTrue("MyException type should have been thrown...", exceptionThrown);
80 }
81
82 /***
83 * Test sync sending
84 * @throws Exception
85 */
86 public void testSyncLock() throws Exception {
87
88 Method method = Invocation.class.getMethod("setMethod", new Class[] { Method.class } );
89
90 Mock tcpSenderFactory = new Mock(TcpSenderFactoryI.class);
91 Mock invocation = new Mock(MethodInvocation.class);
92 Mock serializer = new Mock(Serializer.class);
93 Mock tcpSender = new Mock(TcpSender.class);
94
95 tcpSenderFactory.expectAndReturn("getTcpSender", C.ANY_ARGS, tcpSender.proxy());
96 serializer.expectAndReturn("serialize", C.ANY_ARGS, "blah".getBytes());
97 tcpSender.expectAndReturn("send", C.ANY_ARGS, "result".getBytes());
98 serializer.expectAndReturn("deserialize", C.ANY_ARGS, new MyException());
99 invocation.expectAndReturn("getMethod", method);
100
101 TcpClientInterceptor tcpClientInterceptor = new TcpClientInterceptor(InetAddress.getLocalHost(), 10, (TcpSenderFactoryI) tcpSenderFactory.proxy(), (Serializer) serializer.proxy());
102
103 tcpClientInterceptor.invoke((MethodInvocation) invocation.proxy());
104 }
105
106 }