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

davidely
14.37.2006 f5fd582f02ef755f438a7e73218a8cf94c2078ab
Added convenience methods for converting LDIF StringS into EntryS.
2 files modified
183 ■■■■■ changed files
opends/src/server/org/opends/server/types/LDIFImportConfig.java 46 ●●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java 137 ●●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/types/LDIFImportConfig.java
@@ -38,6 +38,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
@@ -250,7 +251,52 @@
    includeAttributes      = new HashSet<AttributeType>();
  }
  /**
   * Creates a new LDIF import configuration that will read from the
   * provided reader.
   *
   * @param  ldifInputReader  The input stream from which to read the
   *                          LDIF data.
   */
  public LDIFImportConfig(Reader ldifInputReader)
  {
    assert debugConstructor(CLASS_NAME,
                            String.valueOf(ldifInputStream));
    ldifInputStream        = null;
    bufferSize             = DEFAULT_BUFFER_SIZE;
    ldifFiles              = null;
    ldifFileIterator       = null;
    excludeBranches        = new ArrayList<DN>();
    includeBranches        = new ArrayList<DN>();
    excludeFilters         = new ArrayList<SearchFilter>();
    includeFilters         = new ArrayList<SearchFilter>();
    appendToExistingData   = false;
    replaceExistingEntries = false;
    includeObjectClasses   = true;
    invokeImportPlugins    = false;
    isCompressed           = false;
    isEncrypted            = false;
    reader                 = getBufferedReader(ldifInputReader);
    rejectWriter           = null;
    excludeAttributes      = new HashSet<AttributeType>();
    includeAttributes      = new HashSet<AttributeType>();
  }
  /**
   * Wrap reader in a BufferedReader if necessary.
   *
   * @param reader the reader to buffer
   * @return reader as a BufferedReader
   */
  private BufferedReader getBufferedReader(Reader reader) {
    if (reader instanceof BufferedReader) {
      return (BufferedReader)reader;
    } else {
      return new BufferedReader(reader);
    }
  }
  /**
   * Creates a new LDIF import configuration that will generate
opends/tests/unit-tests-testng/src/server/org/opends/server/TestCaseUtils.java
@@ -26,12 +26,19 @@
 */
package org.opends.server;
import org.opends.server.types.Entry;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.util.LDIFReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.List;
import java.util.ArrayList;
import org.opends.server.config.ConfigException;
import org.opends.server.config.ConfigFileHandler;
@@ -274,4 +281,134 @@
  private TestCaseUtils() {
    // No implementation.
  }
  ////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////
  //
  // Various methods for converting LDIF Strings to Entries
  //
  ////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////
  /**
   * Returns a modifiable List of entries parsed from the provided LDIF.
   * It's best to call this after the server has been initialized so
   * that schema checking happens.
   * <p>
   * Also take a look at the makeLdif method below since this makes
   * expressing LDIF a little bit cleaner.
   *
   * @param ldif of the entries to parse.
   * @return a List of EntryS parsed from the ldif string.
   * @see #makeLdif
   */
  public static List<Entry> entriesFromLdifString(String ldif) throws Exception {
    LDIFImportConfig ldifImportConfig = new LDIFImportConfig(new StringReader(ldif));
    LDIFReader reader = new LDIFReader(ldifImportConfig);
    List<Entry> entries = new ArrayList<Entry>();
    Entry entry = null;
    while ((entry = reader.readEntry()) != null) {
      entries.add(entry);
    }
    return entries;
  }
  /**
   * This is used as a convenience when and LDIF string only includes a single
   * entry. It's best to call this after the server has been initialized so
   * that schema checking happens.
   * <p>
   * Also take a look at the makeLdif method below since this makes
   * expressing LDIF a little bit cleaner.
   *
   * @return the first Entry parsed from the ldif String
   * @see #makeLdif
   */
  public static Entry entryFromLdifString(String ldif) throws Exception {
    return entriesFromLdifString(ldif).get(0);
  }
  /**
   * This method provides the minor convenience of not having to specify the
   * newline character at the end of every line of LDIF in test code.
   * This is an admittedly small advantage, but it does make things a little
   * easier and less error prone.  For example, this
   *
     <code>
       private static final String JOHN_SMITH_LDIF = TestCaseUtils.makeLdif(
          "dn: cn=John Smith,dc=example,dc=com",
          "objectclass: inetorgperson",
          "cn: John Smith",
          "sn: Smith",
          "givenname: John");
     </code>
   is a <bold>little</bold> easier to work with than
     <code>
       private static final String JOHN_SMITH_LDIF =
          "dn: cn=John Smith,dc=example,dc=com\n" +
          "objectclass: inetorgperson\n" +
          "cn: John Smith\n" +
          "sn: Smith\n" +
          "givenname: John\n";
     </code>
   *
   * @return the concatenation of each line followed by a newline character
   */
  public static String makeLdif(String... lines) {
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < lines.length; i++) {
      buffer.append(lines[i]).append("\n");
    }
    return buffer.toString();
  }
  /**
   * This is a convience method that constructs an Entry from the specified
   * lines of LDIF.  Here's a sample usage
   *
   <code>
   Entry john = TestCaseUtils.makeEntry(
      "dn: cn=John Smith,dc=example,dc=com",
      "objectclass: inetorgperson",
      "cn: John Smith",
      "sn: Smith",
      "givenname: John");
   </code>
   * @see #makeLdif
   */
  public static Entry makeEntry(String... lines) throws Exception {
     return entryFromLdifString(makeLdif(lines));
  }
  /**
   * This is a convience method that constructs an List of EntryS from the
   * specified lines of LDIF.  Here's a sample usage
   *
   <code>
   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");
   </code>
   * @see #makeLdif
   */
  public static List<Entry> makeEntries(String... lines) throws Exception {
     return entriesFromLdifString(makeLdif(lines));
  }
}