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.roche.sequencing.bioinformatics.common.utils.FileUtil.java

/**
 * //  w  ww  .  j a  v a2  s  .  com
 * @param fileOne
 * @param fileTwo
 * @param ignoreEOL
 * @return true if the files contents are the same.
 * @throws IOException
 */
public static boolean filesContentsAreEqual(File fileOne, File fileTwo, boolean ignoreEOL) throws IOException {
    boolean contentsAreEqual = false;
    if (ignoreEOL) {
        contentsAreEqual = FileUtils.contentEqualsIgnoreEOL(fileOne, fileTwo, null);
    } else {
        contentsAreEqual = FileUtils.contentEquals(fileOne, fileTwo);
    }
    return contentsAreEqual;
}

From source file:com.streamsets.pipeline.stage.origin.remote.TestRemoteDownloadSource.java

@Test
public void testOverrunErrorArchiveFileRecovery() throws Exception {
    path = "remote-download-source/parseRecoveryFromFailure";
    File dir = new File(currentThread().getContextClassLoader()
            .getResource("remote-download-source/parseRecoveryFromFailure").getPath());
    File[] files = dir.listFiles();
    for (File f : files) {
        System.out.println(f.getName());
        if (f.getName().equals("polarbear.txt")) {
            f.setLastModified(18000000L);
        } else if (f.getName().equals("longobject.txt")) {
            f.setLastModified(17500000L);
        } else if (f.getName().equals("sloth.txt")) {
            f.setLastModified(17000000L);
        }/*from www  .  j  av  a2s.  c o m*/
    }
    setupSSHD(path, false);
    File archiveDir = testFolder.newFolder();
    FilenameFilter filter = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.equals("longobject.txt");
        }
    };
    RemoteDownloadSource origin = new RemoteDownloadSource(
            getBean("sftp://localhost:" + String.valueOf(port) + "/", true, "testuser", "pass", null, null,
                    null, true, DataFormat.JSON, archiveDir.toString()));
    SourceRunner runner = new SourceRunner.Builder(RemoteDownloadSource.class, origin).addOutputLane("lane")
            .setOnRecordError(OnRecordError.TO_ERROR).build();
    runner.runInit();
    List<Record> expected = getExpectedRecords();
    Record record = RecordCreator.create();
    record.set(Field.create(new HashMap<String, Field>()));
    record.set("/name", Field.create("polarbear"));
    record.set("/age", Field.create("6"));
    record.set("/characterisitics", Field.create(Arrays.asList(Field.create("cool"), Field.create("cute"),
            Field.create("huge"), Field.create("round"), Field.create("playful"))));
    expected.add(record);
    Assert.assertEquals(0, archiveDir.listFiles().length);
    String offset = "-1";
    for (int i = 0; i < 3; i++) {
        StageRunner.Output op = runner.runProduce(offset, 1000);
        offset = op.getNewOffset();
        List<Record> actual = op.getRecords().get("lane");
        Assert.assertEquals(1, actual.size());
        if (i >= 1) { //longobject
            Assert.assertEquals(1, archiveDir.listFiles().length);
            continue;
        } else {
            Assert.assertEquals(0, archiveDir.listFiles().length);
        }
        Assert.assertEquals(expected.get(i).get(), actual.get(0).get());
    }
    Assert.assertEquals(1, archiveDir.listFiles().length);
    File expectedFile = new File(currentThread().getContextClassLoader()
            .getResource("remote-download-source/parseRecoveryFromFailure").getPath()).listFiles(filter)[0];

    File actualFile = archiveDir.listFiles(filter)[0];
    Assert.assertEquals(expectedFile.getName(), actualFile.getName());
    Assert.assertTrue(FileUtils.contentEquals(expectedFile, actualFile));
}

From source file:edu.ku.brc.util.FileStoreAttachmentManager.java

/**
 * Replace origFile with newFile, attempting to recover from an exception if it occurs.
 * // w w  w. java2 s  .  c om
 * @param origFile the original file
 * @param newFile the replacement version
 * @throws IOException a disk IO error occurred during the process
 */
