1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.codec.delimited.ints;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.fail;
25
26 import java.nio.ByteBuffer;
27 import java.util.Map;
28
29 import org.apache.mina.codec.IoBuffer;
30 import org.apache.mina.codec.ProtocolDecoderException;
31 import org.apache.mina.codec.delimited.ByteBufferEncoder;
32 import org.apache.mina.codec.delimited.IoBufferDecoder;
33 import org.junit.Before;
34 import org.junit.Test;
35
36
37
38
39
40
41 public abstract class IntEncodingTest {
42
43 protected ByteBufferEncoder<Integer> encoder;
44
45 protected IoBufferDecoder<Integer> decoder;
46
47 public abstract IoBufferDecoder<Integer> newDecoderInstance();
48
49 public abstract ByteBufferEncoder<Integer> newEncoderInstance();
50
51 public abstract Map<Integer, ByteBuffer> getEncodingSamples();
52
53 public abstract Iterable<ByteBuffer> getIllegalBuffers();
54
55 @Before
56 public void prepareDecoder() {
57 decoder = newDecoderInstance();
58 encoder = newEncoderInstance();
59 }
60
61 @Test
62 public void testTruncatedValues() {
63 for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) {
64
65 IoBuffer buffer = IoBuffer.wrap(encoder.encode(value));
66
67 for (int i = 0; i < buffer.remaining(); i++) {
68 IoBuffer partialBuffer = buffer.slice();
69 partialBuffer.limit(partialBuffer.position() + i);
70 try {
71 assertNull(decoder.decode(partialBuffer));
72 } catch (ProtocolDecoderException e) {
73 fail("Should not throw exception");
74 }
75 }
76 }
77 }
78
79 @Test
80 public void testSizedValues() {
81 for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) {
82 ByteBuffer buffer = encoder.encode(value);
83
84 try {
85 assertEquals(value, decoder.decode(IoBuffer.wrap(buffer)).intValue());
86 } catch (ProtocolDecoderException e) {
87 fail("Should not throw exception");
88 }
89 }
90 }
91
92 @Test
93 public void testExtendedValues() {
94 for (int value : new int[] { 0, 1, 127, 128, 65536, 198649, Integer.MAX_VALUE }) {
95
96 ByteBuffer buffer = encoder.encode(value);
97
98 for (int i = 1; i < 5; i++) {
99 int size = buffer.remaining() + i;
100 IoBuffer extendedBuffer = IoBuffer.wrap(ByteBuffer.allocate(size));
101 int start = extendedBuffer.position();
102 extendedBuffer.put(buffer.slice());
103 extendedBuffer.position(start);
104 extendedBuffer.limit(start + size);
105
106 try {
107 decoder.decode(extendedBuffer);
108 assertEquals(i, extendedBuffer.remaining());
109 } catch (ProtocolDecoderException e) {
110 fail("Should not throw exception");
111 }
112 }
113 }
114 }
115
116 @Test
117 public void testSamples() {
118 Map<Integer, ByteBuffer> samples = getEncodingSamples();
119 for (Integer val : samples.keySet()) {
120 assertEquals(samples.get(val), encoder.encode(val));
121 try {
122 assertEquals(val, decoder.decode(IoBuffer.wrap(samples.get(val))));
123 } catch (ProtocolDecoderException e) {
124 fail("Should not throw exception");
125 }
126 }
127 }
128
129 @Test
130 public void testOverflow() {
131
132 for (ByteBuffer buffer : getIllegalBuffers()) {
133 try {
134 decoder.decode(IoBuffer.wrap(buffer));
135 fail("Should throw an overflow exception");
136 } catch (ProtocolDecoderException e) {
137
138 }
139 }
140 }
141
142
143
144
145
146
147
148
149 }