From 3dab5cd70ce6e391381ac3244f00540f303d9b3e Mon Sep 17 00:00:00 2001 From: ljacqu Date: Tue, 18 Oct 2016 18:31:49 +0200 Subject: [PATCH] #979 Check that help_xx.yml files are valid YAML - Change existing test checking messages_xx.yml to also have test for help_xx.yml files --- .../message/MessagesFileYamlCheckerTest.java | 75 -------------- .../message/YamlTextFileCheckerTest.java | 98 +++++++++++++++++++ 2 files changed, 98 insertions(+), 75 deletions(-) delete mode 100644 src/test/java/fr/xephi/authme/message/MessagesFileYamlCheckerTest.java create mode 100644 src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java diff --git a/src/test/java/fr/xephi/authme/message/MessagesFileYamlCheckerTest.java b/src/test/java/fr/xephi/authme/message/MessagesFileYamlCheckerTest.java deleted file mode 100644 index ebb04fc2..00000000 --- a/src/test/java/fr/xephi/authme/message/MessagesFileYamlCheckerTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package fr.xephi.authme.message; - -import fr.xephi.authme.TestHelper; -import fr.xephi.authme.util.StringUtils; -import org.bukkit.configuration.file.YamlConfiguration; -import org.junit.Test; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Pattern; - -import static org.junit.Assert.fail; - -/** - * Tests that all YML message files can be loaded. - */ -public class MessagesFileYamlCheckerTest { - - /** Path in the resources folder where the message files are located. */ - private static final String MESSAGES_FOLDER = "/messages/"; - /** Pattern of the message file names. */ - private static final Pattern MESSAGE_FILE_PATTERN = Pattern.compile("messages_\\w+\\.yml"); - /** Message key that is present in all files. Used to make sure that text is returned. */ - private static final MessageKey MESSAGE_KEY = MessageKey.LOGIN_MESSAGE; - - @Test - public void shouldAllBeValidYaml() { - // given - List messageFiles = getMessageFiles(); - - // when - List errors = new ArrayList<>(); - for (File file : messageFiles) { - String error = null; - try { - YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file); - if (StringUtils.isEmpty(configuration.getString(MESSAGE_KEY.getKey()))) { - error = "Message for '" + MESSAGE_KEY + "' is empty"; - } - } catch (Exception e) { - error = "Could not load file: " + StringUtils.formatException(e); - } - if (!StringUtils.isEmpty(error)) { - errors.add(file.getName() + ": " + error); - } - } - - // then - if (!errors.isEmpty()) { - fail("Errors during verification of message files:\n-" + String.join("\n-", errors)); - } - } - - - private List getMessageFiles() { - File folder = TestHelper.getJarFile(MESSAGES_FOLDER); - File[] files = folder.listFiles(); - if (files == null) { - throw new IllegalStateException("Could not read folder '" + folder.getName() + "'"); - } - - List messageFiles = new ArrayList<>(); - for (File file : files) { - if (MESSAGE_FILE_PATTERN.matcher(file.getName()).matches()) { - messageFiles.add(file); - } - } - if (messageFiles.isEmpty()) { - throw new IllegalStateException("Error getting message files: list of files is empty"); - } - return messageFiles; - } - -} diff --git a/src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java b/src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java new file mode 100644 index 00000000..7fa6fecb --- /dev/null +++ b/src/test/java/fr/xephi/authme/message/YamlTextFileCheckerTest.java @@ -0,0 +1,98 @@ +package fr.xephi.authme.message; + +import fr.xephi.authme.TestHelper; +import fr.xephi.authme.command.help.HelpSection; +import fr.xephi.authme.util.StringUtils; +import org.bukkit.configuration.file.YamlConfiguration; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Pattern; + +import static org.junit.Assert.fail; + +/** + * Tests that all YML text files can be loaded. + */ +public class YamlTextFileCheckerTest { + + /** Path in the resources folder where the message files are located. */ + private static final String MESSAGES_FOLDER = "/messages/"; + /** Contains all files of the MESSAGES_FOLDER. */ + private static List messageFiles; + + @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); + } + + @Test + public void testAllMessagesYmlFiles() { + checkFiles( + Pattern.compile("messages_\\w+\\.yml"), + MessageKey.LOGIN_MESSAGE.getKey()); + } + + @Test + public void testAllHelpYmlFiles() { + checkFiles( + Pattern.compile("help_\\w+\\.yml"), + HelpSection.ALTERNATIVES.getKey()); + } + + /** + * Checks all files in the messages folder that match the given pattern. + * + * @param pattern the pattern the file name needs to match + * @param mandatoryKey key present in all matched files + */ + private void checkFiles(Pattern pattern, String mandatoryKey) { + List errors = new ArrayList<>(); + + boolean hasMatch = false; + for (File file : messageFiles) { + if (pattern.matcher(file.getName()).matches()) { + checkFile(file, mandatoryKey, errors); + hasMatch = true; + } + } + + if (!errors.isEmpty()) { + fail("Errors while checking files matching '" + pattern + "':\n-" + String.join("\n-", errors)); + } else if (!hasMatch) { + fail("Could not find any files satisfying pattern '" + pattern + "'"); + } + } + + /** + * Checks that the provided YAML file can be loaded and that it contains a non-empty text + * for the provided mandatory key. + * + * @param file the file to check + * @param mandatoryKey the key for which text must be present + * @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"; + } + } catch (Exception e) { + error = "Could not load file: " + StringUtils.formatException(e); + } + if (!StringUtils.isEmpty(error)) { + errors.add(file.getName() + ": " + error); + } + } +}