mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-23 10:45:23 +01:00
#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
This commit is contained in:
parent
9013b8f9a1
commit
3dab5cd70c
@ -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<File> messageFiles = getMessageFiles();
|
||||
|
||||
// when
|
||||
List<String> 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<File> getMessageFiles() {
|
||||
File folder = TestHelper.getJarFile(MESSAGES_FOLDER);
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null) {
|
||||
throw new IllegalStateException("Could not read folder '" + folder.getName() + "'");
|
||||
}
|
||||
|
||||
List<File> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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<File> 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<String> 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<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user