Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/codec/impl/DataImplTest.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 package org.apache.qpid.proton.codec.impl;
22
23 import static org.junit.Assert.*;
24
25 import java.nio.ByteBuffer;
26 import java.nio.charset.StandardCharsets;
27
28 import org.apache.qpid.proton.amqp.Binary;
29 import org.apache.qpid.proton.amqp.Symbol;
30 import org.apache.qpid.proton.codec.Data;
31 import org.junit.Test;
32
33 public class DataImplTest
34 {
35 @Test
36 public void testEncodeDecodeSymbolArrayUsingPutArray()
37 {
38 Symbol symbol1 = Symbol.valueOf("testRoundtripSymbolArray1");
39 Symbol symbol2 = Symbol.valueOf("testRoundtripSymbolArray2");
40
41 Data data1 = new DataImpl();
42 data1.putArray(false, Data.DataType.SYMBOL);
43 data1.enter();
44 data1.putSymbol(symbol1);
45 data1.putSymbol(symbol2);
46 data1.exit();
47
48 Binary encoded = data1.encode();
49 encoded.asByteBuffer();
50
51 Data data2 = new DataImpl();
52 data2.decode(encoded.asByteBuffer());
53
54 assertEquals("unexpected array length", 2, data2.getArray());
55 assertEquals("unexpected array length", Data.DataType.SYMBOL, data2.getArrayType());
56
57 Object[] array = data2.getJavaArray();
58 assertNotNull("Array should not be null", array);
59 assertEquals("Expected a Symbol array", Symbol[].class, array.getClass());
60 assertEquals("unexpected array length", 2, array.length);
61 assertEquals("unexpected value", symbol1, array[0]);
62 assertEquals("unexpected value", symbol2, array[1]);
63 }
64
65 @Test
66 public void testEncodeDecodeSymbolArrayUsingPutObject()
67 {
68 Symbol symbol1 = Symbol.valueOf("testRoundtripSymbolArray1");
69 Symbol symbol2 = Symbol.valueOf("testRoundtripSymbolArray2");
70 Symbol[] input = new Symbol[]{symbol1, symbol2};
71
72 Data data1 = new DataImpl();
73 data1.putObject(input);
74
75 Binary encoded = data1.encode();
76 encoded.asByteBuffer();
77
78 Data data2 = new DataImpl();
79 data2.decode(encoded.asByteBuffer());
80
81 assertEquals("unexpected array length", 2, data2.getArray());
82 assertEquals("unexpected array length", Data.DataType.SYMBOL, data2.getArrayType());
83
84 Object[] array = data2.getJavaArray();
85 assertArrayEquals("Array not as expected", input, array);
86 }
87
88 @Test
89 public void testEncodeArrayOfLists()
90 {
91 // encode an array of two empty lists
92 Data data = new DataImpl();
93 data.putArray(false, Data.DataType.LIST);
94 data.enter();
95 data.putList();
96 data.putList();
97 data.exit();
98
99 int expectedEncodedSize = 4; // 1b type + 1b size + 1b length + 1b element constructor
100
101
102 Binary encoded = data.encode();
103 assertEquals("unexpected encoding size", expectedEncodedSize, encoded.getLength());
104
105 ByteBuffer expectedEncoding = ByteBuffer.allocate(expectedEncodedSize);
106 expectedEncoding.put((byte)0xe0); // constructor
107 expectedEncoding.put((byte)2); // size
108 expectedEncoding.put((byte)2); // count
109 expectedEncoding.put((byte)0x45); // element constructor
110
111 assertEquals("unexpected encoding", new Binary(expectedEncoding.array()), encoded);
112
113 data = new DataImpl();
114 data.putArray(false, Data.DataType.LIST);
115 data.enter();
116 data.putList();
117 data.putList();
118 data.putList();
119 data.enter();
120 data.putNull();
121 data.exit();
122 data.exit();
123
124 expectedEncodedSize = 11; // 1b type + 1b size + 1b length + 1b element constructor + 3 * (1b size + 1b count) + 1b null elt
125
126 encoded = data.encode();
127 assertEquals("unexpected encoding size", expectedEncodedSize, encoded.getLength());
128
129 expectedEncoding = ByteBuffer.allocate(expectedEncodedSize);
130 expectedEncoding.put((byte)0xe0); // constructor
131 expectedEncoding.put((byte)9); // size
132 expectedEncoding.put((byte)3); // count
133 expectedEncoding.put((byte)0xc0); // element constructor
134 expectedEncoding.put((byte)1); // size
135 expectedEncoding.put((byte)0); // count
136 expectedEncoding.put((byte)1); // size
137 expectedEncoding.put((byte)0); // count
138 expectedEncoding.put((byte)2); // size
139 expectedEncoding.put((byte)1); // count
140 expectedEncoding.put((byte)0x40); // null value
141
142 assertEquals("unexpected encoding", new Binary(expectedEncoding.array()), encoded);
143
144 data = new DataImpl();
145 data.putArray(false, Data.DataType.LIST);
146 data.enter();
147 data.putList();
148 data.putList();
149 data.putList();
150 data.enter();
151 for(int i = 0; i < 256; i++)
152 {
153 data.putNull();
154 }
155 data.exit();
156 data.exit();
157
158 expectedEncodedSize = 290; // 1b type + 4b size + 4b length + 1b element constructor + 3 * (4b size + 4b count) + (256 * 1b) null elt
159 encoded = data.encode();
160 assertEquals("unexpected encoding size", expectedEncodedSize, encoded.getLength());
161
162 expectedEncoding = ByteBuffer.allocate(expectedEncodedSize);
163 expectedEncoding.put((byte)0xf0); // constructor
164 expectedEncoding.putInt(285); // size
165 expectedEncoding.putInt(3); // count
166 expectedEncoding.put((byte)0xd0); // element constructor
167 expectedEncoding.putInt(4); // size
168 expectedEncoding.putInt(0); // count
169 expectedEncoding.putInt(4); // size
170 expectedEncoding.putInt(0); // count
171 expectedEncoding.putInt(260); // size
172 expectedEncoding.putInt(256); // count
173 for(int i = 0; i < 256; i++)
174 {
175 expectedEncoding.put((byte)0x40); // null value
176 }
177
178 assertEquals("unexpected encoding", new Binary(expectedEncoding.array()), encoded);
179
180 }
181
182 @Test
183 public void testEncodeArrayOfMaps()
184 {
185 // encode an array of two empty maps
186 Data data = new DataImpl();
187 data.putArray(false, Data.DataType.MAP);
188 data.enter();
189 data.putMap();
190 data.putMap();
191 data.exit();
192
193 int expectedEncodedSize = 8; // 1b type + 1b size + 1b length + 1b element constructor + 2 * (1b size + 1b count)
194
195
196 Binary encoded = data.encode();
197 assertEquals("unexpected encoding size", expectedEncodedSize, encoded.getLength());
198
199 ByteBuffer expectedEncoding = ByteBuffer.allocate(expectedEncodedSize);
200 expectedEncoding.put((byte) 0xe0); // constructor
201 expectedEncoding.put((byte) 6); // size
202 expectedEncoding.put((byte) 2); // count
203 expectedEncoding.put((byte) 0xc1); // element constructor
204 expectedEncoding.put((byte)1); // size
205 expectedEncoding.put((byte)0); // count
206 expectedEncoding.put((byte)1); // size
207 expectedEncoding.put((byte)0); // count
208
209
210 assertEquals("unexpected encoding", new Binary(expectedEncoding.array()), encoded);
211
212 }
213
214 @Test
215 public void testEncodeString32()
216 {
217 byte[] strPayload = createStringPayloadBytes(256);
218 String content = new String(strPayload, StandardCharsets.UTF_8);
219 assertTrue("Length must be over 255 to ensure use of str32 encoding", content.length() > 255);
220
221 int encodedSize = 1 + 4 + strPayload.length; // 1b type + 4b length + content
222 ByteBuffer expectedEncoding = ByteBuffer.allocate(encodedSize);
223 expectedEncoding.put((byte) 0xB1);
224 expectedEncoding.putInt(strPayload.length);
225 expectedEncoding.put(strPayload);
226
227 Data data = new DataImpl();
228 data.putString(content);
229
230 Binary encoded = data.encode();
231
232 assertEquals("unexpected encoding", new Binary(expectedEncoding.array()), encoded);
233 }
234
235 @Test
236 public void testEncodeDecodeString32()
237 {
238 byte[] payload = createStringPayloadBytes(1025);
239 String content = new String(payload, StandardCharsets.UTF_8);
240 assertTrue("Length must be over 255 to ensure use of str32 encoding", content.length() > 255);
241
242 doEncodeDecodeStringTestImpl(content);
243 }
244
245 @Test
246 public void testEncodeDecodeString8()
247 {
248 String content = "testRoundTripString8";
249 assertTrue("Length must be <= 255 to allow use of str8 encoding", content.length() <= 255);
250
251 doEncodeDecodeStringTestImpl("testRoundTripString8");
252 }
253
254 private void doEncodeDecodeStringTestImpl(String string)
255 {
256 Data data = new DataImpl();
257 data.putString(string);
258
259 Binary encoded = data.encode();
260
261 Data data2 = new DataImpl();
262 data2.decode(encoded.asByteBuffer());
263
264 assertEquals("unexpected type", Data.DataType.STRING, data2.type());
265 assertEquals("unexpected string", string, data2.getString());
266 }
267
268 private byte[] createStringPayloadBytes(int length)
269 {
270 byte[] payload = new byte[length];
271 for (int i = 0; i < length; i++) {
272 payload[i] = (byte) ((i % 10) + 48);
273 }
274
275 return payload;
276 }
277
278 @Test
279 public void testEncodeStringBinary32()
280 {
281 byte[] payload = createStringPayloadBytes(1372);
282 assertTrue("Length must be over 255 to ensure use of vbin32 encoding", payload.length > 255);
283
284 int encodedSize = 1 + 4 + payload.length; // 1b type + 4b length + content
285 ByteBuffer expectedEncoding = ByteBuffer.allocate(encodedSize);
286 expectedEncoding.put((byte) 0xB0);
287 expectedEncoding.putInt(payload.length);
288 expectedEncoding.put(payload);
289
290 Data data = new DataImpl();
291 data.putBinary(new Binary(payload));
292
293 Binary encoded = data.encode();
294
295 assertEquals("unexpected encoding", new Binary(expectedEncoding.array()), encoded);
296 }
297
298 @Test
299 public void testEncodeDecodeBinary32()
300 {
301 byte[] initialPayload = createStringPayloadBytes(1025);
302 String initialContent = new String(initialPayload, StandardCharsets.UTF_8);
303 assertTrue("Length must be over 255 to ensure use of str32 encoding", initialContent.length() > 255);
304
305 byte[] bytesReadBack = doEncodeDecodeBinaryTestImpl(initialPayload);
306 String readBackContent = new String(bytesReadBack, StandardCharsets.UTF_8);
307 assertEquals(initialContent, readBackContent);
308 }
309
310 private byte[] doEncodeDecodeBinaryTestImpl(byte[] payload)
311 {
312 Data data = new DataImpl();
313 data.putBinary(payload);
314
315 Binary encoded = data.encode();
316
317 ByteBuffer byteBuffer = encoded.asByteBuffer();
318 Data data2 = new DataImpl();
319 long decodeResult = data2.decode(byteBuffer);
320 assertTrue(Long.toString(decodeResult), decodeResult > 0);
321
322 assertEquals("unexpected type", Data.DataType.BINARY, data2.type());
323 return data2.getBinary().getArray();
324 }
325 }