KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > RpcDispatcherSerializationTest


1 package org.jgroups.blocks;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5 import org.jgroups.Channel;
6 import org.jgroups.tests.ChannelTestBase;
7 import org.jgroups.util.Rsp;
8 import org.jgroups.util.RspList;
9 import org.jgroups.util.Util;
10
11 import java.io.*;
12 import java.util.Iterator JavaDoc;
13 import java.util.Vector JavaDoc;
14
15
16 public class RpcDispatcherSerializationTest extends ChannelTestBase {
17     private Channel channel, channel2;
18     private RpcDispatcher disp, disp2;
19
20
21     public RpcDispatcherSerializationTest(String JavaDoc testName) {
22         super(testName);
23     }
24
25
26     public void methodA(boolean b, long l) {
27         System.out.println("methodA(" + b + ", " + l + ") called");
28     }
29
30
31     public boolean methodB() {
32         return true;
33     }
34
35     public void methodC() {
36         throw new IllegalArgumentException JavaDoc("dummy exception - for testing only");
37     }
38
39
40     protected void setUp() throws Exception JavaDoc {
41         super.setUp();
42         channel=createChannel("A");
43         channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
44         disp=new RpcDispatcher(channel, null, null, this);
45         channel.connect("RpcDispatcherSerializationTestGroup");
46
47
48         channel2=createChannel("A");
49         disp2=new RpcDispatcher(channel2, null, null, this);
50         channel2.connect("RpcDispatcherSerializationTestGroup");
51     }
52
53
54     protected void tearDown() throws Exception JavaDoc {
55         super.tearDown();
56         channel2.close();
57         disp2.stop();
58
59         disp.stop();
60         channel.close();
61     }
62
63
64     public void testNonSerializableArgument() {
65         try {
66             disp.callRemoteMethods(null, "foo", new Object JavaDoc[]{new NonSerializable()}, new Class JavaDoc[]{NonSerializable.class},
67                                    GroupRequest.GET_ALL, 5000);
68             fail("should throw NotSerializableException");
69         }
70         catch(Throwable JavaDoc t) {
71             Throwable JavaDoc cause=t.getCause();
72             if(cause != null && cause instanceof NotSerializableException) { // this needs to be changed once we change the signature
73
System.out.println("received RuntimeException with NotSerializableException as cause - this is expected");
74             }
75             else
76                 fail("received " + t);
77         }
78     }
79
80     public void testTargetMethodNotFound() {
81         Vector JavaDoc members=channel.getView().getMembers();
82         System.out.println("members are: " + members);
83         RspList rsps=disp.callRemoteMethods(members, "foo", null, new Class JavaDoc[]{String JavaDoc.class, String JavaDoc.class},
84                                             GroupRequest.GET_ALL, 8000);
85         System.out.println("responses:\n" + rsps + ", channel.view: " + channel.getView() + ", channel2.view: " + channel2.getView());
86         assertEquals(members.size(), rsps.size());
87         for(int i=0; i < rsps.size(); i++) {
88             Rsp rsp=(Rsp)rsps.elementAt(i);
89             assertTrue("response value is " + rsp.getValue(), rsp.getValue() instanceof NoSuchMethodException JavaDoc);
90         }
91     }
92
93
94     public void testMarshaller() {
95         RpcDispatcher.Marshaller m=new MyMarshaller();
96         disp.setRequestMarshaller(m);
97         disp.setResponseMarshaller(m);
98         disp2.setRequestMarshaller(m);
99         disp2.setResponseMarshaller(m);
100
101         RspList rsps;
102         rsps=disp.callRemoteMethods(null, "methodA", new Object JavaDoc[]{Boolean.TRUE, new Long JavaDoc(322649)},
103                                     new Class JavaDoc[]{boolean.class, long.class},
104                                     GroupRequest.GET_ALL, 0);
105         assertEquals(2, rsps.size());
106         for(Iterator JavaDoc it=rsps.values().iterator(); it.hasNext();) {
107             Rsp rsp=(Rsp)it.next();
108             assertNull(rsp.getValue());
109             assertTrue(rsp.wasReceived());
110             assertFalse(rsp.wasSuspected());
111         }
112
113         rsps=disp.callRemoteMethods(null, "methodB", null, (Class JavaDoc[])null, GroupRequest.GET_ALL, 0);
114         assertEquals(2, rsps.size());
115         for(Iterator JavaDoc it=rsps.values().iterator(); it.hasNext();) {
116             Rsp rsp=(Rsp)it.next();
117             assertNotNull(rsp.getValue());
118             assertEquals(Boolean.TRUE, rsp.getValue());
119             assertTrue(rsp.wasReceived());
120             assertFalse(rsp.wasSuspected());
121         }
122
123
124         rsps=disp.callRemoteMethods(null, "methodC", null, (Class JavaDoc[])null, GroupRequest.GET_ALL, 0);
125         assertEquals(2, rsps.size());
126         for(Iterator JavaDoc it=rsps.values().iterator(); it.hasNext();) {
127             Rsp rsp=(Rsp)it.next();
128             assertNotNull(rsp.getValue());
129             assertTrue(rsp.getValue() instanceof Throwable JavaDoc);
130             assertTrue(rsp.wasReceived());
131             assertFalse(rsp.wasSuspected());
132         }
133
134         disp.setRequestMarshaller(null);
135         disp.setResponseMarshaller(null);
136         disp2.setRequestMarshaller(null);
137         disp2.setResponseMarshaller(null);
138     }
139
140
141
142     static class MyMarshaller implements RpcDispatcher.Marshaller {
143         static final byte NULL = 0;
144         static final byte BOOL = 1;
145         static final byte LONG = 2;
146         static final byte OBJ = 3;
147
148         public byte[] objectToByteBuffer(Object JavaDoc obj) throws Exception JavaDoc {
149             ByteArrayOutputStream out=new ByteArrayOutputStream(24);
150             ObjectOutputStream oos=new ObjectOutputStream(out);
151
152             try {
153                 if(obj == null) {
154                     oos.writeByte(NULL);
155                 }
156                 else if(obj instanceof Boolean JavaDoc) {
157                     oos.writeByte(BOOL);
158                     oos.writeBoolean(((Boolean JavaDoc)obj).booleanValue());
159                 }
160                 else if(obj instanceof Long JavaDoc) {
161                     oos.writeByte(LONG);
162                     oos.writeLong(((Long JavaDoc)obj).longValue());
163                 }
164                 else {
165                     oos.writeByte(OBJ);
166                     oos.writeObject(obj);
167                 }
168                 oos.flush();
169                 return out.toByteArray();
170             }
171             finally {
172                 Util.close(oos);
173             }
174         }
175
176         public Object JavaDoc objectFromByteBuffer(byte[] buf) throws Exception JavaDoc {
177             ByteArrayInputStream inp=new ByteArrayInputStream(buf);
178             ObjectInputStream in=new ObjectInputStream(inp);
179
180             try {
181                 int type=in.readByte();
182                 switch(type) {
183                     case NULL:
184                         return null;
185                     case BOOL:
186                         return new Boolean JavaDoc(in.readBoolean());
187                     case LONG:
188                         return new Long JavaDoc(in.readLong());
189                     case OBJ:
190                         return in.readObject();
191                     default:
192                         throw new IllegalArgumentException JavaDoc("incorrect type " + type);
193                 }
194             }
195             finally {
196                 Util.close(in);
197             }
198         }
199     }
200
201
202     public static Test suite() {
203         return new TestSuite(RpcDispatcherSerializationTest.class);
204     }
205
206
207     public static void main(String JavaDoc[] args) {
208         junit.textui.TestRunner.run(RpcDispatcherSerializationTest.suite());
209     }
210
211     static class NonSerializable {
212         int i;
213     }
214
215 }
216
Popular Tags