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 2018/11/19 13:00:32 UTC

qpid-proton-j git commit: PROTON-1966: ensure the position is always reset on return

Repository: qpid-proton-j
Updated Branches:
  refs/heads/master 589504d76 -> ba9617831


PROTON-1966: ensure the position is always reset on return


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

Branch: refs/heads/master
Commit: ba9617831fb8c71c89473abfe2796e2e83f7003d
Parents: 589504d
Author: Robbie Gemmell <ro...@apache.org>
Authored: Mon Nov 19 12:52:23 2018 +0000
Committer: Robbie Gemmell <ro...@apache.org>
Committed: Mon Nov 19 12:52:23 2018 +0000

----------------------------------------------------------------------
 .../proton/codec/CompositeReadableBuffer.java   | 19 ++---
 .../codec/CompositeReadableBufferTest.java      | 85 ++++++++++++++++++++
 2 files changed, 95 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/ba961783/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 c614e39..1441dcb 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
@@ -839,17 +839,18 @@ public class CompositeReadableBuffer implements ReadableBuffer {
             return false;
         }
 
-        final int currentPos = position();
-
-        for (int i = buffer.position(); hasRemaining(); i++) {
-            if (!equals(this.get(), buffer.get(i))) {
-                return false;
+        final int origPos = position();
+        try {
+            for (int i = buffer.position(); hasRemaining(); i++) {
+                if (!equals(this.get(), buffer.get(i))) {
+                    return false;
+                }
             }
-        }
 
-        position(currentPos);
-
-        return true;
+            return true;
+        } finally {
+            position(origPos);
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/qpid-proton-j/blob/ba961783/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 86ee4ab..1e8e731 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
@@ -3464,6 +3464,8 @@ public class CompositeReadableBufferTest {
         buffer.append(data);
 
         assertEquals(buffer, buffer);
+
+        assertEquals(0, buffer.position());
     }
 
     @Test
@@ -3481,6 +3483,10 @@ public class CompositeReadableBufferTest {
         assertEquals(buffer1, buffer2);
         assertEquals(buffer2, buffer3);
         assertEquals(buffer3, buffer1);
+
+        assertEquals(0, buffer1.position());
+        assertEquals(0, buffer2.position());
+        assertEquals(0, buffer3.position());
     }
 
     @Test
@@ -3495,6 +3501,9 @@ public class CompositeReadableBufferTest {
         buffer2.append(data2);
 
         assertNotEquals(buffer1, buffer2);
+
+        assertEquals(0, buffer1.position());
+        assertEquals(0, buffer2.position());
     }
 
     @Test
@@ -3509,6 +3518,9 @@ public class CompositeReadableBufferTest {
         buffer2.append(data2).append(data1);
 
         assertNotEquals(buffer1, buffer2);
+
+        assertEquals(0, buffer1.position());
+        assertEquals(0, buffer2.position());
     }
 
     @Test
@@ -3523,6 +3535,79 @@ public class CompositeReadableBufferTest {
         buffer2.append(data1).append(data2);
 
         assertEquals(buffer1, buffer2);
+
+        assertEquals(0, buffer1.position());
+        assertEquals(0, buffer2.position());
+    }
+
+    @Test
+    public void testEqualsWhenContentRemainingWithDifferentStartPositionsSame() throws CharacterCodingException {
+        doEqualsWhenContentRemainingWithDifferentStartPositionsSameTestImpl(false);
+    }
+
+    @Test
+    public void testEqualsWhenContentRemainingWithDifferentStartPositionsSameMultipleArrays() throws CharacterCodingException {
+        doEqualsWhenContentRemainingWithDifferentStartPositionsSameTestImpl(true);
+    }
+
+    private void doEqualsWhenContentRemainingWithDifferentStartPositionsSameTestImpl(boolean multipleArrays) {
+        CompositeReadableBuffer buffer1 = new CompositeReadableBuffer();
+        CompositeReadableBuffer buffer2 = new CompositeReadableBuffer();
+
+        byte[] data1 = new byte[] {-1, -1, 0, 1, 2, 3, 4, 5};
+        byte[] data2 = new byte[] {-1, -1, -1, 0, 1, 2, 3, 4, 5};
+
+        buffer1.append(data1);
+        buffer1.position(2);
+
+        buffer2.append(data2);
+        buffer2.position(3);
+
+        if (multipleArrays) {
+            byte[] data3 = new byte[] { 5, 4, 3, 2, 1 };
+            buffer1.append(data3);
+            buffer2.append(data3);
+        }
+
+        assertEquals(buffer1, buffer2);
+
+        assertEquals(2, buffer1.position());
+        assertEquals(3, buffer2.position());
+    }
+
+    @Test
+    public void testEqualsWhenContentRemainingWithDifferentStartPositionsNotSame() throws CharacterCodingException {
+        doEqualsWhenContentRemainingWithDifferentStartPositionsNotSameTestImpl(false);
+    }
+
+    @Test
+    public void testEqualsWhenContentRemainingWithDifferentStartPositionsNotSameMultipleArrays() throws CharacterCodingException {
+        doEqualsWhenContentRemainingWithDifferentStartPositionsNotSameTestImpl(true);
+    }
+
+    private void doEqualsWhenContentRemainingWithDifferentStartPositionsNotSameTestImpl(boolean multipleArrays) {
+        CompositeReadableBuffer buffer1 = new CompositeReadableBuffer();
+        CompositeReadableBuffer buffer2 = new CompositeReadableBuffer();
+
+        byte[] data1 = new byte[] {-1, -1, 0, 1, 2, 3, 4, 5};
+        byte[] data2 = new byte[] {-1, -1, -1, 0, 1, 2, 3, 4, -1};
+
+        buffer1.append(data1);
+        buffer1.position(2);
+
+        buffer2.append(data2);
+        buffer2.position(3);
+
+        if (multipleArrays) {
+            byte[] data3 = new byte[] { 5, 4, 3, 2, 1 };
+            buffer1.append(data3);
+            buffer2.append(data3);
+        }
+
+        assertNotEquals(buffer1, buffer2);
+
+        assertEquals(2, buffer1.position());
+        assertEquals(3, buffer2.position());
     }
 
     //----- Utility Methods --------------------------------------------------//


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