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.wte4j.impl.service.LocalFileStoreTest.java

@Test
public void storeFile() throws IOException {
    LocalFileStore fileStore = new LocalFileStore();

    fileStore.setTemplateDirectory(tempDir);
    URL url = ClassLoader.getSystemResource("org/wte4j/impl/test.txt");
    File testFile = FileUtils.toFile(url);
    OutputStream out = null;/* www. ja va2s .c  o m*/
    InputStream in = null;
    try {
        final String fileName = "fileStoreTest.txt";
        out = fileStore.getOutStream(fileName);
        in = FileUtils.openInputStream(testFile);
        IOUtils.copy(in, out);

        File[] filesInDir = tempDir.listFiles();
        assertEquals(1, filesInDir.length);
        assertEquals(fileName, filesInDir[0].getName());
        assertTrue(FileUtils.contentEquals(testFile, filesInDir[0]));

    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:org.wte4j.impl.WordTemplateRepositoryTest.java

@Test
@Transactional//w  ww  . j  ava  2  s  .c o m
public void persistTemplateWithFileStore() throws Exception {
    WordTemplate<?> template = newTemplate();

    FileStore fileStore = mock(FileStore.class);
    File file = File.createTempFile("content", "docx");
    OutputStream out = FileUtils.openOutputStream(file);
    when(fileStore.getOutStream(anyString())).thenReturn(out);
    repository.setFileStore(fileStore);

    try {
        repository.persist(template);

        File epected = FileUtils.toFile(getClass().getResource("empty.docx"));
        assertTrue(FileUtils.contentEquals(epected, file));
    } finally {
        file.deleteOnExit();
    }
}

From source file:org.wte4j.impl.WordTemplateRepositoryTest.java

@Test
@Transactional/*from  ww  w .  ja v  a2  s.c  om*/
public void updateTemplateWithFileStore() throws Exception {

    FileStore fileStore = mock(FileStore.class);
    File file = File.createTempFile("content", "docx");
    OutputStream out = FileUtils.openOutputStream(file);
    when(fileStore.getOutStream(anyString())).thenReturn(out);
    repository.setFileStore(fileStore);

    WordTemplate<?> template = unlockedTemplate();
    template.getPersistentData().setContent(getContent("empty.docx"));
    try {
        repository.persist(template);
        File expected = FileUtils.toFile(getClass().getResource("empty.docx"));
        assertTrue(FileUtils.contentEquals(expected, file));
    } finally {
        file.deleteOnExit();
    }
}

From source file:org.wte4j.persistence.WordTemplateRepositoryTest.java

@Test
@Transactional/*from   w  w w . ja  v a 2 s.  co m*/
public void persistTemplateWithFileStore() throws Exception {
    WordTemplate<?> template = newTemplate();
    FileStore fileStore = mock(FileStore.class);
    File file = File.createTempFile("content", "docx");
    OutputStream out = FileUtils.openOutputStream(file);
    when(fileStore.getOutStream(anyString())).thenReturn(out);
    repository.setFileStore(fileStore);
    try {
        repository.persist(template);
        File epected = FileUtils.toFile(getClass().getResource("empty.docx"));
        assertTrue(FileUtils.contentEquals(epected, file));
    } finally {
        file.deleteOnExit();
    }
}

From source file:org.wte4j.persistence.WordTemplateRepositoryTest.java

@Test
@Transactional// w ww . ja  va 2  s  .c om
public void updateTemplateWithFileStore() throws Exception {
    FileStore fileStore = mock(FileStore.class);
    File file = File.createTempFile("content", "docx");
    OutputStream out = FileUtils.openOutputStream(file);
    when(fileStore.getOutStream(anyString())).thenReturn(out);
    repository.setFileStore(fileStore);
    WordTemplate<?> template = unlockedTemplate();
    template.getTemplateData().setContent(getContent("empty.docx"), new User());
    try {
        repository.persist(template);
        File expected = FileUtils.toFile(getClass().getResource("empty.docx"));
        assertTrue(FileUtils.contentEquals(expected, file));
    } finally {
        file.deleteOnExit();
    }
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Compares two ZIP files and returns <code>true</code> if they contain same
 * entries./*from   ww  w .j a v a 2  s  .  co m*/
 * <p>
 * First the two files are compared byte-by-byte. If a difference is found the
 * corresponding entries of both ZIP files are compared. Thus if same contents
 * is packed differently the two archives may still be the same.
 * </p>
 * <p>
 * Two archives are considered the same if
 * <ol>
 * <li>they contain same number of entries,</li>
 * <li>for each entry in the first archive there exists an entry with the same
 * in the second archive</li>
 * <li>for each entry in the first archive and the entry with the same name in
 * the second archive
 * <ol>
 * <li>both are either directories or files,</li>
 * <li>both have the same size,</li>
 * <li>both have the same CRC,</li>
 * <li>both have the same contents (compared byte-by-byte).</li>
 * </ol>
 * </li>
 * </ol>
 *
 * @param f1
 *          first ZIP file.
 * @param f2
 *          second ZIP file.
 * @return <code>true</code> if the two ZIP files contain same entries,
 *         <code>false</code> if a difference was found or an error occurred
 *         during the comparison.
 */
public static boolean archiveEquals(File f1, File f2) {
    try {
        // Check the files byte-by-byte
        if (FileUtils.contentEquals(f1, f2)) {
            return true;
        }

        log.debug("Comparing archives '{}' and '{}'...", f1, f2);

        long start = System.currentTimeMillis();
        boolean result = archiveEqualsInternal(f1, f2);
        long time = System.currentTimeMillis() - start;
        if (time > 0) {
            log.debug("Archives compared in " + time + " ms.");
        }
        return result;
    } catch (Exception e) {
        log.debug("Could not compare '" + f1 + "' and '" + f2 + "':", e);
        return false;
    }
}

From source file:pieShareAppITs.SyncChangesIT.java

@Test(timeOut = 120000)
public void testOneFileChanged() throws Exception {
    ITUtil.waitForProcessToStartup(this.process);

    ITTasksCounter counter = context.getBean(ITTasksCounter.class);

    ITUtil.executeLoginToTestCloud(context, this.cloudName, password);

    FileUtils.writeByteArrayToFile(file, "hello world".getBytes(), true);

    while (counter.getCount(FileTransferCompleteTask.class) <= 0) {
        Thread.sleep(1000);/*from ww w  . j a  va 2 s .  c  om*/
    }

    if (counter.getCount(FileTransferCompleteTask.class) == 1) {
        File botFile = new File(ITUtil.getBotWorkingDir(), "test");

        boolean filesAreEqual = FileUtils.contentEquals(this.file, botFile);

        assertTrue(filesAreEqual);
        assertTrue(ITUtil.waitForFileToBeFreed(this.file, 30));
        assertTrue(ITUtil.waitForFileToBeFreed(botFile, 30));
    } else {
        fail("To much file transerfers?!");
    }
}

From source file:pieShareAppITs.SyncFiveFilesIT.java

@Test(timeOut = 300000)
public void syncFiveFilesTest() throws Exception {
    PieLogger.info(this.getClass(), "IPv4Prop: {}", System.getProperty("java.net.preferIPv4Stack", "false"));
    ITTasksCounter counter = context.getBean(ITTasksCounter.class);
    PieUser user = context.getBean("pieUser", PieUser.class);
    PieShareConfiguration config = user.getPieShareConfiguration();
    IPieExecutorTaskFactory executorFactory = context.getBean("pieExecutorTaskFactory",
            PieExecutorTaskFactory.class);

    executorFactory.removeTaskRegistration(FileTransferCompleteMessage.class);
    executorFactory.registerTaskProvider(FileTransferCompleteMessage.class, new Provider<TestTask>() {
        @Override//from  w w w .  j  a v a2s  .com
        public TestTask get() {
            return context.getBean(TestTask.class);
        }
    });

    IPieExecutorTaskFactory testExecutorFacotry = context.getBean("testTaskFactory",
            PieExecutorTaskFactory.class);
    testExecutorFacotry.registerTaskProvider(FileTransferCompleteMessage.class,
            new Provider<FileTransferCompleteTask>() {

                @Override
                public FileTransferCompleteTask get() {
                    return context.getBean(FileTransferCompleteTask.class);
                }
            });

    this.cloudName = UUID.randomUUID().toString();
    this.password = UUID.randomUUID().toString();
    ITUtil.executeLoginToTestCloud(context, cloudName, password);

    System.out.println("Creating files!");
    for (int i = 0; i < 5; i++) {
        String fileName = String.format("testFile_%s", i);
        File file = new File(config.getWorkingDir(), fileName);
        TestFileUtils.createFile(file, 5);
    }

    System.out.println("Starting bot!");
    this.process = ITUtil.startProcess(FileSyncMain.class, cloudName, password);
    ITUtil.waitForProcessToStartup(this.process);
    System.out.println("Bot started!");

    /*File filex = new File(config.getWorkingDir().getParent(), "test.txt");
        TestFileUtils.createFile(filex, 2);
    File fileMain = new File(config.getWorkingDir(), "test.txt");*/
    PieUser botUser = context.getBean("botPieUser", PieUser.class);
    PieShareConfiguration botConfig = botUser.getPieShareConfiguration();
    //File fileBot = new File(botConfig.getWorkingDir(), "test.txt");

    //FileUtils.moveFile(filex, fileMain);
    //FileUtils.moveFile(filex, fileBot);
    System.out.println("Waiting for transfer complete!");
    while (counter.getCount(FileTransferCompleteTask.class) < 5) {
        Thread.sleep(5000);
    }
    System.out.println("Recived transfer complete!");

    if (counter.getCount(FileTransferCompleteTask.class) == 5) {

        boolean filesAreEqual = true;

        for (int i = 0; filesAreEqual && i < 5; i++) {
            String fileName = String.format("testFile_%s", i);
            File file = new File(user.getPieShareConfiguration().getWorkingDir(), fileName);
            File fileBot = new File(botConfig.getWorkingDir(), fileName);
            assertTrue(ITUtil.waitForFileToBeFreed(file, 30));
            assertTrue(ITUtil.waitForFileToBeFreed(fileBot, 30));
            filesAreEqual = FileUtils.contentEquals(file, fileBot);
            assertTrue(ITUtil.waitForFileToBeFreed(file, 30));
            assertTrue(ITUtil.waitForFileToBeFreed(fileBot, 30));
        }

        assertTrue(filesAreEqual);
    } else {
        fail("To much file transerfers?!");
    }
}

From source file:pieShareAppITs.SyncOneBigFileIT.java

public void syncOneBigFileTest() throws Exception {
    ITTasksCounter counter = context.getBean(ITTasksCounter.class);
    PieUser user = context.getBean("pieUser", PieUser.class);
    PieShareConfiguration config = user.getPieShareConfiguration();
    IPieExecutorTaskFactory executorFactory = context.getBean("pieExecutorTaskFactory",
            PieExecutorTaskFactory.class);

    executorFactory.removeTaskRegistration(FileTransferCompleteMessage.class);
    executorFactory.registerTaskProvider(FileTransferCompleteMessage.class, new Provider<TestTask>() {
        @Override// w w w.j a  va  2 s.c o m
        public TestTask get() {
            return context.getBean(TestTask.class);
        }
    });

    IPieExecutorTaskFactory testExecutorFacotry = context.getBean("testTaskFactory",
            PieExecutorTaskFactory.class);
    testExecutorFacotry.registerTaskProvider(FileTransferCompleteMessage.class,
            new Provider<FileTransferCompleteTask>() {

                @Override
                public FileTransferCompleteTask get() {
                    return context.getBean(FileTransferCompleteTask.class);
                }
            });

    this.cloudName = UUID.randomUUID().toString();
    this.password = UUID.randomUUID().toString();
    this.process = ITUtil.startProcess(FileSyncMain.class, cloudName, password);

    ITUtil.executeLoginToTestCloud(context, cloudName, password);

    ITUtil.waitForProcessToStartup(this.process);

    File filex = new File(config.getWorkingDir().getParent(), "test.txt");

    TestFileUtils.createFile(filex, 1024);

    //ITFileUtils.createFile(filex, 4294967296L);
    File fileMain = new File(config.getWorkingDir(), "test.txt");

    PieUser botUser = context.getBean("botPieUser", PieUser.class);
    PieShareConfiguration botConfig = botUser.getPieShareConfiguration();
    File fileBot = new File(botConfig.getWorkingDir(), "test.txt");

    FileUtils.moveFile(filex, fileMain);
    //FileUtils.moveFile(filex, fileBot);

    while (counter.getCount(FileTransferCompleteTask.class) < 1) {
        Thread.sleep(5000);
    }

    if (counter.getCount(FileTransferCompleteTask.class) == 1) {

        assertTrue(ITUtil.waitForFileToBeFreed(fileMain, 60));
        assertTrue(ITUtil.waitForFileToBeFreed(fileBot, 60));

        boolean filesAreEqual = FileUtils.contentEquals(fileMain, fileBot);

        assertTrue(filesAreEqual);
    } else {
        fail("To much file transerfers?!");
    }
}

From source file:pieShareAppITs.SyncOneFileIT.java

@Test(timeOut = 120000)
public void syncOneFileTest() throws Exception {
    PieLogger.info(this.getClass(), "IPv4Prop: {}", System.getProperty("java.net.preferIPv4Stack", "false"));
    ITTasksCounter counter = context.getBean(ITTasksCounter.class);
    PieUser user = context.getBean("pieUser", PieUser.class);
    PieShareConfiguration config = user.getPieShareConfiguration();
    IPieExecutorTaskFactory executorFactory = context.getBean("pieExecutorTaskFactory",
            PieExecutorTaskFactory.class);
    LocalFileService fileService = context.getBean("localFileService", LocalFileService.class);

    executorFactory.removeTaskRegistration(FileTransferCompleteMessage.class);
    executorFactory.registerTaskProvider(FileTransferCompleteMessage.class, new Provider<TestTask>() {
        @Override//from   w w w. j a  v a2  s.c  om
        public TestTask get() {
            return context.getBean(TestTask.class);
        }
    });

    IPieExecutorTaskFactory testExecutorFacotry = context.getBean("testTaskFactory",
            PieExecutorTaskFactory.class);
    testExecutorFacotry.registerTaskProvider(FileTransferCompleteMessage.class,
            new Provider<FileTransferCompleteTask>() {

                @Override
                public FileTransferCompleteTask get() {
                    return context.getBean(FileTransferCompleteTask.class);
                }
            });

    this.cloudName = UUID.randomUUID().toString();
    this.password = UUID.randomUUID().toString();
    System.out.println("Starting bot!");
    this.process = ITUtil.startProcess(FileSyncMain.class, cloudName, password);

    ITUtil.executeLoginToTestCloud(context, cloudName, password);

    //      Thread.sleep(5000);
    //      System.out.println("Starting bot!");
    //      this.process = ITUtil.startProcess(FileSyncMain.class);

    ITUtil.waitForProcessToStartup(this.process);
    System.out.println("Bot started!");

    File filex = new File(config.getWorkingDir().getParent(), "test.txt");
    TestFileUtils.createFile(filex, 2);
    File fileMain = new File(config.getWorkingDir(), "test.txt");

    PieUser botUser = context.getBean("botPieUser", PieUser.class);
    PieShareConfiguration botConfig = botUser.getPieShareConfiguration();
    File fileBot = new File(botConfig.getWorkingDir(), "test.txt");

    FileUtils.moveFile(filex, fileMain);
    //FileUtils.moveFile(filex, fileBot);

    System.out.println("Waiting for transfer complete!");
    while (counter.getCount(FileTransferCompleteTask.class) < 1) {
        Thread.sleep(5000);
    }
    System.out.println("Recived transfer complete!");

    //Wait for file to copy from temp dir to be finished and wait an extra second just to be safe
    fileService.waitUntilCopyFinished(fileBot);
    Thread.sleep(1000);

    if (counter.getCount(FileTransferCompleteTask.class) == 1) {
        boolean filesAreEqual = FileUtils.contentEquals(fileMain, fileBot);

        assertTrue(filesAreEqual);
        assertTrue(ITUtil.waitForFileToBeFreed(fileMain, 30));
        assertTrue(ITUtil.waitForFileToBeFreed(fileBot, 30));
    } else {
        fail("To much file transerfers?!");
    }
}