Add tests for CommandUtils, move purgeDirectory to its only use

This commit is contained in:
ljacqu 2015-12-14 21:37:20 +01:00
parent 282f777311
commit bfebf6dc44
3 changed files with 74 additions and 24 deletions

View File

@ -5,7 +5,6 @@ import com.google.common.io.Files;
import com.google.gson.*;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.Utils;
import org.bukkit.entity.Player;
import java.io.File;
@ -109,7 +108,7 @@ public class JsonCache {
}
File file = new File(cacheDir, path);
if (file.exists()) {
Utils.purgeDirectory(file);
purgeDirectory(file);
if (!file.delete()) {
ConsoleLogger.showError("Failed to remove" + player.getName() + "cache.");
}
@ -194,4 +193,25 @@ public class JsonCache {
}
}
/**
* Delete a given directory and all its content.
*
* @param directory The directory to remove
*/
private static void purgeDirectory(File directory) {
if (!directory.isDirectory()) {
return;
}
File[] files = directory.listFiles();
if (files == null) {
return;
}
for (File target : files) {
if (target.isDirectory()) {
purgeDirectory(target);
}
target.delete();
}
}
}

View File

@ -15,7 +15,6 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
@ -207,27 +206,6 @@ public final class Utils {
}
}
/**
* Delete a given directory and all his content.
*
* @param directory File
*/
public static void purgeDirectory(File directory) {
if (!directory.isDirectory()) {
return;
}
File[] files = directory.listFiles();
if (files == null) {
return;
}
for (File target : files) {
if (target.isDirectory()) {
purgeDirectory(target);
}
target.delete();
}
}
/**
* Safe way to retrieve the list of online players from the server. Depending on the
* implementation of the server, either an array of {@link Player} instances is being returned,

View File

@ -49,4 +49,56 @@ public class CommandUtilsTest {
// then
assertThat(commandPath, equalTo("/authme help"));
}
// ------
// min / max arguments
// ------
@Test
public void shouldComputeMinAndMaxOnEmptyCommand() {
// given
CommandDescription command = getBuilderForArgsTest().build();
// when / then
checkArgumentCount(command, 0, 0);
}
@Test
public void shouldComputeMinAndMaxOnCommandWithMandatoryArgs() {
// given
CommandDescription command = getBuilderForArgsTest()
.withArgument("Test", "Arg description", false)
.withArgument("Test22", "Arg description 2", false)
.build();
// when / then
checkArgumentCount(command, 2, 2);
}
@Test
public void shouldComputeMinAndMaxOnCommandIncludingOptionalArgs() {
// given
CommandDescription command = getBuilderForArgsTest()
.withArgument("arg1", "Arg description", false)
.withArgument("arg2", "Arg description 2", true)
.withArgument("arg3", "Arg description 3", true)
.build();
// when / then
checkArgumentCount(command, 1, 3);
}
private static void checkArgumentCount(CommandDescription command, int expectedMin, int expectedMax) {
assertThat(CommandUtils.getMinNumberOfArguments(command), equalTo(expectedMin));
assertThat(CommandUtils.getMaxNumberOfArguments(command), equalTo(expectedMax));
}
private static CommandDescription.CommandBuilder getBuilderForArgsTest() {
return CommandDescription.builder()
.labels("authme", "auth")
.description("Base")
.detailedDescription("Test base command.")
.executableCommand(mock(ExecutableCommand.class));
}
}