You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ta...@apache.org on 2018/04/25 19:53:38 UTC

qpid-proton-j git commit: PROTON-1836 Fix empty string decoding from CompositeReadableBuffer

Repository: qpid-proton-j
Updated Branches:
  refs/heads/master f6fb6ad60 -> 62cbea9d7


PROTON-1836 Fix empty string decoding from CompositeReadableBuffer

Ensure that empty string is read as empty and not null string.  Adds
tests and optimizes the StringType encodings to fast return on size zero
strings.

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/62cbea9d
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/tree/62cbea9d
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton-j/diff/62cbea9d

Branch: refs/heads/master
Commit: 62cbea9d79d9c0352a9b5076e64056289b00f0ac
Parents: f6fb6ad
Author: Timothy Bish <ta...@gmail.com>
Authored: Wed Apr 25 15:49:57 2018 -0400
Committer: Timothy Bish <ta...@gmail.com>
Committed: Wed Apr 25 15:49:57 2018 -0400

----------------------------------------------------------------------
 .../proton/codec/CompositeReadableBuffer.java   |  2 +-
 .../apache/qpid/proton/codec/StringType.java    |  4 +-
 .../codec/CompositeReadableBufferTest.java      |  2 +-
 .../qpid/proton/codec/StringTypeTest.java       | 63 ++++++++++++++++++++
 4 files changed, 67 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/62cbea9d/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java
----------------------------------------------------------------------
diff --git a/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java b/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java
index 5ab0348..5a030f0 100644
--- a/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java
+++ b/proton-j/src/main/java/org/apache/qpid/proton/codec/CompositeReadableBuffer.java
@@ -531,7 +531,7 @@ public class CompositeReadableBuffer implements ReadableBuffer {
     @Override
     public String readString(CharsetDecoder decoder) throws CharacterCodingException {
         if (!hasRemaining()) {
-            return null;
+            return "";
         }
 
         CharBuffer decoded = null;

http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/62cbea9d/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
----------------------------------------------------------------------
diff --git a/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java b/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
index dfc449c..24bea47 100644
--- a/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
+++ b/proton-j/src/main/java/org/apache/qpid/proton/codec/StringType.java
@@ -157,7 +157,7 @@ public class StringType extends AbstractPrimitiveType<String>
         {
             DecoderImpl decoder = getDecoder();
             int size = decoder.readRawInt();
-            return decoder.readRaw(_stringCreator, size);
+            return size == 0 ? "" : decoder.readRaw(_stringCreator, size);
         }
 
         public void setValue(final String val, final int length)
@@ -219,7 +219,7 @@ public class StringType extends AbstractPrimitiveType<String>
         {
             DecoderImpl decoder = getDecoder();
             int size = ((int)decoder.readRawByte()) & 0xff;
-            return decoder.readRaw(_stringCreator, size);
+            return size == 0 ? "" : decoder.readRaw(_stringCreator, size);
         }
 
         public void setValue(final String val, final int length)

http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/62cbea9d/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java
----------------------------------------------------------------------
diff --git a/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java b/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java
index 45626bc..8c26555 100644
--- a/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java
+++ b/proton-j/src/test/java/org/apache/qpid/proton/codec/CompositeReadableBufferTest.java
@@ -2079,7 +2079,7 @@ public class CompositeReadableBufferTest {
     public void testReadStringFromEmptyBuffer() throws CharacterCodingException {
         CompositeReadableBuffer buffer = new CompositeReadableBuffer();
 
-        assertNull(buffer.readString(StandardCharsets.UTF_8.newDecoder()));
+        assertEquals("", buffer.readString(StandardCharsets.UTF_8.newDecoder()));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/62cbea9d/proton-j/src/test/java/org/apache/qpid/proton/codec/StringTypeTest.java
----------------------------------------------------------------------
diff --git a/proton-j/src/test/java/org/apache/qpid/proton/codec/StringTypeTest.java b/proton-j/src/test/java/org/apache/qpid/proton/codec/StringTypeTest.java
index 6c376d5..320bd54 100644
--- a/proton-j/src/test/java/org/apache/qpid/proton/codec/StringTypeTest.java
+++ b/proton-j/src/test/java/org/apache/qpid/proton/codec/StringTypeTest.java
@@ -149,6 +149,69 @@ public class StringTypeTest
         assertEquals("read", result);
     }
 
+    @Test
+    public void testEncodeAndDecodeEmptyString() {
+        final DecoderImpl decoder = new DecoderImpl();
+        final EncoderImpl encoder = new EncoderImpl(decoder);
+        AMQPDefinedTypes.registerAllTypes(decoder, encoder);
+
+        final ByteBuffer buffer = ByteBuffer.allocate(64);
+
+        encoder.setByteBuffer(buffer);
+        decoder.setByteBuffer(buffer);
+
+        encoder.writeString("a");
+        encoder.writeString("");
+        encoder.writeString("b");
+
+        buffer.clear();
+
+        TypeConstructor<?> stringType = decoder.readConstructor();
+        assertEquals(String.class, stringType.getTypeClass());
+        stringType.skipValue();
+
+        String result = decoder.readString();
+        assertEquals("", result);
+        result = decoder.readString();
+        assertEquals("b", result);
+    }
+
+    @Test
+    public void testEmptyShortStringEncod() {
+        doTestEmptyStringEncodAsGivenType(EncodingCodes.STR8);
+    }
+
+    @Test
+    public void testEmptyLargeStringEncod() {
+        doTestEmptyStringEncodAsGivenType(EncodingCodes.STR32);
+    }
+
+    public void doTestEmptyStringEncodAsGivenType(byte encodingCode) {
+        final DecoderImpl decoder = new DecoderImpl();
+        final EncoderImpl encoder = new EncoderImpl(decoder);
+        AMQPDefinedTypes.registerAllTypes(decoder, encoder);
+
+        final ByteBuffer buffer = ByteBuffer.allocate(64);
+
+        buffer.put(encodingCode);
+        buffer.putInt(0);
+        buffer.clear();
+
+        byte[] copy = new byte[buffer.remaining()];
+        buffer.get(copy);
+
+        CompositeReadableBuffer composite = new CompositeReadableBuffer();
+        composite.append(copy);
+
+        decoder.setBuffer(composite);
+
+        TypeConstructor<?> stringType = decoder.readConstructor();
+        assertEquals(String.class, stringType.getTypeClass());
+
+        String result = (String) stringType.readValue();
+        assertEquals("", result);
+    }
+
     // build up some test data with a set of suitable Unicode characters
     private static List<String> generateTestData()
     {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org