| 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.message.impl; |
| 22 |
|
| 23 |
import static org.junit.Assert.assertArrayEquals; |
| 24 |
import static org.junit.Assert.assertEquals; |
| 25 |
|
| 26 |
import java.nio.ByteBuffer; |
| 27 |
|
| 28 |
import org.apache.qpid.proton.amqp.Binary; |
| 29 |
import org.apache.qpid.proton.amqp.messaging.Data; |
| 30 |
import org.apache.qpid.proton.codec.WritableBuffer; |
| 31 |
import org.apache.qpid.proton.codec.WritableBuffer.ByteBufferWrapper; |
| 32 |
import org.apache.qpid.proton.message.Message; |
| 33 |
import org.junit.Test; |
| 34 |
|
| 35 |
public class MessageImplTest |
| 36 |
{ |
| 37 |
private static final long DATA_SECTION_ULONG_DESCRIPTOR = 0x0000000000000075L; |
| 38 |
|
| 39 |
@Test |
| 40 |
public void testEncodeOfMessageWithSmallDataBodyOnly() |
| 41 |
{ |
| 42 |
doMessageEncodingWithDataBodySectionTestImpl(5); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void testEncodeOfMessageWithLargerDataBodyOnly() |
| 47 |
{ |
| 48 |
doMessageEncodingWithDataBodySectionTestImpl(1024); |
| 49 |
} |
| 50 |
|
| 51 |
void doMessageEncodingWithDataBodySectionTestImpl(int bytesLength) |
| 52 |
{ |
| 53 |
byte[] bytes = generateByteArray(bytesLength); |
| 54 |
|
| 55 |
byte[] expectedBytes = generateExpectedDataSectionBytes(bytes); |
| 56 |
byte[] encodedBytes = new byte[expectedBytes.length]; |
| 57 |
|
| 58 |
Message msg = Message.Factory.create(); |
| 59 |
msg.setBody(new Data(new Binary(bytes))); |
| 60 |
|
| 61 |
int encodedLength = msg.encode(encodedBytes, 0, encodedBytes.length); |
| 62 |
|
| 63 |
assertArrayEquals("Encoded bytes do not match expectation", expectedBytes, encodedBytes); |
| 64 |
assertEquals("Encoded length different than expected length", encodedLength, encodedBytes.length); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void testEncodeOfMessageWithSmallDataBodyOnlyUsingWritableBuffer() |
| 69 |
{ |
| 70 |
doMessageEncodingWithDataBodySectionTestImpl(5); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void testEncodeOfMessageWithLargerDataBodyOnlyUsingWritableBuffer() |
| 75 |
{ |
| 76 |
doMessageEncodingWithDataBodySectionTestImpl(1024); |
| 77 |
} |
| 78 |
|
| 79 |
void doMessageEncodingWithDataBodySectionTestImplUsingWritableBuffer(int bytesLength) |
| 80 |
{ |
| 81 |
byte[] bytes = generateByteArray(bytesLength); |
| 82 |
|
| 83 |
byte[] expectedBytes = generateExpectedDataSectionBytes(bytes); |
| 84 |
ByteBufferWrapper encodedBytes = WritableBuffer.ByteBufferWrapper.allocate(expectedBytes.length); |
| 85 |
|
| 86 |
Message msg = Message.Factory.create(); |
| 87 |
msg.setBody(new Data(new Binary(bytes))); |
| 88 |
|
| 89 |
int encodedLength = msg.encode(encodedBytes); |
| 90 |
|
| 91 |
assertArrayEquals("Encoded bytes do not match expectation", expectedBytes, encodedBytes.byteBuffer().array()); |
| 92 |
assertEquals("Encoded length different than expected length", encodedLength, encodedBytes.position()); |
| 93 |
} |
| 94 |
|
| 95 |
private byte[] generateByteArray(int bytesLength) |
| 96 |
{ |
| 97 |
byte[] bytes = new byte[bytesLength]; |
| 98 |
for(int i = 0; i < bytesLength; i++) |
| 99 |
{ |
| 100 |
bytes [i] = (byte) (i % 10); |
| 101 |
} |
| 102 |
|
| 103 |
return bytes; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-messaging-v1.0-os.html#type-data |
| 108 |
* http://docs.oasis-open.org/amqp/core/v1.0/os/amqp-core-types-v1.0-os.html |
| 109 |
* |
| 110 |
* ulong encodings: |
| 111 |
* <encoding code="0x80" category="fixed" width="8" label="64-bit unsigned integer in network byte order"/> |
| 112 |
* <encoding name="smallulong" code="0x53" category="fixed" width="1" label="unsigned long value in the range 0 to 255 inclusive"/> |
| 113 |
* <encoding name="ulong0" code="0x44" category="fixed" width="0" label="the ulong value 0"/> |
| 114 |
* |
| 115 |
* binary encodings: |
| 116 |
* <encoding name="vbin8" code="0xa0" category="variable" width="1" label="up to 2^8 - 1 octets of binary data"/> |
| 117 |
* <encoding name="vbin32" code="0xb0" category="variable" width="4" label="up to 2^32 - 1 octets of binary data"/> |
| 118 |
*/ |
| 119 |
byte[] generateExpectedDataSectionBytes(final byte[] payloadBytes) |
| 120 |
{ |
| 121 |
int dataBytesLength = 1; // 0x00 for described-type constructor start |
| 122 |
dataBytesLength += 1; // smallulong encoding format for data section descriptor |
| 123 |
dataBytesLength += 1; // smallulong 8bit value |
| 124 |
dataBytesLength += 1; // vbin variable-width binary encoding format. |
| 125 |
if (payloadBytes.length > 255) |
| 126 |
{ |
| 127 |
dataBytesLength += 4; // 32bit length field. |
| 128 |
} |
| 129 |
else |
| 130 |
{ |
| 131 |
dataBytesLength += 1; // 8bit length field. |
| 132 |
} |
| 133 |
dataBytesLength += payloadBytes.length; // section payload length. |
| 134 |
|
| 135 |
ByteBuffer buffer = ByteBuffer.allocate(dataBytesLength); |
| 136 |
|
| 137 |
buffer.put((byte) 0x00); // 0x00 for described-type constructor start |
| 138 |
buffer.put((byte) 0x53); // smallulong encoding format for data section descriptor |
| 139 |
buffer.put((byte) DATA_SECTION_ULONG_DESCRIPTOR); // smallulong 8bit value |
| 140 |
if (payloadBytes.length > 255) |
| 141 |
{ |
| 142 |
buffer.put((byte) 0xb0); // vbin32 variable-width binary encoding format. |
| 143 |
buffer.putInt(payloadBytes.length); // 32bit length field. |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
buffer.put((byte) 0xa0); // vbin8 variable-width binary encoding format. |
| 148 |
buffer.put((byte) payloadBytes.length); // 8bit length field. |
| 149 |
} |
| 150 |
buffer.put(payloadBytes); // The actual content of given length. |
| 151 |
|
| 152 |
assertEquals("Unexpected buffer position", dataBytesLength, buffer.position()); |
| 153 |
|
| 154 |
return buffer.array(); |
| 155 |
} |
| 156 |
|
| 157 |
} |