diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index 9da225b9..a099ff44 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -5,9 +5,6 @@ import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.data.auth.PlayerAuth; -import fr.xephi.authme.datasource.Columns; -import fr.xephi.authme.datasource.DataSource; -import fr.xephi.authme.datasource.DataSourceType; import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.XFBCRYPT; @@ -15,8 +12,8 @@ import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.DatabaseSettings; import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.SecuritySettings; -import fr.xephi.authme.util.RuntimeUtils; import fr.xephi.authme.util.StringUtils; +import fr.xephi.authme.util.Utils; import java.sql.Blob; import java.sql.Connection; @@ -100,8 +97,8 @@ public class MySQL implements DataSource { this.phpBbGroup = settings.getProperty(HooksSettings.PHPBB_ACTIVATED_GROUP_ID); this.wordpressPrefix = settings.getProperty(HooksSettings.WORDPRESS_TABLE_PREFIX); this.poolSize = settings.getProperty(DatabaseSettings.MYSQL_POOL_SIZE); - if(poolSize == -1) { - poolSize = RuntimeUtils.getCoreCount(); + if (poolSize == -1) { + poolSize = Utils.getCoreCount(); } } diff --git a/src/main/java/fr/xephi/authme/util/RuntimeUtils.java b/src/main/java/fr/xephi/authme/util/RuntimeUtils.java deleted file mode 100644 index 6af0c457..00000000 --- a/src/main/java/fr/xephi/authme/util/RuntimeUtils.java +++ /dev/null @@ -1,20 +0,0 @@ -package fr.xephi.authme.util; - -/** - * Runtime utilities. - */ -public class RuntimeUtils { - - // Utility class - private RuntimeUtils() { - } - - /** - * Return the available core count of the JVM. - * - * @return the core count - */ - public static int getCoreCount() { - return Runtime.getRuntime().availableProcessors(); - } -} diff --git a/src/main/java/fr/xephi/authme/util/Utils.java b/src/main/java/fr/xephi/authme/util/Utils.java index 1136854d..5fef8a95 100644 --- a/src/main/java/fr/xephi/authme/util/Utils.java +++ b/src/main/java/fr/xephi/authme/util/Utils.java @@ -48,4 +48,13 @@ public final class Utils { return false; } } + + /** + * Return the available core count of the JVM. + * + * @return the core count + */ + public static int getCoreCount() { + return Runtime.getRuntime().availableProcessors(); + } } diff --git a/src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java b/src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java index 7fa6fecb..1499c976 100644 --- a/src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java +++ b/src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java @@ -13,6 +13,7 @@ import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; +import static tools.utils.FileIoUtils.listFilesOrThrow; import static org.junit.Assert.fail; /** @@ -28,11 +29,7 @@ public class YamlTextFileCheckerTest { @BeforeClass public static void loadMessagesFiles() { File folder = TestHelper.getJarFile(MESSAGES_FOLDER); - File[] files = folder.listFiles(); - if (files == null || files.length == 0) { - throw new IllegalStateException("Could not read folder '" + folder.getName() + "'"); - } - messageFiles = Arrays.asList(files); + messageFiles = Arrays.asList(listFilesOrThrow(folder)); } @Test @@ -82,17 +79,13 @@ public class YamlTextFileCheckerTest { * @param errors collection of errors to add to if the verification fails */ private void checkFile(File file, String mandatoryKey, List errors) { - String error = null; try { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file); if (StringUtils.isEmpty(configuration.getString(mandatoryKey))) { - error = "Message for '" + mandatoryKey + "' is empty"; + errors.add("Message for '" + mandatoryKey + "' is empty"); } } catch (Exception e) { - error = "Could not load file: " + StringUtils.formatException(e); - } - if (!StringUtils.isEmpty(error)) { - errors.add(file.getName() + ": " + error); + errors.add("Could not load file: " + StringUtils.formatException(e)); } } } diff --git a/src/test/java/tools/docs/translations/TranslationPageGenerator.java b/src/test/java/tools/docs/translations/TranslationPageGenerator.java index 240e7a64..83277927 100644 --- a/src/test/java/tools/docs/translations/TranslationPageGenerator.java +++ b/src/test/java/tools/docs/translations/TranslationPageGenerator.java @@ -34,8 +34,6 @@ public class TranslationPageGenerator implements AutoToolTask { private static final int[] COLOR_1 = {12, 9, 0}; private static final int[] COLOR_2 = { 6, 15, 6}; - private final TranslationsGatherer gatherer = new TranslationsGatherer(); - @Override public String getTaskName() { return "updateTranslations"; @@ -43,6 +41,7 @@ public class TranslationPageGenerator implements AutoToolTask { @Override public void executeDefault() { + TranslationsGatherer gatherer = new TranslationsGatherer(); NestedTagValue translationValuesHolder = new NestedTagValue(); for (TranslationInfo translation : gatherer.getTranslationInfo()) { diff --git a/src/test/java/tools/docs/translations/TranslationsGatherer.java b/src/test/java/tools/docs/translations/TranslationsGatherer.java index 233b9332..f54843a9 100644 --- a/src/test/java/tools/docs/translations/TranslationsGatherer.java +++ b/src/test/java/tools/docs/translations/TranslationsGatherer.java @@ -11,6 +11,8 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static tools.utils.FileIoUtils.listFilesOrThrow; + /** * Gathers all available translations of AuthMe. */ @@ -31,10 +33,7 @@ public class TranslationsGatherer { } private void gatherTranslations() { - File[] files = new File(MESSAGES_FOLDER).listFiles(); - if (files == null) { - throw new IllegalStateException("Cannot read files of '" + MESSAGES_FOLDER + "'"); - } + File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER)); for (File file : files) { String code = getLanguageCode(file.getName()); if (code != null) { diff --git a/src/test/java/tools/helptranslation/VerifyHelpTranslations.java b/src/test/java/tools/helptranslation/VerifyHelpTranslations.java index f97447bf..a8a8daa9 100644 --- a/src/test/java/tools/helptranslation/VerifyHelpTranslations.java +++ b/src/test/java/tools/helptranslation/VerifyHelpTranslations.java @@ -10,6 +10,8 @@ import java.util.Scanner; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static tools.utils.FileIoUtils.listFilesOrThrow; + /** * Verifies the help translations for validity and completeness. */ @@ -57,10 +59,7 @@ public class VerifyHelpTranslations implements ToolTask { } private static List getHelpTranslations() { - File[] files = new File(FOLDER).listFiles(); - if (files == null) { - throw new IllegalStateException("Could not get files from '" + FOLDER + "'"); - } + File[] files = listFilesOrThrow(new File(FOLDER)); List helpFiles = Arrays.stream(files) .filter(file -> HELP_MESSAGE_PATTERN.matcher(file.getName()).matches()) .collect(Collectors.toList()); diff --git a/src/test/java/tools/messages/VerifyMessagesTask.java b/src/test/java/tools/messages/VerifyMessagesTask.java index 5343d231..661ff9d8 100644 --- a/src/test/java/tools/messages/VerifyMessagesTask.java +++ b/src/test/java/tools/messages/VerifyMessagesTask.java @@ -17,6 +17,8 @@ import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static tools.utils.FileIoUtils.listFilesOrThrow; + /** * Task to verify the keys in the messages files. */ @@ -124,12 +126,7 @@ public final class VerifyMessagesTask implements ToolTask { } private static List getMessagesFiles() { - File folder = new File(MESSAGES_FOLDER); - File[] files = folder.listFiles(); - if (files == null) { - throw new IllegalStateException("Could not read files from folder '" + folder.getName() + "'"); - } - + File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER)); List messageFiles = new ArrayList<>(); for (File file : files) { if (MESSAGE_FILE_PATTERN.matcher(file.getName()).matches()) { diff --git a/src/test/java/tools/messages/translation/WriteAllExportsTask.java b/src/test/java/tools/messages/translation/WriteAllExportsTask.java index f0faf3b0..f59d31ef 100644 --- a/src/test/java/tools/messages/translation/WriteAllExportsTask.java +++ b/src/test/java/tools/messages/translation/WriteAllExportsTask.java @@ -8,6 +8,8 @@ import tools.utils.ToolsConstants; import java.io.File; import java.util.Scanner; +import static tools.utils.FileIoUtils.listFilesOrThrow; + /** * Task which exports all messages to a local folder. */ @@ -22,11 +24,7 @@ public class WriteAllExportsTask extends ExportMessagesTask { @Override public void execute(Scanner scanner) { - File[] messageFiles = new File(MESSAGES_FOLDER).listFiles(); - if (messageFiles == null || messageFiles.length == 0) { - throw new IllegalStateException("Could not read messages folder"); - } - + final File[] messageFiles = listFilesOrThrow(new File(MESSAGES_FOLDER)); final FileConfiguration defaultMessages = loadDefaultMessages(); for (File file : messageFiles) { String code = file.getName().substring("messages_".length(), file.getName().length() - ".yml".length()); diff --git a/src/test/java/tools/utils/FileIoUtils.java b/src/test/java/tools/utils/FileIoUtils.java index 4683633f..eadefd2d 100644 --- a/src/test/java/tools/utils/FileIoUtils.java +++ b/src/test/java/tools/utils/FileIoUtils.java @@ -1,5 +1,6 @@ package tools.utils; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -62,4 +63,19 @@ public final class FileIoUtils { } } + /** + * Returns a folder's files or throws an exception if the folder could not be read or if it is empty. + * + * @param folder the folder to read + * @return the files in the folder + */ + public static File[] listFilesOrThrow(File folder) { + File[] files = folder.listFiles(); + if (files == null) { + throw new IllegalStateException("Could not read folder '" + folder + "'"); + } else if (files.length == 0) { + throw new IllegalStateException("Folder '" + folder + "' is empty"); + } + return files; + } }