You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ro...@apache.org on 2017/03/07 11:29:28 UTC
qpid-proton-j git commit: PROTON-1384: make the Data utility encode
str32 lengths using the required 4 bytes width
Repository: qpid-proton-j
Updated Branches:
refs/heads/master 083833d30 -> 2e95aa942
PROTON-1384: make the Data utility encode str32 lengths using the required 4 bytes width
Project: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/commit/2e95aa94
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/tree/2e95aa94
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/diff/2e95aa94
Branch: refs/heads/master
Commit: 2e95aa942b18098ea24b04264f077c51d588e7ee
Parents: 083833d
Author: Robert Gemmell <ro...@apache.org>
Authored: Tue Mar 7 11:28:49 2017 +0000
Committer: Robert Gemmell <ro...@apache.org>
Committed: Tue Mar 7 11:28:49 2017 +0000
----------------------------------------------------------------------
.../qpid/proton/codec/impl/StringElement.java | 2 +-
.../qpid/proton/codec/impl/DataImplTest.java | 67 ++++++++++++++++++++
2 files changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2e95aa94/proton-j/src/main/java/org/apache/qpid/proton/codec/impl/StringElement.java
----------------------------------------------------------------------
diff --git a/proton-j/src/main/java/org/apache/qpid/proton/codec/impl/StringElement.java b/proton-j/src/main/java/org/apache/qpid/proton/codec/impl/StringElement.java
index e3be671..f5b3a5e 100644
--- a/proton-j/src/main/java/org/apache/qpid/proton/codec/impl/StringElement.java
+++ b/proton-j/src/main/java/org/apache/qpid/proton/codec/impl/StringElement.java
@@ -127,7 +127,7 @@ class StringElement extends AtomicElement<String>
else
{
b.put((byte)0xb1);
- b.put((byte)length);
+ b.putInt(length);
}
b.put(bytes);
return size;
http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/2e95aa94/proton-j/src/test/java/org/apache/qpid/proton/codec/impl/DataImplTest.java
----------------------------------------------------------------------
diff --git a/proton-j/src/test/java/org/apache/qpid/proton/codec/impl/DataImplTest.java b/proton-j/src/test/java/org/apache/qpid/proton/codec/impl/DataImplTest.java
index 6bb3f2c..5799f17 100644
--- a/proton-j/src/test/java/org/apache/qpid/proton/codec/impl/DataImplTest.java
+++ b/proton-j/src/test/java/org/apache/qpid/proton/codec/impl/DataImplTest.java
@@ -22,6 +22,9 @@ package org.apache.qpid.proton.codec.impl;
import static org.junit.Assert.*;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.codec.Data;
@@ -81,4 +84,68 @@ public class DataImplTest
Object[] array = data2.getJavaArray();
assertArrayEquals("Array not as expected", input, array);
}
+
+ @Test
+ public void testEncodeString32()
+ {
+ byte[] strPayload = createStringPayloadBytes(256);
+ String content = new String(strPayload, StandardCharsets.UTF_8);
+ assertTrue("Length must be over 255 to ensure use of str32 encoding", content.length() > 255);
+
+ int encodedSize = 1 + 4 + strPayload.length; // 1b type + 4b length + content
+ ByteBuffer expectedEncoding = ByteBuffer.allocate(encodedSize);
+ expectedEncoding.put((byte) 0xB1);
+ expectedEncoding.putInt(strPayload.length);
+ expectedEncoding.put(strPayload);
+
+ Data data = new DataImpl();
+ data.putString(content);
+
+ Binary encoded = data.encode();
+
+ assertEquals("unexpected encoding", new Binary(expectedEncoding.array()), encoded);
+ }
+
+ @Test
+ public void testEncodeDecodeString32()
+ {
+ byte[] payload = createStringPayloadBytes(1025);
+ String content = new String(payload, StandardCharsets.UTF_8);
+ assertTrue("Length must be over 255 to ensure use of str32 encoding", content.length() > 255);
+
+ doEncodeDecodeStringTestImpl(content);
+ }
+
+ @Test
+ public void testEncodeDecodeString8()
+ {
+ String content = "testRoundTripString8";
+ assertTrue("Length must be <= 255 to allow use of str8 encoding", content.length() <= 255);
+
+ doEncodeDecodeStringTestImpl("testRoundTripString8");
+ }
+
+ private void doEncodeDecodeStringTestImpl(String string)
+ {
+ Data data = new DataImpl();
+ data.putString(string);
+
+ Binary encoded = data.encode();
+
+ Data data2 = new DataImpl();
+ data2.decode(encoded.asByteBuffer());
+
+ assertEquals("unexpected type", Data.DataType.STRING, data2.type());
+ assertEquals("unexpected string", string, data2.getString());
+ }
+
+ private byte[] createStringPayloadBytes(int length)
+ {
+ byte[] payload = new byte[length];
+ for (int i = 0; i < length; i++) {
+ payload[i] = (byte) ((i % 10) + 48);
+ }
+
+ return payload;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org