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

Jean-Noël Rouvignac
10.15.2016 352227d9327a40607111ed43bee145a743b812d3
Prep work for OPENDJ-1342: align APIs for RDNs

RDN.java:
Removed constructors that do not exist in the SDK
Removed methods only used in tests
3 files modified
296 ■■■■■ changed files
opendj-server-legacy/src/main/java/org/opends/server/tools/makeldif/TemplateEntry.java 31 ●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/types/RDN.java 135 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/types/TestRDN.java 130 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/main/java/org/opends/server/tools/makeldif/TemplateEntry.java
@@ -35,6 +35,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import org.forgerock.opendj.ldap.AVA;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.opends.server.core.DirectoryServer;
@@ -148,40 +149,20 @@
  {
    if (dn == null)
    {
      RDN rdn;
      AttributeType[] rdnAttrs = template.getRDNAttributes();
      if (rdnAttrs.length == 1)
      AVA[] avas = new AVA[rdnAttrs.length];
      for (int i = 0; i < rdnAttrs.length; i++)
      {
        AttributeType t = rdnAttrs[0];
        AttributeType t = rdnAttrs[i];
        TemplateValue v = getValue(t);
        if (v == null)
        {
          return null;
        }
        rdn = new RDN(t, ByteString.valueOfUtf8(v.getValue().toString()));
      }
      else
      {
        String[]         names  = new String[rdnAttrs.length];
        ByteString[] values = new ByteString[rdnAttrs.length];
        for (int i=0; i < rdnAttrs.length; i++)
        {
          AttributeType t = rdnAttrs[i];
          TemplateValue v = getValue(t);
          if (v == null)
          {
            return null;
          }
          names[i]  = t.getNameOrOID();
          values[i] = ByteString.valueOfUtf8(v.getValue().toString());
        }
        rdn = new RDN(rdnAttrs, names, values);
        avas[i] = new AVA(t, v.getValue());
      }
      dn = parentDN.child(rdn);
      dn = parentDN.child(new RDN(avas));
    }
    return dn;
opendj-server-legacy/src/main/java/org/opends/server/types/RDN.java
@@ -33,6 +33,7 @@
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -106,70 +107,15 @@
    avas.add(new AVA(attributeType, attributeName, attributeValue));
  }
  /**
   * Creates a new RDN with the provided information.  The number of
   * type, name, and value elements must be nonzero and equal.
   * Creates a new RDN with the provided {@link AVA}s.
   *
   * @param  attributeTypes   The set of attribute types for this RDN.
   *                          It must not be empty or {@code null}.
   * @param  attributeNames   The set of user-provided names for this
   *                          RDN.  It must have the same number of
   *                          elements as the {@code attributeTypes}
   *                          argument.
   * @param  attributeValues  The set of values for this RDN.  It must
   *                          have the same number of elements as the
   *                          {@code attributeTypes} argument.
   * @param avas
   *          The AVAs that will define the new RDN
   */
  public RDN(List<AttributeType> attributeTypes,
             List<String> attributeNames,
             List<ByteString> attributeValues)
  public RDN(AVA... avas)
  {
    Reject.ifNull(attributeTypes, attributeNames, attributeValues);
    Reject.ifTrue(attributeTypes.isEmpty(), "attributeTypes must not be empty");
    Reject.ifFalse(attributeNames.size() == attributeTypes.size(),
            "attributeNames must have the same number of elements than attributeTypes");
    Reject.ifFalse(attributeValues.size() == attributeTypes.size(),
            "attributeValues must have the same number of elements than attributeTypes");
    avas = new ArrayList<>(attributeTypes.size());
    for (int i = 0; i < attributeTypes.size(); i++)
    {
      avas.add(new AVA(attributeTypes.get(i), attributeNames.get(i), attributeValues.get(i)));
    }
  }
  /**
   * Creates a new RDN with the provided information.  The number of
   * type, name, and value elements must be nonzero and equal.
   *
   * @param  attributeTypes   The set of attribute types for this RDN.
   *                          It must not be empty or {@code null}.
   * @param  attributeNames   The set of user-provided names for this
   *                          RDN.  It must have the same number of
   *                          elements as the {@code attributeTypes}
   *                          argument.
   * @param  attributeValues  The set of values for this RDN.  It must
   *                          have the same number of elements as the
   *                          {@code attributeTypes} argument.
   */
  public RDN(AttributeType[] attributeTypes, String[] attributeNames, ByteString[] attributeValues)
  {
    Reject.ifNull(attributeTypes, attributeNames, attributeValues);
    Reject.ifFalse(attributeTypes.length > 0, "attributeTypes must not be empty");
    Reject.ifFalse(attributeNames.length == attributeTypes.length,
        "attributeNames must have the same number of elements than attributeTypes");
    Reject.ifFalse(attributeValues.length == attributeTypes.length,
        "attributeValues must have the same number of elements than attributeTypes");
    avas = new ArrayList<>(attributeTypes.length);
    for (int i = 0; i < attributeTypes.length; i++)
    {
      avas.add(new AVA(attributeTypes[i], attributeNames[i], attributeValues[i]));
    }
    this(Arrays.asList(Reject.checkNotNull(avas, "avas must not be null")));
  }
  /**
@@ -185,8 +131,6 @@
    this.avas = new ArrayList<>(avas);
  }
  /**
   * Creates a new RDN with the provided information.
   *
@@ -233,30 +177,6 @@
    return false;
  }
  /**
   * Indicates whether this RDN includes the specified attribute type.
   *
   * @param  lowerName  The name or OID for the attribute type for
   *                    which to make the determination, formatted in
   *                    all lowercase characters.
   *
   * @return  <CODE>true</CODE> if the RDN includes the specified
   *          attribute type, or <CODE>false</CODE> if not.
   */
  public boolean hasAttributeType(String lowerName)
  {
    for (AVA ava : avas)
    {
      if (ava.getAttributeType().hasNameOrOID(lowerName))
      {
        return true;
      }
    }
    return false;
  }
  /**
   * Retrieves the attribute value that is associated with the
   * specified attribute type.
@@ -291,33 +211,6 @@
    return avas.size() > 1;
  }
  /**
   * Indicates whether this RDN contains the specified type-value
   * pair.
   *
   * @param  type   The attribute type for which to make the
   *                determination.
   * @param  value  The value for which to make the determination.
   *
   * @return  <CODE>true</CODE> if this RDN contains the specified
   *          attribute value, or <CODE>false</CODE> if not.
   */
  public boolean hasValue(AttributeType type, ByteString value)
  {
    for (AVA ava : avas)
    {
      if (ava.getAttributeType().equals(type) && ava.getAttributeValue().equals(value))
      {
        return true;
      }
    }
    return false;
  }
  /**
   * Adds the provided type-value pair from this RDN.  Note that this
   * is intended only for internal use when constructing DN values.
@@ -634,22 +527,6 @@
    }
  }
  /**
   * Creates a duplicate of this RDN that can be modified without
   * impacting this RDN.
   *
   * @return  A duplicate of this RDN that can be modified without
   *          impacting this RDN.
   */
  public RDN duplicate()
  {
    return new RDN(avas);
  }
  /**
   * Indicates whether the provided object is equal to this RDN.  It
   * will only be considered equal if it is an RDN object that
opendj-server-legacy/src/test/java/org/opends/server/types/TestRDN.java
@@ -30,7 +30,7 @@
import static org.opends.server.core.DirectoryServer.*;
import static org.testng.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import org.forgerock.opendj.ldap.AVA;
@@ -130,13 +130,7 @@
   */
  @Test
  public void testConstructorMultiAVA() throws Exception {
    AttributeType[]  attrTypes  = { AT_DC, AT_CN };
    String[]         attrNames  = { AT_DC.getNameOrOID(),
                                    AT_CN.getNameOrOID() };
    ByteString[]     attrValues = { AV_DC_ORG, AV_CN };
    RDN rdn = new RDN(attrTypes, attrNames, attrValues);
    RDN rdn = new RDN(new AVA(AT_DC, AV_DC_ORG), new AVA(AT_CN, AV_CN));
    assertEquals(rdn.size(), 2);
    Iterator<AVA> it = rdn.iterator();
@@ -162,19 +156,7 @@
   */
  @Test
  public void testConstructorMultiAVAList() throws Exception {
    ArrayList<AttributeType>  typeList  = new ArrayList<>();
    ArrayList<String>         nameList  = new ArrayList<>();
    ArrayList<ByteString>     valueList = new ArrayList<>();
    typeList.add(AT_DC);
    nameList.add(AT_DC.getNameOrOID());
    valueList.add(AV_DC_ORG);
    typeList.add(AT_CN);
    nameList.add(AT_CN.getNameOrOID());
    valueList.add(AV_CN);
    RDN rdn = new RDN(typeList, nameList, valueList);
    RDN rdn = new RDN(Arrays.asList(new AVA(AT_DC, AV_DC_ORG), new AVA(AT_CN, AV_CN)));
    assertEquals(rdn.size(), 2);
@@ -313,13 +295,7 @@
   */
  @Test
  public void testGetAttributeName() throws Exception {
    AttributeType[]  attrTypes  = { AT_DC, AT_CN };
    String[]         attrNames  = { AT_DC.getNameOrOID(),
                                    AT_CN.getNameOrOID() };
    ByteString[]     attrValues = { AV_DC_ORG, AV_CN };
    RDN rdn = new RDN(attrTypes, attrNames, attrValues);
    RDN rdn = new RDN(new AVA(AT_DC, AV_DC_ORG), new AVA(AT_CN, AV_CN));
    Iterator<AVA> it = rdn.iterator();
    AVA ava1 = it.next();
    AVA ava2 = it.next();
@@ -331,7 +307,7 @@
  @SuppressWarnings("javadoc")
  @Test(expectedExceptions = IllegalArgumentException.class)
  public void ensureRDNIsCreatedWithNonEmptyArguments() throws Exception {
      new RDN(new AttributeType[0], new String[0], new ByteString[0]);
      new RDN(new AVA[0]);
  }
  /**
@@ -342,12 +318,7 @@
   */
  @Test
  public void testGetAttributeType() throws Exception {
    AttributeType[]  attrTypes  = { AT_DC, AT_CN };
    String[]         attrNames  = { AT_DC.getNameOrOID(),
                                    AT_CN.getNameOrOID() };
    ByteString[]     attrValues = { AV_DC_ORG, AV_CN };
    RDN rdn = new RDN(attrTypes, attrNames, attrValues);
    RDN rdn = new RDN(new AVA(AT_DC, AV_DC_ORG), new AVA(AT_CN, AV_CN));
    Iterator<AVA> it = rdn.iterator();
    assertEquals(it.next().getAttributeType(), AT_DC);
    assertEquals(it.next().getAttributeType(), AT_CN);
@@ -361,12 +332,7 @@
   */
  @Test
  public void testGetAttributeValue() throws Exception {
    AttributeType[]  attrTypes  = { AT_DC, AT_CN };
    String[]         attrNames  = { AT_DC.getNameOrOID(),
                                    AT_CN.getNameOrOID() };
    ByteString[]     attrValues = { AV_DC_ORG, AV_CN };
    RDN rdn = new RDN(attrTypes, attrNames, attrValues);
    RDN rdn = new RDN(new AVA(AT_DC, AV_DC_ORG), new AVA(AT_CN, AV_CN));
    Iterator<AVA> it = rdn.iterator();
    assertEquals(it.next().getAttributeValue(), AV_DC_ORG);
    assertEquals(it.next().getAttributeValue(), AV_CN);
@@ -403,27 +369,6 @@
    assertEquals(rdn.size(), 2);
  }
  /**
   * Test hasAttributeType.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test
  public void testHasAttributeType1() throws Exception {
    RDN rdn = new RDN(AT_DC, AV_DC_ORG);
    assertTrue(rdn.hasAttributeType(AT_DC));
    assertTrue(rdn.hasAttributeType("dc"));
    assertTrue(rdn.hasAttributeType(AT_DC.getOID()));
    assertFalse(rdn.hasAttributeType(AT_CN));
    assertFalse(rdn.hasAttributeType("cn"));
  }
  /**
   * Test isMultiValued.
   *
@@ -440,27 +385,6 @@
    assertTrue(rdn.isMultiValued());
  }
  /**
   * Tests hasValue.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test
  public void testHasValue() throws Exception {
    RDN rdn = new RDN(AT_DC, AV_DC_ORG);
    assertTrue(rdn.hasValue(AT_DC, AV_DC_ORG));
    assertFalse(rdn.hasValue(AT_CN, AV_CN));
    rdn.addValue(AT_CN, AT_CN.getNameOrOID(), AV_CN);
    assertTrue(rdn.hasValue(AT_DC, AV_DC_ORG));
    assertTrue(rdn.hasValue(AT_CN, AV_CN));
  }
  /**
   * Tests addValue with a duplicate value.
   *
@@ -494,46 +418,6 @@
    assertEquals(rdn.toString(), stringRDN);
  }
  /**
   * Tests the duplicate method with a single-valued RDN.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test
  public void testDuplicateSingle() {
    RDN rdn1 = new RDN(AT_DC, AV_DC_ORG);
    RDN rdn2 = rdn1.duplicate();
    assertNotSame(rdn1, rdn2);
    assertEquals(rdn1, rdn2);
  }
  /**
   * Tests the duplicate method with a multivalued RDN.
   *
   * @throws Exception
   *           If the test failed unexpectedly.
   */
  @Test
  public void testDuplicateMultiValued() {
    AttributeType[]  types  = new AttributeType[] { AT_DC, AT_CN };
    String[]         names  = new String[] { "dc", "cn" };
    ByteString[]     values = new ByteString[] { AV_DC_ORG, AV_CN };
    RDN rdn1 = new RDN(types, names, values);
    RDN rdn2 = rdn1.duplicate();
    assertNotSame(rdn1, rdn2);
    assertEquals(rdn1, rdn2);
  }
  /**
   * RDN equality test data provider.
   *