Source Code Cross Referenced for RpcDispatcherSerializationTest.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » blocks » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » JGroups 2.4.1 sp3 » org.jgroups.blocks 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.