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

Jean-Noel Rouvignac
28.20.2013 e596637fb0243ee191290b509c1f1251dc805e43
OPENDJ-778 (CR-1341) ldif-diff should exit with different values w.r.t files are identical or not

LDIFFDiff.java:
In mainDiff(), added new arguments "-r, --useCompareResultCode" to ldiff-diff + unified the return pathes for when differences might be found, removing some duplication.

LDIFDiffTestCase.java:
Added more asserts for the "--useCompareResultCode" case within the existing tests.
2 files modified
203 ■■■■ changed files
opends/src/server/org/opends/server/tools/LDIFDiff.java 54 ●●●● patch | view | raw | blame | history
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDIFDiffTestCase.java 149 ●●●● patch | view | raw | blame | history
opends/src/server/org/opends/server/tools/LDIFDiff.java
@@ -43,7 +43,21 @@
import org.opends.messages.Message;
import org.opends.server.core.DirectoryServer;
import org.opends.server.extensions.ConfigFileHandler;
import org.opends.server.types.*;
import org.opends.server.types.Attribute;
import org.opends.server.types.AttributeBuilder;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.AttributeValues;
import org.opends.server.types.DN;
import org.opends.server.types.DirectoryException;
import org.opends.server.types.Entry;
import org.opends.server.types.ExistingFileBehavior;
import org.opends.server.types.LDIFExportConfig;
import org.opends.server.types.LDIFImportConfig;
import org.opends.server.types.Modification;
import org.opends.server.types.ModificationType;
import org.opends.server.types.NullOutputStream;
import org.opends.server.types.ObjectClass;
import org.opends.server.util.LDIFReader;
import org.opends.server.util.LDIFWriter;
import org.opends.server.util.args.ArgumentException;
@@ -153,6 +167,7 @@
    BooleanArgument overwriteExisting;
    BooleanArgument showUsage;
    BooleanArgument useCompareResultCode;
    BooleanArgument singleValueChanges;
    BooleanArgument doCheckSchema;
    StringArgument  configClass;
@@ -238,11 +253,17 @@
                                      OPTION_LONG_HELP,
                                      INFO_DESCRIPTION_USAGE.get());
      argParser.addArgument(showUsage);
      useCompareResultCode =
          new BooleanArgument("usecompareresultcode", 'r',
              "useCompareResultCode", INFO_ENCPW_DESCRIPTION_USE_COMPARE_RESULT
                  .get());
      argParser.addArgument(useCompareResultCode);
      argParser.setUsageArgument(showUsage);
    }
    catch (ArgumentException ae)
    {
      Message message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
      err.println(message);
      return OPERATIONS_ERROR;
@@ -256,9 +277,7 @@
    }
    catch (ArgumentException ae)
    {
      Message message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
      err.println(message);
      err.println(argParser.getUsage());
      return CLIENT_SIDE_PARAM_ERROR;
@@ -550,15 +569,15 @@
    try
    {
      boolean differenceFound;
      // Check to see if either or both of the source and target maps are empty.
      if (sourceMap.isEmpty())
      {
        if (targetMap.isEmpty())
        {
          // They're both empty, so there are no differences.
          Message message = INFO_LDIFDIFF_NO_DIFFERENCES.get();
          writer.writeComment(message, 0);
          return SUCCESS;
          differenceFound = false;
        }
        else
        {
@@ -568,7 +587,7 @@
          {
            writeAdd(writer, targetMap.get(targetIterator.next()));
          }
          return SUCCESS;
          differenceFound = true;
        }
      }
      else if (targetMap.isEmpty())
@@ -579,10 +598,11 @@
        {
          writeDelete(writer, sourceMap.get(sourceIterator.next()));
        }
        return SUCCESS;
        differenceFound = true;
      }
      else
      {
        differenceFound = false;
        // Iterate through all the entries in the source and target maps and
        // identify the differences.
        Iterator<DN> sourceIterator  = sourceMap.keySet().iterator();
@@ -591,7 +611,6 @@
        DN           targetDN        = targetIterator.next();
        Entry        sourceEntry     = sourceMap.get(sourceDN);
        Entry        targetEntry     = targetMap.get(targetDN);
        boolean      differenceFound = false;
        while (true)
        {
@@ -705,13 +724,16 @@
            }
          }
        }
      }
        if (! differenceFound)
        {
          Message message = INFO_LDIFDIFF_NO_DIFFERENCES.get();
          writer.writeComment(message, 0);
        }
      if (!differenceFound)
      {
        Message message = INFO_LDIFDIFF_NO_DIFFERENCES.get();
        writer.writeComment(message, 0);
      }
      if (useCompareResultCode.isPresent())
      {
        return !differenceFound ? COMPARE_TRUE : COMPARE_FALSE;
      }
    }
    catch (IOException e)
opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDIFDiffTestCase.java
@@ -23,6 +23,7 @@
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2013 ForgeRock AS.
 */
package org.opends.server.tools;
@@ -32,12 +33,13 @@
import java.io.File;
import java.io.FileReader;
import org.opends.server.TestCaseUtils;
import org.opends.server.core.DirectoryServer;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.opends.server.TestCaseUtils;
import org.opends.server.core.DirectoryServer;
import static org.opends.server.protocols.ldap.LDAPResultCode.*;
import static org.testng.Assert.*;
@@ -153,99 +155,116 @@
      // Both files are empty.
      new Object[] { ldifRoot + "source-empty.ldif",
                     ldifRoot + "target-empty.ldif",
                     noDiffsFile, noDiffsFile },
                     noDiffsFile, noDiffsFile,
                     COMPARE_TRUE },
      // Both files are the single-entry source.
      new Object[] { ldifRoot + "source-singleentry.ldif",
                     ldifRoot + "source-singleentry.ldif",
                     noDiffsFile, noDiffsFile },
                     noDiffsFile, noDiffsFile,
                     COMPARE_TRUE },
      // Both files are the single-entry target.
      new Object[] { ldifRoot + "target-singleentry.ldif",
                     ldifRoot + "target-singleentry.ldif",
                     noDiffsFile, noDiffsFile },
                     noDiffsFile, noDiffsFile,
                     COMPARE_TRUE },
      // Both files are the multiple-entry source.
      new Object[] { ldifRoot + "source-multipleentries.ldif",
                     ldifRoot + "source-multipleentries.ldif",
                     noDiffsFile, noDiffsFile },
                     noDiffsFile, noDiffsFile,
                     COMPARE_TRUE },
      // Both files are the multiple-entry target.
      new Object[] { ldifRoot + "target-multipleentries.ldif",
                     ldifRoot + "target-multipleentries.ldif",
                     noDiffsFile, noDiffsFile },
                     noDiffsFile, noDiffsFile,
                     COMPARE_TRUE },
      // The source is empty but the target has a single entry.
      new Object[] { ldifRoot + "source-empty.ldif",
                     ldifRoot + "target-singleentry.ldif",
                     ldifRoot + "diff-emptytosingle.ldif",
                     ldifRoot + "diff-emptytosingle.ldif" },
                     ldifRoot + "diff-emptytosingle.ldif",
                     COMPARE_FALSE },
      // The source has a single entry but the target is empty.
      new Object[] { ldifRoot + "source-singleentry.ldif",
                     ldifRoot + "target-empty.ldif",
                     ldifRoot + "diff-singletoempty.ldif",
                     ldifRoot + "diff-singletoempty.ldif" },
                     ldifRoot + "diff-singletoempty.ldif",
                     COMPARE_FALSE },
      // Make a change to only a single entry in the source->target direction.
      new Object[] { ldifRoot + "source-singleentry.ldif",
                     ldifRoot + "target-singleentry.ldif",
                     ldifRoot + "diff-singleentry.ldif",
                     ldifRoot + "diff-singleentry.ldif" },
                     ldifRoot + "diff-singleentry.ldif",
                     COMPARE_FALSE },
      // Make a change to only a single entry in the target->source direction.
      new Object[] { ldifRoot + "target-singleentry.ldif",
                     ldifRoot + "source-singleentry.ldif",
                     ldifRoot + "diff-singleentry-reverse.ldif",
                     ldifRoot + "diff-singleentry-reverse.ldif" },
                     ldifRoot + "diff-singleentry-reverse.ldif",
                     COMPARE_FALSE },
      // Make changes to multiple entries in the source->target direction.
      new Object[] { ldifRoot + "source-multipleentries.ldif",
                     ldifRoot + "target-multipleentries.ldif",
                     ldifRoot + "diff-multipleentries.ldif",
                     ldifRoot + "diff-multipleentries-singlevalue.ldif" },
                     ldifRoot + "diff-multipleentries-singlevalue.ldif",
                     COMPARE_FALSE },
      // Make changes to multiple entries in the target->source direction.
      new Object[] { ldifRoot + "target-multipleentries.ldif",
                     ldifRoot + "source-multipleentries.ldif",
                     ldifRoot + "diff-multipleentries-reverse.ldif",
                     ldifRoot +
                          "diff-multipleentries-reverse-singlevalue.ldif" },
                          "diff-multipleentries-reverse-singlevalue.ldif",
                     COMPARE_FALSE },
      // Go from one entry to multiple in the source->target direction.
      new Object[] { ldifRoot + "source-singleentry.ldif",
                     ldifRoot + "target-multipleentries.ldif",
                     ldifRoot + "diff-singletomultiple.ldif",
                     ldifRoot + "diff-singletomultiple-singlevalue.ldif" },
                     ldifRoot + "diff-singletomultiple-singlevalue.ldif",
                     COMPARE_FALSE },
      // Go from one entry to multiple in the target->source direction.
      new Object[] { ldifRoot + "target-singleentry.ldif",
                     ldifRoot + "source-multipleentries.ldif",
                     ldifRoot + "diff-singletomultiple-reverse.ldif",
                     ldifRoot + "diff-singletomultiple-reverse.ldif" },
                     ldifRoot + "diff-singletomultiple-reverse.ldif",
                     COMPARE_FALSE },
      // Go from multiple entries to one in the source->target direction.
      new Object[] { ldifRoot + "source-multipleentries.ldif",
                     ldifRoot + "target-singleentry.ldif",
                     ldifRoot + "diff-multipletosingle.ldif",
                     ldifRoot + "diff-multipletosingle.ldif" },
                     ldifRoot + "diff-multipletosingle.ldif",
                     COMPARE_FALSE },
      // Go from multiple entries to one in the target->source direction.
      new Object[] { ldifRoot + "target-multipleentries.ldif",
                     ldifRoot + "source-singleentry.ldif",
                     ldifRoot + "diff-multipletosingle-reverse.ldif",
                     ldifRoot +
                          "diff-multipletosingle-reverse-singlevalue.ldif" },
                          "diff-multipletosingle-reverse-singlevalue.ldif",
                     COMPARE_FALSE },
      // The source file doesn't exist.
      new Object[] { ldifRoot + "source-notfound.ldif",
                     ldifRoot + "target-singleentry.ldif",
                     null, null },
                     null, null,
                     COMPARE_FALSE },
      // The target file doesn't exist.
      new Object[] { ldifRoot + "source-singleentry.ldif",
                     ldifRoot + "target-notfound.ldif",
                     null, null }
                     null, null,
                     COMPARE_FALSE }
    };
  }
