Create FileUtils#delete and write tests for FileUtils

This commit is contained in:
ljacqu 2016-09-17 00:42:18 +02:00
parent 0aa02b70f0
commit bdf8819aa7
4 changed files with 150 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.EmailSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.BukkitService;
import fr.xephi.authme.util.FileUtils;
import fr.xephi.authme.util.StringUtils;
import org.apache.commons.mail.EmailConstants;
import org.apache.commons.mail.EmailException;
@ -91,9 +92,7 @@ public class SendMailSSL {
}
sendEmail(content, email);
if (file != null) {
file.delete();
}
FileUtils.delete(file);
}
});
}

View File

@ -68,7 +68,22 @@ public final class FileUtils {
if (target.isDirectory()) {
purgeDirectory(target);
}
target.delete();
delete(target);
}
}
/**
* Delete the given file or directory and log a message if it was unsuccessful.
* Method is null safe and does nothing when null is passed.
*
* @param file the file to delete
*/
public static void delete(File file) {
if (file != null) {
boolean result = file.delete();
if (!result) {
ConsoleLogger.warning("Could not delete file '" + file + "'");
}
}
}
}

View File

@ -64,9 +64,7 @@ public class GeoLiteAPI {
return false;
}
} else {
if (!dataFile.delete()) {
ConsoleLogger.warning("Failed to delete GeoLiteAPI database");
}
FileUtils.delete(dataFile);
}
}
// Ok, let's try to download the data file!

View File

@ -0,0 +1,131 @@
package fr.xephi.authme.util;
import com.google.common.io.Files;
import fr.xephi.authme.TestHelper;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link FileUtils}.
*/
public class FileUtilsTest {
@BeforeClass
public static void initLogger() {
TestHelper.setupLogger();
}
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@Test
public void shouldNotCopyFile() throws IOException {
// given
File folder = temporaryFolder.newFolder();
File file = new File(folder, "config.yml");
// purposely don't copy config.yml to verify that config.yml isn't copied by the method
File emailJarFile = TestHelper.getJarFile("/email.html");
Files.copy(emailJarFile, file);
// when
boolean result = FileUtils.copyFileFromResource(file, "config.yml");
// then
assertThat(result, equalTo(true));
assertThat(file.length(), equalTo(emailJarFile.length()));
}
@Test
public void shouldCopyFileFromJar() throws IOException {
// given
File folder = temporaryFolder.newFolder();
File file = new File(folder, "some/folders/config.yml");
// when
boolean result = FileUtils.copyFileFromResource(file, "config.yml");
// then
assertThat(result, equalTo(true));
assertThat(file.exists(), equalTo(true));
File configJarFile = TestHelper.getJarFile("/config.yml");
assertThat(file.length(), equalTo(configJarFile.length()));
}
@Test
public void shouldReturnFalseForInvalidJarFile() throws IOException {
// given
File folder = temporaryFolder.newFolder();
File file = new File(folder, "bogus");
// when
boolean result = FileUtils.copyFileFromResource(file, "does-not-exist");
// then
assertThat(result, equalTo(false));
assertThat(file.exists(), equalTo(false));
}
@Test
public void shouldPurgeDirectory() throws IOException {
// given
File root = temporaryFolder.newFolder();
File file1 = new File(root, "a/b/c/test.html");
File file2 = new File(root, "a/b/f/toast.txt");
File file3 = new File(root, "a/g/rest.png");
File file4 = new File(root, "j/l/roast.tiff");
createFiles(file1, file2, file3, file4);
// when
FileUtils.purgeDirectory(new File(root, "a"));
// then
assertThat(file1.exists(), equalTo(false));
assertThat(file2.exists(), equalTo(false));
assertThat(file3.exists(), equalTo(false));
assertThat(file4.exists(), equalTo(true));
assertThat(new File(root, "a").exists(), equalTo(true));
}
@Test
public void shouldDeleteFile() throws IOException {
// given
File file = temporaryFolder.newFile();
assertThat(file.exists(), equalTo(true));
// when
FileUtils.delete(file);
// then
assertThat(file.exists(), equalTo(false));
}
@Test
public void shouldDoNothingForNullFile() {
// given
File file = null;
// when
FileUtils.delete(file);
// then
// Nothing happens
}
private static void createFiles(File... files) throws IOException {
for (File file : files) {
boolean result = file.getParentFile().mkdirs() & file.createNewFile();
if (!result) {
throw new IllegalStateException("Cannot create file '" + file + "'");
}
}
}
}