From 087d073b1216dadebdfe2364ff7f222d20b86808 Mon Sep 17 00:00:00 2001
From: coulbeck <coulbeck@localhost>
Date: Tue, 19 Sep 2006 18:03:10 +0000
Subject: [PATCH] Expand coverage of SearchResultEntryProtocolOp.

---
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/LdapTestCase.java                    |   48 +++++++++++++++-
 opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchResultEntryProtocolOp.java |   90 ++++++++++++++++++++++++++---
 2 files changed, 125 insertions(+), 13 deletions(-)

diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/LdapTestCase.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/LdapTestCase.java
index ade46dd..83fdb45 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/LdapTestCase.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/LdapTestCase.java
@@ -29,10 +29,52 @@
 import org.opends.server.DirectoryServerTestCase;
 import org.testng.annotations.Test;
 
+import java.util.LinkedList;
+import java.util.ListIterator;
+
 /**
- * An abstract class that all types  unit test should extend. 
+ * An abstract class that all types  unit test should extend.
  */
 
 @Test(groups = { "precommit", "ldap" })
-public abstract class LdapTestCase extends DirectoryServerTestCase 
-{}
+public abstract class LdapTestCase extends DirectoryServerTestCase
+{
+  /**
+   * Determine whether one LDAPAttribute is equal to another.
+   * The values of the attribute must be identical and in the same order.
+   * @param a1 The first LDAPAttribute.
+   * @param a2 The second LDAPAttribute.
+   * @return true if the first LDAPAttribute is equal to the second.
+   */
+  static boolean testEqual(LDAPAttribute a1, LDAPAttribute a2)
+  {
+    if (!a1.getAttributeType().equals(a2.getAttributeType()))
+    {
+      return false;
+    }
+    return a1.getValues().equals(a2.getValues());
+  }
+
+  /**
+   * Determine whether one list of LDAPAttribute is equal to another.
+   * @param list1 The first list of LDAPAttribute.
+   * @param list2 The second list of LDAPAttribute.
+   * @return true if the first list of LDAPAttribute is equal to the second.
+   */
+  static boolean testEqual(LinkedList<LDAPAttribute> list1,
+                           LinkedList<LDAPAttribute> list2)
+  {
+    ListIterator<LDAPAttribute> e1 = list1.listIterator();
+    ListIterator<LDAPAttribute> e2 = list2.listIterator();
+    while(e1.hasNext() && e2.hasNext()) {
+      LDAPAttribute o1 = e1.next();
+      LDAPAttribute o2 = e2.next();
+      if (!(o1==null ? o2==null : testEqual(o1, o2)))
+        return false;
+    }
+    return !(e1.hasNext() || e2.hasNext());
+  }
+
+
+
+}
diff --git a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchResultEntryProtocolOp.java b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchResultEntryProtocolOp.java
index 56117d3..a2635e6 100644
--- a/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchResultEntryProtocolOp.java
+++ b/opendj-sdk/opends/tests/unit-tests-testng/src/server/org/opends/server/protocols/ldap/TestSearchResultEntryProtocolOp.java
@@ -31,12 +31,15 @@
 import org.opends.server.types.Entry;
 import org.opends.server.types.SearchResultEntry;
 import org.opends.server.TestCaseUtils;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.fail;
+import org.opends.server.protocols.asn1.*;
+import static org.opends.server.protocols.ldap.LDAPConstants.
+     OP_TYPE_SEARCH_RESULT_ENTRY;
+import static org.testng.Assert.*;
 import org.testng.annotations.Test;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
 
