From 817f2a5cb7c44e3122855fe678d0f757667ddbe9 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Tue, 17 Dec 2013 13:59:53 +0000
Subject: [PATCH] Add TestCaseUtils#makeEntries and TestCaseUtils#makeEntry methods to ease creation of entries from LDIF lines in test cases.

---
 opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java |   66 ++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java b/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
index 6065f08..4c39dce 100644
--- a/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
+++ b/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
@@ -31,20 +31,22 @@
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
-
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.SocketAddress;
+import java.util.ArrayList;
 import java.util.List;
 
+import org.forgerock.opendj.ldif.LDIFEntryReader;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.mockito.stubbing.OngoingStubbing;
 
 import com.forgerock.opendj.util.CompletedFutureResult;
+import com.forgerock.opendj.util.StaticUtils;
 import com.forgerock.opendj.util.TimeSource;
 
 /**
@@ -241,4 +243,66 @@
         }
         return mock;
     }
+
+    /**
+     * Builds an entry from the provided lines of LDIF.
+     * <p>
+     * Here's a sample usage:
+     * <pre>
+     * Entry john = makeEntry(
+     *   "dn: cn=John Smith,dc=example,dc=com",
+     *   "objectclass: inetorgperson",
+     *   "cn: John Smith",
+     *   "sn: Smith",
+     *   "givenname: John");
+     * </pre>
+     *
+     * @param lines
+     *          LDIF lines that contains entry definition.
+     * @return an entry
+     * @throws IOException
+     *          If an error occurs.
+     */
+    public static Entry makeEntry(String... lines) throws IOException {
+       return makeEntries(lines).get(0);
+    }
+
+    /**
+     * Builds a list of entries from the provided lines of LDIF.
+     * <p>
+     * Here's a sample usage
+     * <pre>
+     * List<Entry> smiths = TestCaseUtils.makeEntries(
+     *   "dn: cn=John Smith,dc=example,dc=com",
+     *   "objectclass: inetorgperson",
+     *   "cn: John Smith",
+     *   "sn: Smith",
+     *   "givenname: John",
+     *   "",
+     *   "dn: cn=Jane Smith,dc=example,dc=com",
+     *   "objectclass: inetorgperson",
+     *   "cn: Jane Smith",
+     *   "sn: Smith",
+     *   "givenname: Jane");
+     * </pre>
+     * @param ldifLines
+     *          LDIF lines that contains entries definition.
+     * @return a list of entries
+     * @throws IOException
+     *          If an error occurs.
+     */
+    public static List<Entry> makeEntries(String... ldifLines) throws IOException {
+        List<Entry> entries = new ArrayList<Entry>();
+        LDIFEntryReader reader = null;
+        try {
+            reader = new LDIFEntryReader(ldifLines);
+            while (reader.hasNext()) {
+                entries.add(reader.readEntry());
+            }
+        } finally {
+            StaticUtils.closeSilently(reader);
+        }
+        return entries;
+    }
+
 }

--
Gitblit v1.10.0