/**
   * 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 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.
   *
   * @param sourceFile The path to the file containing the source data set.
   * @param targetFile The path to the file containing the target data set.
   * @param normalDiffFile The path to the file containing the expected diff in "normal" form (at
   *     most one record per entry), or {@code null} if the diff is supposed to fail.
   * @throws Exception If an unexpected problem occurs.
   */
  @Test(dataProvider = "ignoreentriesdata")
  public void testVerifyIgnoreEntries(
      String sourceFile, String targetFile, String ignoreEntriesFile, String normalDiffFile)
      throws Exception {
    File outputFile = File.createTempFile("difftest", "ldif");
    outputFile.deleteOnExit();

    String[] args = {
      "-s", sourceFile,
      "-t", targetFile,
      "-e", ignoreEntriesFile,
      "-o", outputFile.getAbsolutePath(),
      "-O"
    };

    if (ignoreEntriesFile.endsWith("does-not-exist")) {
      // We expect this to fail, so just make sure that it does.
      assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 1);
      return;
    }

    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
    assertEquals(calcChecksum(outputFile), calcChecksum(normalDiffFile));

    outputFile.delete();
  }
  /**
   * 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 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.
   *
   * @param sourceFile The path to the file containing the source data set.
   * @param targetFile The path to the file containing the target data set.
   * @param normalDiffFile The path to the file containing the expected diff in "normal" form (at
   *     most one record per entry), or {@code null} if the diff is supposed to fail.
   * @param singleValueDiffFile The path to the file containing the expected 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,
      int resultCode)
      throws Exception {
    File outputFile = File.createTempFile("difftest", "ldif");
    outputFile.deleteOnExit();

    String[] args = {
      "-s", sourceFile, "-t", targetFile, "-o", outputFile.getAbsolutePath(), "-O", "-S"
    };
    String[] argsUseCompare = {
      "-s", sourceFile,
      "-t", targetFile,
      // No need to write to the outputFile
      "--useCompareResultCode"
    };

    if (singleValueDiffFile == null) {
      // We expect this to fail, so just make sure that it does.
      assertFalse(LDIFDiff.mainDiff(args, true, System.out, System.err) == 0);
      outputFile.delete();
      return;
    }

    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();
  }
  /**
   * Tests the LDIFDiff tool with an argument that will simply cause it to display usage
   * information.
   */
  @Test
  public void testUsage() {
    String[] args = {"--help"};
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);

    args = new String[] {"-H"};
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);

    args = new String[] {"-?"};
    assertEquals(LDIFDiff.mainDiff(args, true, System.out, System.err), 0);
  }
  /**
   * Tests the LDIFDiff tool by first identifying the differences between the source and the target
   * (using the single-value format) and then using the LDIFModify tool to apply the identified
   * changes to the source LDIF and verify that it matches the target.
   *
   * @param sourceFile The path to the file containing the source data set.
   * @param targetFile The path to the file containing the target data set.
   * @param normalDiffFile The path to the file containing the expected diff in "normal" form (at
   *     most one record per entry), or {@code null} if the diff is supposed to fail.
   * @param singleValueDiffFile The path to the file containing the expected 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,
      int resultCode)
      throws Exception {
    // If the command is expected to fail, or if there aren't any differences,
    // then bail out now.
    if (normalDiffFile == null || singleValueDiffFile.equals(noDiffsFile)) {
      return;
    }

    // Generate the diff file.
    File diffOutputFile = File.createTempFile("difftest", "ldif");
    diffOutputFile.deleteOnExit();

    String[] args = {
      "-s", sourceFile, "-t", targetFile, "-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.
    File newTargetFile = File.createTempFile("difftest", "newtarget.ldif");
    newTargetFile.deleteOnExit();

    DirectoryServer.getInstance();
    args =
        new String[] {
          "-c", DirectoryServer.getConfigFile(),
          "-s", sourceFile,
          "-m", diffOutputFile.getAbsolutePath(),
          "-t", newTargetFile.getAbsolutePath()
        };

    assertEquals(LDIFModify.ldifModifyMain(args, true, System.out, System.err), 0);

    // Use LDIFDiff again to verify that there are effectively no differences
    // between the original target and the new target.
    File newDiffFile = File.createTempFile("difftest", "newdiff.ldif");
    newDiffFile.deleteOnExit();

    args =
        new String[] {
          "-s", targetFile,
          "-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();
    newDiffFile.delete();
  }
  /** Tests the LDIFDiff tool with an invalid set of arguments. */
  @Test
  public void testInvalidArguments() {
    String[] args = {"--invalid"};

    assertFalse(LDIFDiff.mainDiff(args, true, System.out, System.err) == 0);
  }