protected void replaceFile(final File origFile, final File newFile) throws IOException {
    File tmpOrig = File.createTempFile("sp6-", ".tmp");
    FileUtils.copyFile(origFile, tmpOrig);
    try {
        // try to copy the new attachment into place
        // if this fails, we should try to copy the old original back into place
        FileUtils.copyFile(newFile, origFile);
    } catch (IOException e) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FileStoreAttachmentManager.class, e);
        // if the attachment file differs from the original that we copied to a tmp location...
        if (!FileUtils.contentEquals(origFile, tmpOrig)) {
            // copy the tmp version back into place
            FileUtils.copyFile(tmpOrig, origFile);
        }
    }
}

From source file:com.spectralogic.ds3client.integration.GetJobManagement_Test.java

private void doReadJobWithJobStarter(final ReadJobStarter readJobStarter) throws IOException,
        URISyntaxException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final String tempPathPrefix = null;
    final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

    try {/*from w w w  .jav  a 2 s  . c om*/
        final String DIR_NAME = "largeFiles/";
        final String FILE_NAME = "lesmis.txt";

        final Path objPath = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME);
        final long bookSize = Files.size(objPath);
        final Ds3Object obj = new Ds3Object(FILE_NAME, bookSize);

        final Ds3ClientShim ds3ClientShim = new Ds3ClientShim((Ds3ClientImpl) client);

        final int maxNumBlockAllocationRetries = 1;
        final int maxNumObjectTransferAttempts = 3;
        final Ds3ClientHelpers ds3ClientHelpers = Ds3ClientHelpers.wrap(ds3ClientShim,
                maxNumBlockAllocationRetries, maxNumObjectTransferAttempts);

        final Ds3ClientHelpers.Job readJob = readJobStarter.startReadJob(ds3ClientHelpers, BUCKET_NAME,
                Arrays.asList(obj));

        final AtomicBoolean dataTransferredEventReceived = new AtomicBoolean(false);
        final AtomicBoolean objectCompletedEventReceived = new AtomicBoolean(false);
        final AtomicBoolean checksumEventReceived = new AtomicBoolean(false);
        final AtomicBoolean metadataEventReceived = new AtomicBoolean(false);
        final AtomicBoolean waitingForChunksEventReceived = new AtomicBoolean(false);
        final AtomicBoolean failureEventReceived = new AtomicBoolean(false);

        readJob.attachDataTransferredListener(new DataTransferredListener() {
            @Override
            public void dataTransferred(final long size) {
                dataTransferredEventReceived.set(true);
                assertEquals(bookSize, size);
            }
        });
        readJob.attachObjectCompletedListener(new ObjectCompletedListener() {
            @Override
            public void objectCompleted(final String name) {
                objectCompletedEventReceived.set(true);
            }
        });
        readJob.attachChecksumListener(new ChecksumListener() {
            @Override
            public void value(final BulkObject obj, final ChecksumType.Type type, final String checksum) {
                checksumEventReceived.set(true);
                assertEquals("69+JXWeZuzl2HFTM6Lbo8A==", checksum);
            }
        });
        readJob.attachMetadataReceivedListener(new MetadataReceivedListener() {
            @Override
            public void metadataReceived(final String filename, final Metadata metadata) {
                metadataEventReceived.set(true);
            }
        });
        readJob.attachWaitingForChunksListener(new WaitingForChunksListener() {
            @Override
            public void waiting(final int secondsToWait) {
                waitingForChunksEventReceived.set(true);
            }
        });
        readJob.attachFailureEventListener(new FailureEventListener() {
            @Override
            public void onFailure(final FailureEvent failureEvent) {
                failureEventReceived.set(true);
            }
        });

        readJob.transfer(new FileObjectGetter(tempDirectory));

        final File originalFile = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME).toFile();
        final File fileCopiedFromBP = Paths.get(tempDirectory.toString(), FILE_NAME).toFile();
        assertTrue(FileUtils.contentEquals(originalFile, fileCopiedFromBP));

        assertTrue(dataTransferredEventReceived.get());
        assertTrue(objectCompletedEventReceived.get());
        assertTrue(checksumEventReceived.get());
        assertTrue(metadataEventReceived.get());
        assertFalse(waitingForChunksEventReceived.get());
        assertFalse(failureEventReceived.get());
    } finally {
        FileUtils.deleteDirectory(tempDirectory.toFile());
    }
}

