You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2019/02/19 21:19:35 UTC

[geode] branch feature/GEODE-6414 updated: added unit tests for copyRemainingBytes on PdxWriterImpl

This is an automated email from the ASF dual-hosted git repository.

dschneider pushed a commit to branch feature/GEODE-6414
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-6414 by this push:
     new 3c8a13e  added unit tests for copyRemainingBytes on PdxWriterImpl
3c8a13e is described below

commit 3c8a13e4874580087e1154b197b1c457aca3741b
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Tue Feb 19 13:19:03 2019 -0800

    added unit tests for copyRemainingBytes on PdxWriterImpl
---
 .../apache/geode/pdx/internal/PdxWriterImpl.java   | 19 +++--
 .../geode/pdx/internal/PdxWriterImplTest.java      | 83 ++++++++++++++++++++++
 2 files changed, 96 insertions(+), 6 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxWriterImpl.java b/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxWriterImpl.java
index e5c44e0..610ef07 100644
--- a/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxWriterImpl.java
+++ b/geode-core/src/main/java/org/apache/geode/pdx/internal/PdxWriterImpl.java
@@ -954,13 +954,20 @@ public class PdxWriterImpl implements PdxWriter {
     if (pt == null) {
       pt = this.existingType;
     }
-    bb = bb.slice();
+    return new PdxInstanceImpl(pt, new PdxInputStream(copyRemainingBytes(bb)), len);
+  }
 
-    ByteBuffer bbCopy = ByteBuffer.allocate(bb.capacity());
-    bb.rewind();
-    bbCopy.put(bb);
-    bbCopy.flip();
-    return new PdxInstanceImpl(pt, new PdxInputStream(bbCopy), len);
+  /**
+   * Copies the remaining bytes in source
+   * (that is, from its current position to its limit)
+   * into a new heap ByteBuffer that is returned.
+   */
+  static ByteBuffer copyRemainingBytes(ByteBuffer source) {
+    ByteBuffer slice = source.slice();
+    ByteBuffer result = ByteBuffer.allocate(slice.capacity());
+    result.put(slice);
+    result.flip();
+    return result;
   }
 
   public static boolean isPdx(byte[] valueBytes) {
diff --git a/geode-core/src/test/java/org/apache/geode/pdx/internal/PdxWriterImplTest.java b/geode-core/src/test/java/org/apache/geode/pdx/internal/PdxWriterImplTest.java
new file mode 100644
index 0000000..6a2068f
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/pdx/internal/PdxWriterImplTest.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.geode.pdx.internal;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.test.junit.categories.SerializationTest;
+
+@Category({SerializationTest.class})
+public class PdxWriterImplTest {
+
+  @Test
+  public void copyingEmptySourceReturnsEmptyResult() {
+    ByteBuffer source = ByteBuffer.allocate(10);
+    source.limit(0);
+
+    ByteBuffer result = PdxWriterImpl.copyRemainingBytes(source);
+
+    assertThat(source.position()).isEqualTo(0);
+    assertThat(source.limit()).isEqualTo(0);
+    assertThat(result.capacity()).isEqualTo(0);
+    assertThat(result.remaining()).isEqualTo(0);
+  }
+
+  @Test
+  public void copyingFullSourceReturnsFullResult() {
+    ByteBuffer source = ByteBuffer.allocate(10);
+    source.put((byte) 1);
+    source.put((byte) 2);
+    source.put((byte) 3);
+    source.flip();
+
+    ByteBuffer result = PdxWriterImpl.copyRemainingBytes(source);
+
+    assertThat(source.position()).isEqualTo(0);
+    assertThat(source.limit()).isEqualTo(3);
+    assertThat(result.remaining()).isEqualTo(3);
+    assertThat(result.capacity()).isEqualTo(3);
+    assertThat(result.get(0)).isEqualTo((byte) 1);
+    assertThat(result.get(1)).isEqualTo((byte) 2);
+    assertThat(result.get(2)).isEqualTo((byte) 3);
+  }
+
+  @Test
+  public void copyingPartialSourceReturnsPartialResult() {
+    ByteBuffer source = ByteBuffer.allocate(10);
+    source.put((byte) 1);
+    source.put((byte) 2);
+    source.put((byte) 3);
+    source.put((byte) 4);
+    source.position(1);
+    source.limit(3);
+
+    ByteBuffer result = PdxWriterImpl.copyRemainingBytes(source);
+
+    assertThat(source.position()).isEqualTo(1);
+    assertThat(source.limit()).isEqualTo(3);
+    assertThat(result.remaining()).isEqualTo(2);
+    assertThat(result.capacity()).isEqualTo(2);
+    assertThat(result.get(0)).isEqualTo((byte) 2);
+    assertThat(result.get(1)).isEqualTo((byte) 3);
+  }
+
+}