From 989e90644450bf9c04893c40b54a7a06406f1f7e Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 14 Aug 2015 16:46:14 +0000
Subject: [PATCH] Code cleanup

---
 opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalAttributeValueTestCase.java |   57 ++++++++++++++
 opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistAttrModificationKey.java          |   20 ++--
 opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistoricalAttributeValue.java         |  105 ++++++++++++++++----------
 3 files changed, 130 insertions(+), 52 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistAttrModificationKey.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistAttrModificationKey.java
index 16a5dbf..1f5439c 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistAttrModificationKey.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistAttrModificationKey.java
@@ -41,7 +41,7 @@
   /** The key for attribute value deletion. */
   DEL("del"),
   /** The key for attribute deletion. */
-  DELATTR("delAttr"),
+  DELATTR("attrDel"),
   /** The key for attribute replace. */
   REPL("repl"),
   /** The key for attribute value addition. */
@@ -68,16 +68,14 @@
    */
   public static HistAttrModificationKey decodeKey(String histkey)
   {
-     if ("repl".equals(histkey)) {
-       return HistAttrModificationKey.REPL;
-     } else if ("add".equals(histkey)) {
-       return HistAttrModificationKey.ADD;
-     } else if ("del".equals(histkey)) {
-       return HistAttrModificationKey.DEL;
-     } else if ("attrDel".equals(histkey)) {
-       return HistAttrModificationKey.DELATTR;
-     }
-     return null;
+    for (HistAttrModificationKey histKey : values())
+    {
+      if (histKey.toString().equals(histkey))
+      {
+        return histKey;
+      }
+    }
+    return null;
   }
 
   /**
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistoricalAttributeValue.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistoricalAttributeValue.java
index 58db60e..531d4ec 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistoricalAttributeValue.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/plugin/HistoricalAttributeValue.java
@@ -26,6 +26,8 @@
  */
 package org.opends.server.replication.plugin;
 
+import static org.opends.server.replication.plugin.HistAttrModificationKey.*;
+
 import java.util.LinkedHashSet;
 import java.util.Set;
 
@@ -46,19 +48,19 @@
  *
  *
  * an historical attribute value looks like :
- *  description:00000108b3a6554100000001:add:added_value
+ *  description:00000108b3a65541000000000001:add:added_value
  *  or
- *  description:00000108b3a6cbb800000001:del:deleted_value
+ *  description:00000108b3a6cbb8000000000001:del:deleted_value
  *  or
- *  description:00000108b3a6cbb800000001:repl:new_value
+ *  description:00000108b3a6cbb8000000000001:repl:new_value
  *  or
- *  description:00000108b3a6cbb800000001:delAttr
+ *  description:00000108b3a6cbb8000000000001:attrDel
  *  or
- *  description:00000108b3a6554100000001:add
+ *  description:00000108b3a65541000000000001:add
  *  or
- *  dn:00000108b3a6554100000001:add (ADD operation)
+ *  dn:00000108b3a65541000000000001:add (ADD operation)
  *  or
- *  dn:00000108b3a6554100000001:moddn (MODIFYDN operation)
+ *  dn:00000108b3a65541000000000001:moddn (MODIFYDN operation)
  *
  *  so after split
  *  token[0] will contain the attribute name
@@ -68,23 +70,23 @@
  *
  *  options are stored with the attribute names using; as a separator
  *  example :
- *  description;FR;France:00000108b3a6554100000001:add:added_value
+ *  description;FR;France:00000108b3a65541000000000001:add:added_value
  */
