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:org.corehunter.tests.data.simple.SimplePhenotypeDataTest.java

@Test
public void toCsvFileWithAllIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    SimplePhenotypeData phenotypeData = new SimplePhenotypeData("Phenotype Data", OBJECT_FEATURES_MIN_MAX_COL,
            OBJECT_TABLE_AS_LIST);// ww  w. ja va 2  s  .c  o m

    Set<Integer> ids = phenotypeData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "Phenotype-AllIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);
    }

    Path path;

    // write with integer ids
    dataName = "with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, true, true, true);

    assertTrue("Output is not correct!",
            FileUtils
                    .contentEquals(
                            new File(SimpleDistanceMatrixDataTest.class
                                    .getResource("/phenotypes/out/all-with-ids.csv").getPath()),
                            path.toFile()));

    // write with integer ids
    dataName = "no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, false, true, true);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(new File(
                    SimpleDistanceMatrixDataTest.class.getResource("/phenotypes/out/all-no-ids.csv").getPath()),
                    path.toFile()));

}

From source file:org.corehunter.tests.data.simple.SimplePhenotypeDataTest.java

@Test
public void toCsvFileWithSelectedIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    SimplePhenotypeData phenotypeData = new SimplePhenotypeData("Phenotype Data", OBJECT_FEATURES_MIN_MAX_COL,
            OBJECT_TABLE_AS_LIST);/*from   w  w  w .  j av  a2 s  .  c o m*/

    Set<Integer> ids = phenotypeData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "Phenotype-SelectedIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);
    }

    Path path;

    // write with integer ids
    dataName = "with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, true, true, false);

    assertTrue("Output is not correct!",
            FileUtils
                    .contentEquals(
                            new File(SimpleDistanceMatrixDataTest.class
                                    .getResource("/phenotypes/out/sel-with-ids.csv").getPath()),
                            path.toFile()));

    // write with integer ids
    dataName = "no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, false, true, false);

    assertTrue("Output is not correct!",
            FileUtils.contentEquals(new File(
                    SimpleDistanceMatrixDataTest.class.getResource("/phenotypes/out/sel-no-ids.csv").getPath()),
                    path.toFile()));

}

From source file:org.corehunter.tests.data.simple.SimplePhenotypeDataTest.java

@Test
public void toCsvFileWithUnselectedIds() throws IOException {
    expectedHeaders = HEADERS_UNIQUE_NAMES;
    SimplePhenotypeData phenotypeData = new SimplePhenotypeData("Phenotype Data", OBJECT_FEATURES_MIN_MAX_COL,
            OBJECT_TABLE_AS_LIST);/*  w  ww  .ja  v a 2  s  .  co  m*/

    Set<Integer> ids = phenotypeData.getIDs();

    Path dirPath = Paths.get(TEST_OUTPUT);

    Files.createDirectories(dirPath);

    dirPath = Files.createTempDirectory(dirPath, "Phenotype-UnselectedIds");

    // create solution
    SubsetSolution solution = new SubsetSolution(ids);
    for (int sel : SELECTION) {
        solution.select(sel);
    }

    Path path;

    // write with integer ids
    dataName = "with-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, true, false, true);

    assertTrue("Output is not correct!",
            FileUtils
                    .contentEquals(
                            new File(SimpleDistanceMatrixDataTest.class
                                    .getResource("/phenotypes/out/unsel-with-ids.csv").getPath()),
                            path.toFile()));

    // write with integer ids
    dataName = "no-ids.csv";

    path = Paths.get(dirPath.toString(), dataName);

    System.out.println(" |- Write phenotype file (with solution) " + dataName);

    phenotypeData.writeData(path, FileType.CSV, solution, false, false, true);

    assertTrue("Output is not correct!",
            FileUtils
                    .contentEquals(
                            new File(SimpleDistanceMatrixDataTest.class
                                    .getResource("/phenotypes/out/unsel-no-ids.csv").getPath()),
                            path.toFile()));

}

From source file:org.corpus_tools.peppermodules.treetagger.RoundtripTest.java