From source file:net.sourceforge.vulcan.spring.SpringFileStore.java

@Override
public synchronized void storeConfiguration(StateManagerConfigDto config) throws StoreException {
    beanEncoder.reset();// w  w w  .jav a2  s  .  c  o m
    beanEncoder.addBean("configuration", config);

    final File configFile = getConfigFile();
    final File tmp;

    try {
        tmp = File.createTempFile("config", "xml", configFile.getParentFile());
    } catch (IOException e) {
        throw new StoreException("Cannot create temp file in " + configFile.getParent(), e);
    }

    writeBeanConfig(tmp);

    try {
        if (FileUtils.contentEquals(configFile, tmp)) {
            tmp.delete();
        } else {
            if (tmp.renameTo(configFile) == false) {
                // MS Windows work-around:
                FileUtils.copyFile(tmp, configFile, true);
                tmp.delete();
            }
        }
    } catch (IOException e) {
        throw new StoreException("Error comparing config files", e);
    }
}

From source file:nl.minvenj.pef.stream.LiveCaptureTest.java

@Ignore("This interface does not exist anymore")
@Test//  w  w  w .  jav a  2  s.  c o m
public void compareOutputJNetPcapMetal() throws IOException {
    // Create a dumper using both the libraries. Wrapper around the others.
    final String[] cmd = String.format("-4 30313233343536373839414243444546 /16 test -c all %s",
            Paths.get(".").toAbsolutePath().normalize().toString()).split(" ");
    LiveCapture.main(cmd);
    final File metalFile = new File("metal-test.pcap");
    final File jnetpcapFile = new File("jnetpcap-test.pcap");
    //There is not test for comparing with the reference file because if no DNS was found
    // no packets have been modified.
    final File referenceFile = new File("reference");

    Assert.assertTrue("The files should be identical!", FileUtils.contentEquals(metalFile, jnetpcapFile));

    // Temporary cleanup.
    if (metalFile.exists()) {
        metalFile.delete();
    }
    if (jnetpcapFile.exists()) {
        jnetpcapFile.delete();
    }
    if (referenceFile.exists()) {
        referenceFile.delete();
    }
}

From source file:nl.minvenj.pef.stream.PEFPseudonymizerTest.java

@Test
public void dumpFiles() throws IOException, InvalidKeyException {
    final StringBuilder errbuf = new StringBuilder(); // For any error msgs
    final String file = _testdir + File.separator + "56packets.pcap";
    final Pcap pcap = Pcap.openOffline(file, errbuf);

    PcapPacketHandler<PseudoPacketHandler> dumpHandler = new PcapPacketHandler<PseudoPacketHandler>() {
        public void nextPacket(PcapPacket packet, PseudoPacketHandler handler) {
            handler.handle(packet);/* w w  w .j a v  a 2  s .  c  om*/
        }
    };

    if (pcap == null) {
        System.out.printf("Error while opening device for capture: " + errbuf.toString());
    }

    final FramePseudonymizerBuilder builder = new FramePseudonymizerBuilder();
    builder.pseudoIPv4("30313233343536373839414243444546", 16);
    final FramePseudonymizer pseudonymizer = builder.build();
    final String destination = "pseudo-capture-file.cap";
    final PEFPseudonymizeDumper pseudonymizeDumper = new PEFPseudonymizeDumper(pcap, destination,
            pseudonymizer);

    pcap.loop(56, dumpHandler, pseudonymizeDumper);

    pseudonymizeDumper.close();
    pcap.close();
    Assert.assertFalse("The files are not identical!",
            FileUtils.contentEquals(new File(file), new File(destination)));
}

From source file:nl.mpi.oai.harvester.cycle.XMLOverviewTest.java

@Test
/**/*  w  w  w  . j  a  v  a 2 s .c  om*/
 * Test saving a harvest overview in a file different from the original one
 */
