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 static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.net.SocketAddress;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.mina.core.buffer.IoBuffer;
32 import org.apache.mina.core.service.DefaultTransportMetadata;
33 import org.apache.mina.core.session.IoSession;
34 import org.apache.mina.core.session.IoSessionConfig;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38
39
40
41
42
43
44 public class CumulativeProtocolDecoderTest {
45 private final ProtocolCodecSession session = new ProtocolCodecSession();
46
47 private IoBuffer buf;
48
49 private IntegerDecoder decoder;
50
51 @Before
52 public void setUp() throws Exception {
53 buf = IoBuffer.allocate(16);
54 decoder = new IntegerDecoder();
55 session.setTransportMetadata(new DefaultTransportMetadata("mina", "dummy", false, true, SocketAddress.class,
56 IoSessionConfig.class, IoBuffer.class));
57 }
58
59 @After
60 public void tearDown() throws Exception {
61 decoder.dispose(session);
62 }
63
64 @Test
65 public void testCumulation() throws Exception {
66 buf.put((byte) 0);
67 buf.flip();
68
69 decoder.decode(session, buf, session.getDecoderOutput());
70 assertEquals(0, session.getDecoderOutputQueue().size());
71 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 assertEquals(1, session.getDecoderOutputQueue().size());
81 assertEquals(new Integer(1), session.getDecoderOutputQueue().poll());
82 assertEquals(buf.limit(), buf.position());
83 }
84
85 @Test
86 public void testRepeatitiveDecode() throws Exception {
87 for (int i = 0; i < 4; i++) {
88 buf.putInt(i);
89 }
90 buf.flip();
91
92 decoder.decode(session, buf, session.getDecoderOutput());
93 assertEquals(4, session.getDecoderOutputQueue().size());
94 assertEquals(buf.limit(), buf.position());
95
96 List<Object> expected = new ArrayList<Object>();
97
98 for (int i = 0; i < 4; i++) {
99 assertTrue(session.getDecoderOutputQueue().contains(i));
100 }
101 }
102
103 @Test
104 public void testWrongImplementationDetection() throws Exception {
105 try {
106 new WrongDecoder().decode(session, buf, session.getDecoderOutput());
107 fail();
108 } catch (IllegalStateException e) {
109
110 }
111 }
112
113 @Test
114 public void testBufferDerivation() throws Exception {
115 decoder = new DuplicatingIntegerDecoder();
116
117 buf.putInt(1);
118
119
120 buf.put((byte) 0);
121 buf.flip();
122
123 decoder.decode(session, buf, session.getDecoderOutput());
124 assertEquals(1, session.getDecoderOutputQueue().size());
125 assertEquals(1, session.getDecoderOutputQueue().poll());
126 assertEquals(buf.limit(), buf.position());
127
128
129
130
131
132
133
134
135 for (int i = 2; i < 10; i++) {
136 buf.clear();
137 buf.putInt(i);
138
139 buf.put((byte) 0);
140 buf.flip();
141 buf.position(1);
142
143 decoder.decode(session, buf, session.getDecoderOutput());
144 assertEquals(1, session.getDecoderOutputQueue().size());
145 assertEquals(i, session.getDecoderOutputQueue().poll());
146 assertEquals(buf.limit(), buf.position());
147 }
148 }
149
150 private static class IntegerDecoder extends CumulativeProtocolDecoder {
151
152
153
154 public IntegerDecoder() {
155 super();
156 }
157
158 @Override
159 protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
160 assertTrue(in.hasRemaining());
161
162 if (in.remaining() < 4) {
163 return false;
164 }
165
166 out.write(new Integer(in.getInt()));
167 return true;
168 }
169
170 public void dispose() throws Exception {
171
172 }
173 }
174
175 private static class WrongDecoder extends CumulativeProtocolDecoder {
176
177
178
179 public WrongDecoder() {
180 super();
181 }
182
183 @Override
184 protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
185 return true;
186 }
187
188 public void dispose() throws Exception {
189
190 }
191 }
192
193 private static class DuplicatingIntegerDecoder extends IntegerDecoder {
194
195
196
197 public DuplicatingIntegerDecoder() {
198 super();
199 }
200
201 @Override
202 protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
203 in.duplicate();
204 assertFalse(in.isAutoExpand());
205 return super.doDecode(session, in, out);
206 }
207
208 public void dispose() throws Exception {
209
210 }
211 }
212 }