Add some tests, minor Checkstyle fixes
This commit is contained in:
@@ -75,6 +75,20 @@ public class FileUtilsTest {
|
||||
assertThat(file.exists(), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFalseForParentInvalidParentFolders() throws IOException {
|
||||
// given
|
||||
File folder = temporaryFolder.newFolder();
|
||||
new File(folder, "hello").createNewFile();
|
||||
File fileToCreate = new File(folder, "hello/test");
|
||||
|
||||
// when
|
||||
boolean result = FileUtils.copyFileFromResource(fileToCreate, "welcome.txt");
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPurgeDirectory() throws IOException {
|
||||
// given
|
||||
@@ -137,6 +151,36 @@ public class FileUtilsTest {
|
||||
assertThat(result, equalTo("path" + File.separator + "to" + File.separator + "test-file.txt"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateDirectory() throws IOException {
|
||||
// given
|
||||
File root = temporaryFolder.newFolder();
|
||||
File dir = new File(root, "folder/folder2/myFolder");
|
||||
|
||||
// when
|
||||
boolean result = FileUtils.createDirectory(dir);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(true));
|
||||
assertThat(dir.exists(), equalTo(true));
|
||||
assertThat(dir.isDirectory(), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnFalseOnDirectoryCreateFail() throws IOException {
|
||||
// given
|
||||
File root = temporaryFolder.newFolder();
|
||||
File dirAsFile = new File(root, "file");
|
||||
dirAsFile.createNewFile();
|
||||
|
||||
// when
|
||||
boolean result = FileUtils.createDirectory(dirAsFile);
|
||||
|
||||
// then
|
||||
assertThat(result, equalTo(false));
|
||||
assertThat(dirAsFile.isFile(), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHaveHiddenConstructor() {
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(FileUtils.class);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -19,4 +20,10 @@ public class InternetProtocolUtilsTest {
|
||||
assertThat(InternetProtocolUtils.isLocalAddress("192.168.0.1"), equalTo(true));
|
||||
assertThat(InternetProtocolUtils.isLocalAddress("94.32.34.5"), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHavePrivateConstructor() {
|
||||
// given / when / then
|
||||
TestHelper.validateHasOnlyPrivateEmptyConstructor(InternetProtocolUtils.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class RandomStringUtilsTest {
|
||||
|
||||
// when / then
|
||||
for (int length : lengths) {
|
||||
String result = RandomStringUtils.generateHex(length);
|
||||
String result = RandomStringUtils.generateLowerUpper(length);
|
||||
assertThat("Result '" + result + "' should have length " + length,
|
||||
result.length(), equalTo(length));
|
||||
assertThat("Result '" + result + "' should only have characters a-z, A-Z, 0-9",
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -99,6 +105,68 @@ public class UtilsTest {
|
||||
verifyZeroInteractions(sender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckIfCollectionIsEmpty() {
|
||||
// given
|
||||
List<String> emptyList = Collections.emptyList();
|
||||
Collection<Integer> nonEmptyColl = Arrays.asList(3, 4, 5);
|
||||
|
||||
// when / then
|
||||
assertThat(Utils.isCollectionEmpty(emptyList), equalTo(true));
|
||||
assertThat(Utils.isCollectionEmpty(nonEmptyColl), equalTo(false));
|
||||
assertThat(Utils.isCollectionEmpty(null), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnCoreCount() {
|
||||
// given / when / then
|
||||
assertThat(Utils.getCoreCount(), greaterThan(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLogAndSendWarning() {
|
||||
// given
|
||||
Logger logger = TestHelper.setupLogger();
|
||||
String message = "Error while performing action";
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
Utils.logAndSendWarning(sender, message);
|
||||
|
||||
// then
|
||||
verify(logger).warning(message);
|
||||
verify(sender).sendMessage(ChatColor.RED + message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLogWarningAndNotSendToConsoleSender() {
|
||||
// given
|
||||
Logger logger = TestHelper.setupLogger();
|
||||
String message = "Error while performing action";
|
||||
CommandSender sender = mock(ConsoleCommandSender.class);
|
||||
|
||||
// when
|
||||
Utils.logAndSendWarning(sender, message);
|
||||
|
||||
// then
|
||||
verify(logger).warning(message);
|
||||
verifyZeroInteractions(sender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLogWarningAndHandleNullCommandSender() {
|
||||
// given
|
||||
Logger logger = TestHelper.setupLogger();
|
||||
String message = "Error while performing action";
|
||||
CommandSender sender = null;
|
||||
|
||||
// when
|
||||
Utils.logAndSendWarning(sender, message);
|
||||
|
||||
// then
|
||||
verify(logger).warning(message);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCheckIfClassIsLoaded() {
|
||||
// given / when / then
|
||||
|
||||
Reference in New Issue
Block a user