#349 Add tags field to MessageKey, add test to verify presence in files

This commit is contained in:
ljacqu 2015-12-15 13:21:53 +01:00
parent 3de7a7584e
commit 94356bb23f
4 changed files with 79 additions and 26 deletions

View File

@ -79,7 +79,7 @@ public enum MessageKey {
INVALID_NAME_LENGTH("name_len"), INVALID_NAME_LENGTH("name_len"),
INVALID_NAME_CHARACTERS("regex"), INVALID_NAME_CHARACTERS("regex", "REG_EX"),
ADD_EMAIL_MESSAGE("add_email"), ADD_EMAIL_MESSAGE("add_email"),
@ -87,7 +87,7 @@ public enum MessageKey {
USAGE_CAPTCHA("usage_captcha"), USAGE_CAPTCHA("usage_captcha"),
CAPTCHA_WRONG_ERROR("wrong_captcha"), CAPTCHA_WRONG_ERROR("wrong_captcha", "THE_CAPTCHA"),
CAPTCHA_SUCCESS("valid_captcha"), CAPTCHA_SUCCESS("valid_captcha"),
@ -119,16 +119,32 @@ public enum MessageKey {
ANTIBOT_AUTO_ENABLED_MESSAGE("antibot_auto_enabled"), ANTIBOT_AUTO_ENABLED_MESSAGE("antibot_auto_enabled"),
ANTIBOT_AUTO_DISABLED_MESSAGE("antibot_auto_disabled"); ANTIBOT_AUTO_DISABLED_MESSAGE("antibot_auto_disabled", "%m");
private String key; private String key;
private String[] tags;
MessageKey(String key) { MessageKey(String key, String... tags) {
this.key = key; this.key = key;
this.tags = tags;
} }
/**
* Return the key used in the messages file.
*
* @return The key
*/
public String getKey() { public String getKey() {
return key; return key;
} }
/**
* Return a list of tags (texts) that are replaced with actual content in AuthMe.
*
* @return List of tags
*/
public String[] getTags() {
return tags;
}
} }

View File

@ -14,7 +14,7 @@ user_regged: '&cYou already have registered this username!'
usage_reg: '&cUsage: /register <password> <ConfirmPassword>' usage_reg: '&cUsage: /register <password> <ConfirmPassword>'
max_reg: '&cYou have exceeded the maximum number of registrations for your connection!' max_reg: '&cYou have exceeded the maximum number of registrations for your connection!'
no_perm: '&4You don''t have the permission to perform this action!' no_perm: '&4You don''t have the permission to perform this action!'
error: '&4An unexpected error occurred, please contact an Administrator!' error: '&4An unexpected error occurred, please contact an administrator!'
login_msg: '&cPlease, login with the command "/login <password>"' login_msg: '&cPlease, login with the command "/login <password>"'
reg_msg: '&3Please, register to the server with the command "/register <password> <ConfirmPassword>"' reg_msg: '&3Please, register to the server with the command "/register <password> <ConfirmPassword>"'
reg_email_msg: '&3Please, register to the server with the command "/register <email> <confirmEmail>"' reg_email_msg: '&3Please, register to the server with the command "/register <email> <confirmEmail>"'
@ -39,20 +39,20 @@ regex: '&4Your username contains illegal characters. Allowed chars: REG_EX'
add_email: '&3Please add your email to your account with the command "/email add <yourEmail> <confirmEmail>"' add_email: '&3Please add your email to your account with the command "/email add <yourEmail> <confirmEmail>"'
recovery_email: '&3Forgot your password? Please use the command "/email recovery <yourEmail>"' recovery_email: '&3Forgot your password? Please use the command "/email recovery <yourEmail>"'
usage_captcha: '&3To login you have to solve a captcha code, please use the command "/captcha <theCaptcha>"' usage_captcha: '&3To login you have to solve a captcha code, please use the command "/captcha <theCaptcha>"'
wrong_captcha: '&cWrong Captcha, please type "/captcha THE_CAPTCHA" into the chat!' wrong_captcha: '&cWrong captcha, please type "/captcha THE_CAPTCHA" into the chat!'
valid_captcha: '&2Captcha code solved correctly!' valid_captcha: '&2Captcha code solved correctly!'
kick_forvip: '&3A VIP Player has joined the server when it was full!' kick_forvip: '&3A VIP player has joined the server when it was full!'
kick_fullserver: '&4The server is full, try again later!' kick_fullserver: '&4The server is full, try again later!'
usage_email_add: '&cUsage: /email add <email> <confirmEmail>' usage_email_add: '&cUsage: /email add <email> <confirmEmail>'
usage_email_change: '&cUsage: /email change <oldEmail> <newEmail>' usage_email_change: '&cUsage: /email change <oldEmail> <newEmail>'
usage_email_recovery: '&cUsage: /email recovery <Email>' usage_email_recovery: '&cUsage: /email recovery <Email>'
new_email_invalid: '&cInvalid New Email, try again!' new_email_invalid: '&cInvalid new email, try again!'
old_email_invalid: '&cInvalid Old Email, try again!' old_email_invalid: '&cInvalid old email, try again!'
email_invalid: '&cInvalid Email address, try again!' email_invalid: '&cInvalid email address, try again!'
email_added: '&2Email address successfully added to your account!' email_added: '&2Email address successfully added to your account!'
email_confirm: '&cPlease confirm your email address!' email_confirm: '&cPlease confirm your email address!'
email_changed: '&2Email address changed correctly!' email_changed: '&2Email address changed correctly!'
email_send: '&2Recovery email sent correctly! Check your email inbox!' email_send: '&2Recovery email sent successfully! Please check your email inbox!'
email_exists: '&cA recovery email was already sent! You can discard it and send a new one using the command below:' email_exists: '&cA recovery email was already sent! You can discard it and send a new one using the command below:'
country_banned: '&4Your country is banned from this server!' country_banned: '&4Your country is banned from this server!'
antibot_auto_enabled: '&4[AntiBotService] AntiBot enabled due to the huge number of connections!' antibot_auto_enabled: '&4[AntiBotService] AntiBot enabled due to the huge number of connections!'

View File

@ -1,11 +1,15 @@
package messages; package messages;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import utils.FileUtils; import utils.FileUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -22,6 +26,7 @@ public class MessageFileVerifier {
private final Set<String> unknownKeys = new HashSet<>(); private final Set<String> unknownKeys = new HashSet<>();
// Map with the missing key and a boolean indicating whether or not it was added to the file by this object // Map with the missing key and a boolean indicating whether or not it was added to the file by this object
private final Map<String, Boolean> missingKeys = new HashMap<>(); private final Map<String, Boolean> missingKeys = new HashMap<>();
private final Multimap<String, String> missingTags = HashMultimap.create();
/** /**
* Create a verifier that verifies the given messages file. * Create a verifier that verifies the given messages file.
@ -53,8 +58,17 @@ public class MessageFileVerifier {
return missingKeys; return missingKeys;
} }
/**
* Return the collection of tags the message key defines that aren't present in the read line.
*
* @return Collection of missing tags per message key. Key = message key, value = missing tag.
*/
public Multimap<String, String> getMissingTags() {
return missingTags;
}
private void verifyKeys() { private void verifyKeys() {
Set<String> messageKeys = getAllMessageKeys(); List<MessageKey> messageKeys = getAllMessageKeys();
List<String> fileLines = FileUtils.readLinesFromFile(messagesFile); List<String> fileLines = FileUtils.readLinesFromFile(messagesFile);
for (String line : fileLines) { for (String line : fileLines) {
// Skip comments and empty lines // Skip comments and empty lines
@ -64,22 +78,38 @@ public class MessageFileVerifier {
} }
// All keys that remain are keys that are absent in the file // All keys that remain are keys that are absent in the file
for (String missingKey : messageKeys) { for (MessageKey missingKey : messageKeys) {
missingKeys.put(missingKey, false); missingKeys.put(missingKey.getKey(), false);
} }
} }
private void processKeyInFile(String line, Set<String> messageKeys) { private void processKeyInFile(String line, List<MessageKey> messageKeys) {
if (line.indexOf(':') == -1) { if (line.indexOf(':') == -1) {
System.out.println("Skipping line in unknown format: '" + line + "'"); System.out.println("Skipping line in unknown format: '" + line + "'");
return; return;
} }
final String key = line.substring(0, line.indexOf(':')); final String readKey = line.substring(0, line.indexOf(':'));
if (messageKeys.contains(key)) { boolean foundKey = false;
messageKeys.remove(key); for (Iterator<MessageKey> it = messageKeys.iterator(); it.hasNext(); ) {
} else { MessageKey messageKey = it.next();
unknownKeys.add(key); if (messageKey.getKey().equals(readKey)) {
checkTagsInMessage(readKey, line.substring(line.indexOf(':')), messageKey.getTags());
it.remove();
foundKey = true;
break;
}
}
if (!foundKey) {
unknownKeys.add(readKey);
}
}
private void checkTagsInMessage(String key, String message, String[] tags) {
for (String tag : tags) {
if (!message.contains(tag)) {
missingTags.put(key, tag);
}
} }
} }
@ -105,11 +135,7 @@ public class MessageFileVerifier {
FileUtils.appendToFile(messagesFile, sb.toString()); FileUtils.appendToFile(messagesFile, sb.toString());
} }
private static Set<String> getAllMessageKeys() { private static List<MessageKey> getAllMessageKeys() {
Set<String> messageKeys = new HashSet<>(MessageKey.values().length); return new ArrayList<>(Arrays.asList(MessageKey.values()));
for (MessageKey key : MessageKey.values()) {
messageKeys.add(key.getKey());
}
return messageKeys;
} }
} }

