Fix #1493 Extract handling of message file paths to a separate class with constants
This commit is contained in:
parent
ff2f43bdc5
commit
4be130b71b
@ -14,13 +14,13 @@ import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
import java.io.File;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
|
||||
|
||||
/**
|
||||
* Handles a YAML message file with a default file fallback.
|
||||
*/
|
||||
public abstract class AbstractMessageFileHandler implements Reloadable {
|
||||
|
||||
protected static final String DEFAULT_LANGUAGE = "en";
|
||||
|
||||
@DataFolder
|
||||
@Inject
|
||||
private File dataFolder;
|
||||
|
||||
@ -9,6 +9,8 @@ import javax.inject.Inject;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
|
||||
|
||||
/**
|
||||
* File handler for the help_xx.yml resource.
|
||||
*/
|
||||
@ -57,6 +59,6 @@ public class HelpMessagesFileHandler extends AbstractMessageFileHandler {
|
||||
|
||||
@Override
|
||||
protected String createFilePath(String language) {
|
||||
return "messages/help_" + language + ".yml";
|
||||
return MessagePathHelper.createHelpMessageFilePath(language);
|
||||
}
|
||||
}
|
||||
|
||||
77
src/main/java/fr/xephi/authme/message/MessagePathHelper.java
Normal file
77
src/main/java/fr/xephi/authme/message/MessagePathHelper.java
Normal file
@ -0,0 +1,77 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Helper for creating and processing paths to message files.
|
||||
*/
|
||||
public final class MessagePathHelper {
|
||||
|
||||
/** The default language (used as fallback, assumed to be complete, etc.). */
|
||||
public static final String DEFAULT_LANGUAGE = "en";
|
||||
/** Local path to the folder containing the message files. */
|
||||
public static final String MESSAGES_FOLDER = "messages/";
|
||||
/** Local path to the default messages file (messages/messages_en.yml). */
|
||||
public static final String DEFAULT_MESSAGES_FILE = createMessageFilePath(DEFAULT_LANGUAGE);
|
||||
|
||||
private static final Pattern MESSAGE_FILE_PATTERN = Pattern.compile("messages_([a-z]+)\\.yml");
|
||||
private static final Pattern HELP_MESSAGES_FILE = Pattern.compile("help_[a-z]+\\.yml");
|
||||
|
||||
private MessagePathHelper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the local path to the messages file for the provided language code.
|
||||
*
|
||||
* @param languageCode the language code
|
||||
* @return local path to the messages file of the given language
|
||||
*/
|
||||
public static String createMessageFilePath(String languageCode) {
|
||||
return "messages/messages_" + languageCode + ".yml";
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the local path to the help messages file for the provided language code.
|
||||
*
|
||||
* @param languageCode the language code
|
||||
* @return local path to the help messages file of the given language
|
||||
*/
|
||||
public static String createHelpMessageFilePath(String languageCode) {
|
||||
return "messages/help_" + languageCode + ".yml";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given file name is a messages file.
|
||||
*
|
||||
* @param filename the file name to test
|
||||
* @return true if it is a messages file, false otherwise
|
||||
*/
|
||||
public static boolean isMessagesFile(String filename) {
|
||||
return MESSAGE_FILE_PATTERN.matcher(filename).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language code the given file name is for if it is a messages file, otherwise null is returned.
|
||||
*
|
||||
* @param filename the file name to process
|
||||
* @return the language code the file name is a messages file for, or null if not applicable
|
||||
*/
|
||||
public static String getLanguageIfIsMessagesFile(String filename) {
|
||||
Matcher matcher = MESSAGE_FILE_PATTERN.matcher(filename);
|
||||
if (matcher.matches()) {
|
||||
return matcher.group(1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the given file name is a help messages file.
|
||||
*
|
||||
* @param filename the file name to test
|
||||
* @return true if it is a help messages file, false otherwise
|
||||
*/
|
||||
public static boolean isHelpFile(String filename) {
|
||||
return HELP_MESSAGES_FILE.matcher(filename).matches();
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,8 @@ import fr.xephi.authme.message.updater.MessageUpdater;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_LANGUAGE;
|
||||
|
||||
/**
|
||||
* File handler for the messages_xx.yml resource.
|
||||
*/
|
||||
@ -38,6 +40,6 @@ public class MessagesFileHandler extends AbstractMessageFileHandler {
|
||||
|
||||
@Override
|
||||
protected String createFilePath(String language) {
|
||||
return "messages/messages_" + language + ".yml";
|
||||
return MessagePathHelper.createMessageFilePath(language);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ import fr.xephi.authme.command.help.HelpMessage;
|
||||
import fr.xephi.authme.command.help.HelpMessagesService;
|
||||
import fr.xephi.authme.command.help.HelpSection;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.message.MessagePathHelper;
|
||||
import fr.xephi.authme.permission.DefaultPermission;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
@ -49,7 +50,7 @@ public class HelpTranslationGenerator {
|
||||
*/
|
||||
public File updateHelpFile() throws IOException {
|
||||
String languageCode = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
|
||||
File helpFile = new File(dataFolder, "messages/help_" + languageCode + ".yml");
|
||||
File helpFile = new File(dataFolder, MessagePathHelper.createHelpMessageFilePath(languageCode));
|
||||
Map<String, Object> helpEntries = generateHelpMessageEntries();
|
||||
|
||||
String helpEntriesYaml = exportToYaml(helpEntries);
|
||||
|
||||
@ -5,6 +5,7 @@ import ch.jalu.configme.resource.YamlFileReader;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.CommandInitializer;
|
||||
import fr.xephi.authme.message.MessagePathHelper;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@ -25,7 +26,8 @@ import static org.junit.Assert.assertThat;
|
||||
*/
|
||||
public class HelpMessagesConsistencyTest {
|
||||
|
||||
private static final File DEFAULT_MESSAGES_FILE = TestHelper.getJarFile("/messages/help_en.yml");
|
||||
private static final File DEFAULT_MESSAGES_FILE =
|
||||
TestHelper.getJarFile("/" + MessagePathHelper.createHelpMessageFilePath(MessagePathHelper.DEFAULT_LANGUAGE));
|
||||
|
||||
@Test
|
||||
public void shouldHaveIdenticalTexts() {
|
||||
|
||||
@ -7,6 +7,7 @@ import fr.xephi.authme.command.CommandDescription;
|
||||
import fr.xephi.authme.command.TestCommandsUtil;
|
||||
import fr.xephi.authme.message.AbstractMessageFileHandler;
|
||||
import fr.xephi.authme.message.HelpMessagesFileHandler;
|
||||
import fr.xephi.authme.message.MessagePathHelper;
|
||||
import fr.xephi.authme.permission.DefaultPermission;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
@ -32,7 +33,7 @@ import static org.mockito.Mockito.mock;
|
||||
*/
|
||||
public class HelpMessagesServiceTest {
|
||||
|
||||
private static final String TEST_FILE = "/fr/xephi/authme/command/help/help_test.yml";
|
||||
private static final String TEST_FILE = TestHelper.PROJECT_ROOT + "command/help/help_test.yml";
|
||||
private static final Collection<CommandDescription> COMMANDS = TestCommandsUtil.generateCommands();
|
||||
|
||||
private HelpMessagesService helpMessagesService;
|
||||
@ -42,10 +43,10 @@ public class HelpMessagesServiceTest {
|
||||
private File dataFolder;
|
||||
|
||||
@Before
|
||||
public void initializeHandler() throws IOException, InstantiationException, IllegalAccessException {
|
||||
public void initializeHandler() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
new File(dataFolder, "messages").mkdirs();
|
||||
File messagesFile = new File(dataFolder, "messages/help_test.yml");
|
||||
File messagesFile = new File(dataFolder, MessagePathHelper.createHelpMessageFilePath("test"));
|
||||
Files.copy(TestHelper.getJarFile(TEST_FILE), messagesFile);
|
||||
|
||||
HelpMessagesFileHandler helpMessagesFileHandler = createMessagesFileHandler();
|
||||
@ -146,7 +147,7 @@ public class HelpMessagesServiceTest {
|
||||
assertThat(description, equalTo(command.getDescription()));
|
||||
}
|
||||
|
||||
private HelpMessagesFileHandler createMessagesFileHandler() throws IllegalAccessException, InstantiationException {
|
||||
private HelpMessagesFileHandler createMessagesFileHandler() {
|
||||
Settings settings = mock(Settings.class);
|
||||
given(settings.getProperty(PluginSettings.MESSAGES_LANGUAGE)).willReturn("test");
|
||||
|
||||
|
||||
@ -26,6 +26,8 @@ import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.UUID;
|
||||
|
||||
import static fr.xephi.authme.TestHelper.PROJECT_ROOT;
|
||||
import static fr.xephi.authme.TestHelper.TEST_RESOURCES_FOLDER;
|
||||
import static fr.xephi.authme.data.limbo.LimboPlayerMatchers.hasLocation;
|
||||
import static fr.xephi.authme.data.limbo.LimboPlayerMatchers.isLimbo;
|
||||
import static java.util.UUID.fromString;
|
||||
@ -96,7 +98,7 @@ public class DistributedFilesPersistenceHandlerTest {
|
||||
playerDataFolder = new File(dataFolder, "playerdata");
|
||||
playerDataFolder.mkdir();
|
||||
|
||||
File limboFilesFolder = new File("src/test/resources/fr/xephi/authme/data/limbo");
|
||||
File limboFilesFolder = new File(TEST_RESOURCES_FOLDER + PROJECT_ROOT + "data/limbo");
|
||||
for (File file : limboFilesFolder.listFiles()) {
|
||||
File from = new File(playerDataFolder, file.getName());
|
||||
Files.copy(file, from);
|
||||
|
||||
@ -13,9 +13,9 @@ import org.junit.Test;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.MESSAGES_FOLDER;
|
||||
import static org.hamcrest.Matchers.both;
|
||||
import static org.hamcrest.Matchers.emptyString;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
@ -28,20 +28,17 @@ import static org.junit.Assert.assertThat;
|
||||
*/
|
||||
public class HelpMessageConsistencyTest {
|
||||
|
||||
private static final String MESSAGES_FOLDER = "/messages";
|
||||
private static final Pattern HELP_MESSAGES_FILE = Pattern.compile("help_[a-z]+\\.yml");
|
||||
|
||||
private List<File> helpFiles;
|
||||
|
||||
@Before
|
||||
public void findHelpMessagesFiles() {
|
||||
File folder = TestHelper.getJarFile(MESSAGES_FOLDER);
|
||||
File folder = TestHelper.getJarFile("/" + MESSAGES_FOLDER);
|
||||
File[] files = folder.listFiles();
|
||||
if (files == null || files.length == 0) {
|
||||
throw new IllegalStateException("Could not get files from '" + MESSAGES_FOLDER + "'");
|
||||
}
|
||||
helpFiles = Arrays.stream(files)
|
||||
.filter(file -> HELP_MESSAGES_FILE.matcher(file.getName()).matches())
|
||||
.filter(file -> MessagePathHelper.isHelpFile(file.getName()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@ -14,9 +14,9 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.MESSAGES_FOLDER;
|
||||
import static org.junit.Assert.fail;
|
||||
import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
|
||||
@ -27,12 +27,6 @@ import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
@RunWith(Parameterized.class)
|
||||
public class MessageFilePlaceholderTest {
|
||||
|
||||
/** Path in the resources folder where the message files are located. */
|
||||
private static final String MESSAGES_FOLDER = "/messages/";
|
||||
|
||||
/** Pattern for detecting messages files. */
|
||||
private static final Pattern IS_MESSAGES_FILE = Pattern.compile("messages_.*?\\.yml");
|
||||
|
||||
/** Defines exclusions: a (key, tag) pair in this map will not be checked in the test. */
|
||||
private static final Multimap<MessageKey, String> EXCLUSIONS = ImmutableMultimap.<MessageKey, String>builder()
|
||||
.put(MessageKey.INCORRECT_RECOVERY_CODE, "%count")
|
||||
@ -80,10 +74,10 @@ public class MessageFilePlaceholderTest {
|
||||
|
||||
@Parameterized.Parameters(name = "{1}")
|
||||
public static List<Object[]> buildParams() {
|
||||
File folder = TestHelper.getJarFile(MESSAGES_FOLDER);
|
||||
File folder = TestHelper.getJarFile("/" + MESSAGES_FOLDER);
|
||||
|
||||
List<Object[]> messageFiles = Arrays.stream(listFilesOrThrow(folder))
|
||||
.filter(file -> IS_MESSAGES_FILE.matcher(file.getName()).matches())
|
||||
.filter(file -> MessagePathHelper.isMessagesFile(file.getName()))
|
||||
.map(file -> new Object[]{file, file.getName()})
|
||||
.collect(Collectors.toList());
|
||||
if (messageFiles.isEmpty()) {
|
||||
|
||||
@ -0,0 +1,67 @@
|
||||
package fr.xephi.authme.message;
|
||||
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link MessagePathHelper}.
|
||||
*/
|
||||
public class MessagePathHelperTest {
|
||||
|
||||
@Test
|
||||
public void shouldHaveLanguageInSyncWithConfigurations() {
|
||||
// given / when / then
|
||||
assertThat(MessagePathHelper.DEFAULT_LANGUAGE, equalTo(PluginSettings.MESSAGES_LANGUAGE.getDefaultValue()));
|
||||
assertThat(MessagePathHelper.DEFAULT_MESSAGES_FILE, equalTo(MessagePathHelper.createMessageFilePath(MessagePathHelper.DEFAULT_LANGUAGE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBuildTextFilePaths() {
|
||||
// given / when / then
|
||||
assertThat(MessagePathHelper.createMessageFilePath("qq"), equalTo(MessagePathHelper.MESSAGES_FOLDER + "messages_qq.yml"));
|
||||
assertThat(MessagePathHelper.createHelpMessageFilePath("qq"), equalTo(MessagePathHelper.MESSAGES_FOLDER + "help_qq.yml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRecognizeIfIsMessagesFile() {
|
||||
// given / when / then
|
||||
assertThat(MessagePathHelper.isMessagesFile("messages_nl.yml"), equalTo(true));
|
||||
assertThat(MessagePathHelper.isMessagesFile("messages_testtest.yml"), equalTo(true));
|
||||
|
||||
assertThat(MessagePathHelper.isMessagesFile("messages/messages_fr.yml"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isMessagesFile("Messages_fr.yml"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isMessagesFile("otherfile.txt"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isMessagesFile("messages_de.txt"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isMessagesFile(""), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnLanguageForMessagesFile() {
|
||||
// given / when / then
|
||||
assertThat(MessagePathHelper.getLanguageIfIsMessagesFile("messages_nl.yml"), equalTo("nl"));
|
||||
assertThat(MessagePathHelper.getLanguageIfIsMessagesFile("messages_testtest.yml"), equalTo("testtest"));
|
||||
|
||||
assertThat(MessagePathHelper.getLanguageIfIsMessagesFile("messages/messages_fr.yml"), nullValue());
|
||||
assertThat(MessagePathHelper.getLanguageIfIsMessagesFile("Messages_fr.yml"), nullValue());
|
||||
assertThat(MessagePathHelper.getLanguageIfIsMessagesFile("otherfile.txt"), nullValue());
|
||||
assertThat(MessagePathHelper.getLanguageIfIsMessagesFile("messages_de.txt"), nullValue());
|
||||
assertThat(MessagePathHelper.getLanguageIfIsMessagesFile(""), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRecognizeIfIsHelpFile() {
|
||||
// given / when / then
|
||||
assertThat(MessagePathHelper.isHelpFile("help_nl.yml"), equalTo(true));
|
||||
assertThat(MessagePathHelper.isHelpFile("help_testtest.yml"), equalTo(true));
|
||||
|
||||
assertThat(MessagePathHelper.isHelpFile("messages/help_fr.yml"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isHelpFile("Help_fr.yml"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isHelpFile("otherfile.txt"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isHelpFile("help_de.txt"), equalTo(false));
|
||||
assertThat(MessagePathHelper.isHelpFile(""), equalTo(false));
|
||||
}
|
||||
}
|
||||
@ -19,11 +19,11 @@ import static org.junit.Assert.fail;
|
||||
*/
|
||||
public class MessagesFileConsistencyTest {
|
||||
|
||||
private static final String MESSAGES_FILE = "/messages/messages_en.yml";
|
||||
private static final String MESSAGES_FILE = MessagePathHelper.DEFAULT_MESSAGES_FILE;
|
||||
|
||||
@Test
|
||||
public void shouldHaveAllMessages() {
|
||||
File file = TestHelper.getJarFile(MESSAGES_FILE);
|
||||
File file = TestHelper.getJarFile("/" + MESSAGES_FILE);
|
||||
PropertyReader reader = new YamlFileReader(file);
|
||||
List<String> errors = new ArrayList<>();
|
||||
for (MessageKey messageKey : MessageKey.values()) {
|
||||
|
||||
@ -44,7 +44,8 @@ import static org.mockito.hamcrest.MockitoHamcrest.argThat;
|
||||
*/
|
||||
public class MessagesIntegrationTest {
|
||||
|
||||
private static final String YML_TEST_FILE = TestHelper.PROJECT_ROOT + "message/messages_test.yml";
|
||||
private static final String TEST_MESSAGES_LOCAL_PATH = "message/messages_test.yml";
|
||||
private static final String YML_TEST_FILE = TestHelper.PROJECT_ROOT + TEST_MESSAGES_LOCAL_PATH;
|
||||
private Messages messages;
|
||||
private MessagesFileHandler messagesFileHandler;
|
||||
|
||||
@ -68,8 +69,8 @@ public class MessagesIntegrationTest {
|
||||
@Before
|
||||
public void setUpMessages() throws IOException {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
File testFile = new File(dataFolder, "messages/messages_test.yml");
|
||||
new File(dataFolder, "messages").mkdirs();
|
||||
File testFile = new File(dataFolder, MessagePathHelper.createMessageFilePath("test"));
|
||||
new File(dataFolder, MessagePathHelper.MESSAGES_FOLDER).mkdirs();
|
||||
FileUtils.create(testFile);
|
||||
Files.copy(TestHelper.getJarFile(YML_TEST_FILE), testFile);
|
||||
|
||||
@ -276,8 +277,8 @@ public class MessagesIntegrationTest {
|
||||
public void shouldFormatDurationObjects() throws IOException {
|
||||
// given
|
||||
// Use the JAR's messages_en.yml file for this, so copy to the file we're using and reload the file handler
|
||||
File testFile = new File(dataFolder, "messages/messages_test.yml");
|
||||
Files.copy(TestHelper.getJarFile("/messages/messages_en.yml"), testFile);
|
||||
File testFile = new File(dataFolder, MessagePathHelper.createMessageFilePath("test"));
|
||||
Files.copy(TestHelper.getJarFile("/" + MessagePathHelper.DEFAULT_MESSAGES_FILE), testFile);
|
||||
messagesFileHandler.reload();
|
||||
|
||||
Map<Duration, String> expectedTexts = ImmutableMap.<Duration, String>builder()
|
||||
|
||||
@ -11,10 +11,10 @@ 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 java.util.function.Predicate;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.MESSAGES_FOLDER;
|
||||
import static org.junit.Assert.fail;
|
||||
import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
|
||||
@ -23,52 +23,50 @@ import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
*/
|
||||
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;
|
||||
private static File[] messageFiles;
|
||||
|
||||
@BeforeClass
|
||||
public static void loadMessagesFiles() {
|
||||
File folder = TestHelper.getJarFile(MESSAGES_FOLDER);
|
||||
messageFiles = Arrays.asList(listFilesOrThrow(folder));
|
||||
File folder = TestHelper.getJarFile("/" + MESSAGES_FOLDER);
|
||||
messageFiles = listFilesOrThrow(folder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllMessagesYmlFiles() {
|
||||
checkFiles(
|
||||
Pattern.compile("messages_\\w+\\.yml"),
|
||||
MessagePathHelper::isMessagesFile,
|
||||
MessageKey.LOGIN_MESSAGE.getKey());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllHelpYmlFiles() {
|
||||
checkFiles(
|
||||
Pattern.compile("help_\\w+\\.yml"),
|
||||
MessagePathHelper::isHelpFile,
|
||||
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 isRelevantFilePredicate predicate determining which files should be tested
|
||||
* @param mandatoryKey key present in all matched files
|
||||
*/
|
||||
private void checkFiles(Pattern pattern, String mandatoryKey) {
|
||||
private void checkFiles(Predicate<String> isRelevantFilePredicate, String mandatoryKey) {
|
||||
List<String> errors = new ArrayList<>();
|
||||
|
||||
boolean hasMatch = false;
|
||||
for (File file : messageFiles) {
|
||||
if (pattern.matcher(file.getName()).matches()) {
|
||||
if (isRelevantFilePredicate.test(file.getName())) {
|
||||
checkFile(file, mandatoryKey, errors);
|
||||
hasMatch = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
fail("Errors while checking files matching '" + pattern + "':\n-" + String.join("\n-", errors));
|
||||
fail("Errors while checking files\n-" + String.join("\n-", errors));
|
||||
} else if (!hasMatch) {
|
||||
fail("Could not find any files satisfying pattern '" + pattern + "'");
|
||||
fail("Could not find any files matching criteria");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -19,6 +19,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_MESSAGES_FILE;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@ -40,7 +41,7 @@ public class MessageUpdaterTest {
|
||||
@Test
|
||||
public void shouldNotUpdateDefaultFile() throws IOException {
|
||||
// given
|
||||
String messagesFilePath = "messages/messages_en.yml";
|
||||
String messagesFilePath = DEFAULT_MESSAGES_FILE;
|
||||
File messagesFile = temporaryFolder.newFile();
|
||||
Files.copy(TestHelper.getJarFile("/" + messagesFilePath), messagesFile);
|
||||
long modifiedDate = messagesFile.lastModified();
|
||||
@ -60,7 +61,7 @@ public class MessageUpdaterTest {
|
||||
Files.copy(TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "message/messages_test.yml"), messagesFile);
|
||||
|
||||
// when
|
||||
boolean wasChanged = messageUpdater.migrateAndSave(messagesFile, "does-not-exist", "messages/messages_en.yml");
|
||||
boolean wasChanged = messageUpdater.migrateAndSave(messagesFile, "does-not-exist", DEFAULT_MESSAGES_FILE);
|
||||
|
||||
// then
|
||||
assertThat(wasChanged, equalTo(true));
|
||||
@ -80,7 +81,7 @@ public class MessageUpdaterTest {
|
||||
Files.copy(TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "message/messages_en_old.yml"), messagesFile);
|
||||
|
||||
// when
|
||||
boolean wasChanged = messageUpdater.migrateAndSave(messagesFile, "messages/messages_en.yml", "messages/messages_en.yml");
|
||||
boolean wasChanged = messageUpdater.migrateAndSave(messagesFile, DEFAULT_MESSAGES_FILE, DEFAULT_MESSAGES_FILE);
|
||||
|
||||
// then
|
||||
assertThat(wasChanged, equalTo(true));
|
||||
@ -106,7 +107,7 @@ public class MessageUpdaterTest {
|
||||
Files.copy(TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "message/messages_test2.yml"), messagesFile);
|
||||
|
||||
// when
|
||||
boolean wasChanged = messageUpdater.migrateAndSave(messagesFile, "messages/messages_en.yml", "messages/messages_en.yml");
|
||||
boolean wasChanged = messageUpdater.migrateAndSave(messagesFile, DEFAULT_MESSAGES_FILE, DEFAULT_MESSAGES_FILE);
|
||||
|
||||
// then
|
||||
assertThat(wasChanged, equalTo(true));
|
||||
|
||||
@ -2,14 +2,14 @@ package tools.docs.translations;
|
||||
|
||||
import ch.jalu.configme.resource.PropertyReader;
|
||||
import ch.jalu.configme.resource.YamlFileReader;
|
||||
import fr.xephi.authme.message.MessagePathHelper;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import tools.utils.ToolsConstants;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
|
||||
@ -18,14 +18,13 @@ import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
*/
|
||||
public class TranslationsGatherer {
|
||||
|
||||
private static final Pattern MESSAGES_PATTERN = Pattern.compile("messages_([a-z]{2,4})\\.yml");
|
||||
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER;
|
||||
|
||||
private List<TranslationInfo> translationInfo = new ArrayList<>();
|
||||
|
||||
public TranslationsGatherer() {
|
||||
gatherTranslations();
|
||||
translationInfo.sort((e1, e2) -> getSortCode(e1).compareTo(getSortCode(e2)));
|
||||
translationInfo.sort(Comparator.comparing(TranslationsGatherer::getSortCode));
|
||||
}
|
||||
|
||||
public List<TranslationInfo> getTranslationInfo() {
|
||||
@ -35,7 +34,7 @@ public class TranslationsGatherer {
|
||||
private void gatherTranslations() {
|
||||
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
|
||||
for (File file : files) {
|
||||
String code = getLanguageCode(file.getName());
|
||||
String code = MessagePathHelper.getLanguageIfIsMessagesFile(file.getName());
|
||||
if (code != null) {
|
||||
processMessagesFile(code, file);
|
||||
}
|
||||
@ -53,14 +52,6 @@ public class TranslationsGatherer {
|
||||
translationInfo.add(new TranslationInfo(code, (double) availableMessages / MessageKey.values().length));
|
||||
}
|
||||
|
||||
private String getLanguageCode(String messagesFile) {
|
||||
Matcher matcher = MESSAGES_PATTERN.matcher(messagesFile);
|
||||
if (matcher.find()) {
|
||||
return matcher.group(1);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the language code from the translation info for sorting purposes.
|
||||
* Returns "a" for "en" language code to sort English on top.
|
||||
@ -69,7 +60,7 @@ public class TranslationsGatherer {
|
||||
* @return the language code for sorting
|
||||
*/
|
||||
private static String getSortCode(TranslationInfo info) {
|
||||
return "en".equals(info.code) ? "a" : info.code;
|
||||
return MessagePathHelper.DEFAULT_LANGUAGE.equals(info.code) ? "a" : info.code;
|
||||
}
|
||||
|
||||
public static final class TranslationInfo {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package tools.messages;
|
||||
|
||||
import fr.xephi.authme.message.MessagePathHelper;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
@ -15,7 +16,8 @@ import java.util.List;
|
||||
*/
|
||||
public class AddJavaDocToMessageEnumTask implements AutoToolTask {
|
||||
|
||||
private static final String MESSAGES_FILE = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/messages_en.yml";
|
||||
private static final String MESSAGES_FILE =
|
||||
ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.DEFAULT_MESSAGES_FILE;
|
||||
|
||||
private FileConfiguration configuration;
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
package tools.messages;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import fr.xephi.authme.message.MessagePathHelper;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@ -15,8 +16,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_MESSAGES_FILE;
|
||||
import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
|
||||
/**
|
||||
@ -25,11 +26,7 @@ import static tools.utils.FileIoUtils.listFilesOrThrow;
|
||||
public final class VerifyMessagesTask implements ToolTask {
|
||||
|
||||
/** The folder containing the message files. */
|
||||
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||
/** Pattern of the message file names. */
|
||||
private static final Pattern MESSAGE_FILE_PATTERN = Pattern.compile("messages_[a-z]{2,7}\\.yml");
|
||||
/** File to get default messages from (assumes that it is complete). */
|
||||
private static final String DEFAULT_MESSAGES_FILE = MESSAGES_FOLDER + "messages_en.yml";
|
||||
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER;
|
||||
|
||||
@Override
|
||||
public String getTaskName() {
|
||||
@ -50,7 +47,7 @@ public final class VerifyMessagesTask implements ToolTask {
|
||||
if (StringUtils.isEmpty(inputFile)) {
|
||||
messageFiles = getMessagesFiles();
|
||||
} else {
|
||||
File customFile = new File(MESSAGES_FOLDER, "messages_" + inputFile + ".yml");
|
||||
File customFile = new File(ToolsConstants.MAIN_RESOURCES_ROOT, MessagePathHelper.createMessageFilePath(inputFile));
|
||||
messageFiles = Collections.singletonList(customFile);
|
||||
}
|
||||
|
||||
@ -118,7 +115,7 @@ public final class VerifyMessagesTask implements ToolTask {
|
||||
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
|
||||
List<File> messageFiles = new ArrayList<>();
|
||||
for (File file : files) {
|
||||
if (MESSAGE_FILE_PATTERN.matcher(file.getName()).matches()) {
|
||||
if (MessagePathHelper.isMessagesFile(file.getName())) {
|
||||
messageFiles.add(file);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user