Source Code Cross Referenced for StreamableTest.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » tests » 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.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // $Id: StreamableTest.java,v 1.5 2006/09/09 13:16:19 belaban Exp $
002:
003:        package org.jgroups.tests;
004:
005:        import junit.framework.Test;
006:        import junit.framework.TestCase;
007:        import junit.framework.TestSuite;
008:        import org.jgroups.*;
009:        import org.jgroups.util.Util;
010:        import org.jgroups.conf.ClassConfigurator;
011:        import org.jgroups.protocols.PingHeader;
012:        import org.jgroups.protocols.UdpHeader;
013:        import org.jgroups.protocols.WanPipeAddress;
014:        import org.jgroups.protocols.PingRsp;
015:        import org.jgroups.stack.IpAddress;
016:
017:        import java.io.ByteArrayInputStream;
018:        import java.io.ByteArrayOutputStream;
019:        import java.io.DataInputStream;
020:        import java.io.DataOutputStream;
021:        import java.util.Vector;
022:
023:        public class StreamableTest extends TestCase {
024:            Message m1, m2;
025:
026:            public StreamableTest(String name) {
027:                super (name);
028:            }
029:
030:            static {
031:                try {
032:                    ClassConfigurator.getInstance(true);
033:                } catch (ChannelException e) {
034:                    e.printStackTrace();
035:                }
036:            }
037:
038:            public void testStreamable() throws Exception {
039:                byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
040:                byte[] tmp;
041:                m1 = new Message(null, null, buf, 0, 4);
042:                m2 = new Message(null, null, buf, 4, 3);
043:
044:                ByteArrayOutputStream output = new ByteArrayOutputStream();
045:                DataOutputStream out = new DataOutputStream(output);
046:                m1.writeTo(out);
047:                out.close();
048:                tmp = output.toByteArray();
049:                output.close();
050:
051:                ByteArrayInputStream input = new ByteArrayInputStream(tmp);
052:                DataInputStream in = new DataInputStream(input);
053:                Message m3, m4;
054:
055:                m3 = new Message(false);
056:                m3.readFrom(in);
057:
058:                assertEquals(4, m3.getLength());
059:                assertEquals(4, m3.getRawBuffer().length);
060:                assertEquals(4, m3.getBuffer().length);
061:                assertEquals(0, m3.getOffset());
062:
063:                output = new ByteArrayOutputStream();
064:                out = new DataOutputStream(output);
065:                // out.writeObject(m2);
066:                m2.writeTo(out);
067:                out.close();
068:                tmp = output.toByteArray();
069:                output.close();
070:
071:                System.out.println("-- serialized buffer is " + tmp.length
072:                        + " bytes");
073:
074:                input = new ByteArrayInputStream(tmp);
075:                in = new DataInputStream(input);
076:
077:                // m4=(Message)in.readObject();
078:                m4 = new Message();
079:                m4.readFrom(in);
080:
081:                assertEquals(3, m4.getLength());
082:                assertEquals(3, m4.getBuffer().length);
083:                assertEquals(3, m4.getRawBuffer().length);
084:                assertEquals(0, m4.getOffset());
085:            }
086:
087:            public void testStreamable2() throws Exception {
088:                byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
089:                Message msg = new Message(null, null, buf, 0, 4);
090:                stream(msg);
091:            }
092:
093:            public void testStreamable3() throws Exception {
094:                byte[] buf = { 'b', 'e', 'l', 'a', 'b', 'a', 'n' };
095:                Message msg = new Message(null, null, buf, 4, 3);
096:                stream(msg);
097:            }
098:
099:            public void testNullBuffer() throws Exception {
100:                Message msg = new Message();
101:                stream(msg);
102:            }
103:
104:            public void testNonNullBuffer() throws Exception {
105:                Message msg = new Message(null, null, "Hello world".getBytes());
106:                stream(msg);
107:            }
108:
109:            public void testNonNullAddress() throws Exception {
110:                Address dest, src;
111:                dest = new IpAddress("228.1.2.3", 5555);
112:                src = new IpAddress("127.0.0.1", 6666);
113:                Message msg = new Message(dest, src, "Hello world".getBytes());
114:                stream(msg);
115:            }
116:
117:            public void testHeaders() throws Exception {
118:                Address dest, src;
119:                dest = new IpAddress("228.1.2.3", 5555);
120:                src = new IpAddress("127.0.0.1", 6666);
121:                Message msg = new Message(dest, src, "Hello world".getBytes());
122:                PingHeader hdr = new PingHeader(PingHeader.GET_MBRS_REQ,
123:                        new PingRsp(src, src, true));
124:                msg.putHeader("ping-header", hdr);
125:                UdpHeader udp_hdr = new UdpHeader("bla");
126:                msg.putHeader("udp-header", udp_hdr);
127:                stream(msg);
128:            }
129:
130:            public void testAdditionalData() throws Exception {
131:                IpAddress dest, src;
132:                dest = new IpAddress("228.1.2.3", 5555);
133:                dest.setAdditionalData("foo".getBytes());
134:                src = new IpAddress("127.0.0.1", 6666);
135:                src.setAdditionalData("foobar".getBytes());
136:                Message msg = new Message(dest, src, "Hello world".getBytes());
137:                PingHeader hdr = new PingHeader(PingHeader.GET_MBRS_REQ,
138:                        new PingRsp(src, src, false));
139:                msg.putHeader("ping-header", hdr);
140:                UdpHeader udp_hdr = new UdpHeader("bla");
141:                msg.putHeader("udp-header", udp_hdr);
142:                stream(msg);
143:            }
144:
145:            public void testDifferentAddress() throws Exception {
146:                Address dest, src;
147:                dest = new WanPipeAddress("foo");
148:                src = new WanPipeAddress("foobar");
149:                Message msg = new Message(dest, src, "Hello world".getBytes());
150:                PingHeader hdr = new PingHeader(PingHeader.GET_MBRS_REQ,
151:                        new PingRsp(src, src, false));
152:                msg.putHeader("ping-header", hdr);
153:                UdpHeader udp_hdr = new UdpHeader("bla");
154:                msg.putHeader("udp-header", udp_hdr);
155:                stream(msg);
156:            }
157:
158:            public void testMergeView() throws Exception {
159:                Vector tmp_m1, tmp_m2, m3, all, subgroups;
160:                Address a, b, c, d, e, f;
161:                View v1, v2, v3, v4, v5, view_all;
162:
163:                a = new IpAddress(1000);
164:                b = new IpAddress(2000);
165:                c = new IpAddress(3000);
166:                d = new IpAddress(4000);
167:                e = new IpAddress(5000);
168:                f = new IpAddress(6000);
169:
170:                tmp_m1 = new Vector();
171:                tmp_m2 = new Vector();
172:                m3 = new Vector();
173:                all = new Vector();
174:                subgroups = new Vector();
175:                tmp_m1.add(a);
176:                tmp_m1.add(b);
177:                tmp_m1.add(c);
178:                tmp_m2.add(d);
179:                m3.add(e);
180:                m3.add(f);
181:                all.add(a);
182:                all.add(b);
183:                all.add(c);
184:                all.add(d);
185:                all.add(e);
186:                all.add(f);
187:
188:                v1 = new View(a, 1, tmp_m1);
189:                v2 = new MergeView(d, 2, tmp_m2, new Vector());
190:                v3 = new View(e, 3, m3);
191:                v4 = new MergeView(e, 4, m3, null);
192:                v5 = new View(e, 5, m3);
193:                subgroups.add(v1);
194:                subgroups.add(v2);
195:                subgroups.add(v3);
196:                subgroups.add(v4);
197:                subgroups.add(v5);
198:
199:                view_all = new MergeView(a, 5, all, subgroups);
200:                System.out.println("MergeView: " + view_all);
201:                Vector sub = ((MergeView) view_all).getSubgroups();
202:                assertTrue(sub.get(0) instanceof  View);
203:                assertTrue(sub.get(1) instanceof  MergeView);
204:                assertTrue(sub.get(2) instanceof  View);
205:                assertTrue(sub.get(3) instanceof  MergeView);
206:                assertTrue(sub.get(4) instanceof  View);
207:
208:                byte[] buf = Util.streamableToByteBuffer(view_all);
209:                assertNotNull(buf);
210:                assertTrue(buf.length > 0);
211:
212:                MergeView merge_view = (MergeView) Util
213:                        .streamableFromByteBuffer(MergeView.class, buf);
214:                assertNotNull(merge_view);
215:                System.out.println("MergeView: " + merge_view);
216:                sub = merge_view.getSubgroups();
217:                assertTrue(sub.get(0) instanceof  View);
218:                assertTrue(sub.get(1) instanceof  MergeView);
219:                assertTrue(sub.get(2) instanceof  View);
220:                assertTrue(sub.get(3) instanceof  MergeView);
221:                assertTrue(sub.get(4) instanceof  View);
222:            }
223:
224:            private void stream(Message msg) throws Exception {
225:                int length, bufLength;
226:                byte[] tmp;
227:                Message msg2;
228:                Address src;
229:                int num_headers = getNumHeaders(msg);
230:
231:                length = msg.getLength();
232:                bufLength = getBufLength(msg);
233:                src = msg.getSrc();
234:
235:                ByteArrayOutputStream output = new ByteArrayOutputStream();
236:                DataOutputStream out = new DataOutputStream(output);
237:                msg.writeTo(out);
238:                out.close();
239:                tmp = output.toByteArray();
240:                output.close();
241:
242:                System.out.println("-- serialized buffer is " + tmp.length
243:                        + " bytes");
244:
245:                ByteArrayInputStream input = new ByteArrayInputStream(tmp);
246:                DataInputStream in = new DataInputStream(input);
247:
248:                msg2 = new Message();
249:                msg2.readFrom(in);
250:
251:                assertEquals(length, msg2.getLength());
252:                assertEquals(bufLength, getBufLength(msg2));
253:                // assertTrue(match(dest, msg2.getDest()));
254:                assertNull(msg2.getDest()); // we don't marshal the destination address
255:                assertTrue(match(src, msg2.getSrc()));
256:                assertEquals(num_headers, getNumHeaders(msg2));
257:            }
258:
259:            private int getNumHeaders(Message msg) {
260:                return msg.getHeaders() != null ? msg.getHeaders().size() : 0;
261:            }
262:
263:            private boolean match(Address a1, Address a2) {
264:                if (a1 == null && a2 == null)
265:                    return true;
266:                if (a1 != null)
267:                    return a1.equals(a2);
268:                else
269:                    return a2.equals(a1);
270:            }
271:
272:            //    private int getRawBufLength(Message msg) {
273:            //        return msg.getRawBuffer() != null? msg.getRawBuffer().length : 0;
274:            //    }
275:
276:            private int getBufLength(Message msg) {
277:                return msg.getBuffer() != null ? msg.getBuffer().length : 0;
278:            }
279:
280:            public static Test suite() {
281:                TestSuite s = new TestSuite(StreamableTest.class);
282:                return s;
283:            }
284:
285:            public static void main(String[] args) {
286:                junit.textui.TestRunner.run(suite());
287:            }
288:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.