@Test
public void whenProcessingRoundtripForSimpleCorpus_SourceAndTargetCorpusMustBeEqual() throws IOException {
    final URI in = URI.createFileURI(PepperTestUtil.getTestResources() + "/roundTrip/simple/");
    final URI out = URI.createFileURI(PepperUtil.getTempTestFile("/roundTrip/").getAbsolutePath());
    final TreetaggerImporter importer = new TreetaggerImporter();
    importer.setCorpusDesc(new CorpusDesc.Builder().withCorpusPath(in).build());
    final TreetaggerExporter exporter = new TreetaggerExporter();
    exporter.setCorpusDesc(new CorpusDesc.Builder().withCorpusPath(out).build());
    PepperTestUtil.runPepperForTest(importer, exporter);

    File actual = new File(out.toFileString() + "/simple/doc1.tt");
    File expected = new File(in.toFileString() + "doc1.tt");
    assertTrue("comparing '" + expected + "' with '" + actual + "'", FileUtils.contentEquals(expected, actual));

    actual = new File(out.toFileString() + "/simple/doc2.tt");
    expected = new File(in.toFileString() + "doc2.tt");
    assertTrue("comparing '" + expected + "' with '" + actual + "'", FileUtils.contentEquals(expected, actual));
}

From source file:org.corpus_tools.peppermodules.treetagger.RoundtripTest.java

@Test
public void whenProcessingRoundtripForCorpusWithFiveColumns_SourceAndTargetCorpusMustBeEqual()
        throws IOException {
    final URI in = URI.createFileURI(PepperTestUtil.getTestResources() + "/roundTrip/fiveColumns/");
    final URI out = URI.createFileURI(PepperUtil.getTempTestFile("/roundTrip/").getAbsolutePath());
    final TreetaggerImporter importer = new TreetaggerImporter();
    importer.setCorpusDesc(new CorpusDesc.Builder().withCorpusPath(in).build());
    importer.getProperties().setPropertyValue(TreetaggerImporterProperties.PROP_COLUMN_NAMES,
            "pos, lemma, claws, tok_func");
    final TreetaggerExporter exporter = new TreetaggerExporter();
    exporter.setCorpusDesc(new CorpusDesc.Builder().withCorpusPath(out).build());
    PepperTestUtil.runPepperForTest(importer, exporter);

    File actual = new File(out.toFileString() + "/fiveColumns/GUM_interview_ants.tt");
    File expected = new File(in.toFileString() + "GUM_interview_ants.tt");
    assertTrue("comparing '" + expected + "' with '" + actual + "'", FileUtils.contentEquals(expected, actual));
}

From source file:org.cservenak.streams.CoderTest.java

public void testSomeNastyClass() throws IOException {
    File baseDir = new File("target/test-classes");
    File inputFile = new File(baseDir, "input.txt");
    File middleFile = new File(baseDir, "middle.txt");
    File outputFile = new File(baseDir, "output.txt");

    InputStream is = null;/*w  w  w  . j ava  2s. c  om*/
    OutputStream os = null;

    // copy original - middle
    is = new FileInputStream(inputFile);
    os = new DummyOutputStream(new FileOutputStream(middleFile), 'a');

    IOUtils.copy(is, os);
    os.flush();
    IOUtils.closeQuietly(os);
    IOUtils.closeQuietly(is);

    // copy middle - output
    is = new DummyInputStream(new FileInputStream(middleFile), 'b');
    os = new FileOutputStream(outputFile);

    IOUtils.copy(is, os);
    os.flush();
    IOUtils.closeQuietly(os);
    IOUtils.closeQuietly(is);

    // assertions
    // inputFile is 8 byte length, some random content
    Assert.assertEquals("input file is 8 bytes!", 8, inputFile.length());

    // middleFile is also 8 byte length, and is not equal with inputFile since it's all 'a's.
    Assert.assertEquals("middle file is 8 bytes!", 8, middleFile.length());
    Assert.assertFalse("input and middle file should differ!", FileUtils.contentEquals(inputFile, middleFile));

    // outputFile is also 8 byte length, and is not equal with input and with middle file neither, since it's all 'b's
    Assert.assertEquals("middle file is 8 bytes!", 8, outputFile.length());
    Assert.assertFalse("input and output file should differ!", FileUtils.contentEquals(inputFile, outputFile));
    Assert.assertFalse("middle and output file should differ!",
            FileUtils.contentEquals(middleFile, outputFile));
}

