From bfebf6dc44760595129a090d5a79b0911845a805 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Mon, 14 Dec 2015 21:37:20 +0100 Subject: [PATCH] Add tests for CommandUtils, move purgeDirectory to its only use --- .../xephi/authme/cache/backup/JsonCache.java | 24 ++++++++- src/main/java/fr/xephi/authme/util/Utils.java | 22 -------- .../authme/command/CommandUtilsTest.java | 52 +++++++++++++++++++ 3 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java index b07c74d9e..38784b16d 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java +++ b/src/main/java/fr/xephi/authme/cache/backup/JsonCache.java @@ -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(); + } + } + } diff --git a/src/main/java/fr/xephi/authme/util/Utils.java b/src/main/java/fr/xephi/authme/util/Utils.java index e45b04a8b..c96f47c60 100644 --- a/src/main/java/fr/xephi/authme/util/Utils.java +++ b/src/main/java/fr/xephi/authme/util/Utils.java @@ -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, diff --git a/src/test/java/fr/xephi/authme/command/CommandUtilsTest.java b/src/test/java/fr/xephi/authme/command/CommandUtilsTest.java index d808f8b4f..14dc8f7fe 100644 --- a/src/test/java/fr/xephi/authme/command/CommandUtilsTest.java +++ b/src/test/java/fr/xephi/authme/command/CommandUtilsTest.java @@ -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)); + } }