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;
21
22 import java.net.SocketAddress;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import junit.framework.Assert;
27 import junit.framework.TestCase;
28
29 import org.apache.mina.common.DefaultTransportMetadata;
30 import org.apache.mina.common.IoBuffer;
31 import org.apache.mina.common.IoSession;
32 import org.apache.mina.common.IoSessionConfig;
33
34
35
36
37
38
39
40 public class CumulativeProtocolDecoderTest extends TestCase {
41 private final ProtocolCodecSession session = new ProtocolCodecSession();
42
43 private IoBuffer buf;
44 private IntegerDecoder decoder;
45
46 public static void main(String[] args) {
47 junit.textui.TestRunner.run(CumulativeProtocolDecoderTest.class);
48 }
49
50 @Override
51 protected void setUp() throws Exception {
52 buf = IoBuffer.allocate(16);
53 decoder = new IntegerDecoder();
54 session.setTransportMetadata(
55 new DefaultTransportMetadata(
56 "mina", "dummy", false, true, SocketAddress.class,
57 IoSessionConfig.class, IoBuffer.class));
58 }
59
60 @Override
61 protected void tearDown() throws Exception {
62 decoder.dispose(session);
63 }
64
65 public void testCumulation() throws Exception {
66 buf.put((byte) 0);
67 buf.flip();
68
69 decoder.decode(session, buf, session.getDecoderOutput());
70 Assert.assertEquals(0, session.getDecoderOutputQueue().size());
71 Assert.assertEquals(buf.limit(), buf.position());
72
73 buf.clear();
74 buf.put((byte) 0);
75 buf.put((byte) 0);
76 buf.put((byte) 1);
77 buf.flip();
78
79 decoder.decode(session, buf, session.getDecoderOutput());
80 Assert.assertEquals(1, session.getDecoderOutputQueue().size());
81 Assert.assertEquals(new Integer(1), session.getDecoderOutputQueue().poll());
82 Assert.assertEquals(buf.limit(), buf.position());
83 }
84
85 public void testRepeatitiveDecode() throws Exception {
86 for (int i = 0; i < 4; i++) {
87 buf.putInt(i);
88 }
89 buf.flip();
90
91 decoder.decode(session, buf, session.getDecoderOutput());
92 Assert.assertEquals(4, session.getDecoderOutputQueue().size());
93 Assert.assertEquals(buf.limit(), buf.position());
94
95 List<Object> expected = new ArrayList<Object>();
96 for (int i = 0; i < 4; i++) {
97 expected.add(new Integer(i));
98 }
99 Assert.assertEquals(expected, session.getDecoderOutputQueue());
100 }
101
102 public void testWrongImplementationDetection() throws Exception {
103 try {
104 new WrongDecoder().decode(session, buf, session.getDecoderOutput());
105 Assert.fail();
106 } catch (IllegalStateException e) {
107
108 }
109 }
110
111 private static class IntegerDecoder extends CumulativeProtocolDecoder {
112
113 @Override
114 protected boolean doDecode(IoSession session, IoBuffer in,
115 ProtocolDecoderOutput out) throws Exception {
116 Assert.assertTrue(in.hasRemaining());
117 if (in.remaining() < 4) {
118 return false;
119 }
120
121 out.write(new Integer(in.getInt()));
122 return true;
123 }
124
125 public void dispose() throws Exception {
126 }
127
128 }
129
130 private static class WrongDecoder extends CumulativeProtocolDecoder {
131
132 @Override
133 protected boolean doDecode(IoSession session, IoBuffer in,
134 ProtocolDecoderOutput out) throws Exception {
135 return true;
136 }
137
138 public void dispose() throws Exception {
139 }
140 }
141 }