Example usage for org.apache.commons.io FileUtils contentEquals

List of usage examples for org.apache.commons.io FileUtils contentEquals

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils contentEquals.

Prototype

public static boolean contentEquals(File file1, File file2) throws IOException 

Source Link

Document

Compares the contents of two files to determine if they are equal or not.

Usage

From source file:com.amazonaws.ant.AssertFileEqualsTask.java

public void execute() {
    checkParams();/*from   ww w  . j  a va2 s.com*/
    File file1 = new File(firstFile);
    if (!file1.exists()) {
        throw new BuildException("The file " + firstFile + "does not exist");
    }
    File file2 = new File(secondFile);
    if (!file2.exists()) {
        throw new BuildException("The file " + secondFile + "does not exist");
    }

    try {
        if (!FileUtils.contentEquals(file1, file2)) {
            throw new BuildException("The two input files are not equal in content");
        }
    } catch (IOException e) {
        throw new BuildException("IOException while trying to compare content of files: " + e.getMessage());
    }
}

From source file:com.ftb2om2.view.ConverterNGTest.java

/**
 * Test of convert method, of class Converter.
 *//*www . ja v  a2  s.  c  om*/
@Test(enabled = false)
public void FtbAirToOsuMania() throws Exception {
    System.out.println("Testing conversion between FtBAir and o!m");
    String x = getClass().getResource("/").getPath();
    String inputFilePath = getClass().getResource("/ftbAirTest1.txt").getPath();
    String outputFilePath = getClass().getResource("/").getPath();
    String name = "osuTestOutput1";
    Integer volume = 100;
    Metadata metadata = new Metadata("", "", "", "", "", "");
    Converter instance = new Converter(new FtbAirReader(), new OsuManiaV14Writer());
    instance.convert(inputFilePath, outputFilePath, name, volume, metadata);
    // TODO review the generated test code and remove the default call to fail.
    File correctOutput = new File(getClass().getResource("/osuCorrectOutput1.osu").getPath());
    File generatedOutput = new File(getClass().getResource("/osuTestOutput1.osu").getPath());
    assertTrue(FileUtils.contentEquals(correctOutput, generatedOutput));
}

From source file:com.microsoft.alm.plugin.idea.common.setup.WindowsStartup.java

/**
 * Check if registry keys exist and if the cmd file the key contains matches the latest cmd file
 *
 * @param newCmd//from   w  w w.  ja  v a2s .c o  m
 * @return if keys exists or not
 */
protected static boolean doesKeyNeedUpdated(final File newCmd) throws IOException {
    try {
        final String existingKey = Advapi32Util.registryGetStringValue(WinReg.HKEY_CLASSES_ROOT, VSOI_KEY,
                StringUtils.EMPTY);
        final File existingCmd = new File(existingKey.replace("\"%1\"", "").trim());
        if (!existingCmd.exists()) {
            logger.debug("The registry key needs updated because the old key cmd file doesn't exist.");
            return true;
        }

        if (existingCmd.getPath().equalsIgnoreCase(newCmd.getPath())
                || FileUtils.contentEquals(existingCmd, newCmd)) {
            logger.debug("The registry key does not need updated because {}",
                    existingCmd.getPath().equalsIgnoreCase(newCmd.getPath()) ? "the file paths are the same"
                            : "the contents of the files are the same.");
            return false;
        }

        // use the newest cmd file so update if existing cmd file is older
        // TODO: version cmd file if we continually iterate on it so we chose the correct file more reliably
        logger.debug("The existing cmd file is {} old and the the cmd file is {} old",
                existingCmd.lastModified(), newCmd.lastModified());
        return existingCmd.lastModified() < newCmd.lastModified() ? true : false;
    } catch (Win32Exception e) {
        // Error occurred reading the registry (possible key doesn't exist or is empty) so update just to be safe
        logger.debug("There was an issue reading the registry so updating the key to be safe.");
        return true;
    }
}

From source file:at.makubi.maven.plugin.avrohugger.AvrohuggerGeneratorTest.java

