From 379b85d1ec1f3145de3959d559599bbd6f5295d9 Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Fri, 20 Dec 2013 18:49:54 +0000
Subject: [PATCH]
---
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java | 65 +++++++++++++
opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldif/LDIFTestCase.java | 72 ++++++++++++++
opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java | 57 +++++++++++
opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java | 64 ------------
4 files changed, 194 insertions(+), 64 deletions(-)
diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
index a8f1698..054cd43 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldap/Entries.java
@@ -28,15 +28,19 @@
package org.forgerock.opendj.ldap;
import static org.forgerock.opendj.ldap.AttributeDescription.objectClass;
+
import static com.forgerock.opendj.ldap.CoreMessages.*;
+
import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.List;
import java.util.Set;
import org.forgerock.i18n.LocalizableMessage;
@@ -48,6 +52,7 @@
import org.forgerock.opendj.ldap.schema.Schema;
import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy;
import org.forgerock.opendj.ldap.schema.UnknownSchemaElementException;
+import org.forgerock.opendj.ldif.LDIF;
import org.forgerock.util.Reject;
import com.forgerock.opendj.util.Iterables;
@@ -581,6 +586,58 @@
}
/**
+ * Builds an entry from the provided lines of LDIF.
+ * <p>
+ * 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 ldifLines
+ * LDIF lines that contains entry definition.
+ * @return an entry
+ * @throws IOException
+ * If an error occurs.
+ */
+ public static Entry makeEntry(String... ldifLines) throws IOException {
+ return LDIF.makeEntry(ldifLines);
+ }
+
+ /**
+ * Builds a list of entries from the provided lines of LDIF.
+ * <p>
+ * 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.
+ * Entries are separated by an empty string: {@code ""}.
+ * @return a list of entries
+ * @throws IOException
+ * If an error occurs.
+ */
+ public static List<Entry> makeEntries(String... ldifLines) throws IOException {
+ return LDIF.makeEntries(ldifLines);
+ }
+
+ /**
* Returns the structural object class associated with the provided entry,
* or {@code null} if none was found. If the entry contains multiple
* structural object classes then the first will be returned. This method
diff --git a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
index 69ec6b5..d053bef 100644
--- a/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
+++ b/opendj-sdk/opendj-core/src/main/java/org/forgerock/opendj/ldif/LDIF.java
@@ -70,6 +70,8 @@
import org.forgerock.opendj.ldap.schema.AttributeUsage;
import org.forgerock.opendj.ldap.schema.Schema;
+import com.forgerock.opendj.util.StaticUtils;
+
/**
* This class contains common utility methods for creating and manipulating
* readers and writers.
@@ -241,6 +243,69 @@
}
/**
+ * Builds an entry from the provided lines of LDIF.
+ * <p>
+ * 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 ldifLines
+ * LDIF lines that contains entry definition.
+ * @return an entry, or {@code null} if no ldif line is provided
+ * @throws IOException
+ * If an error occurs.
+ */
+ public static Entry makeEntry(String... ldifLines) throws IOException {
+ List<Entry> entries = makeEntries(ldifLines);
+ return entries.isEmpty() ? null : entries.get(0);
+ }
+
+ /**
+ * Builds a list of entries from the provided lines of LDIF.
+ * <p>
+ * 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.
+ * Entries are separated by an empty string: {@code ""}.
+ * @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;
+ }
+
+ /**
* Returns an entry reader over the provided entry collection.
*
* @param entries
diff --git a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
index 106fa5b..3292513 100644
--- a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
+++ b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldap/TestCaseUtils.java
@@ -37,16 +37,13 @@
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;
/**
@@ -244,65 +241,4 @@
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;
- }
-
}
diff --git a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldif/LDIFTestCase.java b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldif/LDIFTestCase.java
index 95a1e7f..cd7cb01 100644
--- a/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldif/LDIFTestCase.java
+++ b/opendj-sdk/opendj-core/src/test/java/org/forgerock/opendj/ldif/LDIFTestCase.java
@@ -2744,4 +2744,76 @@
public final void testLdifPatchDoesntAllowNull() throws Exception {
LDIF.patch(null, null);
}
+
+ @Test
+ public void testMakeEntry() throws Exception {
+ // @formatter:off
+ final Entry entry = LDIF.makeEntry(
+ "dn: uid=user.1,ou=People,dc=example,dc=com",
+ "objectClass: top",
+ "objectClass: person",
+ "objectClass: organizationalperson",
+ "objectClass: inetorgperson",
+ "givenName: Eniko",
+ "sn: Atpco",
+ "cn: Eniko Atpco",
+ "uid: user.1"
+ );
+ // @formatter:on
+ assertThat(entry.getName().toString()).isEqualTo("uid=user.1,ou=People,dc=example,dc=com");
+ assertThat(entry.getAttribute("objectClass").firstValueAsString()).isEqualTo("top");
+ assertThat(entry.getAttribute("uid").firstValueAsString()).isEqualTo("user.1");
+ assertThat(entry.getAttribute("givenName").firstValueAsString()).isEqualTo("Eniko");
+ assertThat(entry.getAttribute("sn").firstValueAsString()).isEqualTo("Atpco");
+ }
+
+ @Test
+ public void testMakeEntries() throws Exception {
+ // @formatter:off
+ final List<Entry> entries = LDIF.makeEntries(
+ "dn: uid=user.1,ou=People,dc=example,dc=com",
+ "objectClass: top",
+ "objectClass: person",
+ "objectClass: organizationalperson",
+ "objectClass: inetorgperson",
+ "givenName: Eniko",
+ "sn: Atpco",
+ "uid: user.1",
+ "",
+ "dn: uid=user.2,ou=People,dc=example,dc=com",
+ "objectClass: top",
+ "objectClass: person",
+ "objectClass: organizationalperson",
+ "objectClass: inetorgperson",
+ "givenName: Aaaron",
+ "sn: Atp",
+ "uid: user.2"
+ );
+ // @formatter:on
+ assertThat(entries).hasSize(2);
+ assertThat(entries.get(0).getName().toString()).isEqualTo("uid=user.1,ou=People,dc=example,dc=com");
+ assertThat(entries.get(1).getName().toString()).isEqualTo("uid=user.2,ou=People,dc=example,dc=com");
+ }
+
+ @Test
+ public void testMakeEntryEmpty() throws Exception {
+ final Entry entry = LDIF.makeEntry();
+ assertThat(entry).isNull();
+ }
+
+ @Test(expectedExceptions = DecodeException.class)
+ public void testMakeEntryBadLDif() throws Exception {
+ LDIF.makeEntry("dummy: uid=user.1,ou=People,dc=example,dc=com");
+ }
+
+ @Test
+ public void testMakeEntriesEmpty() throws Exception {
+ final List<Entry> entries = LDIF.makeEntries();
+ assertThat(entries).hasSize(0);
+ }
+
+ @Test(expectedExceptions = DecodeException.class)
+ public void testMakeEntriesBadLDif() throws Exception {
+ LDIF.makeEntries("dummy: uid=user.1,ou=People,dc=example,dc=com");
+ }
}
--
Gitblit v1.10.0