public void testAlternativeOverview() {

    File originalFile = TestHelper.getFile("/OverviewNormalMode.xml");

    // get the overview from an existing test XML overview file
    final XMLOverview xmlOverview = new XMLOverview(originalFile);

    // create a new temporary file
    File newFile = TestHelper.copyToTemporary(temporaryFolder, originalFile, "\"CopyOfNormalModeFile.xml\"");

    // the content of both the original and the new file should be the same
    try {
        assertTrue(FileUtils.contentEquals(originalFile, newFile));
    } catch (IOException e) {
        fail();
        e.printStackTrace();
    }
}

From source file:nl.mpi.oai.harvester.cycle.XMLOverviewTest.java

@Test
/**//from   w  w  w .j av  a 2 s  .  c om
 * Test rotating overview files
 */
public void testOverviewRotate() {

    // get the overview from a test XML overview file
    final XMLOverview xmlOverview = new XMLOverview(TestHelper.getFile("/OverviewNormalMode.xml"));

    /* Instead of rotating the overview file itself, test by rotating a
       copy of this file. Therefore, try to save a copy of the overview
       in a temporary folder first.
     */
    try {
        // create a new temporary file
        final File newFile = temporaryFolder.newFile("CopyOfNormalModeFile.xml");

        // save the overview in the temporary file, creating a copy
        xmlOverview.save(newFile);

    } catch (IOException e) {
        fail();
        e.printStackTrace();
    }

    /* Now rotate the copy. This will create a file with a date and
       timestamp, and a new file containing the overview.
     */
    boolean done = xmlOverview.rotateAndSave();

    if (!done) {
        fail();
    }

    // create a filter for finding the overview XML files
    String[] allowedExtensions = new String[] { "xml" };
    IOFileFilter filter = new SuffixFileFilter(allowedExtensions, IOCase.SENSITIVE);

    // create an iterator based on the filter
    Iterator iterator = FileUtils.iterateFiles(temporaryFolder.getRoot(), filter, null);

    // iterate over the temporary files
    File file1 = (File) iterator.next();
    File file2 = (File) iterator.next();

    if (iterator.hasNext()) {
        // there should only be two files in the temporary folder
        fail();
    }

    // determine which file is the rotated file and which is the new file
    File rotatedFile, newFile;

    int atIndex = file1.getPath().lastIndexOf(" at ");

    if (atIndex < 0) {
        // did not find it in this file
        atIndex = file2.getPath().lastIndexOf(" at ");
        if (atIndex < 0) {
            // did not find it in this file either
            rotatedFile = null;
            newFile = null;
            fail();
        } else {
            rotatedFile = file2;
            newFile = file1;
        }
    } else {
        rotatedFile = file1;
        newFile = file2;
    }

    // determine the index of the first character in the timestamp
    int first = atIndex + 4;

    // determine the index of the last character in the timestamp
    int last = rotatedFile.getPath().lastIndexOf("xml") - 1;

    // get the timestamp from the rotated file
    String timeStamp = rotatedFile.getPath().substring(first, last);

    // the timestamp should be before now
    DateTime rotatedAt = new DateTime(timeStamp);
    assertTrue(rotatedAt.isBeforeNow());

    // the path of the files up to the timestamp should be equal
    String partOfRotated = rotatedFile.getPath().substring(0, atIndex);
    String partOfNew = newFile.getPath().substring(0, atIndex);
    assertTrue(partOfRotated.equals(partOfNew));

    // both files should have equal content
    try {
        assertTrue(FileUtils.contentEquals(rotatedFile, newFile));
    } catch (IOException e) {
        fail();
        e.printStackTrace();
    }
}

From source file:opendap.aggregation.FilterAsciiHeaderStreamTest.java

@Test
public void testWrite() throws Exception {
    _in = new FileInputStream("resources/aggregation/unit-tests/source_1.txt");
    _out = new FilterAsciiHeaderStream(new FileOutputStream("src/opendap/aggregation/dest.txt"));

    _out.set(true);//from   w  w  w  . ja v  a 2 s .com
    grind(true);

    // validate dest.txt here
    Assert.assertTrue(FileUtils.contentEquals(new File("src/opendap/aggregation/dest.txt"),
            new File("resources/aggregation/unit-tests/baseline_2.txt")));
}