From source file:org.dataconservancy.dcs.util.extraction.GZipPackageExtractorTest.java

@Test
public void testFileEquality() throws Exception {
    URL gzipFileUrl = GZipPackageExtractorTest.class.getResource(FILE_ONLY_GZIP);
    Assert.assertNotNull(gzipFileUrl);//  ww w.j  a v  a 2 s .  c o  m

    File gzipFile = new File(gzipFileUrl.toURI());
    Assert.assertNotNull(gzipFile);
    String fileName = gzipFile.getName().substring(0, gzipFile.getName().length() - 7);
    List<File> files = packageExtractor.getFilesFromPackageFile(fileName, gzipFile);
    Assert.assertNotNull(files);
    Assert.assertEquals("Expected there to be 1 files in the package", 1, files.size());

    URL fileOneURL = GZipPackageExtractorTest.class.getResource(FILE_ONE);
    Assert.assertNotNull(fileOneURL);

    File fileOne = new File(fileOneURL.toURI());
    Assert.assertNotNull(fileOne);

    boolean foundFileOne = false;

    for (File file : files) {
        if (file.getName().equalsIgnoreCase("GzippedFile1.txt")) {
            Assert.assertTrue("Expected file to equal File1.txt", FileUtils.contentEquals(file, fileOne));
            foundFileOne = true;
        }
    }

    //Test to make sure that our comparisson was actually run
    Assert.assertTrue("File comparison wasn't run on file one", foundFileOne);
    packageExtractor.cleanUpExtractedPackage(new File("./" + fileName));
}

From source file:org.dataconservancy.dcs.util.extraction.TarPackageExtractorTest.java

@Test
public void testFileEquality() throws Exception {
    URL tarFileUrl = TarPackageExtractorTest.class.getResource(FILES_ONLY_TAR);
    Assert.assertNotNull(tarFileUrl);/*from  w  w  w  .j  ava  2s. c o m*/

    File tarFile = new File(tarFileUrl.toURI());
    Assert.assertNotNull(tarFile);

    String fileName = tarFile.getName().substring(0, tarFile.getName().length() - 4);

    List<File> files = packageExtractor.getFilesFromPackageFile(fileName, tarFile);
    Assert.assertNotNull(files);
    Assert.assertEquals("Expected there to be 3 files in the package", 3, files.size());

    URL fileOneURL = TarPackageExtractorTest.class.getResource(FILE_ONE);
    Assert.assertNotNull(fileOneURL);
    URL fileTwoURL = TarPackageExtractorTest.class.getResource(FILE_TWO);
    Assert.assertNotNull(fileTwoURL);

    File fileOne = new File(fileOneURL.toURI());
    Assert.assertNotNull(fileOne);
    File fileTwo = new File(fileTwoURL.toURI());
    Assert.assertNotNull(fileTwo);

    boolean foundFileOne = false;
    boolean foundFileTwo = false;

    for (File file : files) {
        if (file.getName().equalsIgnoreCase("TarFile1.txt")) {
            Assert.assertTrue("Expected file to equal File1.txt", FileUtils.contentEquals(file, fileOne));
            foundFileOne = true;
        }

        if (file.getName().equalsIgnoreCase("TarFile2.txt")) {
            Assert.assertTrue("Expected file to equal File2.txt", FileUtils.contentEquals(file, fileTwo));
            foundFileTwo = true;
        }
    }

    //Test to make sure that our comparisson was actually run
    Assert.assertTrue("File comparison wasn't run on file one", foundFileOne);
    Assert.assertTrue("File comparison wasn't run on file two", foundFileTwo);
    packageExtractor.cleanUpExtractedPackage(new File("./" + fileName));
}