@@ -253,7 +272,7 @@
  /**
   * Tests the LDIFDiff tool with the provided information to ensure that the
   * normal mode of operation works as expected.  This is a bit tricky because
   * the attributes and values will be written in an indeterminite order, so we
   * the attributes and values will be written in an indeterminate order, so we
   * can't just use string equality.  We'll have to use a crude checksum
   * mechanism to test whether they are equal.  Combined with other methods in
   * this class, this should be good enough.
@@ -270,14 +289,15 @@
   *                              diff in "single-value" form, where each
   *                              attribute-level change results in a separate
   *                              entry per attribute value.
   * @param resultCode            The result code that should be returned with
   *                              --useCompareResultCode flag
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "testdata")
  public void testVerifyNormal(String sourceFile, String targetFile,
                               String normalDiffFile,
                               String singleValueDiffFile)
         throws Exception
      String normalDiffFile, String singleValueDiffFile, int resultCode)
      throws Exception
  {
    File outputFile = File.createTempFile("difftest", "ldif");
    outputFile.deleteOnExit();
@@ -289,6 +309,13 @@
      "-o", outputFile.getAbsolutePath(),
      "-O"
    };
    String[] argsUseCompare =
    {
      "-s", sourceFile,
      "-t", targetFile,
      // No need to write to the outputFile
      "--useCompareResultCode"
    };
    if (normalDiffFile == null)
    {
@@ -300,6 +327,9 @@
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
    assertEquals(calcChecksum(outputFile), calcChecksum(normalDiffFile));
    assertEquals(LDIFDiff
        .mainDiff(argsUseCompare, true, System.out, System.err), resultCode);
    outputFile.delete();
  }
@@ -310,7 +340,7 @@
   * Tests the LDIFDiff tool with the provided information to ensure that the
   * single value changes mode of operation works as expected.  This is a bit
   * tricky because the attributes and values will be written in an
   * indeterminite order, so we can't just use string equality.  We'll have to
   * indeterminate order, so we can't just use string equality.  We'll have to
   * use a crude checksum mechanism to test whether they are equal.  Combined
   * with other methods in this class, this should be good enough.
   *
@@ -326,14 +356,15 @@
   *                              diff in "single-value" form, where each
   *                              attribute-level change results in a separate
   *                              entry per attribute value.
   * @param resultCode            The result code that should be returned with
   *                              --useCompareResultCode flag
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "testdata")
  public void testVerifySingleValue(String sourceFile, String targetFile,
                                    String normalDiffFile,
                                    String singleValueDiffFile)
         throws Exception
      String normalDiffFile, String singleValueDiffFile, int resultCode)
      throws Exception
  {
    File outputFile = File.createTempFile("difftest", "ldif");
    outputFile.deleteOnExit();
@@ -346,6 +377,13 @@
      "-O",
      "-S"
    };
    String[] argsUseCompare =
    {
      "-s", sourceFile,
      "-t", targetFile,
      // No need to write to the outputFile
      "--useCompareResultCode"
    };
    if (singleValueDiffFile == null)
    {
@@ -357,6 +395,9 @@
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
    assertEquals(calcChecksum(outputFile), calcChecksum(singleValueDiffFile));
    assertEquals(LDIFDiff
        .mainDiff(argsUseCompare, true, System.out, System.err), resultCode);
    outputFile.delete();
  }
@@ -380,14 +421,15 @@
   *                              diff in "single-value" form, where each
   *                              attribute-level change results in a separate
   *                              entry per attribute value.
   * @param resultCode            The result code that should be returned with
   *                              --useCompareResultCode flag
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "testdata")
  public void testReconstructNormal(String sourceFile, String targetFile,
                                    String normalDiffFile,
                                    String singleValueDiffFile)
         throws Exception
      String normalDiffFile, String singleValueDiffFile, int resultCode)
      throws Exception
  {
    // If the command is expected to fail, or if there aren't any differences,
    // then bail out now.
@@ -407,8 +449,17 @@
      "-t", targetFile,
      "-o", diffOutputFile.getAbsolutePath()
    };
    String[] argsUseCompare =
    {
      "-s", sourceFile,
      "-t", targetFile,
      // No need to write to the outputFile
      "--useCompareResultCode"
    };
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
    assertEquals(LDIFDiff
        .mainDiff(argsUseCompare, true, System.out, System.err), resultCode);
    // Use LDIFModify to generate a new target file.
@@ -439,9 +490,18 @@
      "-t", newTargetFile.getAbsolutePath(),
      "-o", newDiffFile.getAbsolutePath()
    };
    argsUseCompare = new String[]
    {
      "-s", targetFile,
      "-t", newTargetFile.getAbsolutePath(),
      // No need to write to the outputFile
      "--useCompareResultCode"
    };
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
    assertEquals(calcChecksum(newDiffFile), calcChecksum(noDiffsFile));
    assertEquals(LDIFDiff
        .mainDiff(argsUseCompare, true, System.out, System.err), COMPARE_TRUE);
    diffOutputFile.delete();
    newTargetFile.delete();
@@ -468,14 +528,15 @@
   *                              diff in "single-value" form, where each
   *                              attribute-level change results in a separate
   *                              entry per attribute value.
   * @param resultCode            The result code that should be returned with
   *                              --useCompareResultCode flag
   *
   * @throws  Exception  If an unexpected problem occurs.
   */
  @Test(dataProvider = "testdata")
  public void testReconstructSingleValue(String sourceFile, String targetFile,
                                         String normalDiffFile,
                                         String singleValueDiffFile)
         throws Exception
      String normalDiffFile, String singleValueDiffFile, int resultCode)
      throws Exception
  {
    // If the command is expected to fail, or if there aren't any differences,
    // then bail out now.
@@ -496,8 +557,17 @@
      "-o", diffOutputFile.getAbsolutePath(),
      "-S"
    };
    String[] argsUseCompare =
    {
      "-s", sourceFile,
      "-t", targetFile,
      // No need to write to the outputFile
      "--useCompareResultCode"
    };
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
    assertEquals(LDIFDiff
        .mainDiff(argsUseCompare, true, System.out, System.err), resultCode);
    // Use LDIFModify to generate a new target file.
