This commit is contained in:
Phillip Webb
2014-04-06 21:44:20 -07:00
parent 8295e82ea0
commit 2bb0f744e0
10 changed files with 143 additions and 143 deletions

View File

@@ -1,9 +1,9 @@
/*
* originright 2012-2013 the copyal author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a origin of the License at
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@@ -26,24 +26,24 @@ import java.io.File;
public class FileUtils {
/**
* Utility to remove duplicate files from a "copy" directory if they already exist in
* an "origin". Recursively scans the origin directory looking for files (not
* Utility to remove duplicate files from an "output" directory if they already exist
* in an "origin". Recursively scans the origin directory looking for files (not
* directories) that exist in both places and deleting the copy.
*
* @param copy the copy directory
* @param origin the origin directory
* @param outputDirectory the output directory
* @param originDirectory the origin directory
*/
public static void removeDuplicatesFromCopy(File copy, File origin) {
if (origin.isDirectory()) {
for (String name : origin.list()) {
File targetFile = new File(copy, name);
public static void removeDuplicatesFromOutputDirectory(File outputDirectory,
File originDirectory) {
if (originDirectory.isDirectory()) {
for (String name : originDirectory.list()) {
File targetFile = new File(outputDirectory, name);
if (targetFile.exists() && targetFile.canWrite()) {
if (!targetFile.isDirectory()) {
targetFile.delete();
}
else {
FileUtils.removeDuplicatesFromCopy(targetFile, new File(origin,
name));
FileUtils.removeDuplicatesFromOutputDirectory(targetFile,
new File(originDirectory, name));
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,60 +27,67 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Tests fir {@link FileUtils}.
*
* @author Dave Syer
*/
public class FileUtilsTests {
private File origin;
private File target;
private File outputDirectory;
private File originDirectory;
@Before
public void init() {
this.origin = new File("target/test/remove");
this.target = new File("target/test/keep");
FileSystemUtils.deleteRecursively(this.origin);
FileSystemUtils.deleteRecursively(this.target);
this.origin.mkdirs();
this.target.mkdirs();
this.outputDirectory = new File("target/test/remove");
this.originDirectory = new File("target/test/keep");
FileSystemUtils.deleteRecursively(this.outputDirectory);
FileSystemUtils.deleteRecursively(this.originDirectory);
this.outputDirectory.mkdirs();
this.originDirectory.mkdirs();
}
@Test
public void simpleDuplicateFile() throws IOException {
File file = new File(this.origin, "logback.xml");
File file = new File(this.outputDirectory, "logback.xml");
file.createNewFile();
new File(this.target, "logback.xml").createNewFile();
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
new File(this.originDirectory, "logback.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
assertFalse(file.exists());
}
@Test
public void nestedDuplicateFile() throws IOException {
assertTrue(new File(this.origin, "sub").mkdirs());
assertTrue(new File(this.target, "sub").mkdirs());
File file = new File(this.origin, "sub/logback.xml");
assertTrue(new File(this.outputDirectory, "sub").mkdirs());
assertTrue(new File(this.originDirectory, "sub").mkdirs());
File file = new File(this.outputDirectory, "sub/logback.xml");
file.createNewFile();
new File(this.target, "sub/logback.xml").createNewFile();
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
new File(this.originDirectory, "sub/logback.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
assertFalse(file.exists());
}
@Test
public void nestedNonDuplicateFile() throws IOException {
assertTrue(new File(this.origin, "sub").mkdirs());
assertTrue(new File(this.target, "sub").mkdirs());
File file = new File(this.origin, "sub/logback.xml");
assertTrue(new File(this.outputDirectory, "sub").mkdirs());
assertTrue(new File(this.originDirectory, "sub").mkdirs());
File file = new File(this.outputDirectory, "sub/logback.xml");
file.createNewFile();
new File(this.target, "sub/different.xml").createNewFile();
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
new File(this.originDirectory, "sub/different.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
assertTrue(file.exists());
}
@Test
public void nonDuplicateFile() throws IOException {
File file = new File(this.origin, "logback.xml");
File file = new File(this.outputDirectory, "logback.xml");
file.createNewFile();
new File(this.target, "different.xml").createNewFile();
FileUtils.removeDuplicatesFromCopy(this.origin, this.target);
new File(this.originDirectory, "different.xml").createNewFile();
FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory,
this.originDirectory);
assertTrue(file.exists());
}