From source file:org.dataconservancy.dcs.util.extraction.ZipPackageExtractorTest.java

@Test
public void testFileEquality() throws Exception {
    URL zipFileUrl = ZipPackageExtractorTest.class.getResource(FILES_ONLY_ZIP);
    Assert.assertNotNull(zipFileUrl);//w  ww . j a v  a 2 s  .  co m

    File zipFile = new File(zipFileUrl.toURI());
    Assert.assertNotNull(zipFile);
    String fileName = zipFile.getName().substring(0, zipFile.getName().length() - 4);
    List<File> files = packageExtractor.getFilesFromPackageFile(fileName, zipFile);
    Assert.assertNotNull(files);
    Assert.assertEquals("Expected there to be 3 files in the package", 3, files.size());

    URL fileOneURL = ZipPackageExtractorTest.class.getResource(FILE_ONE);
    Assert.assertNotNull(fileOneURL);
    URL fileTwoURL = ZipPackageExtractorTest.class.getResource(FILE_TWO);
    Assert.assertNotNull(fileTwoURL);

    File fileOne = new File(fileOneURL.toURI());
    Assert.assertNotNull(fileOne);
    File fileTwo = new File(fileTwoURL.toURI());
    Assert.assertNotNull(fileTwo);

    boolean foundFileOne = false;
    boolean foundFileTwo = false;

    for (File file : files) {
        if (file.getName().equalsIgnoreCase("File1.txt")) {
            Assert.assertTrue("Expected file to equal File1.txt", FileUtils.contentEquals(file, fileOne));
            foundFileOne = true;
        }

        if (file.getName().equalsIgnoreCase("File2.txt")) {
            Assert.assertTrue("Expected file to equal File2.txt", FileUtils.contentEquals(file, fileTwo));
            foundFileTwo = true;
        }
    }

    //Test to make sure that our comparisson was actually run
    Assert.assertTrue("File comparison wasn't run on file one", foundFileOne);
    Assert.assertTrue("File comparison wasn't run on file two", foundFileTwo);
    packageExtractor.cleanUpExtractedPackage(new File("./" + fileName));
}

From source file:org.ebayopensource.turmeric.eclipse.test.util.ProjectArtifactValidator.java

