From c97dc892a8c400aba9bb8c88a38605509d19de2e Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Wed, 23 Apr 2014 15:24:15 +0000
Subject: [PATCH] OPENDJ-1259 Make the Medium Consistency Point support replicas temporarily leaving the topology

---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/ByteSequenceReaderTest.java |  102 ++++++++++++++++++++++-----------
 opendj-sdk/opends/src/server/org/opends/server/types/ByteSequenceReader.java                             |   39 ++++++++++--
 2 files changed, 99 insertions(+), 42 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/types/ByteSequenceReader.java b/opendj-sdk/opends/src/server/org/opends/server/types/ByteSequenceReader.java
index 3113b15..94bd89a 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/types/ByteSequenceReader.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/types/ByteSequenceReader.java
@@ -22,11 +22,10 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
+ *      Portions copyright 2014 ForgeRock AS
  */
 package org.opends.server.types;
 
-
-
 /**
  * An interface for iteratively reading date from a
  * {@link ByteSequence}. {@code ByteSequenceReader} must be created
@@ -36,10 +35,10 @@
 public final class ByteSequenceReader
 {
 
-  // The current position in the byte sequence.
+  /** The current position in the byte sequence. */
   private int pos = 0;
 
-  // The underlying byte sequence.
+  /** The underlying byte sequence. */
   private final ByteSequence sequence;
 
 
@@ -400,7 +399,35 @@
     return str;
   }
 
+  /**
+   * Returns the byte situated at the current position. The byte is not
+   * consumed.
+   *
+   * @return the byte situated at the current position
+   * @throws IndexOutOfBoundsException
+   *           If the position is negative or larger than the length of the
+   *           underlying byte sequence.
+   */
+  public byte peek() throws IndexOutOfBoundsException
+  {
+    return sequence.byteAt(pos);
+  }
 