View File

@ -1,5 +1,6 @@
package messages; package messages;
import com.google.common.collect.Multimap;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import utils.FileUtils; import utils.FileUtils;
import utils.ToolTask; import utils.ToolTask;
@ -84,6 +85,11 @@ public final class VerifyMessagesTask implements ToolTask {
if (!unknownKeys.isEmpty()) { if (!unknownKeys.isEmpty()) {
System.out.println(" Unknown keys: " + unknownKeys); System.out.println(" Unknown keys: " + unknownKeys);
} }
Multimap<String, String> missingTags = verifier.getMissingTags();
for (Map.Entry<String, String> entry : missingTags.entries()) {
System.out.println(" Missing tag '" + entry.getValue() + "' in entry with key '" + entry.getKey() + "'");
}
} }
private static void verifyFileAndAddKeys(MessageFileVerifier verifier, Map<String, String> defaultMessages) { private static void verifyFileAndAddKeys(MessageFileVerifier verifier, Map<String, String> defaultMessages) {
@ -106,6 +112,11 @@ public final class VerifyMessagesTask implements ToolTask {
if (!unknownKeys.isEmpty()) { if (!unknownKeys.isEmpty()) {
System.out.println(" Unknown keys: " + unknownKeys); System.out.println(" Unknown keys: " + unknownKeys);
} }
Multimap<String, String> missingTags = verifier.getMissingTags();
for (Map.Entry<String, String> entry : missingTags.entries()) {
System.out.println(" Missing tag '" + entry.getValue() + "' in entry with key '" + entry.getKey() + "'");
}
} }
private static Map<String, String> constructDefaultMessages() { private static Map<String, String> constructDefaultMessages() {