001: package org.jgroups.blocks;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.jgroups.Channel;
007: import org.jgroups.JChannel;
008: import org.jgroups.util.Rsp;
009: import org.jgroups.util.RspList;
010: import org.jgroups.util.Util;
011:
012: import java.io.*;
013: import java.util.Iterator;
014: import java.util.Vector;
015:
016: public class RpcDispatcherSerializationTest extends TestCase {
017: private JChannel channel, channel2;
018: private RpcDispatcher disp, disp2;
019: private String props = null;
020:
021: public RpcDispatcherSerializationTest(String testName) {
022: super (testName);
023: }
024:
025: public void methodA(boolean b, long l) {
026: System.out.println("methodA(" + b + ", " + l + ") called");
027: }
028:
029: public boolean methodB() {
030: return true;
031: }
032:
033: public void methodC() {
034: throw new IllegalArgumentException(
035: "dummy exception - for testing only");
036: }
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040: channel = new JChannel(props);
041: channel.setOpt(Channel.AUTO_RECONNECT, Boolean.TRUE);
042: disp = new RpcDispatcher(channel, null, null, this );
043: channel.connect("RpcDispatcherSerializationTestGroup");
044:
045: channel2 = new JChannel(props);
046: disp2 = new RpcDispatcher(channel2, null, null, this );
047: channel2.connect("RpcDispatcherSerializationTestGroup");
048: }
049:
050: protected void tearDown() throws Exception {
051: super .tearDown();
052: channel2.close();
053: disp2.stop();
054:
055: disp.stop();
056: channel.close();
057: }
058:
059: public void testNonSerializableArgument() {
060: try {
061: disp.callRemoteMethods(null, "foo",
062: new Object[] { new NonSerializable() },
063: new Class[] { NonSerializable.class },
064: GroupRequest.GET_ALL, 5000);
065: fail("should throw NotSerializableException");
066: } catch (Throwable t) {
067: Throwable cause = t.getCause();
068: if (cause != null
069: && cause instanceof NotSerializableException) { // this needs to be changed once we change the signature
070: System.out
071: .println("received RuntimeException with NotSerializableException as cause - this is expected");
072: } else
073: fail("received " + t);
074: }
075: }
076:
077: public void testTargetMethodNotFound() {
078: Vector members = channel.getView().getMembers();
079: System.out.println("members are: " + members);
080: RspList rsps = disp.callRemoteMethods(members, "foo", null,
081: new Class[] { String.class, String.class },
082: GroupRequest.GET_ALL, 8000);
083: System.out.println("responses:\n" + rsps + ", channel.view: "
084: + channel.getView() + ", channel2.view: "
085: + channel2.getView());
086: assertEquals(members.size(), rsps.size());
087: for (int i = 0; i < rsps.size(); i++) {
088: Rsp rsp = (Rsp) rsps.elementAt(i);
089: assertTrue(rsp.getValue() instanceof NoSuchMethodException);
090: }
091: }
092:
093: public void testMarshaller() {
094: RpcDispatcher.Marshaller m = new MyMarshaller();
095: disp.setRequestMarshaller(m);
096: disp.setResponseMarshaller(m);
097: disp2.setRequestMarshaller(m);
098: disp2.setResponseMarshaller(m);
099:
100: RspList rsps;
101: rsps = disp.callRemoteMethods(null, "methodA", new Object[] {
102: Boolean.TRUE, new Long(322649) }, new Class[] {
103: boolean.class, long.class }, GroupRequest.GET_ALL, 0);
104: assertEquals(2, rsps.size());
105: for (Iterator it = rsps.values().iterator(); it.hasNext();) {
106: Rsp rsp = (Rsp) it.next();
107: assertNull(rsp.getValue());
108: assertTrue(rsp.wasReceived());
109: assertFalse(rsp.wasSuspected());
110: }
111:
112: rsps = disp.callRemoteMethods(null, "methodB", null,
113: (Class[]) null, GroupRequest.GET_ALL, 0);
114: assertEquals(2, rsps.size());
115: for (Iterator 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: rsps = disp.callRemoteMethods(null, "methodC", null,
124: (Class[]) null, GroupRequest.GET_ALL, 0);
125: assertEquals(2, rsps.size());
126: for (Iterator it = rsps.values().iterator(); it.hasNext();) {
127: Rsp rsp = (Rsp) it.next();
128: assertNotNull(rsp.getValue());
129: assertTrue(rsp.getValue() instanceof Throwable);
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: static class MyMarshaller implements RpcDispatcher.Marshaller {
141: static final byte NULL = 0;
142: static final byte BOOL = 1;
143: static final byte LONG = 2;
144: static final byte OBJ = 3;
145:
146: public byte[] objectToByteBuffer(Object obj) throws Exception {
147: ByteArrayOutputStream out = new ByteArrayOutputStream(24);
148: ObjectOutputStream oos = new ObjectOutputStream(out);
149:
150: try {
151: if (obj == null) {
152: oos.writeByte(NULL);
153: } else if (obj instanceof Boolean) {
154: oos.writeByte(BOOL);
155: oos.writeBoolean(((Boolean) obj).booleanValue());
156: } else if (obj instanceof Long) {
157: oos.writeByte(LONG);
158: oos.writeLong(((Long) obj).longValue());
159: } else {
160: oos.writeByte(OBJ);
161: oos.writeObject(obj);
162: }
163: oos.flush();
164: return out.toByteArray();
165: } finally {
166: Util.close(oos);
167: }
168: }
169:
170: public Object objectFromByteBuffer(byte[] buf) throws Exception {
171: ByteArrayInputStream inp = new ByteArrayInputStream(buf);
172: ObjectInputStream in = new ObjectInputStream(inp);
173:
174: try {
175: int type = in.readByte();
176: switch (type) {
177: case NULL:
178: return null;
179: case BOOL:
180: return new Boolean(in.readBoolean());
181: case LONG:
182: return new Long(in.readLong());
183: case OBJ:
184: return in.readObject();
185: default:
186: throw new IllegalArgumentException(
187: "incorrect type " + type);
188: }
189: } finally {
190: Util.close(in);
191: }
192: }
193: }
194:
195: public static Test suite() {
196: return new TestSuite(RpcDispatcherSerializationTest.class);
197: }
198:
199: public static void main(String[] args) {
200: junit.textui.TestRunner.run(RpcDispatcherSerializationTest
201: .suite());
202: }
203:
204: static class NonSerializable {
205: int i;
206: }
207:
208: }
|