-public class HistoricalAttributeValue
+class HistoricalAttributeValue
 {
-  private AttributeType attrType;
-  private String attrString;
-  private ByteString attributeValue;
-  private CSN csn;
-  private Set<String> options;
-  private HistAttrModificationKey histKey;
-  private String stringValue;
+  private final AttributeType attrType;
+  private final String attrString;
+  private final ByteString attributeValue;
+  private final CSN csn;
+  private final Set<String> options = new LinkedHashSet<>();
+  private final HistAttrModificationKey histKey;
+  private final String stringValue;
 
   /**
    * This flag indicates that this value was generated to store the last date
    * when the entry was renamed.
    */
-  private boolean ismodDN;
+  private boolean isModDN;
 
   /**
    * Create a new object from the String encoded form.
@@ -92,11 +94,10 @@
    * @param strVal The String encoded form of historical attribute value.
    * @see EntryHistorical#encodeAndPurge() encoding in EntryHistorical
    */
-  public HistoricalAttributeValue(String strVal)
+  HistoricalAttributeValue(String strVal)
   {
     String[] token = strVal.split(":", 4);
 
-    options = new LinkedHashSet<>();
     if (token[0].contains(";"))
     {
       String[] optionsToken = token[0].split(";");
@@ -116,7 +117,7 @@
     if (attrString.compareTo("dn") != 0)
     {
       // This HistVal was used to store the date when some
-       // modifications were done to the entries.
+      // modifications were done to the entries.
       attrType = DirectoryServer.getAttributeTypeOrDefault(attrString);
     }
     else
@@ -126,14 +127,13 @@
       attrType = null;
       if (token.length >= 3 && token[2].compareTo("moddn") == 0)
       {
-        ismodDN = true;
+        isModDN = true;
       }
     }
 
     csn = new CSN(token[1]);
     histKey = HistAttrModificationKey.decodeKey(token[2]);
-    stringValue = null;
-    if (histKey != HistAttrModificationKey.DELATTR)
+    if (histKey != DELATTR)
     {
       if (token.length == 4)
       {
@@ -142,6 +142,7 @@
       }
       else
       {
+        stringValue = null;
         attributeValue = null;
       }
     }
@@ -221,54 +222,76 @@
     AttributeBuilder builder = new AttributeBuilder(attrType, attrString);
     builder.setOptions(options);
 
-    if (histKey != HistAttrModificationKey.DELATTR)
+    if (histKey != DELATTR)
     {
       builder.add(attributeValue);
     }
     Attribute attr = builder.toAttribute();
 
-    Modification mod;
     switch (histKey)
     {
     case ADD:
-      mod = new Modification(ModificationType.ADD, attr);
-      break;
+      return new Modification(ModificationType.ADD, attr);
     case DEL:
-      mod = new Modification(ModificationType.DELETE, attr);
-      break;
+      return new Modification(ModificationType.DELETE, attr);
     case REPL:
-      mod = new Modification(ModificationType.REPLACE, attr);
-      break;
+      return new Modification(ModificationType.REPLACE, attr);
     case DELATTR:
-      mod = new Modification(ModificationType.DELETE, attr);
-      break;
+      return new Modification(ModificationType.DELETE, attr);
     default:
-      mod = null;
+      return null;
     }
-    return mod;
   }
 
   /**
    * Indicates if this value of the historical attribute was generated
    * for a ADD operation.
    *
-   * @return a boolean indicating if this was generated for a ADD
-   *         operation.
+   * @return a boolean indicating if this was generated for a ADD operation.
    */
   public boolean isADDOperation()
   {
-    return attrType == null && !ismodDN;
+    return attrType == null && !isModDN;
   }
 
   /**
    * Indicates if this value of the historical attribute was generated
    * for a MODDN operation.
    *
-   * @return a boolean indicating if this was generated for a ADDMODDN
-   *         operation.
+   * @return a boolean indicating if this was generated for a ADDMODDN operation.
    */
   public boolean isMODDNOperation()
   {
-    return attrType == null && ismodDN;
+    return attrType == null && isModDN;
+  }
+
+  @Override
+  public String toString()
+  {
+    final StringBuilder sb = new StringBuilder();
+    sb.append(attrString);
+    for (String option : this.options)
+    {
+      sb.append(";").append(option);
+    }
+    sb.append(":").append(csn).append(":").append(getModificationType());
+    if (stringValue != null)
+    {
+      sb.append(":").append(stringValue);
+    }
+    return sb.toString();
+  }
+
+  private String getModificationType()
+  {
+    if (isModDN)
+    {
+      return "moddn";
+    }
+    else if (histKey != null)
+    {
+      return histKey.toString();
+    }
+    return "TODO";
   }
 }
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalAttributeValueTestCase.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalAttributeValueTestCase.java
new file mode 100644
index 0000000..a55849f
--- /dev/null
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/HistoricalAttributeValueTestCase.java
@@ -0,0 +1,57 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
+ * or http://forgerock.org/license/CDDLv1.0.html.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at legal-notices/CDDLv1_0.txt.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *      Copyright 2015 ForgeRock AS
+ */
+package org.opends.server.replication.plugin;
+
+import org.assertj.core.api.Assertions;
+import org.opends.server.replication.ReplicationTestCase;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+/** Test the naming conflict resolution code. */
+@SuppressWarnings("javadoc")
+public class HistoricalAttributeValueTestCase extends ReplicationTestCase
+{
+  @DataProvider
+  public Object[][] values()
+  {
+    return new Object[][] {
+      { "description:0000014f2d0c9f53000100000001:add:added_value" },
+      { "description:0000014f2d0c9f53000100000001:del:deleted_value" },
+      { "description:0000014f2d0c9f53000100000001:repl:new_value" },
+      { "description:0000014f2d0c9f53000100000001:attrDel" },
+      { "description:0000014f2d0c9f53000100000001:add" },
+      { "dn:0000014f2d0c9f53000100000001:add" },
+      { "dn:0000014f2d0c9f53000100000001:moddn" },
+      { "description;FR;France:0000014f2d0c9f53000100000001:add:added_value" },
+    };
+  }
+
+  @Test(dataProvider = "values")
+  public void testCtor(String strVal)
+  {
+    HistoricalAttributeValue val = new HistoricalAttributeValue(strVal);
+    Assertions.assertThat(strVal).isEqualTo(val.toString());
+  }
+}

--
Gitblit v1.10.0