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

Jean-Noel Rouvignac
01.54.2013 de6d2e34656fc7cfc48154fee47309f001c55834
Code cleanup

LDAPAttribute.java:
In ctor, used attribute.getNameWithOptions() to remove duplicated code.

*.java:
Used interfaces instead of concrete classes.
4 files modified
304 ■■■■■ changed files
opends/src/server/org/opends/server/protocols/ldap/LDAPAttribute.java 34 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/types/RawAttribute.java 26 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/plugins/AttributeCleanupPluginTestCase.java 71 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestAddRequestProtocolOp.java 173 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/protocols/ldap/LDAPAttribute.java
@@ -23,11 +23,10 @@
 *
 *
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 *      Portions copyright 2013 ForgeRock AS
 */
package org.opends.server.protocols.ldap;
import static org.opends.messages.ProtocolMessages.*;
import static org.opends.server.util.ServerConstants.*;
@@ -38,7 +37,6 @@
import org.opends.messages.Message;
import org.opends.server.types.*;
/**
 * This class defines the data structures and methods to use when interacting
 * with an LDAP attribute, which is the basic unit of information in an LDAP
@@ -47,10 +45,10 @@
public class LDAPAttribute
       extends RawAttribute
{
  // The set of values for this attribute.
  private ArrayList<ByteString> values;
  /** The set of values for this attribute. */
  private List<ByteString> values;
  // The attribute type for this attribute.
  /** The attribute type for this attribute. */
  private String attributeType;
