use of org.apache.qpid.protonj2.types.UnsignedInteger in project qpid-protonj2 by apache.
the class DeliveryAnnotationsTypeCodecTest method doTestEncodeAndDecodeAnnotationsWithEmbeddedMaps.
public void doTestEncodeAndDecodeAnnotationsWithEmbeddedMaps(boolean fromStream) throws IOException {
final Symbol SYMBOL_1 = Symbol.valueOf("x-opt-test1");
final Symbol SYMBOL_2 = Symbol.valueOf("x-opt-test2");
final String VALUE_1 = "string";
final UnsignedInteger VALUE_2 = UnsignedInteger.valueOf(42);
final UUID VALUE_3 = UUID.randomUUID();
Map<String, Object> stringKeyedMap = new HashMap<>();
stringKeyedMap.put("key1", VALUE_1);
stringKeyedMap.put("key2", VALUE_2);
stringKeyedMap.put("key3", VALUE_3);
Map<Symbol, Object> symbolKeyedMap = new HashMap<>();
symbolKeyedMap.put(Symbol.valueOf("key1"), VALUE_1);
symbolKeyedMap.put(Symbol.valueOf("key2"), VALUE_2);
symbolKeyedMap.put(Symbol.valueOf("key3"), VALUE_3);
DeliveryAnnotations annotations = new DeliveryAnnotations(new HashMap<>());
annotations.getValue().put(SYMBOL_1, stringKeyedMap);
annotations.getValue().put(SYMBOL_2, symbolKeyedMap);
ProtonBuffer buffer = ProtonByteBufferAllocator.DEFAULT.allocate();
InputStream stream = new ProtonBufferInputStream(buffer);
encoder.writeObject(buffer, encoderState, annotations);
final Object result;
if (fromStream) {
result = streamDecoder.readObject(stream, streamDecoderState);
} else {
result = decoder.readObject(buffer, decoderState);
}
assertNotNull(result);
assertTrue(result instanceof DeliveryAnnotations);
DeliveryAnnotations readAnnotations = (DeliveryAnnotations) result;
Map<Symbol, Object> resultMap = readAnnotations.getValue();
assertEquals(annotations.getValue().size(), resultMap.size());
assertEquals(resultMap.get(SYMBOL_1), stringKeyedMap);
assertEquals(resultMap.get(SYMBOL_2), symbolKeyedMap);
}
use of org.apache.qpid.protonj2.types.UnsignedInteger in project qpid-protonj2 by apache.
the class UnsignedIntegerTypeCodecTest method testArrayOfObjects.
private void testArrayOfObjects(boolean fromStream) throws IOException {
ProtonBuffer buffer = ProtonByteBufferAllocator.DEFAULT.allocate();
InputStream stream = new ProtonBufferInputStream(buffer);
final int size = 10;
UnsignedInteger[] source = new UnsignedInteger[size];
for (int i = 0; i < size; ++i) {
source[i] = UnsignedInteger.valueOf(i);
}
encoder.writeArray(buffer, encoderState, source);
final Object result;
if (fromStream) {
result = streamDecoder.readObject(stream, streamDecoderState);
} else {
result = decoder.readObject(buffer, decoderState);
}
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertFalse(result.getClass().getComponentType().isPrimitive());
UnsignedInteger[] array = (UnsignedInteger[]) result;
assertEquals(size, array.length);
for (int i = 0; i < size; ++i) {
assertEquals(source[i], array[i]);
}
}
use of org.apache.qpid.protonj2.types.UnsignedInteger in project qpid-protonj2 by apache.
the class UnsignedIntegerTypeCodecTest method testEncodeDecodeLong.
private void testEncodeDecodeLong(boolean fromStream) throws Exception {
ProtonBuffer buffer = ProtonByteBufferAllocator.DEFAULT.allocate();
InputStream stream = new ProtonBufferInputStream(buffer);
encoder.writeUnsignedInteger(buffer, encoderState, 640l);
encoder.writeUnsignedInteger(buffer, encoderState, 0l);
if (fromStream) {
Object result = streamDecoder.readObject(stream, streamDecoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(640, ((UnsignedInteger) result).intValue());
result = streamDecoder.readObject(stream, streamDecoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(0, ((UnsignedInteger) result).intValue());
} else {
Object result = decoder.readObject(buffer, decoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(640, ((UnsignedInteger) result).intValue());
result = decoder.readObject(buffer, decoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(0, ((UnsignedInteger) result).intValue());
}
try {
encoder.writeUnsignedInteger(buffer, encoderState, UnsignedInteger.MAX_VALUE.longValue() + 1);
fail("Should fail on value out of range");
} catch (IllegalArgumentException iae) {
}
}
use of org.apache.qpid.protonj2.types.UnsignedInteger in project qpid-protonj2 by apache.
the class UnsignedIntegerTypeCodecTest method testEncodeDecodeByte.
private void testEncodeDecodeByte(boolean fromStream) throws Exception {
ProtonBuffer buffer = ProtonByteBufferAllocator.DEFAULT.allocate();
InputStream stream = new ProtonBufferInputStream(buffer);
encoder.writeUnsignedInteger(buffer, encoderState, (byte) 64);
encoder.writeUnsignedInteger(buffer, encoderState, (byte) 0);
if (fromStream) {
Object result = streamDecoder.readObject(stream, streamDecoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(64, ((UnsignedInteger) result).byteValue());
result = streamDecoder.readObject(stream, streamDecoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(0, ((UnsignedInteger) result).byteValue());
} else {
Object result = decoder.readObject(buffer, decoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(64, ((UnsignedInteger) result).byteValue());
result = decoder.readObject(buffer, decoderState);
assertTrue(result instanceof UnsignedInteger);
assertEquals(0, ((UnsignedInteger) result).byteValue());
}
}
use of org.apache.qpid.protonj2.types.UnsignedInteger in project qpid-protonj2 by apache.
the class UnsignedIntegerTypeCodecTest method doTestDecodeUnsignedIntegerSeries.
private void doTestDecodeUnsignedIntegerSeries(int size, boolean fromStream) throws IOException {
ProtonBuffer buffer = ProtonByteBufferAllocator.DEFAULT.allocate();
InputStream stream = new ProtonBufferInputStream(buffer);
for (int i = 0; i < size; ++i) {
encoder.writeUnsignedInteger(buffer, encoderState, i);
}
for (int i = 0; i < size; ++i) {
final UnsignedInteger result;
if (fromStream) {
result = streamDecoder.readUnsignedInteger(stream, streamDecoderState);
} else {
result = decoder.readUnsignedInteger(buffer, decoderState);
}
assertNotNull(result);
assertEquals(i, result.intValue());
}
}
Aggregations