+  /**
+   * Returns the byte situated at the given offset from current position. The
+   * byte is not consumed.
+   *
+   * @param offset
+   *          The offset where to look at from current position.
+   * @return the byte situated at the given offset from current position
+   * @throws IndexOutOfBoundsException
+   *           If the position is negative or larger than the length of the
+   *           underlying byte sequence.
+   */
+  public byte peek(int offset) throws IndexOutOfBoundsException
+  {
+    return sequence.byteAt(pos + offset);
+  }
 
   /**
    * Returns this reader's position.
@@ -498,9 +525,7 @@
     position(pos + length);
   }
 
-  /**
-   * {@inheritDoc}
-   */
+  /** {@inheritDoc} */
   @Override
   public String toString() {
     return sequence.toString();
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/ByteSequenceReaderTest.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/ByteSequenceReaderTest.java
index 8875ba4..c28b9a0 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/ByteSequenceReaderTest.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/types/ByteSequenceReaderTest.java
@@ -22,24 +22,30 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
+ *      Portions Copyright 2014 ForgeRock AS
  */
-
 package org.opends.server.types;
 
-import org.testng.annotations.Test;
-import org.testng.annotations.DataProvider;
-import org.testng.Assert;
-
 import java.util.Arrays;
 
+import org.testng.Assert;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
 /**
  * Test class for ByteSequenceReader
  */
+@SuppressWarnings("javadoc")
 public class ByteSequenceReaderTest extends TypesTestCase
 {
     private static final byte[] eightBytes =
-      new byte[]{ (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04,
-          (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08 };
+      new byte[]{ b(0x01), b(0x02), b(0x03), b(0x04),
+          b(0x05), b(0x06), b(0x07), b(0x08) };
+
+    private static byte b(int i)
+    {
+      return (byte) i;
+    }
 
   @DataProvider(name = "readerProvider")
   public Object[][] byteSequenceReaderProvider()
@@ -57,9 +63,9 @@
   public void testGet(ByteSequenceReader reader, byte[] ba)
   {
     reader.rewind();
-    for(int i = 0; i < ba.length; i++)
+    for (byte b : ba)
     {
-      Assert.assertEquals(reader.get(), ba[i]);
+      Assert.assertEquals(reader.get(), b);
     }
 
     // The next get should result in IOB exception.
@@ -148,26 +154,26 @@
   public void testGetBERLength()
   {
     ByteSequenceReader reader = ByteString.wrap(new byte[]{
-        (byte) 0x00, (byte) 0x01, (byte) 0x0F, (byte) 0x10,
-        (byte) 0x7F,
+        b(0x00), b(0x01), b(0x0F), b(0x10),
+        b(0x7F),
 
-        (byte) 0x81, (byte) 0xFF,
+        b(0x81), b(0xFF),
 
-        (byte) 0x82, (byte) 0x01, (byte) 0x00,
-        (byte) 0x82, (byte) 0x0F, (byte) 0xFF, (byte) 0x82, (byte) 0x10,
-        (byte) 0x00, (byte) 0x82, (byte) 0xFF, (byte) 0xFF,
+        b(0x82), b(0x01), b(0x00),
+        b(0x82), b(0x0F), b(0xFF), b(0x82), b(0x10),
+        b(0x00), b(0x82), b(0xFF), b(0xFF),
 
-        (byte) 0x83, (byte) 0x01, (byte) 0x00, (byte) 0x00,
-        (byte) 0x83, (byte) 0x0F, (byte) 0xFF, (byte) 0xFF,
-        (byte) 0x83, (byte) 0x10, (byte) 0x00, (byte) 0x00,
-        (byte) 0x83, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+        b(0x83), b(0x01), b(0x00), b(0x00),
+        b(0x83), b(0x0F), b(0xFF), b(0xFF),
+        b(0x83), b(0x10), b(0x00), b(0x00),
+        b(0x83), b(0xFF), b(0xFF), b(0xFF),
 
-        (byte) 0x84, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-        (byte) 0x84, (byte) 0x0F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
-        (byte) 0x84, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-        (byte) 0x84, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+        b(0x84), b(0x01), b(0x00), b(0x00), b(0x00),
+        b(0x84), b(0x0F), b(0xFF), b(0xFF), b(0xFF),
+        b(0x84), b(0x10), b(0x00), b(0x00), b(0x00),
+        b(0x84), b(0xFF), b(0xFF), b(0xFF), b(0xFF),
 
-        (byte) 0x84, (byte) 0x10, (byte) 0x00
+        b(0x84), b(0x10), b(0x00)
     }).asReader();
 
     int[] expectedLength = new int[]{
@@ -183,9 +189,9 @@
         0x01000000, 0x0FFFFFFF, 0x10000000, 0xFFFFFFFF
     };
 
-    for(int i = 0; i < expectedLength.length; i++)
+    for (int length : expectedLength)
     {
-      Assert.assertEquals(reader.getBERLength(), expectedLength[i]);
+      Assert.assertEquals(reader.getBERLength(), length);
     }
 
     // Last one is incomplete and should throw error
@@ -196,8 +202,8 @@
   public void testOversizedGetBERLength()
   {
     ByteSequenceReader reader = ByteString.wrap(new byte[]{
-        (byte) 0x85, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-        (byte) 0x00 }).asReader();
+        b(0x85), b(0x10), b(0x00), b(0x00), b(0x00), b(0x00)
+    }).asReader();
 
     // Shouldn't be able to reader over a 4 byte length.
     reader.getBERLength();
@@ -270,7 +276,7 @@
   public void testGetShort()
   {
     ByteSequenceReader reader = ByteString.wrap(new byte[]{
-        (byte) 0x80, (byte) 0x00, (byte) 0x7F, (byte) 0xFF, (byte) 0xFF
+        b(0x80), b(0x00), b(0x7F), b(0xFF), b(0xFF)
     }).asReader();
 
     Assert.assertEquals(reader.getShort(), Short.MIN_VALUE);
@@ -284,8 +290,8 @@
   public void testGetInt()
   {
     ByteSequenceReader reader = ByteString.wrap(new byte[]{
-        (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x7F,
-        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }).asReader();
+        b(0x80), b(0x00), b(0x00), b(0x00), b(0x7F),
+        b(0xFF), b(0xFF), b(0xFF), b(0xFF) }).asReader();
 
     Assert.assertEquals(reader.getInt(), Integer.MIN_VALUE);
     Assert.assertEquals(reader.getInt(), Integer.MAX_VALUE);
@@ -298,10 +304,10 @@
   public void testGetLong()
   {
     ByteSequenceReader reader = ByteString.wrap(new byte[]{
-        (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
-        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x7F, (byte) 0xFF,
-        (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
-        (byte) 0xFF, (byte) 0xFF }).asReader();
+        b(0x80), b(0x00), b(0x00), b(0x00), b(0x00),
+        b(0x00), b(0x00), b(0x00), b(0x7F), b(0xFF),
+        b(0xFF), b(0xFF), b(0xFF), b(0xFF), b(0xFF),
+        b(0xFF), b(0xFF) }).asReader();
 
     Assert.assertEquals(reader.getLong(), Long.MIN_VALUE);
     Assert.assertEquals(reader.getLong(), Long.MAX_VALUE);
@@ -398,4 +404,30 @@
     // Any more skips should result in IOB exception.
     reader.skip(1);
   }
+
+  @Test(dataProvider = "readerProvider")
+  public void testPeek(ByteSequenceReader reader, byte[] ba)
+  {
+    reader.rewind();
+
+    int length = ba.length;
+    int pos = 0;
+    for (int i = 0; i < length; i++)
+    {
+      for (int j = 0; j < length - i; j++)
+      {
+        if (j == 0)
+        {
+          Assert.assertEquals(reader.peek(), ba[pos]);
+        }
+        Assert.assertEquals(reader.peek(j), ba[pos + j]);
+      }
+      pos++;
+      if (pos < length)
+      {
+        reader.skip(1);
+      }
+    }
+  }
+
 }

--
Gitblit v1.10.0