public void testAvrohuggerGenerator() throws IOException {
    Path inputDirectory = Paths.get(getBasedir()).resolve("src/test/resources/unit/avrohugger-maven-plugin");
    Path schemaDirectory = inputDirectory.resolve("schema");

    File expectedRecord = inputDirectory.resolve("expected/Record.scala").toFile();
    File actualRecords = outputDirectory.resolve("at/makubi/maven/plugin/model/Record.scala").toFile();

    avrohuggerGenerator.generateScalaFiles(schemaDirectory.toFile(), outputDirectory.toString(),
            new SystemStreamLog(), false);

    assertTrue("Generated Scala file does not match expected one",
            FileUtils.contentEquals(expectedRecord, actualRecords));
}

From source file:com.googlecode.refit.runner.TreeRunnerTest.java

@Test
public void runSuite() throws IOException, JAXBException {
    File inputDir = new File("src/test/fit");
    File outputDir = new File("target/fit");

    ReportGenerator reportGenerator = new ReportGenerator(inputDir, outputDir);
    TreeRunner runner = new TreeRunner(inputDir, outputDir, reportGenerator);
    boolean passed = runner.run();
    assertFalse(passed);/*  ww  w .j  av a  2s .co m*/

    reportGenerator.createReports();

    File xmlReport = new File(outputDir, ReportIO.FIT_REPORT_XML);
    checkXmlReport(xmlReport);

    File htmlReport = new File(outputDir, ReportIO.FIT_REPORT_HTML);
    assertTrue(htmlReport.exists());

    File htmlExpected = new File("src/test/resources/expected", ReportIO.FIT_REPORT_HTML);
    assertTrue(FileUtils.contentEquals(htmlExpected, htmlReport));

    File css = new File(outputDir, ReportIO.FIT_CSS);
    assertTrue(css.exists());
}

From source file:de.uzk.hki.da.utils.FolderUtils.java

/**
 * Compares two folders for equality. Compares the folderstructure and the
 * compares the bitwise equality of the included files.
 *
 * @param lhs//from ww  w .  j  a  v a  2 s.com
 *            relative path to the first folder
 * @param rhs
 *            relative path to the second folder
 * @return true if lhs equals rhs
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static boolean compareFolders(File lhs, File rhs) throws IOException {

    String rhsParentPath = rhs.getAbsolutePath();
    String lhsParentPath = lhs.getAbsolutePath();

    String lhsChildren[] = lhs.list();
    String rhsChildren[] = rhs.list();

    // Sometimes the order of the listings are not equal even though the
    // files contained are
    Arrays.sort(lhsChildren);
    Arrays.sort(rhsChildren);

    boolean filesAreEqual = true;
    for (int i = 0; i < lhsChildren.length; i++) {

        File lhsf = new File(lhsParentPath + "/" + lhsChildren[i]);
        File rhsf = new File(rhsParentPath + "/" + rhsChildren[i]);

        if (lhsf.isFile()) {

            if (!FileUtils.contentEquals(lhsf, rhsf))
                filesAreEqual = false;
        }

        if (lhsf.isDirectory()) {
            if (!compareFolders(lhsf, rhsf))
                filesAreEqual = false;
        }
    }
    return filesAreEqual;
}

From source file:de.douglas.maven.plugin.rpmsystemd.rpm.PostInstMojoTest.java

public void testGenerateRpmPostInstFileWithDefaultSettings() throws Exception {
    String testPomName = "pom-defaults.xml";

    Path testBaseDir = createDir(postInstTestsBaseDir.resolve("defaults"));
    Path pomPath = testBaseDir.resolve(testPomName);

    Path srcPom = postInstSrcDir.resolve(testPomName);
    Files.copy(srcPom, pomPath, StandardCopyOption.REPLACE_EXISTING);

    createDir(SystemPaths$.MODULE$.generationRootDir(testBaseDir));

    PostInstMojo postInstMojo = (PostInstMojo) lookupMojo(pomPath, "generate-rpm-postinst-file");
    postInstMojo.execute();//from   ww w . j  a v a 2s  .  c  om

    assertTrue("Generated postinst file does not equal expected one", FileUtils.contentEquals(
            postInstSrcDir.resolve("postinst-defaults-expected").toFile(),
            testBaseDir.resolve("target").resolve("rpm-systemd-maven-plugin").resolve("postinst").toFile()));
}

From source file:de.douglas.maven.plugin.rpmsystemd.systemd.SystemdMojoTest.java

public void testGenerateSystemdFileWithDefaultSettings() throws Exception {
    String testPomName = "pom-defaults.xml";

    Path testBaseDir = createDir(systemdTestsBaseDir.resolve("defaults"));
    Path pomPath = testBaseDir.resolve(testPomName);

    Path srcPom = systemdSrcDir.resolve(testPomName);
    Files.copy(srcPom, pomPath, StandardCopyOption.REPLACE_EXISTING);

    createDir(SystemPaths$.MODULE$.generationRootDir(testBaseDir));

    SystemdMojo systemdMojo = (SystemdMojo) lookupMojo(pomPath, "generate-systemd-file");
    systemdMojo.execute();/*from w  ww  .j av a2s.c  o  m*/

    assertTrue("Generated systemd service file does not equal expected one",
            FileUtils.contentEquals(systemdSrcDir.resolve("test-module.service-defaults-expected").toFile(),
                    testBaseDir.resolve("target").resolve("rpm-systemd-maven-plugin")
                            .resolve("test-module.service").toFile()));
}