+import java.util.ArrayList;
 
 public class TestSearchResultEntryProtocolOp extends LdapTestCase
 {
@@ -52,7 +55,10 @@
     return new Object[][] {
          {
               TestCaseUtils.makeEntry(
-                   "dn:: dWlkPXJvZ2FzYXdhcmEsb3U95Za25qWt6YOoLG89QWlyaXVz",
+                   "dn: cn=This is an extremely long relative distinguished " +
+                        "name that does not fit in two line of eighty " +
+                        "columns each.  It is intended to exercise the LDIF " +
+                        "line wrapping code.,o=airius",
                    "userpassword: {SHA}O3HSv1MusyL4kTjP+HKI5uxuNoM=",
                    "objectclass: top",
                    "objectclass: person",
@@ -109,7 +115,6 @@
   }
 
 
-
   /**
    * Test that going from SearchResultEntry to SearchResultEntryProtocolOp
    * and back again preserves the entry contents.
@@ -120,9 +125,8 @@
   @Test(dataProvider = "entries")
   public void testEntryTransformation(Entry from) throws Exception
   {
-    SearchResultEntry searchResultEntry = new SearchResultEntry(from);
     SearchResultEntryProtocolOp protocolOp =
-         new SearchResultEntryProtocolOp(searchResultEntry);
+         new SearchResultEntryProtocolOp(new SearchResultEntry(from));
     Entry to = protocolOp.toSearchResultEntry();
 
     // FIXME Issue 660: Need to provide Entry.equals(Object)
@@ -142,9 +146,8 @@
   public void testToLdif(Entry from) throws Exception
   {
     int wrapColumn = 80;
-    SearchResultEntry searchResultEntry = new SearchResultEntry(from);
     SearchResultEntryProtocolOp protocolOp =
-         new SearchResultEntryProtocolOp(searchResultEntry);
+         new SearchResultEntryProtocolOp(new SearchResultEntry(from));
     StringBuilder builder = new StringBuilder();
     protocolOp.toLDIF(builder, wrapColumn);
     Entry to = TestCaseUtils.entryFromLdifString(builder.toString());
@@ -157,9 +160,8 @@
   public void testToLdifWrapping(Entry from) throws Exception
   {
     int wrapColumn = 78;
-    SearchResultEntry searchResultEntry = new SearchResultEntry(from);
     SearchResultEntryProtocolOp protocolOp =
-         new SearchResultEntryProtocolOp(searchResultEntry);
+         new SearchResultEntryProtocolOp(new SearchResultEntry(from));
     StringBuilder builder = new StringBuilder();
     protocolOp.toLDIF(builder, wrapColumn);
 
@@ -179,4 +181,72 @@
     // FIXME Issue 660: Need to provide Entry.equals(Object)
 //    assertEquals(to, from);
   }
+
+  @Test(dataProvider = "entries")
+  public void testToString(Entry entry) throws Exception
+  {
+    SearchResultEntryProtocolOp protocolOp =
+         new SearchResultEntryProtocolOp(new SearchResultEntry(entry));
+    StringBuilder sb = new StringBuilder();
+    protocolOp.toString();
+    protocolOp.toString(sb, 1);
+  }
+
+  @Test(dataProvider = "entries")
+  public void testEncodeDecode(Entry entry) throws Exception
+  {
+    SearchResultEntryProtocolOp protocolOp =
+         new SearchResultEntryProtocolOp(new SearchResultEntry(entry));
+
+    // Encode to ASN1.
+    ASN1Element element = protocolOp.encode();
+
+    // Decode to a new protocol op.
+    SearchResultEntryProtocolOp decodedProtocolOp =
+         (SearchResultEntryProtocolOp)ProtocolOp.decode(element);
+
+    assertEquals(decodedProtocolOp.getDN(), protocolOp.getDN());
+    assertTrue(testEqual(decodedProtocolOp.getAttributes(),
+                         protocolOp.getAttributes()));
+  }
+
+  @Test (expectedExceptions = LDAPException.class)
+  public void testInvalidSequence() throws Exception
+  {
+    ProtocolOp.decode(new ASN1Integer(OP_TYPE_SEARCH_RESULT_ENTRY, 0));
+  }
+
+  @Test (dataProvider = "entries", expectedExceptions = LDAPException.class)
+  public void testTooManyElements(Entry entry) throws Exception
+  {
+    SearchResultEntryProtocolOp protocolOp =
+         new SearchResultEntryProtocolOp(new SearchResultEntry(entry));
+    ASN1Element element = protocolOp.encode();
+    ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
+    elements.add(new ASN1Boolean(true));
+    ProtocolOp.decode(new ASN1Sequence(OP_TYPE_SEARCH_RESULT_ENTRY, elements));
+  }
+
+  @Test (dataProvider = "entries", expectedExceptions = LDAPException.class)
+  public void testTooFewElements(Entry entry) throws Exception
+  {
+    SearchResultEntryProtocolOp protocolOp =
+         new SearchResultEntryProtocolOp(new SearchResultEntry(entry));
+    ASN1Element element = protocolOp.encode();
+    ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
+    elements.remove(0);
+    ProtocolOp.decode(new ASN1Sequence(OP_TYPE_SEARCH_RESULT_ENTRY, elements));
+  }
+
+  @Test (dataProvider = "entries", expectedExceptions = LDAPException.class)
+  public void testInvalidElement1(Entry entry) throws Exception
+  {
+    SearchResultEntryProtocolOp protocolOp =
+         new SearchResultEntryProtocolOp(new SearchResultEntry(entry));
+    ASN1Element element = protocolOp.encode();
+    ArrayList<ASN1Element> elements = ((ASN1Sequence)element).elements();
+    elements.set(1, new ASN1OctetString("cn"));
+    ProtocolOp.decode(new ASN1Sequence(OP_TYPE_SEARCH_RESULT_ENTRY, elements));
+  }
+
 }

--
Gitblit v1.10.0