mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
21.16.2014 183e31c0e78920b50a704780c022291a6139c032
OPENDJ-1368 (CR-3232) Remove AttributeValue


JebFormat.java:
In entryIDUndefinedSizeFromDatabase(), reverted r10561.
Improved javadocs.

TestJebFormat.java:
Added tests for JebFormat.entryIDUndefinedSizeToDatabase() and JebFormat.entryIDUndefinedSizeFromDatabase().
Extracted DataProvider methods + adapted tests to use them.
2 files modified
137 ■■■■■ changed files
opendj3-server-dev/src/server/org/opends/server/backends/jeb/JebFormat.java 24 ●●●● patch | view | raw | blame | history
opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestJebFormat.java 113 ●●●●● patch | view | raw | blame | history
opendj3-server-dev/src/server/org/opends/server/backends/jeb/JebFormat.java
@@ -63,6 +63,7 @@
   *
   * @param bytes The database value of the entry ID.
   * @return The entry ID value.
   * @see #entryIDToDatabase(long)
   */
  public static long entryIDFromDatabase(byte[] bytes)
  {
@@ -100,6 +101,8 @@
   *
   * @param bytes The database value of the entry ID count.
   * @return The entry ID count.
   *  Cannot be negative if encoded with #entryIDUndefinedSizeToDatabase(long)
   * @see #entryIDUndefinedSizeToDatabase(long)
   */
  public static long entryIDUndefinedSizeFromDatabase(byte[] bytes)
  {
@@ -110,7 +113,14 @@
    if(bytes.length == 8)
    {
      return entryIDFromDatabase(bytes);
      long v = 0;
      v |= (bytes[0] & 0x7F);
      for (int i = 1; i < 8; i++)
      {
        v <<= 8;
        v |= (bytes[i] & 0xFF);
      }
      return v;
    }
    return Long.MAX_VALUE;
  }
@@ -122,8 +132,8 @@
   *              hence no entry IDs. Note that this method will throw an
   *              ArrayIndexOutOfBoundsException if the bytes array length is
   *              not a multiple of 8.
   *
   * @return An array of entry ID values.
   * @see #entryIDListToDatabase(long[])
   */
  public static long[] entryIDListFromDatabase(byte[] bytes)
  {
@@ -174,8 +184,10 @@
  /**
   * Encode an entry ID value to its database representation.
   *
   * @param id The entry ID value to be encoded.
   * @return The encoded database value of the entry ID.
   * @see #entryIDFromDatabase(byte[])
   */
  public static byte[] entryIDToDatabase(long id)
  {
@@ -191,8 +203,10 @@
  /**
   * Encode an entry ID set count to its database representation.
   *
   * @param count The entry ID set count to be encoded.
   * @return The encoded database value of the entry ID.
   * @return The encoded database value of the entry ID set count.
   * @see #entryIDUndefinedSizeFromDatabase(byte[])
   */
  public static byte[] entryIDUndefinedSizeToDatabase(long count)
  {
@@ -211,8 +225,8 @@
   * Encode an array of entry ID values to its database representation.
   *
   * @param entryIDArray An array of entry ID values.
   *
   * @return The encoded database value.
   * @see #entryIDListFromDatabase(byte[])
   */
  public static byte[] entryIDListToDatabase(long[] entryIDArray)
  {
@@ -248,6 +262,7 @@
   * @param prefix The DN to prefix the deocded DN value.
   * @return The decoded DN value.
   * @throws DirectoryException if an error occurs while decoding the DN value.
   * @see #dnToDNKey(DN, int)
   */
  public static DN dnFromDNKey(byte[] dnKey, int offset, int length, DN prefix)
      throws DirectoryException
@@ -332,6 +347,7 @@
   * @param prefixRDNs The number of prefix RDNs to remove from the encoded
   *                   representation.
   * @return A DatabaseEntry containing the key.
   * @see #dnFromDNKey(byte[], int, int, DN)
   */
  public static byte[] dnToDNKey(DN dn, int prefixRDNs)
  {
opendj3-server-dev/tests/unit-tests-testng/src/server/org/opends/server/backends/jeb/TestJebFormat.java
@@ -26,29 +26,28 @@
 */
package org.opends.server.backends.jeb;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ByteStringBuilder;
import org.opends.server.TestCaseUtils;
import org.opends.server.core.DirectoryServer;
import org.opends.server.types.*;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.ByteStringBuilder;
import org.opends.server.util.LDIFReader;
import org.opends.server.util.StaticUtils;
import static org.opends.server.util.StaticUtils.getBytes;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.opends.server.util.StaticUtils.*;
import static org.testng.Assert.*;
/**
 * JebFormat Tester.
 */
@SuppressWarnings("javadoc")
public class TestJebFormat extends JebTestCase {
  private static final String ldifString =
    "dn: uid=user.1,ou=People,dc=example,dc=com\n"
@@ -134,55 +133,79 @@
      + "cn;lang-en: Rodney Ogasawara\n"
      + "title;lang-en: Sales, Director\n" + "\n" + "";
  /**
   * Test entry IDs.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test()
  public void testEntryIDToAndFromDatabase() throws Exception {
    long[] vals = { 128, 1234567, 0, 1, -1, 2 ^ 32 - 1, 2 ^ 63 - 1 };
    for (long before : vals) {
      byte[] bytes = JebFormat.entryIDToDatabase(before);
      long after = JebFormat.entryIDFromDatabase(bytes);
      assertEquals(before, after);
    }
  @DataProvider
  public Object[][] entryIDToAndFromDatabaseDataProvider()
  {
    return new Object[][] {
      { 128 }, { 1234567 }, { 0 }, { 1 }, { -1 },
      { 2 ^ 32 - 1 }, { 2 ^ 63 - 1 }, { Long.MIN_VALUE }, { Long.MAX_VALUE },
    };
  }
  private void entryIDListToAndFromDatabase(long[] before) throws Exception {
    byte[] bytes = JebFormat.entryIDListToDatabase(before);
    /*
     * printError(String.format("encoded count=%d len=%d",
     * before.length, bytes.length));
     */
    long[] after = JebFormat.entryIDListFromDatabase(bytes);
  /**
   * Test entry IDs.
   */
  @Test(dataProvider = "entryIDToAndFromDatabaseDataProvider")
  public void testEntryIDToAndFromDatabase(long before) throws Exception
  {
    byte[] bytes = JebFormat.entryIDToDatabase(before);
    long after = JebFormat.entryIDFromDatabase(bytes);
    assertEquals(after, before);
  }
    assertTrue(Arrays.equals(before, after));
  @DataProvider
  public Object[][] entryIDUndefinedSizeToAndFromDatabaseDataProvider()
  {
    return new Object[][] {
      { 128 }, { 1234567 }, { 0 }, { 1 },
      { 2 ^ 32 - 1 }, { 2 ^ 63 - 1 }, { Long.MAX_VALUE },
    };
  }
  /**
   * Test entry ID set counts.
   */
  @Test(dataProvider = "entryIDUndefinedSizeToAndFromDatabaseDataProvider")
  public void testEntryIDUndefinedSizeToAndFromDatabase(long before) throws Exception
  {
    byte[] bytes = JebFormat.entryIDUndefinedSizeToDatabase(before);
    assertEquals(bytes[0] & 0x80, 0x80);
    long after = JebFormat.entryIDUndefinedSizeFromDatabase(bytes);
    assertEquals(after, before);
  }
  @Test
  public void testEntryIDUndefinedSizeFromDatabase() throws Exception
  {
    assertEquals(JebFormat.entryIDUndefinedSizeFromDatabase(null), 0);
    assertEquals(JebFormat.entryIDUndefinedSizeFromDatabase(new byte[0]), Long.MAX_VALUE);
    assertEquals(JebFormat.entryIDUndefinedSizeFromDatabase(new byte[9]), Long.MAX_VALUE);
  }
  /**
   * Test entry ID lists.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test()
  public void testEntryIDListToAndFromDatabase() throws Exception {
    long[] array;
    array = new long[] { 1, 2, 3, 4, 5 };
    entryIDListToAndFromDatabase(array);
    array = new long[] { 999999 };
    entryIDListToAndFromDatabase(array);
    array = new long[] { 1, 128, 1234567 };
    entryIDListToAndFromDatabase(array);
    array = new long[100000];
  @Test(dataProvider = "entryIDListToAndFromDatabaseDataProvider")
  public void entryIDListToAndFromDatabase(long[] before) throws Exception
  {
    byte[] bytes = JebFormat.entryIDListToDatabase(before);
    long[] after = JebFormat.entryIDListFromDatabase(bytes);
    assertTrue(Arrays.equals(before, after));
  }
  @DataProvider
  public Object[][] entryIDListToAndFromDatabaseDataProvider() throws Exception
  {
    long[] array = new long[100000];
    for (int i = 0; i < 100000; i++) {
      array[i] = i * 2 + 1;
    }
    entryIDListToAndFromDatabase(array);
    return new Object[][] {
      { new long[] { 1, 2, 3, 4, 5 } },
      { new long[] { 999999 } },
      { new long[] { 1, 128, 1234567 } },
      { array },
    };
  }
  /**