From source file:h2backup.BackupFileService.java

public Path backupDatabase(BackupMethod method, String directory, String filePrefix,
        String dateTimeFormatPattern) {
    try {//from www  . java  2  s .  c  o  m
        String fileNameWithoutExt = filePrefix + "_"
                + DateTimeFormatter.ofPattern(dateTimeFormatPattern).format(LocalDateTime.now(clock)) + "_"
                + method.getFileSuffix();
        final Path path = Paths.get(directory, fileNameWithoutExt + ZIP_EXTENSION);
        final Path tmpPath = Paths.get(directory, fileNameWithoutExt + TMP_SUFFIX + ZIP_EXTENSION);

        log.info("Starting backup to file {} using {} method", tmpPath, method);
        method.getStrategy().backupDatabase(dataSource, tmpPath.toString());

        if (Files.exists(path) && FileUtils.contentEquals(tmpPath.toFile(), path.toFile())) {
            log.info("Deleting backup {} because it is identical to previous backup {}", tmpPath, path);
            Files.delete(tmpPath);
            return null;
        }

        int maxIndex = 0;
        do {
            maxIndex++;
        } while (Files.exists(Paths.get(directory, fileNameWithoutExt + "_" + maxIndex + ZIP_EXTENSION)));

        for (int index = maxIndex; index >= 1; index--) {
            Path oldPath = Paths.get(directory,
                    fileNameWithoutExt + (index > 1 ? "_" + (index - 1) : "") + ZIP_EXTENSION);
            Path newPath = Paths.get(directory, fileNameWithoutExt + "_" + index + ZIP_EXTENSION);
            if (Files.exists(oldPath)) {
                log.debug("Moving file {} to {}", oldPath, newPath);
                Files.move(oldPath, newPath);
            }
        }

        Files.move(tmpPath, path);

        return path;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sangupta.andruil.commands.file.FileCompare.java

@Override
public void execute(String[] args) {
    if (args.length != 2) {
        System.out.println(WindowsErrorMessages.INCORRECT_SYNTAX);
        return;/*from   ww w . j  a  va 2  s .c  o  m*/
    }

    File file1 = new File(args[0]);
    File file2 = new File(args[1]);

    if (!file1.exists()) {
        System.out.println("FC: Cannot open " + file1.getName() + " - No such file");
        return;
    }

    if (!file2.exists()) {
        System.out.println("FC: Cannot open " + file2.getName() + " - No such file");
        return;
    }

    if (!file1.isFile()) {
        System.out.println("FC: " + file1.getName() + " is a folder");
        return;
    }

    if (!file2.isFile()) {
        System.out.println("FC: " + file2.getName() + " is a folder");
        return;
    }

    boolean equals = false;
    try {
        equals = FileUtils.contentEquals(file1, file2);
    } catch (IOException e) {
        // TODO: fix this
        // eat up and assume we could not compare
    }

    if (equals) {
        System.out.println("Files are identical");
        return;
    }

    System.out.println("Files are not identical");
}