Search in sources :

Example 1 with ICollisionCallback

use of org.omegat.util.FileUtil.ICollisionCallback in project omegat by omegat-org.

the class FileUtilTest method testCopyFilesTo.

@Test
public void testCopyFilesTo() throws Exception {
    File targetDir = makeDir(base, "target");
    // Make structure to copy into targetDir
    File sourceDir = makeDir(base, "source");
    File file1 = writeFile(new File(sourceDir, "file1"), "file1-first");
    File file2 = writeFile(new File(sourceDir, "sub1/file2"), "file2-first");
    // Copy all files. Make sure they are identical.
    FileUtil.copyFilesTo(targetDir, sourceDir.listFiles(), null);
    final File file1trg = new File(targetDir, "file1");
    final File sub1trg = new File(targetDir, "sub1");
    final File file2trg = new File(targetDir, "sub1/file2");
    assertTrue(fileContentsAreEqual(file1, file1trg));
    assertTrue(new File(targetDir, "sub1").isDirectory());
    assertTrue(fileContentsAreEqual(file2, file2trg));
    // Modify source files.
    File file3 = new File(sourceDir, "file3");
    writeFile(file3, "file3-first");
    writeFile(file1, "file1-second");
    writeFile(file2, "file2-second");
    // Do copy but don't overwrite anything. Only file3 should be copied.
    FileUtil.copyFilesTo(targetDir, sourceDir.listFiles(), new ICollisionCallback() {

        @Override
        public boolean shouldReplace(File file, int thisFile, int totalFiles) {
            return false;
        }

        @Override
        public boolean isCanceled() {
            return false;
        }
    });
    File file3trg = new File(targetDir, "file3");
    assertFalse(fileContentsAreEqual(file1, file1trg));
    assertFalse(fileContentsAreEqual(file2, file2trg));
    assertTrue(fileContentsAreEqual(file3, file3trg));
    // Add sub1/file4 to target; this will disappear after replacing sub1.
    File file4trg = writeFile(new File(targetDir, "sub1/file4"), "file4");
    // Do copy and overwrite sub1 only. sub1/file2 should be replaced
    // and sub1/file4 should disappear.
    FileUtil.copyFilesTo(targetDir, sourceDir.listFiles(), new ICollisionCallback() {

        @Override
        public boolean shouldReplace(File file, int thisFile, int totalFiles) {
            return file.equals(sub1trg);
        }

        @Override
        public boolean isCanceled() {
            return false;
        }
    });
    assertFalse(fileContentsAreEqual(file1, file1trg));
    assertTrue(fileContentsAreEqual(file2, file2trg));
    assertTrue(fileContentsAreEqual(file3, file3trg));
    assertFalse(file4trg.exists());
    // Do copy and cancel on last file. None of the files should be changed.
    // shouldReplace() should be called once for all files in source/.
    CountingCallback callback = new CountingCallback() {

        private boolean isCanceled = false;

        @Override
        public boolean shouldReplace(File file, int thisFile, int totalFiles) {
            calledTimes++;
            isCanceled = thisFile + 1 == totalFiles;
            return !isCanceled();
        }

        @Override
        public boolean isCanceled() {
            return isCanceled;
        }
    };
    FileUtil.copyFilesTo(targetDir, sourceDir.listFiles(), callback);
    assertEquals(sourceDir.listFiles().length, callback.calledTimes);
    assertFalse(fileContentsAreEqual(file1, file1trg));
    assertTrue(fileContentsAreEqual(file2, file2trg));
    assertTrue(fileContentsAreEqual(file3, file3trg));
    // Try copying to non-existent destination.
    File newTarget = new File(base, "newtarget");
    FileUtil.copyFilesTo(newTarget, sourceDir.listFiles(), null);
    assertTrue(newTarget.isDirectory());
    assertTrue(fileContentsAreEqual(file1, new File(newTarget, "file1")));
    // Try copying to destination that exists but is a file.
    File targetFile = writeFile(new File(base, "targetFile"), "");
    try {
        FileUtil.copyFilesTo(targetFile, sourceDir.listFiles(), null);
        fail("copyFilesTo should fail when target dir is a file.");
    } catch (IOException ex) {
    }
}
Also used : ICollisionCallback(org.omegat.util.FileUtil.ICollisionCallback) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Test (org.junit.Test)1 ICollisionCallback (org.omegat.util.FileUtil.ICollisionCallback)1