Java Code Examples for com.google.common.io.Files#equal()

The following examples show how to use com.google.common.io.Files#equal() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FoldersComparison.java    From butterfly with MIT License 6 votes vote down vote up
private static void assertEqualFolderContent(File baselineApplication, File expected, File actual, boolean xmlSemanticComparison, TreeSet<String> different) throws IOException {
    for (File expectedFile : expected.listFiles()) {
        File actualFile = new File(actual, expectedFile.getName());
        if (expectedFile.isDirectory() && actualFile.exists() && actualFile.isDirectory()) {
            assertEqualFolderContent(baselineApplication, expectedFile, actualFile, xmlSemanticComparison, different);
        } else if (expectedFile.isFile() && actualFile.exists() && actualFile.isFile()) {
            boolean equal;
            if (xmlSemanticComparison && expectedFile.getName().endsWith(".xml")) {
                equal = xmlEqual(expectedFile, actualFile);
            } else {
                equal = Files.equal(expectedFile, actualFile);
            }
            if(!equal) {
                different.add(getRelativePath(baselineApplication, expectedFile));
            }
        }
    }
}
 
Example 2
Source File: BackupManager.java    From presto with Apache License 2.0 5 votes vote down vote up
private static boolean filesEqual(File file1, File file2)
{
    try {
        return Files.equal(file1, file2);
    }
    catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 3
Source File: TransformationUtilityTestHelper.java    From butterfly with MIT License 5 votes vote down vote up
/**
 * Return true if the file is not identical to how it was originally
 *
 * @param relativeFilePath relative path to file to be evaluated
 * @return true if the file is not identical to how it was originally
 * @throws IOException
 */
protected boolean fileHasChanged(String relativeFilePath) throws IOException {
    File originalFile = new File(appFolder, relativeFilePath);
    Assert.assertTrue(originalFile.exists());

    File transformedFile = new File(transformedAppFolder, relativeFilePath);
    Assert.assertTrue(transformedFile.exists());

    return !Files.equal(originalFile, transformedFile);
}
 
Example 4
Source File: TestSparkeyWriter.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
public static void writeHashAndCompare(final SparkeyWriter writer2) throws IOException {
  final SingleThreadedSparkeyWriter writer = (SingleThreadedSparkeyWriter) writer2;

  final File indexFile = writer.indexFile;
  final File memFile = Sparkey.setEnding(indexFile, ".mem.spi");

  try {
    writer.setConstructionMethod(SparkeyWriter.ConstructionMethod.IN_MEMORY);
    writer.writeHash();
    indexFile.renameTo(memFile);
    final IndexHeader memHeader = IndexHeader.read(memFile);

    writer.setHashSeed(memHeader.getHashSeed());

    writer.setConstructionMethod(SparkeyWriter.ConstructionMethod.SORTING);
    writer.writeHash();
    final IndexHeader sortHeader = IndexHeader.read(indexFile);

    if (!Files.equal(indexFile, memFile)) {
      throw new RuntimeException(
          "Files are not equal: " + indexFile + ", " + memFile + "\n" +
          sortHeader.toString() + "\n" + memHeader.toString());
    }
  } finally {
    writer.setConstructionMethod(SparkeyWriter.ConstructionMethod.AUTO);
    memFile.delete();
  }
}
 
Example 5
Source File: IOTestBase.java    From mojito with Apache License 2.0 4 votes vote down vote up
/**
 * Checks that the two directories have the same structure and that their
 * files content are the same
 *
 * @param dir1
 * @param dir2
 * @throws DifferentDirectoryContentException if the two directories are
 *                                            different
 */
protected void checkDirectoriesContainSameContent(File dir1, File dir2) throws DifferentDirectoryContentException {

    try {
        Collection<File> listFiles1 = FileUtils.listFiles(dir1, null, true);
        Collection<File> listFiles2 = FileUtils.listFiles(dir2, null, true);

        // Get all the files inside the source directory, recursively
        for (File file1 : listFiles1) {

            Path relativePath1 = dir1.toPath().relativize(file1.toPath());

            logger.debug("Test file: {}", relativePath1);
            File file2 = new File(dir2, relativePath1.toString());

            // Check if the file exists in the other directory
            if (!file2.exists()) {
                throw new DifferentDirectoryContentException("File: " + file2.toString() + " doesn't exist");
            }

            // If that's a file, check that the both files have the same content
            if (file2.isFile() && !Files.equal(file1, file2)) {
                throw new DifferentDirectoryContentException("File: " + file1.toString() +
                        " and file: " + file2.toString() + " have different content");
            }
        }

        if (listFiles1.size() < listFiles2.size()) {
            List<Path> listPath1 = listFiles1.stream().map(file -> dir1.toPath().relativize(file.toPath())).collect(toList());
            String extraFiles = listFiles2.stream().map(file -> dir2.toPath().relativize(file.toPath()))
                    .filter(path -> !listPath1.contains(path))
                    .map(Path::toString)
                    .collect(joining("\n"));
            throw new DifferentDirectoryContentException("Additional files in dir2:\n" + extraFiles);
        } else {
            logger.debug("Same file list size");
        }

    } catch (IOException e) {
        throw new DifferentDirectoryContentException("Failed to compare ", e);
    }
}