From f804b528e5a5a3104953a9e2ad2f1e03baccf314 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 17 Sep 2016 10:56:30 +0200 Subject: [PATCH] Change message verification task to add todo comments in same order - Make verification task add todo comments to YML files in the same order as the MessageKey enum - Use DefaultCharsets everywhere instead of Guava's Charsets class (thanks to DNx5) --- .../cache/backup/PlayerDataStorage.java | 6 +-- .../authme/security/crypts/CRAZYCRYPT1.java | 4 +- .../fr/xephi/authme/settings/Settings.java | 4 +- .../fr/xephi/authme/ConsoleLoggerTest.java | 6 +-- .../tools/messages/MessageFileVerifier.java | 31 ++++++++-------- src/test/java/tools/messages/MissingKey.java | 37 +++++++++++++++++++ .../tools/messages/VerifyMessagesTask.java | 24 ++++++------ .../translation/ImportMessagesTask.java | 4 +- src/test/java/tools/utils/FileUtils.java | 7 ++-- 9 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 src/test/java/tools/messages/MissingKey.java diff --git a/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java b/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java index 73c96e219..e10e88efa 100644 --- a/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java +++ b/src/main/java/fr/xephi/authme/cache/backup/PlayerDataStorage.java @@ -1,6 +1,5 @@ package fr.xephi.authme.cache.backup; -import com.google.common.base.Charsets; import com.google.common.io.Files; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -26,6 +25,7 @@ import javax.inject.Inject; import java.io.File; import java.io.IOException; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; /** * Class used to store player's data (OP, flying, speed, position) to disk. @@ -71,7 +71,7 @@ public class PlayerDataStorage { } try { - String str = Files.toString(file, Charsets.UTF_8); + String str = Files.toString(file, StandardCharsets.UTF_8); return gson.fromJson(str, PlayerData.class); } catch (IOException e) { ConsoleLogger.logException("Could not read player data on disk for '" + player.getName() + "'", e); @@ -100,7 +100,7 @@ public class PlayerDataStorage { File file = new File(cacheDir, id + File.separator + "data.json"); Files.createParentDirs(file); Files.touch(file); - Files.write(gson.toJson(playerData), file, Charsets.UTF_8); + Files.write(gson.toJson(playerData), file, StandardCharsets.UTF_8); } catch (IOException e) { ConsoleLogger.logException("Failed to write " + player.getName() + " data.", e); } diff --git a/src/main/java/fr/xephi/authme/security/crypts/CRAZYCRYPT1.java b/src/main/java/fr/xephi/authme/security/crypts/CRAZYCRYPT1.java index d9ec045f4..595842314 100644 --- a/src/main/java/fr/xephi/authme/security/crypts/CRAZYCRYPT1.java +++ b/src/main/java/fr/xephi/authme/security/crypts/CRAZYCRYPT1.java @@ -1,9 +1,9 @@ package fr.xephi.authme.security.crypts; -import com.google.common.base.Charsets; import fr.xephi.authme.security.HashUtils; import fr.xephi.authme.security.MessageDigestAlgorithm; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; public class CRAZYCRYPT1 extends UsernameSaltMethod { @@ -24,7 +24,7 @@ public class CRAZYCRYPT1 extends UsernameSaltMethod { public HashedPassword computeHash(String password, String name) { final String text = "ÜÄaeut//&/=I " + password + "7421€547" + name + "__+IÄIH§%NK " + password; final MessageDigest md = HashUtils.getDigest(MessageDigestAlgorithm.SHA512); - md.update(text.getBytes(Charsets.UTF_8), 0, text.length()); + md.update(text.getBytes(StandardCharsets.UTF_8), 0, text.length()); return new HashedPassword(byteArrayToHexString(md.digest())); } diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 9a1cb5fa5..fc3e929cd 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -4,7 +4,6 @@ import com.github.authme.configme.SettingsManager; import com.github.authme.configme.knownproperties.PropertyEntry; import com.github.authme.configme.migration.MigrationService; import com.github.authme.configme.resource.PropertyResource; -import com.google.common.base.Charsets; import com.google.common.io.Files; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.settings.properties.PluginSettings; @@ -12,6 +11,7 @@ import fr.xephi.authme.util.StringUtils; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.List; import static fr.xephi.authme.util.FileUtils.copyFileFromResource; @@ -133,7 +133,7 @@ public class Settings extends SettingsManager { final File file = new File(pluginFolder, filename); if (copyFileFromResource(file, filename)) { try { - return Files.toString(file, Charsets.UTF_8); + return Files.toString(file, StandardCharsets.UTF_8); } catch (IOException e) { ConsoleLogger.logException("Failed to read file '" + filename + "':", e); } diff --git a/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java b/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java index 04aee4198..8d331f9cf 100644 --- a/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java +++ b/src/test/java/fr/xephi/authme/ConsoleLoggerTest.java @@ -1,6 +1,5 @@ package fr.xephi.authme; -import com.google.common.base.Charsets; import fr.xephi.authme.output.LogLevel; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.properties.PluginSettings; @@ -18,6 +17,7 @@ import org.mockito.runners.MockitoJUnitRunner; import java.io.File; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.List; import java.util.logging.Logger; @@ -91,7 +91,7 @@ public class ConsoleLoggerTest { // then verify(logger, times(2)).info(anyString()); verifyNoMoreInteractions(logger); - List loggedLines = Files.readAllLines(logFile.toPath(), Charsets.UTF_8); + List loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8); assertThat(loggedLines, hasSize(2)); assertThat(loggedLines.get(0), containsString("[FINE] Logging a FINE message")); assertThat(loggedLines.get(1), containsString("[INFO] This is an INFO message")); @@ -129,7 +129,7 @@ public class ConsoleLoggerTest { verify(logger).info("Info text"); verify(logger).warning("Exception occurred: [IllegalStateException]: Test exception message"); verifyNoMoreInteractions(logger); - List loggedLines = Files.readAllLines(logFile.toPath(), Charsets.UTF_8); + List loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8); assertThat(loggedLines.size(), greaterThan(3)); assertThat(loggedLines.get(0), containsString("[INFO] Info text")); assertThat(loggedLines.get(1), diff --git a/src/test/java/tools/messages/MessageFileVerifier.java b/src/test/java/tools/messages/MessageFileVerifier.java index 1157284f1..a3da7ac9f 100644 --- a/src/test/java/tools/messages/MessageFileVerifier.java +++ b/src/test/java/tools/messages/MessageFileVerifier.java @@ -5,7 +5,6 @@ import com.google.common.collect.HashMultimap; import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import fr.xephi.authme.output.MessageKey; -import fr.xephi.authme.util.StringUtils; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import tools.utils.FileUtils; @@ -14,7 +13,6 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -29,7 +27,7 @@ public class MessageFileVerifier { private final String messagesFile; private final Set unknownKeys = new HashSet<>(); // Map with the missing key and a boolean indicating whether or not it was added to the file by this object - private final Map missingKeys = new HashMap<>(); + private final List missingKeys = new ArrayList<>(); private final Multimap missingTags = HashMultimap.create(); /** @@ -58,7 +56,7 @@ public class MessageFileVerifier { * * @return The list of missing keys in the file */ - public Map getMissingKeys() { + public List getMissingKeys() { return missingKeys; } @@ -80,7 +78,7 @@ public class MessageFileVerifier { if (configuration.isString(key)) { checkTagsInMessage(messageKey, configuration.getString(key)); } else { - missingKeys.put(key, false); + missingKeys.add(new MissingKey(key)); } } @@ -109,26 +107,27 @@ public class MessageFileVerifier { final List fileLines = new ArrayList<>( Arrays.asList(FileUtils.readFromFile(messagesFile).split("\\n"))); - List keysToAdd = new ArrayList<>(); - for (Map.Entry entry : missingKeys.entrySet()) { + List keysToAdd = new ArrayList<>(); + for (MissingKey entry : missingKeys) { final String key = entry.getKey(); - if (Boolean.FALSE.equals(entry.getValue()) && defaultMessages.get(key) != null) { - keysToAdd.add(key); + if (!entry.getWasAdded() && defaultMessages.get(key) != null) { + keysToAdd.add(entry); } } // Add missing keys as comments to the bottom of the file - for (String keyToAdd : keysToAdd) { - int indexOfComment = Iterables.indexOf(fileLines, isCommentFor(keyToAdd)); + for (MissingKey keyToAdd : keysToAdd) { + final String key = keyToAdd.getKey(); + int indexOfComment = Iterables.indexOf(fileLines, isCommentFor(key)); if (indexOfComment != -1) { // Comment for keyToAdd already exists, so remove it since we're going to add it fileLines.remove(indexOfComment); } - String comment = commentForKey(keyToAdd) + "'" + - defaultMessages.getString(keyToAdd).replace("'", "''") + "'"; + String comment = commentForKey(key) + "'" + + defaultMessages.getString(key).replace("'", "''") + "'"; fileLines.add(comment); - missingKeys.put(keyToAdd, Boolean.TRUE); + keyToAdd.setWasAdded(true); } // Add a comment above messages missing a tag @@ -137,7 +136,7 @@ public class MessageFileVerifier { addCommentForMissingTags(fileLines, key, entry.getValue()); } - FileUtils.writeToFile(messagesFile, StringUtils.join("\n", fileLines)); + FileUtils.writeToFile(messagesFile, String.join("\n", fileLines)); } /** @@ -167,7 +166,7 @@ public class MessageFileVerifier { String tagWord = tags.size() > 1 ? "tags" : "tag"; fileLines.add(indexForComment, commentForKey(key) - + String.format("Missing %s %s", tagWord, StringUtils.join(", ", tags))); + + String.format("Missing %s %s", tagWord, String.join(", ", tags))); } private static String commentForKey(String key) { diff --git a/src/test/java/tools/messages/MissingKey.java b/src/test/java/tools/messages/MissingKey.java new file mode 100644 index 000000000..667768012 --- /dev/null +++ b/src/test/java/tools/messages/MissingKey.java @@ -0,0 +1,37 @@ +package tools.messages; + +/** + * Missing message key in a file. + */ +public class MissingKey { + + private final String key; + private boolean wasAdded; + + public MissingKey(String key) { + this.key = key; + } + + /** + * @return the message key that is missing + */ + public String getKey() { + return key; + } + + public void setWasAdded(boolean wasAdded) { + this.wasAdded = wasAdded; + } + + /** + * @return true if a comment was added to the file, false otherwise + */ + public boolean getWasAdded() { + return wasAdded; + } + + @Override + public String toString() { + return key; + } +} diff --git a/src/test/java/tools/messages/VerifyMessagesTask.java b/src/test/java/tools/messages/VerifyMessagesTask.java index de888be71..8ecf32e64 100644 --- a/src/test/java/tools/messages/VerifyMessagesTask.java +++ b/src/test/java/tools/messages/VerifyMessagesTask.java @@ -15,6 +15,7 @@ import java.util.Map; import java.util.Scanner; import java.util.Set; import java.util.regex.Pattern; +import java.util.stream.Collectors; import static java.lang.String.format; @@ -77,9 +78,9 @@ public final class VerifyMessagesTask implements ToolTask { } private static void verifyFile(MessageFileVerifier verifier) { - Map missingKeys = verifier.getMissingKeys(); + List missingKeys = verifier.getMissingKeys(); if (!missingKeys.isEmpty()) { - System.out.println(" Missing keys: " + missingKeys.keySet()); + System.out.println(" Missing keys: " + missingKeys); } Set unknownKeys = verifier.getUnknownKeys(); @@ -94,13 +95,13 @@ public final class VerifyMessagesTask implements ToolTask { } public static void verifyFileAndAddKeys(MessageFileVerifier verifier, FileConfiguration defaultMessages) { - Map missingKeys = verifier.getMissingKeys(); + List missingKeys = verifier.getMissingKeys(); if (!missingKeys.isEmpty() || !verifier.getMissingTags().isEmpty()) { verifier.addMissingKeys(defaultMessages); - List addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys); + List addedKeys = getMissingKeysWithAdded(missingKeys, true); System.out.println(" Added missing keys " + addedKeys); - List unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys); + List unsuccessfulKeys = getMissingKeysWithAdded(missingKeys, false); if (!unsuccessfulKeys.isEmpty()) { System.err.println(" Warning! Could not add all missing keys (problem with loading " + "default messages?)"); @@ -119,14 +120,11 @@ public final class VerifyMessagesTask implements ToolTask { } } - private static List getKeysWithValue(V value, Map map) { - List result = new ArrayList<>(); - for (Map.Entry entry : map.entrySet()) { - if (value.equals(entry.getValue())) { - result.add(entry.getKey()); - } - } - return result; + private static List getMissingKeysWithAdded(List missingKeys, boolean wasAdded) { + return missingKeys.stream() + .filter(e -> e.getWasAdded() == wasAdded) + .map(MissingKey::getKey) + .collect(Collectors.toList()); } private static List getMessagesFiles() { diff --git a/src/test/java/tools/messages/translation/ImportMessagesTask.java b/src/test/java/tools/messages/translation/ImportMessagesTask.java index ab087a94b..69e15c249 100644 --- a/src/test/java/tools/messages/translation/ImportMessagesTask.java +++ b/src/test/java/tools/messages/translation/ImportMessagesTask.java @@ -1,6 +1,5 @@ package tools.messages.translation; -import com.google.common.base.Charsets; import com.google.common.io.Resources; import com.google.gson.Gson; import fr.xephi.authme.output.MessageKey; @@ -15,6 +14,7 @@ import tools.utils.ToolsConstants; import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Scanner; import java.util.Set; @@ -56,7 +56,7 @@ public class ImportMessagesTask implements ToolTask { private LanguageExport getLanguageExportFromUrl(String location) { try { URL url = new URL(location); - String json = Resources.toString(url, Charsets.UTF_8); + String json = Resources.toString(url, StandardCharsets.UTF_8); return gson.fromJson(json, LanguageExport.class); } catch (IOException e) { throw new IllegalStateException(e); diff --git a/src/test/java/tools/utils/FileUtils.java b/src/test/java/tools/utils/FileUtils.java index 7049f94a0..fe9901de3 100644 --- a/src/test/java/tools/utils/FileUtils.java +++ b/src/test/java/tools/utils/FileUtils.java @@ -1,8 +1,7 @@ package tools.utils; -import com.google.common.base.Charsets; - import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; @@ -40,7 +39,7 @@ public final class FileUtils { public static String readFromFile(String file) { try { - return new String(Files.readAllBytes(Paths.get(file)), Charsets.UTF_8); + return new String(Files.readAllBytes(Paths.get(file)), StandardCharsets.UTF_8); } catch (IOException e) { throw new UnsupportedOperationException("Could not read from file '" + file + "'", e); } @@ -48,7 +47,7 @@ public final class FileUtils { public static List readLinesFromFile(String file) { try { - return Files.readAllLines(Paths.get(file), Charsets.UTF_8); + return Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8); } catch (IOException e) { throw new UnsupportedOperationException("Could not read from file '" + file + "'", e); }