@Override
public boolean visit(IResource resource) throws CoreException {

    // if (!matches)
    // return false;

    IPath path = resource.getProjectRelativePath();

    String filePath = path.toString();

    if (StringUtils.isEmpty(filePath))
        return true;

    for (String dir : filterDirList) {
        IPath dirPath = new Path(dir);
        if (path.matchingFirstSegments(dirPath) == dirPath.segmentCount()) {
            return true;
        }/*from  w w  w. j a  va2  s.c o  m*/
    }

    String lastSegment = path.lastSegment();
    for (String file : this.filterFileList) {
        if (lastSegment.equalsIgnoreCase(file))
            return true;
    }

    // now compare with the files in the gold copy
    IPath rsrcPath = resource.getProject().getLocation().append(path);

    if (rsrcPath.toFile().isFile()) {

        if (!rsrcPath.toFile().exists()
                && (new File(goldCopyDir + File.separator + path.toString()).exists())) {

            formatMessage(rsrcPath, "generated copy does not exist");

        }

        System.out.println(" --- Functional test generated file : " + rsrcPath.toOSString());
        if ("properties".equals(rsrcPath.getFileExtension())) {

            Properties srcProp = new Properties();
            InputStream ins = null;
            try {
                ins = new FileInputStream(rsrcPath.toFile());
                srcProp.load(ins);
            } catch (Exception e) {
                e.printStackTrace();
                matches = false;
            } finally {
                IOUtils.closeQuietly(ins);
            }
            Properties goldCopyProp = new Properties();
            try {
                ins = new FileInputStream(new File(goldCopyDir + File.separator + path.toString()));
                goldCopyProp.load(ins);
            } catch (Exception e) {
                e.printStackTrace();
                matches = false;
            } finally {
                IOUtils.closeQuietly(ins);
            }
            if (matches) {
                if (srcProp.containsKey("original_wsdl_uri")) {
                    srcProp.remove("original_wsdl_uri");
                    goldCopyProp.remove("original_wsdl_uri");
                }
                if (PropertiesFileUtil.isEqual(srcProp, goldCopyProp) == false) {

                    formatMessage(rsrcPath, "gold copy did not match");

                    System.out.println("the following did not match: " + goldCopyDir + "/" + path);
                    matches = false;
                }
            }

        } else {
            try {

                if ("java".equals(rsrcPath.getFileExtension())) {

                    //                  if(rsrcPath.lastSegment().contains("TypeDefsBuilder")){
                    //                  //failing in linux box.   
                    //                  return true;
                    //                     
                    //                  }

                    System.out.println("java file : " + rsrcPath.toOSString());
                    File goldCopyJava = new File(goldCopyDir + File.separator + path.toString());

                    if (!goldCopyJava.exists()) {
                        formatMessage(rsrcPath, "gold copy do not exist");
                        System.out.println("File not in gold copy" + goldCopyDir + "/" + path);
                        matches = false;
                        return true;
                    }
                    if (compareTwoFiles(rsrcPath.toFile(), goldCopyJava) == false) {

                        formatMessage(rsrcPath, "gold copy did not match");
                        System.out.println("the following did not match: " + goldCopyDir + "/" + path);

                        matches = false;
                    }

                } else {
                    if ("xml".equals(rsrcPath.getFileExtension())) {
                        System.out.println("xml file : " + rsrcPath.toOSString());

                        /*
                         * if (FileUtils.contentEquals( rsrcPath.toFile(),
                         * new File(goldCopyDir + "/" + path.toString())) ==
                         * false) { formatMessage(rsrcPath); System.out
                         * .println("the following did not match: " +
                         * goldCopyDir + "/" + path); matches = false; }
                         */

                        try {
                            String xmlfile = readFileAsString(rsrcPath.toOSString());

                            // Only Validated if we have a schema with the
                            // file
                            // XMLAssert.assertXMLValid(xmlfile);
                            // XMLAssert.assertXMLValid(goldCopyDir
                            // + File.separator + path);
                            XMLUnit.setIgnoreAttributeOrder(true);
                            XMLUnit.setIgnoreWhitespace(true);

                            String goldFile = readFileAsString(goldCopyDir + File.separator + path);
                            //                        XMLAssert.assertXMLEqual(goldFile,xmlfile);
                            Diff diff = new Diff(goldFile, xmlfile);
                            //Whichever element has the attribute "name" in the goldfile will be compared with 
                            //all the elements that have attribute "name" in the xmlfile, takes care of order differences
                            diff.overrideElementQualifier(new ElementNameAndAttributeQualifier("name"));
                            if (!diff.similar()) {
                                formatMessage(rsrcPath, "gold copy did not match");
                                System.out.println("the following did not match: " + goldCopyDir + "/" + path);
                                matches = false;
                            }
                            //                        XMLAssert.assertXMLEqual(goldFile,xmlfile);

                        } catch (Exception e) {
                            formatMessage(rsrcPath, "gold copy did not match");
                            e.printStackTrace();
                        }

                    } else if ("wsdl".equals(rsrcPath.getFileExtension())) {

                        System.out.println("wsdlFile file : " + rsrcPath.toOSString());

                    } else {

                        System.out.println("other file : " + rsrcPath.toOSString());

                        if (FileUtils.contentEquals(rsrcPath.toFile(),
                                new File(goldCopyDir + "/" + path.toString())) == false) {
                            formatMessage(rsrcPath, "gold copy did not match");
                            System.out.println("the following did not match: " + goldCopyDir + "/" + path);
                            matches = false;
                        }
                    }

                }
            } catch (FileNotFoundException fnfe) {
                formatMessage(rsrcPath, "file not found exception");

            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("the following did not match: " + goldCopyDir + "/" + path);
                matches = false;
            }
        }
    } else {
        files.remove(new File(goldCopyDir + "/" + path.toString()));
    }
    return true;

}