Small cleanups / changes amassed over time

- Small javadoc fixes
- Simplifications
- Move logException method from StringUtils to ExceptionUtils
This commit is contained in:
ljacqu 2018-04-22 12:45:34 +02:00
parent baec034909
commit ecaffbabfc
13 changed files with 44 additions and 61 deletions

View File

@ -2,9 +2,7 @@ package fr.xephi.authme;
import ch.jalu.injector.Injector;
import ch.jalu.injector.InjectorBuilder;
import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.api.NewAPI;
import fr.xephi.authme.command.CommandHandler;
import fr.xephi.authme.datasource.DataSource;
@ -35,9 +33,6 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.task.CleanupTask;
import fr.xephi.authme.task.purge.PurgeService;
import fr.xephi.authme.util.ExceptionUtils;
import java.io.File;
import org.apache.commons.lang.SystemUtils;
import org.bukkit.Server;
import org.bukkit.command.Command;
@ -48,6 +43,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.bukkit.scheduler.BukkitScheduler;
import java.io.File;
import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE;
import static fr.xephi.authme.util.Utils.isClassLoaded;

View File

@ -5,7 +5,7 @@ import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.ExceptionUtils;
import java.io.File;
import java.io.FileWriter;
@ -101,7 +101,7 @@ public final class ConsoleLogger {
* @param th The Throwable to log
*/
public static void logException(String message, Throwable th) {
warning(message + " " + StringUtils.formatException(th));
warning(message + " " + ExceptionUtils.formatException(th));
writeLog(Throwables.getStackTraceAsString(th));
}

View File

@ -32,7 +32,7 @@ public enum AllowFlightRestoreType {
}
},
/** Always set flight enabled to false. */
/** The user's flight handling is not modified. */
NOTHING {
@Override
public void restoreAllowFlight(Player player, LimboPlayer limbo) {

View File

@ -14,7 +14,6 @@ import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.service.CommonService;
import fr.xephi.authme.service.bungeecord.BungeeSender;
import fr.xephi.authme.service.bungeecord.MessageType;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.util.PlayerUtils;
@ -82,9 +81,8 @@ public class AsyncRegister implements AsynchronousProcess {
return false;
}
boolean isAsync = service.getProperty(PluginSettings.USE_ASYNC_TASKS);
AuthMeAsyncPreRegisterEvent event = new AuthMeAsyncPreRegisterEvent(player, isAsync);
bukkitService.callEvent(event);
AuthMeAsyncPreRegisterEvent event = bukkitService.createAndCallEvent(
isAsync -> new AuthMeAsyncPreRegisterEvent(player, isAsync));
if (!event.canRegister()) {
return false;
}

View File

@ -8,7 +8,7 @@ import fr.xephi.authme.security.crypts.description.SaltType;
import fr.xephi.authme.security.crypts.description.Usage;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.HooksSettings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.ExceptionUtils;
import javax.inject.Inject;
@ -39,7 +39,7 @@ public class BCrypt implements EncryptionMethod {
try {
return HashUtils.isValidBcryptHash(hash.getHash()) && BCryptService.checkpw(password, hash.getHash());
} catch (IllegalArgumentException e) {
ConsoleLogger.warning("Bcrypt checkpw() returned " + StringUtils.formatException(e));
ConsoleLogger.warning("Bcrypt checkpw() returned " + ExceptionUtils.formatException(e));
}
return false;
}

View File

@ -2,12 +2,12 @@ package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.util.RandomStringUtils;
import fr.xephi.authme.security.crypts.description.HasSalt;
import fr.xephi.authme.security.crypts.description.Recommendation;
import fr.xephi.authme.security.crypts.description.SaltType;
import fr.xephi.authme.security.crypts.description.Usage;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.ExceptionUtils;
import fr.xephi.authme.util.RandomStringUtils;
/**
@ -37,7 +37,7 @@ public class Ipb4 implements EncryptionMethod {
try {
return HashUtils.isValidBcryptHash(hash.getHash()) && BCryptService.checkpw(password, hash.getHash());
} catch (IllegalArgumentException e) {
ConsoleLogger.warning("Bcrypt checkpw() returned " + StringUtils.formatException(e));
ConsoleLogger.warning("Bcrypt checkpw() returned " + ExceptionUtils.formatException(e));
}
return false;
}

View File

@ -2,7 +2,7 @@ package fr.xephi.authme.security.crypts;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.ExceptionUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -32,7 +32,7 @@ public class XfBCrypt implements EncryptionMethod {
try {
return HashUtils.isValidBcryptHash(hash.getHash()) && BCryptService.checkpw(password, hash.getHash());
} catch (IllegalArgumentException e) {
ConsoleLogger.warning("XfBCrypt checkpw() returned " + StringUtils.formatException(e));
ConsoleLogger.warning("XfBCrypt checkpw() returned " + ExceptionUtils.formatException(e));
}
return false;
}

View File

@ -33,4 +33,14 @@ public final class ExceptionUtils {
}
return null;
}
/**
* Format the information from a Throwable as string, retaining the type and its message.
*
* @param th the throwable to process
* @return string with the type of the Throwable and its message, e.g. "[IOException]: Could not open stream"
*/
public static String formatException(Throwable th) {
return "[" + th.getClass().getSimpleName() + "]: " + th.getMessage();
}
}

View File

@ -66,17 +66,6 @@ public final class StringUtils {
return str == null || str.trim().isEmpty();
}
/**
* Format the information from a Throwable as string, retaining the type and its message.
*
* @param th The throwable to process
*
* @return String with the type of the Throwable and its message, e.g. "[IOException]: Could not open stream"
*/
public static String formatException(Throwable th) {
return "[" + th.getClass().getSimpleName() + "]: " + th.getMessage();
}
/**
* Check that the given needle is in the middle of the haystack, i.e. that the haystack
* contains the needle and that it is not at the very start or end.

View File

@ -30,23 +30,11 @@ public class CodeClimateConfigTest {
assertThat(excludePaths, not(empty()));
removeTestsExclusionOrThrow(excludePaths);
for (String path : excludePaths) {
verifySourceFileExists(path);
}
}
private static void verifySourceFileExists(String path) {
// Note ljacqu 20170323: In the future, we could have legitimate exclusions that don't fulfill these checks,
// in which case this test needs to be adapted accordingly.
if (!path.startsWith(TestHelper.SOURCES_FOLDER)) {
fail("Unexpected path '" + path + "': expected to start with sources folder");
} else if (!path.endsWith(".java")) {
fail("Expected path '" + path + "' to end with '.java'");
}
if (!new File(path).exists()) {
fail("Path '" + path + "' does not exist!");
}
}
}
private static void removeTestsExclusionOrThrow(List<String> excludePaths) {
boolean wasRemoved = excludePaths.removeIf("src/test/java/**/*Test.java"::equals);

View File

@ -2,6 +2,7 @@ package fr.xephi.authme.message;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.help.HelpSection;
import fr.xephi.authme.util.ExceptionUtils;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass;
@ -85,7 +86,7 @@ public class YamlTextFileCheckerTest {
errors.add("Message for '" + mandatoryKey + "' is empty");
}
} catch (Exception e) {
errors.add("Could not load file: " + StringUtils.formatException(e));
errors.add("Could not load file: " + ExceptionUtils.formatException(e));
}
}
}

View File

@ -4,8 +4,10 @@ import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import java.net.MalformedURLException;
import java.util.ConcurrentModificationException;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
@ -58,4 +60,16 @@ public class ExceptionUtilsTest {
// given / when / then
TestHelper.validateHasOnlyPrivateEmptyConstructor(ExceptionUtils.class);
}
@Test
public void shouldFormatException() {
// given
MalformedURLException ex = new MalformedURLException("Unrecognized URL format");
// when
String result = ExceptionUtils.formatException(ex);
// then
assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format"));
}
}

View File

@ -3,8 +3,6 @@ package fr.xephi.authme.util;
import fr.xephi.authme.TestHelper;
import org.junit.Test;
import java.net.MalformedURLException;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
@ -63,18 +61,6 @@ public class StringUtilsTest {
assertFalse(StringUtils.isEmpty(" test"));
}
@Test
public void shouldFormatException() {
// given
MalformedURLException ex = new MalformedURLException("Unrecognized URL format");
// when
String result = StringUtils.formatException(ex);
// then
assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format"));
}
@Test
public void shouldGetDifferenceWithNullString() {
// given/when/then