예제 #1
0
  private static List<File> getRecursiveFileListNoSort(
      File root, boolean includeDirectories, boolean followSymlinkDirectories) {
    List<File> result = new ArrayList<File>();
    List<File> filesDirs = Arrays.asList(root.listFiles());

    for (File file : filesDirs) {
      boolean isDirectory = file.isDirectory();
      boolean isSymlinkDirectory = isDirectory && FileUtil.isSymlink(file);
      boolean includeFile = !isDirectory || includeDirectories;
      boolean followDirectory =
          (isSymlinkDirectory && followSymlinkDirectories) || (isDirectory && !isSymlinkDirectory);

      if (includeFile) {
        result.add(file);
      }

      if (followDirectory) {
        List<File> deeperList =
            getRecursiveFileListNoSort(file, includeDirectories, followSymlinkDirectories);
        result.addAll(deeperList);
      }
    }

    return result;
  }
  public static void assertFileEquals(
      File expectedFile, File actualFile, FileChange... allowedChanges)
      throws ArrayComparisonFailure, Exception {
    if (expectedFile == null && actualFile == null) {
      return;
    }

    assertNotNull(
        "Files are not equal: Actual file is " + actualFile + ", expected file is null.",
        expectedFile);
    assertNotNull(
        "Files are not equal: Expected file is " + expectedFile + ", actual file is null.",
        actualFile);

    Path root = Paths.get(actualFile.getAbsolutePath()).getRoot();
    FileVersionComparator fileVersionComparator = new FileVersionComparator(root.toFile(), "SHA1");

    if (!FileUtil.exists(expectedFile)) {
      fail("Files are not equal: Expected file " + expectedFile + " does not exist.");
    }

    if (!FileUtil.exists(actualFile)) {
      fail("Files are not equal: Actual file " + actualFile + " does not exist.");
    }

    if (FileUtil.isSymlink(actualFile) && FileUtil.isSymlink(expectedFile)) {
      return;
    }

    if (actualFile.isDirectory() != expectedFile.isDirectory()) {
      fail(
          "Files are not equal: Comparing a directory with a file (actual is dir = "
              + actualFile.isDirectory()
              + ", expected is dir = "
              + expectedFile.isDirectory()
              + ")");
    }

    if (actualFile.isDirectory() && expectedFile.isDirectory()) {
      return;
    }

    if (actualFile.length() != expectedFile.length()) {
      fail(
          "Files are not equal: Actual file size ("
              + actualFile
              + " = "
              + actualFile.length()
              + ") does not match expected file size ("
              + expectedFile
              + " = "
              + expectedFile.length()
              + ")");
    }

    byte[] expectedFileChecksum = TestFileUtil.createChecksum(expectedFile);
    byte[] actualFileChecksum = TestFileUtil.createChecksum(actualFile);

    assertArrayEquals(
        "Files are not equal: Actual file checksum ("
            + StringUtil.toHex(actualFileChecksum)
            + ") and expected file checksum ("
            + StringUtil.toHex(expectedFileChecksum)
            + ") do not match.",
        expectedFileChecksum,
        actualFileChecksum);

    FileProperties actualFileProperties =
        fileVersionComparator.captureFileProperties(actualFile, null, true);
    FileProperties expectedFileProperties =
        fileVersionComparator.captureFileProperties(expectedFile, null, true);

    FileVersionComparison fileVersionComparison =
        fileVersionComparator.compare(expectedFileProperties, actualFileProperties, true);

    List<FileChange> allowedChangesList = new ArrayList<FileChange>(Arrays.asList(allowedChanges));
    allowedChangesList.add(FileChange.CHANGED_PATH);

    if (!CollectionUtil.containsOnly(fileVersionComparison.getFileChanges(), allowedChangesList)) {
      fail(
          "Files are not equal: Actual file differs from expected file: "
              + fileVersionComparison.getFileChanges());
    }
  }