1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.filter.codec.serialization;
21
22 import java.io.ByteArrayOutputStream;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26
27 import org.apache.mina.core.buffer.IoBuffer;
28 import org.apache.mina.filter.codec.ProtocolCodecSession;
29 import org.apache.mina.filter.codec.ProtocolDecoder;
30 import org.apache.mina.filter.codec.ProtocolDecoderOutput;
31 import org.apache.mina.filter.codec.ProtocolEncoder;
32 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
33
34
35
36
37
38
39
40 public class ObjectSerializationTest extends TestCase {
41 public void testEncoder() throws Exception {
42 final String expected = "1234";
43
44 ProtocolCodecSession session = new ProtocolCodecSession();
45 ProtocolEncoderOutput out = session.getEncoderOutput();
46
47 ProtocolEncoder encoder = new ObjectSerializationEncoder();
48 encoder.encode(session, expected, out);
49
50 Assert.assertEquals(1, session.getEncoderOutputQueue().size());
51 IoBuffer buf = (IoBuffer) session.getEncoderOutputQueue().poll();
52
53 testDecoderAndInputStream(expected, buf);
54 }
55
56 public void testOutputStream() throws Exception {
57 final String expected = "1234";
58
59 ByteArrayOutputStream baos = new ByteArrayOutputStream();
60 ObjectSerializationOutputStream osos = new ObjectSerializationOutputStream(
61 baos);
62
63 osos.writeObject(expected);
64 osos.flush();
65
66 testDecoderAndInputStream(expected, IoBuffer.wrap(baos.toByteArray()));
67 }
68
69 private void testDecoderAndInputStream(String expected, IoBuffer in)
70 throws Exception {
71
72 ObjectSerializationInputStream osis = new ObjectSerializationInputStream(
73 in.duplicate().asInputStream());
74
75 Object actual = osis.readObject();
76 assertEquals(expected, actual);
77
78
79 ProtocolDecoder decoder = new ObjectSerializationDecoder();
80 ProtocolCodecSession session = new ProtocolCodecSession();
81 ProtocolDecoderOutput decoderOut = session.getDecoderOutput();
82 decoder.decode(session, in.duplicate(), decoderOut);
83
84 Assert.assertEquals(1, session.getDecoderOutputQueue().size());
85 Assert.assertEquals(expected, session.getDecoderOutputQueue().poll());
86 }
87 }