@@ -528,9 +598,18 @@
      "-t", newTargetFile.getAbsolutePath(),
      "-o", newDiffFile.getAbsolutePath()
    };
    argsUseCompare = new String[]
    {
      "-s", targetFile,
      "-t", newTargetFile.getAbsolutePath(),
      // No need to write to the outputFile
      "--useCompareResultCode"
    };
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
    assertEquals(calcChecksum(newDiffFile), calcChecksum(noDiffsFile));
    assertEquals(LDIFDiff
        .mainDiff(argsUseCompare, true, System.out, System.err), COMPARE_TRUE);
    diffOutputFile.delete();
    newTargetFile.delete();
@@ -578,7 +657,7 @@
  /**
   * Tests the LDIFDiff tool with the provided information to ensure that the
   * normal mode of operation works as expected.  This is a bit tricky because
   * the attributes and values will be written in an indeterminite order, so we
   * the attributes and values will be written in an indeterminate order, so we
   * can't just use string equality.  We'll have to use a crude checksum
   * mechanism to test whether they are equal.  Combined with other methods in
   * this class, this should be good enough.
@@ -665,7 +744,7 @@
  /**
   * Tests the LDIFDiff tool with the provided information to ensure that the
   * normal mode of operation works as expected.  This is a bit tricky because
   * the attributes and values will be written in an indeterminite order, so we
   * the attributes and values will be written in an indeterminate order, so we
   * can't just use string equality.  We'll have to use a crude checksum
   * mechanism to test whether they are equal.  Combined with other methods in
   * this class, this should be good enough.