1   /*
2    * Created on Jan 20, 2004
3    *
4    */
5   package org.neo.swarm.services.tcp;
6   
7   import junit.framework.TestCase;
8   
9   import org.neo.swarm.ApplicationContext;
10  import org.neo.swarm.core.aop.silc.comp.Invocation;
11  import org.neo.swarm.services.tcp.ServerCompRouter;
12  import org.neo.swarm.util.serialize.Serializer;
13  
14  import com.mockobjects.dynamic.C;
15  import com.mockobjects.dynamic.Mock;
16  
17  /***
18   *  @author navery
19   */
20  public class ServerCompRouterTest extends TestCase {
21  
22  	public void testAppContextRemoteLookupWorks() throws Exception {
23  		
24  		Mock ac = new Mock(ApplicationContext.class);
25  		Mock serializer = new Mock(Serializer.class);
26  		Mock invocation = new Mock(Invocation.class);	
27  		
28  		invocation.expectAndReturn("getKey", "blah");
29  		invocation.expectAndReturn("getMethod", ServerCompRouter.class.getMethods()[0]);
30  		ac.expectAndReturn("inject", C.ANY_ARGS, "returnStuff");
31  		
32  		serializer.expectAndReturn("deserialize", C.ANY_ARGS, invocation.proxy());
33  		serializer.expectAndReturn("serialize", C.ANY_ARGS, new byte[0] );
34  		ServerCompRouter scr = new ServerCompRouter((ApplicationContext) ac.proxy(), (Serializer) serializer.proxy());
35  		String result = new String(scr.messageDataReceived( new String("boo").getBytes()));
36  		ac.verify();
37  		invocation.verify();
38  		serializer.verify();
39  	}
40  
41  	public void testThatExceptionsGetSerialized() throws Exception {
42  		
43  		Mock ac = new Mock(ApplicationContext.class);
44  		Mock serializer = new Mock(Serializer.class);
45  		Mock invocation = new Mock(Invocation.class);	
46  		
47  		serializer.expectAndReturn("deserialize", C.ANY_ARGS, invocation.proxy());
48  		serializer.expectAndReturn("serialize", C.isA(MyException.class), "exception".getBytes());
49  		invocation.expectAndReturn("getKey", "blah");		
50  		
51  		// exercies the exception path in the server comp router
52  		ac.expectAndThrow("inject", C.ANY_ARGS, new MyException());
53  		
54  		ServerCompRouter scr = new ServerCompRouter((ApplicationContext) ac.proxy(), (Serializer) serializer.proxy());
55  		byte[] obj = scr.messageDataReceived(new String("boo").getBytes());
56  		assertEquals("exception", new String(obj));
57  		ac.verify();
58  		serializer.verify();
59  		invocation.verify();
60  	}
61  }