@@ -156,21 +154,7 @@
   */
  public LDAPAttribute(Attribute attribute)
  {
    if (attribute.hasOptions())
    {
      StringBuilder attrName = new StringBuilder(attribute.getName());
      for (String o : attribute.getOptions())
      {
        attrName.append(";");
        attrName.append(o);
      }
      this.attributeType = attrName.toString();
    }
    else
    {
      this.attributeType = attribute.getName();
    }
    this.attributeType = attribute.getNameWithOptions();
    if (attribute.isVirtual())
    {
@@ -199,6 +183,7 @@
   *
   * @return  The attribute type for this attribute.
   */
  @Override
  public String getAttributeType()
  {
    return attributeType;
@@ -211,6 +196,7 @@
   *
   * @param  attributeType  The attribute type for this attribute.
   */
  @Override
  public void setAttributeType(String attributeType)
  {
    this.attributeType = attributeType;
@@ -224,7 +210,8 @@
   *
   * @return  The set of values for this attribute.
   */
  public ArrayList<ByteString> getValues()
  @Override
  public List<ByteString> getValues()
  {
    return values;
  }
@@ -241,6 +228,7 @@
   * @throws  LDAPException  If the provided value is invalid according to the
   *                         attribute syntax.
   */
  @Override
  public Attribute toAttribute()
         throws LDAPException
  {
@@ -311,6 +299,7 @@
   *
   * @param  buffer  The buffer to which the information should be appended.
   */
  @Override
  public void toString(StringBuilder buffer)
  {
    buffer.append("LDAPAttribute(type=");
@@ -341,6 +330,7 @@
   * @param  indent  The number of spaces from the margin that the lines should
   *                 be indented.
   */
  @Override
  public void toString(StringBuilder buffer, int indent)
  {
    StringBuilder indentBuf = new StringBuilder(indent);
opends/src/server/org/opends/server/types/RawAttribute.java
@@ -23,25 +23,24 @@
 *
 *
 *      Copyright 2006-2009 Sun Microsystems, Inc.
 *      Portions copyright 2013 ForgeRock AS
 */
package org.opends.server.types;
import org.opends.messages.Message;
import java.util.ArrayList;
import java.io.IOException;
import org.opends.server.protocols.asn1.*;
import org.opends.server.protocols.ldap.LDAPAttribute;
import static org.opends.server.loggers.debug.DebugLogger.*;
import org.opends.server.loggers.debug.DebugTracer;
import static org.opends.messages.ProtocolMessages.*;
import static org.opends.server.loggers.debug.DebugLogger.*;
import static org.opends.server.protocols.ldap.LDAPResultCode.*;
import static org.opends.server.util.Validator.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.opends.messages.Message;
import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.protocols.asn1.ASN1Reader;
import org.opends.server.protocols.asn1.ASN1Writer;
import org.opends.server.protocols.ldap.LDAPAttribute;
/**
 * This class defines a raw attribute, which has a type (which may
@@ -180,7 +179,7 @@
   *
   * @return  The set of values for this attribute.
   */
  public abstract ArrayList<ByteString> getValues();
  public abstract List<ByteString> getValues();
@@ -210,7 +209,7 @@
    stream.writeOctetString(getAttributeType());
    stream.writeStartSet();
    ArrayList<ByteString> values = getValues();
    List<ByteString> values = getValues();
    if ((values != null))
    {
      for(ByteString value : values)
@@ -322,6 +321,7 @@
   *
   * @return  A string representation of this attribute.
   */
  @Override
  public String toString()
  {
    StringBuilder buffer = new StringBuilder();
opends/tests/unit-tests-testng/src/server/org/opends/server/plugins/AttributeCleanupPluginTestCase.java
@@ -23,6 +23,7 @@
 *
 *
 *      Copyright 2011 profiq s.r.o.
 *      Portions copyright 2013 ForgeRock AS
 */
package org.opends.server.plugins;
@@ -31,6 +32,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.opends.server.TestCaseUtils;
import org.opends.server.admin.server.AdminTestCaseUtils;
@@ -50,14 +52,12 @@
/**
 * Tests for the attribute cleanup plugin.
 */
@SuppressWarnings("javadoc")
public class AttributeCleanupPluginTestCase extends PluginTestCase
{
  @BeforeClass()
  public void startServer()
         throws Exception
  public void startServer() throws Exception
  {
    TestCaseUtils.startServer();
  }
@@ -67,7 +67,6 @@
  @DataProvider(name = "validConfigs")
  public Object[][] getValidConfigs() throws Exception
  {
    List<Entry> entries = TestCaseUtils.makeEntries(
      "dn: cn=Attribute Cleanup,cn=Plugins,cn=config",
      "objectClass: top",
@@ -102,22 +101,14 @@
      "ds-cfg-java-class: org.opends.server.plugins.AttributeCleanupPlugin",
      "ds-cfg-rename-inbound-attributes: cn:uid");
    Object[][] array = new Object[entries.size()][1];
    for (int i=0; i < array.length; i++)
    {
      array[i] = new Object[] { entries.get(i) };
    }
    return array;
    return toArrayArray(entries);
  }
  @Test(dataProvider = "validConfigs")
  public void testInitializeWithValidConfigs(Entry e)
    throws Exception
  {
    HashSet<PluginType> pluginTypes = getPluginTypes(e);
    Set<PluginType> pluginTypes = getPluginTypes(e);
    assertTrue(!pluginTypes.isEmpty());
    AttributeCleanupPluginCfg config =
@@ -180,6 +171,11 @@
      "ds-cfg-java-class: org.opends.server.plugins.AttributeCleanupPlugin",
      "ds-cfg-rename-inbound-attributes: cn:cn");
    return toArrayArray(entries);
  }
  private Object[][] toArrayArray(List<Entry> entries)
  {
    Object[][] array = new Object[entries.size()][1];
    for (int i=0; i < array.length; i++)
    {
@@ -196,7 +192,7 @@
  public void testInitializeWithInvalidConfigs(Entry e)
    throws ConfigException, InitializationException
  {
    HashSet<PluginType> pluginTypes = new HashSet<PluginType>();
    Set<PluginType> pluginTypes = new HashSet<PluginType>();
    List<Attribute> attrList = e.getAttribute("ds-cfg-plugin-type");
    assertNotNull(attrList);
@@ -234,9 +230,7 @@
  @Test()
  public void testRenameAttributesForAddOperation() throws Exception
  {
    /* Configure the plugint to rename incoming 'cn' attributes to
     * 'description'.
     */
    // Configure the plugin to rename incoming 'cn' attributes to 'description'.
    Entry confEntry = TestCaseUtils.makeEntry(
      "dn: cn=Attribute Cleanup,cn=Plugins,cn=config",
      "objectClass: top",
@@ -249,7 +243,7 @@
      "ds-cfg-rename-inbound-attributes: cn:description",
      "ds-cfg-java-class: org.opends.server.plugins.AttributeCleanupPlugin");
    HashSet<PluginType> pluginTypes = getPluginTypes(confEntry);
    Set<PluginType> pluginTypes = getPluginTypes(confEntry);
    AttributeCleanupPluginCfg config =
      AdminTestCaseUtils.getConfiguration(
@@ -277,7 +271,6 @@
    values.add(ByteString.valueOf("inetorgperson"));
    List<RawAttribute> rawAttributes = new ArrayList<RawAttribute>();
    rawAttributes.add(RawAttribute.create("objectClass", values));
    rawAttributes.add(RawAttribute.create("uid", "test"));
    rawAttributes.add(RawAttribute.create("cn", "Name Surname"));
@@ -310,7 +303,7 @@
    {
      if(rawAttr.getAttributeType().equalsIgnoreCase("description"))
      {
        ArrayList<ByteString> attrVals = rawAttr.getValues();
        List<ByteString> attrVals = rawAttr.getValues();
        assertEquals("Name Surname", attrVals.get(0).toString());
        plugin.finalizePlugin();
        return;
@@ -345,7 +338,7 @@
      "ds-cfg-remove-inbound-attributes: createTimeStamp",
      "ds-cfg-java-class: org.opends.server.plugins.AttributeCleanupPlugin");
    HashSet<PluginType> pluginTypes = getPluginTypes(confEntry);
    Set<PluginType> pluginTypes = getPluginTypes(confEntry);
    AttributeCleanupPluginCfg config =
      AdminTestCaseUtils.getConfiguration(
@@ -407,11 +400,9 @@
    for(RawAttribute rawAttr : rawAttrs)
    {
      if(rawAttr.getAttributeType().equalsIgnoreCase("modifyTimeStamp")
         || rawAttr.getAttributeType().equalsIgnoreCase("createTimeStamp"))
      {
        fail("Attribute '" + rawAttr.getAttributeType() + "' exists and it shouldn't");
      }
      assertFalse(rawAttr.getAttributeType().equalsIgnoreCase("modifyTimeStamp")
          || rawAttr.getAttributeType().equalsIgnoreCase("createTimeStamp"),
          "Attribute '" + rawAttr.getAttributeType() + "' exists and it shouldn't");
    }
    plugin.finalizePlugin();
@@ -446,7 +437,7 @@
      "ds-cfg-remove-inbound-attributes: createTimeStamp",
      "ds-cfg-java-class: org.opends.server.plugins.AttributeCleanupPlugin");
    HashSet<PluginType> pluginTypes = getPluginTypes(confEntry);
    Set<PluginType> pluginTypes = getPluginTypes(confEntry);
    AttributeCleanupPluginCfg config =
      AdminTestCaseUtils.getConfiguration(
@@ -520,7 +511,7 @@
      "ds-cfg-remove-inbound-attributes: createTimeStamp",
      "ds-cfg-java-class: org.opends.server.plugins.AttributeCleanupPlugin");
    HashSet<PluginType> pluginTypes = getPluginTypes(confEntry);
    Set<PluginType> pluginTypes = getPluginTypes(confEntry);
    AttributeCleanupPluginCfg config =
      AdminTestCaseUtils.getConfiguration(
@@ -601,7 +592,6 @@
  /**
   * Verify the attribute renaming for the MODIFY operation.
   * @throws Exception
   */
  @Test()
  public void testRenameAttributesForModifyOperation() throws Exception
@@ -621,7 +611,7 @@
      "ds-cfg-rename-inbound-attributes: modifyTimeStamp:description",
      "ds-cfg-java-class: org.opends.server.plugins.AttributeCleanupPlugin");
    HashSet<PluginType> pluginTypes = getPluginTypes(confEntry);
    Set<PluginType> pluginTypes = getPluginTypes(confEntry);
    AttributeCleanupPluginCfg config =
      AdminTestCaseUtils.getConfiguration(
@@ -683,31 +673,28 @@
      RawAttribute modAttr = rawMod.getAttribute();
      if (modAttr.getAttributeType().equalsIgnoreCase("description"))
      {
        ArrayList<ByteString> descrValues = modAttr.getValues();
        List<ByteString> descrValues = modAttr.getValues();
        assertEquals("2011091212400000Z", descrValues.get(0).toString());
        plugin.finalizePlugin();
        return;
      }
      if (modAttr.getAttributeType().equalsIgnoreCase("modifyTimeStamp"))
      {
        fail("modifyTimeStamp shouldn't exist but it does.");
      }
      assertFalse(modAttr.getAttributeType().equalsIgnoreCase("modifyTimeStamp"),
          "modifyTimeStamp shouldn't exist but it does.");
    }
    fail();
  }
  /**
   * Helper method to get the plugin types from the configuration entry.
   *
   * @param e Configuration entry.
   * @return HashSet of plugin types.
   * @return Set of plugin types.
   */
  private HashSet<PluginType> getPluginTypes(Entry e)
  private Set<PluginType> getPluginTypes(Entry e)
  {
    HashSet<PluginType> pluginTypes = new HashSet<PluginType>();
    Set<PluginType> pluginTypes = new HashSet<PluginType>();
    List<Attribute> attrList = e.getAttribute("ds-cfg-plugin-type");
    for(Attribute a : attrList)
opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestAddRequestProtocolOp.java
@@ -23,27 +23,27 @@
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions copyright 2013 ForgeRock AS
 */
package org.opends.server.protocols.ldap;
import org.opends.server.types.LDAPException;
import org.opends.server.types.RawAttribute;
import org.opends.server.types.ByteString;
import org.opends.server.types.ByteStringBuilder;
import static org.opends.server.util.ServerConstants.EOL;
import org.opends.server.util.Base64;
import org.opends.server.protocols.asn1.ASN1Writer;
import org.opends.server.protocols.asn1.ASN1;
import org.opends.server.protocols.asn1.ASN1Reader;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.StringReader;
import static org.opends.server.util.ServerConstants.*;
import static org.testng.Assert.*;
import org.testng.annotations.*;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.opends.server.protocols.asn1.ASN1;
import org.opends.server.protocols.asn1.ASN1Reader;
import org.opends.server.protocols.asn1.ASN1Writer;
import org.opends.server.types.ByteString;
import org.opends.server.types.ByteStringBuilder;
import org.opends.server.types.LDAPException;
import org.opends.server.types.RawAttribute;
import org.opends.server.util.Base64;
import org.testng.annotations.Test;
/**
 * This class defines a set of tests for the
@@ -89,25 +89,20 @@
   * @return              The generate attributes.
   *
   */
  private ArrayList<RawAttribute> generateAttributes(int numAttributes,
  private List<RawAttribute> generateAttributes(int numAttributes,
                                                      int numValues,
                                                      String prefix)
  {
    ArrayList<RawAttribute> attributes = new ArrayList<RawAttribute>();
    LDAPAttribute attribute;
    ByteString value;
    int i, j;
    List<RawAttribute> attributes = new ArrayList<RawAttribute>();
    for(i = 0; i < numAttributes; i++)
    for (int i = 0; i < numAttributes; i++)
    {
      ArrayList<ByteString> values = new ArrayList<ByteString>();
      for(j = 0; j < numValues; j++)
      for (int j = 0; j < numValues; j++)
      {
        value = ByteString.valueOf(prefix + "Value"+i+"."+j);
        values.add(value);
        values.add(ByteString.valueOf(prefix + "Value" + i + "." + j));
      }
      attribute = new LDAPAttribute("testAttribute"+i, values);
      attributes.add(attribute);
      attributes.add(new LDAPAttribute("testAttribute" + i, values));
    }
    return attributes;
@@ -121,16 +116,10 @@
      return false;
    }
    int i, j;
    RawAttribute attribute1;
    RawAttribute attribute2;
    ArrayList<ByteString> values1;
    ArrayList<ByteString> values2;
    for(i = 0; i < attributes1.size(); i++)
    for (int i = 0; i < attributes1.size(); i++)
    {
      attribute1 = attributes1.get(i);
      attribute2 = attributes2.get(i);
      RawAttribute attribute1 = attributes1.get(i);
      RawAttribute attribute2 = attributes2.get(i);
      if(!attribute1.getAttributeType().equals(attribute2.getAttributeType()))
      {
        return false;
@@ -139,9 +128,9 @@
      {
        return false;
      }
      values1 = attribute1.getValues();
      values2 = attribute2.getValues();
      for(j = 0; j < values1.size(); j++)
      List<ByteString> values1 = attribute1.getValues();
      List<ByteString> values2 = attribute2.getValues();
      for (int j = 0; j < values1.size(); j++)
      {
        if(!values1.get(j).equals(values2.get(j)))
        {
@@ -186,7 +175,7 @@
  public void testConstructors() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<RawAttribute> attributes;
    List<RawAttribute> attributes;
    //Test to make sure the constructor with dn param works.
    addRequest = new AddRequestProtocolOp(dn);
@@ -286,14 +275,14 @@
  {
    ByteStringBuilder builder = new ByteStringBuilder();
    ASN1Writer writer = ASN1.getWriter(builder);
    AddRequestProtocolOp addEncoded;
    AddRequestProtocolOp addDecoded;
    addEncoded = new AddRequestProtocolOp(null, null);
    AddRequestProtocolOp addEncoded = new AddRequestProtocolOp(null, null);
    addEncoded.write(writer);
    ASN1Reader reader = ASN1.getReader(builder.toByteString());
    addDecoded = (AddRequestProtocolOp)LDAPReader.readProtocolOp(reader);
    AddRequestProtocolOp addDecoded =
        (AddRequestProtocolOp) LDAPReader.readProtocolOp(reader);
    assertEquals(addEncoded, addDecoded);
  }
  /**
@@ -308,7 +297,7 @@
    ASN1Writer writer = ASN1.getWriter(builder);
    AddRequestProtocolOp addEncoded;
    AddRequestProtocolOp addDecoded;
    ArrayList<RawAttribute> attributes;
    List<RawAttribute> attributes;
    //Test case for a full encode decode operation with normal params.
@@ -355,26 +344,22 @@
  @Test
  public void testToLDIF() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<RawAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    BufferedReader reader;
    String line;
    int i, j;
    int numAttributes, numValues;
    numAttributes = 10;
    numValues = 5;
    attributes = generateAttributes(numAttributes, numValues, "test");
    addRequest = new AddRequestProtocolOp(dn, attributes);
    int numAttributes = 10;
    int numValues = 5;
    List<RawAttribute> attributes =
        generateAttributes(numAttributes, numValues, "test");
    AddRequestProtocolOp addRequest = new AddRequestProtocolOp(dn, attributes);
    addRequest.toLDIF(buffer, 80);
    reader = new BufferedReader(new StringReader(buffer.toString()));
    line = reader.readLine();
    BufferedReader reader =
        new BufferedReader(new StringReader(buffer.toString()));
    String line = reader.readLine();
    assertEquals(line, "dn: "+dn);
    for(i = 0; i < numAttributes; i++)
    for (int i = 0; i < numAttributes; i++)
    {
      for(j = 0; j < numValues; j++)
      for (int j = 0; j < numValues; j++)
      {
        line = reader.readLine();
        assertEquals(line, "testAttribute"+i+": "+"testValue"+i+"."+j);
@@ -390,35 +375,31 @@
  @Test
  public void testToLDIFBase64() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<RawAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    BufferedReader reader;
    String line;
    String expectedLine;
    int i, j;
    int numAttributes, numValues;
    numAttributes = 10;
    numValues = 5;
    attributes = generateAttributes(numAttributes, numValues, " test");
    int numAttributes = 10;
    int numValues = 5;
    List<RawAttribute> attributes =
        generateAttributes(numAttributes, numValues, " test");
    ByteString dnNeedsBase64 =
      ByteString.valueOf("dc=example,dc=com ");
    ByteString dnNeedsBase64 = ByteString.valueOf("dc=example,dc=com ");
    addRequest = new AddRequestProtocolOp(dnNeedsBase64, attributes);
    AddRequestProtocolOp addRequest =
        new AddRequestProtocolOp(dnNeedsBase64, attributes);
    addRequest.toLDIF(buffer, 80);
    reader = new BufferedReader(new StringReader(buffer.toString()));
    line = reader.readLine();
    BufferedReader reader =
        new BufferedReader(new StringReader(buffer.toString()));
    String line = reader.readLine();
    assertEquals(line, "dn:: "+Base64.encode(dnNeedsBase64));
    for(i = 0; i < numAttributes; i++)
    for (int i = 0; i < numAttributes; i++)
    {
      for(j = 0; j < numValues; j++)
      for (int j = 0; j < numValues; j++)
      {
        line = reader.readLine();
        expectedLine =  " testValue"+i+"."+j;
        assertEquals(line, "testAttribute"+i+":: "+Base64.encode(expectedLine.getBytes()));
        String expectedLine = " testValue" + i + "." + j;
        assertEquals(line, "testAttribute" + i + ":: "
            + Base64.encode(expectedLine.getBytes()));
      }
    }
  }
@@ -431,21 +412,18 @@
  @Test
  public void TestToStringSingleLine() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<RawAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int numAttributes, numValues;
    numAttributes = 10;
    numValues = 5;
    attributes = generateAttributes(numAttributes, numValues, "test");
    addRequest = new AddRequestProtocolOp(dn, attributes);
    int numAttributes = 10;
    int numValues = 5;
    List<RawAttribute> attributes =
        generateAttributes(numAttributes, numValues, "test");
    AddRequestProtocolOp addRequest = new AddRequestProtocolOp(dn, attributes);
    addRequest.toString(buffer);
    StringBuilder key = new StringBuilder();
    key.append("AddRequest(dn="+dn+", attrs={");
    for(i = 0; i < numAttributes; i++)
    for (int i = 0; i < numAttributes; i++)
    {
      attributes.get(i).toString(key);
      if(i < numAttributes - 1)
@@ -466,26 +444,23 @@
  @Test
  public void TestToStringMultiLine() throws Exception
  {
    AddRequestProtocolOp addRequest;
    ArrayList<RawAttribute> attributes;
    StringBuilder buffer = new StringBuilder();
    StringBuilder key = new StringBuilder();
    int i;
    int numAttributes, numValues, indent;
    numAttributes = 10;
    numValues = 5;
    indent = 5;
    attributes = generateAttributes(numAttributes, numValues, "test");
    addRequest = new AddRequestProtocolOp(dn, attributes);
    int numAttributes = 10;
    int numValues = 5;
    int indent = 5;
    List<RawAttribute> attributes =
        generateAttributes(numAttributes, numValues, "test");
    AddRequestProtocolOp addRequest = new AddRequestProtocolOp(dn, attributes);
    addRequest.toString(buffer, indent);
    StringBuilder indentBuf = new StringBuilder(indent);
    for (i=0 ; i < indent; i++)
    for (int i = 0; i < indent; i++)
    {
      indentBuf.append(' ');
    }
    StringBuilder key = new StringBuilder();
    key.append(indentBuf);
    key.append("Add Request");
    key.append(EOL);