Search in sources :

Example 1 with UnsignedInteger

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);
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) HashMap(java.util.HashMap) Symbol(org.apache.qpid.protonj2.types.Symbol) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) InputStream(java.io.InputStream) DeliveryAnnotations(org.apache.qpid.protonj2.types.messaging.DeliveryAnnotations) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) UnsignedInteger(org.apache.qpid.protonj2.types.UnsignedInteger) UUID(java.util.UUID)

Example 2 with UnsignedInteger

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]);
    }
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) InputStream(java.io.InputStream) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) UnsignedInteger(org.apache.qpid.protonj2.types.UnsignedInteger)

Example 3 with UnsignedInteger

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) {
    }
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) InputStream(java.io.InputStream) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) UnsignedInteger(org.apache.qpid.protonj2.types.UnsignedInteger)

Example 4 with UnsignedInteger

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());
    }
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) InputStream(java.io.InputStream) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) UnsignedInteger(org.apache.qpid.protonj2.types.UnsignedInteger)

Example 5 with UnsignedInteger

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());
    }
}
Also used : ProtonBuffer(org.apache.qpid.protonj2.buffer.ProtonBuffer) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) InputStream(java.io.InputStream) ProtonBufferInputStream(org.apache.qpid.protonj2.buffer.ProtonBufferInputStream) UnsignedInteger(org.apache.qpid.protonj2.types.UnsignedInteger)

Aggregations

UnsignedInteger (org.apache.qpid.protonj2.types.UnsignedInteger)17 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)13 InputStream (java.io.InputStream)11 ProtonBufferInputStream (org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)11 Symbol (org.apache.qpid.protonj2.types.Symbol)7 UUID (java.util.UUID)4 DecodeException (org.apache.qpid.protonj2.codec.DecodeException)4 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 UnsignedInteger (org.apache.qpid.protonj2.test.driver.codec.primitives.UnsignedInteger)2 Outcome (org.apache.qpid.protonj2.types.messaging.Outcome)2 Source (org.apache.qpid.protonj2.types.messaging.Source)2 Target (org.apache.qpid.protonj2.types.messaging.Target)2 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 PrimitiveTypeDecoder (org.apache.qpid.protonj2.codec.decoders.PrimitiveTypeDecoder)1