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)
This commit is contained in:
ljacqu 2016-09-17 10:56:30 +02:00
parent 20fdc3693a
commit f804b528e5
9 changed files with 78 additions and 45 deletions

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.cache.backup; package fr.xephi.authme.cache.backup;
import com.google.common.base.Charsets;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -26,6 +25,7 @@ import javax.inject.Inject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
/** /**
* Class used to store player's data (OP, flying, speed, position) to disk. * Class used to store player's data (OP, flying, speed, position) to disk.
@ -71,7 +71,7 @@ public class PlayerDataStorage {
} }
try { try {
String str = Files.toString(file, Charsets.UTF_8); String str = Files.toString(file, StandardCharsets.UTF_8);
return gson.fromJson(str, PlayerData.class); return gson.fromJson(str, PlayerData.class);
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.logException("Could not read player data on disk for '" + player.getName() + "'", 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"); File file = new File(cacheDir, id + File.separator + "data.json");
Files.createParentDirs(file); Files.createParentDirs(file);
Files.touch(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) { } catch (IOException e) {
ConsoleLogger.logException("Failed to write " + player.getName() + " data.", e); ConsoleLogger.logException("Failed to write " + player.getName() + " data.", e);
} }

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.security.crypts; package fr.xephi.authme.security.crypts;
import com.google.common.base.Charsets;
import fr.xephi.authme.security.HashUtils; import fr.xephi.authme.security.HashUtils;
import fr.xephi.authme.security.MessageDigestAlgorithm; import fr.xephi.authme.security.MessageDigestAlgorithm;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
public class CRAZYCRYPT1 extends UsernameSaltMethod { public class CRAZYCRYPT1 extends UsernameSaltMethod {
@ -24,7 +24,7 @@ public class CRAZYCRYPT1 extends UsernameSaltMethod {
public HashedPassword computeHash(String password, String name) { public HashedPassword computeHash(String password, String name) {
final String text = "ÜÄaeut//&/=I " + password + "7421€547" + name + "__+IÄIH§%NK " + password; final String text = "ÜÄaeut//&/=I " + password + "7421€547" + name + "__+IÄIH§%NK " + password;
final MessageDigest md = HashUtils.getDigest(MessageDigestAlgorithm.SHA512); 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())); return new HashedPassword(byteArrayToHexString(md.digest()));
} }

View File

@ -4,7 +4,6 @@ import com.github.authme.configme.SettingsManager;
import com.github.authme.configme.knownproperties.PropertyEntry; import com.github.authme.configme.knownproperties.PropertyEntry;
import com.github.authme.configme.migration.MigrationService; import com.github.authme.configme.migration.MigrationService;
import com.github.authme.configme.resource.PropertyResource; import com.github.authme.configme.resource.PropertyResource;
import com.google.common.base.Charsets;
import com.google.common.io.Files; import com.google.common.io.Files;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
@ -12,6 +11,7 @@ import fr.xephi.authme.util.StringUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import static fr.xephi.authme.util.FileUtils.copyFileFromResource; import static fr.xephi.authme.util.FileUtils.copyFileFromResource;
@ -133,7 +133,7 @@ public class Settings extends SettingsManager {
final File file = new File(pluginFolder, filename); final File file = new File(pluginFolder, filename);
if (copyFileFromResource(file, filename)) { if (copyFileFromResource(file, filename)) {
try { try {
return Files.toString(file, Charsets.UTF_8); return Files.toString(file, StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.logException("Failed to read file '" + filename + "':", e); ConsoleLogger.logException("Failed to read file '" + filename + "':", e);
} }

View File

@ -1,6 +1,5 @@
package fr.xephi.authme; package fr.xephi.authme;
import com.google.common.base.Charsets;
import fr.xephi.authme.output.LogLevel; import fr.xephi.authme.output.LogLevel;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
@ -18,6 +17,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.List; import java.util.List;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -91,7 +91,7 @@ public class ConsoleLoggerTest {
// then // then
verify(logger, times(2)).info(anyString()); verify(logger, times(2)).info(anyString());
verifyNoMoreInteractions(logger); verifyNoMoreInteractions(logger);
List<String> loggedLines = Files.readAllLines(logFile.toPath(), Charsets.UTF_8); List<String> loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8);
assertThat(loggedLines, hasSize(2)); assertThat(loggedLines, hasSize(2));
assertThat(loggedLines.get(0), containsString("[FINE] Logging a FINE message")); assertThat(loggedLines.get(0), containsString("[FINE] Logging a FINE message"));
assertThat(loggedLines.get(1), containsString("[INFO] This is an INFO 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).info("Info text");
verify(logger).warning("Exception occurred: [IllegalStateException]: Test exception message"); verify(logger).warning("Exception occurred: [IllegalStateException]: Test exception message");
verifyNoMoreInteractions(logger); verifyNoMoreInteractions(logger);
List<String> loggedLines = Files.readAllLines(logFile.toPath(), Charsets.UTF_8); List<String> loggedLines = Files.readAllLines(logFile.toPath(), StandardCharsets.UTF_8);
assertThat(loggedLines.size(), greaterThan(3)); assertThat(loggedLines.size(), greaterThan(3));
assertThat(loggedLines.get(0), containsString("[INFO] Info text")); assertThat(loggedLines.get(0), containsString("[INFO] Info text"));
assertThat(loggedLines.get(1), assertThat(loggedLines.get(1),

View File

@ -5,7 +5,6 @@ import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import tools.utils.FileUtils; import tools.utils.FileUtils;
@ -14,7 +13,6 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -29,7 +27,7 @@ public class MessageFileVerifier {
private final String messagesFile; private final String messagesFile;
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 List<MissingKey> missingKeys = new ArrayList<>();
private final Multimap<String, String> missingTags = HashMultimap.create(); private final Multimap<String, String> missingTags = HashMultimap.create();
/** /**
@ -58,7 +56,7 @@ public class MessageFileVerifier {
* *
* @return The list of missing keys in the file * @return The list of missing keys in the file
*/ */
public Map<String, Boolean> getMissingKeys() { public List<MissingKey> getMissingKeys() {
return missingKeys; return missingKeys;
} }
@ -80,7 +78,7 @@ public class MessageFileVerifier {
if (configuration.isString(key)) { if (configuration.isString(key)) {
checkTagsInMessage(messageKey, configuration.getString(key)); checkTagsInMessage(messageKey, configuration.getString(key));
} else { } else {
missingKeys.put(key, false); missingKeys.add(new MissingKey(key));
} }
} }
@ -109,26 +107,27 @@ public class MessageFileVerifier {
final List<String> fileLines = new ArrayList<>( final List<String> fileLines = new ArrayList<>(
Arrays.asList(FileUtils.readFromFile(messagesFile).split("\\n"))); Arrays.asList(FileUtils.readFromFile(messagesFile).split("\\n")));
List<String> keysToAdd = new ArrayList<>(); List<MissingKey> keysToAdd = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : missingKeys.entrySet()) { for (MissingKey entry : missingKeys) {
final String key = entry.getKey(); final String key = entry.getKey();
if (Boolean.FALSE.equals(entry.getValue()) && defaultMessages.get(key) != null) { if (!entry.getWasAdded() && defaultMessages.get(key) != null) {
keysToAdd.add(key); keysToAdd.add(entry);
} }
} }
// Add missing keys as comments to the bottom of the file // Add missing keys as comments to the bottom of the file
for (String keyToAdd : keysToAdd) { for (MissingKey keyToAdd : keysToAdd) {
int indexOfComment = Iterables.indexOf(fileLines, isCommentFor(keyToAdd)); final String key = keyToAdd.getKey();
int indexOfComment = Iterables.indexOf(fileLines, isCommentFor(key));
if (indexOfComment != -1) { if (indexOfComment != -1) {
// Comment for keyToAdd already exists, so remove it since we're going to add it // Comment for keyToAdd already exists, so remove it since we're going to add it
fileLines.remove(indexOfComment); fileLines.remove(indexOfComment);
} }
String comment = commentForKey(keyToAdd) + "'" + String comment = commentForKey(key) + "'" +
defaultMessages.getString(keyToAdd).replace("'", "''") + "'"; defaultMessages.getString(key).replace("'", "''") + "'";
fileLines.add(comment); fileLines.add(comment);
missingKeys.put(keyToAdd, Boolean.TRUE); keyToAdd.setWasAdded(true);
} }
// Add a comment above messages missing a tag // Add a comment above messages missing a tag
@ -137,7 +136,7 @@ public class MessageFileVerifier {
addCommentForMissingTags(fileLines, key, entry.getValue()); 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"; String tagWord = tags.size() > 1 ? "tags" : "tag";
fileLines.add(indexForComment, commentForKey(key) 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) { private static String commentForKey(String key) {

View File

@ -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;
}
}

View File

@ -15,6 +15,7 @@ import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.lang.String.format; import static java.lang.String.format;
@ -77,9 +78,9 @@ public final class VerifyMessagesTask implements ToolTask {
} }
private static void verifyFile(MessageFileVerifier verifier) { private static void verifyFile(MessageFileVerifier verifier) {
Map<String, Boolean> missingKeys = verifier.getMissingKeys(); List<MissingKey> missingKeys = verifier.getMissingKeys();
if (!missingKeys.isEmpty()) { if (!missingKeys.isEmpty()) {
System.out.println(" Missing keys: " + missingKeys.keySet()); System.out.println(" Missing keys: " + missingKeys);
} }
Set<String> unknownKeys = verifier.getUnknownKeys(); Set<String> unknownKeys = verifier.getUnknownKeys();
@ -94,13 +95,13 @@ public final class VerifyMessagesTask implements ToolTask {
} }
public static void verifyFileAndAddKeys(MessageFileVerifier verifier, FileConfiguration defaultMessages) { public static void verifyFileAndAddKeys(MessageFileVerifier verifier, FileConfiguration defaultMessages) {
Map<String, Boolean> missingKeys = verifier.getMissingKeys(); List<MissingKey> missingKeys = verifier.getMissingKeys();
if (!missingKeys.isEmpty() || !verifier.getMissingTags().isEmpty()) { if (!missingKeys.isEmpty() || !verifier.getMissingTags().isEmpty()) {
verifier.addMissingKeys(defaultMessages); verifier.addMissingKeys(defaultMessages);
List<String> addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys); List<String> addedKeys = getMissingKeysWithAdded(missingKeys, true);
System.out.println(" Added missing keys " + addedKeys); System.out.println(" Added missing keys " + addedKeys);
List<String> unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys); List<String> unsuccessfulKeys = getMissingKeysWithAdded(missingKeys, false);
if (!unsuccessfulKeys.isEmpty()) { if (!unsuccessfulKeys.isEmpty()) {
System.err.println(" Warning! Could not add all missing keys (problem with loading " + System.err.println(" Warning! Could not add all missing keys (problem with loading " +
"default messages?)"); "default messages?)");
@ -119,14 +120,11 @@ public final class VerifyMessagesTask implements ToolTask {
} }
} }
private static <K, V> List<K> getKeysWithValue(V value, Map<K, V> map) { private static List<String> getMissingKeysWithAdded(List<MissingKey> missingKeys, boolean wasAdded) {
List<K> result = new ArrayList<>(); return missingKeys.stream()
for (Map.Entry<K, V> entry : map.entrySet()) { .filter(e -> e.getWasAdded() == wasAdded)
if (value.equals(entry.getValue())) { .map(MissingKey::getKey)
result.add(entry.getKey()); .collect(Collectors.toList());
}
}
return result;
} }
private static List<File> getMessagesFiles() { private static List<File> getMessagesFiles() {

View File

@ -1,6 +1,5 @@
package tools.messages.translation; package tools.messages.translation;
import com.google.common.base.Charsets;
import com.google.common.io.Resources; import com.google.common.io.Resources;
import com.google.gson.Gson; import com.google.gson.Gson;
import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.output.MessageKey;
@ -15,6 +14,7 @@ import tools.utils.ToolsConstants;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashSet; import java.util.HashSet;
import java.util.Scanner; import java.util.Scanner;
import java.util.Set; import java.util.Set;
@ -56,7 +56,7 @@ public class ImportMessagesTask implements ToolTask {
private LanguageExport getLanguageExportFromUrl(String location) { private LanguageExport getLanguageExportFromUrl(String location) {
try { try {
URL url = new URL(location); 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); return gson.fromJson(json, LanguageExport.class);
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);

View File

@ -1,8 +1,7 @@
package tools.utils; package tools.utils;
import com.google.common.base.Charsets;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
@ -40,7 +39,7 @@ public final class FileUtils {
public static String readFromFile(String file) { public static String readFromFile(String file) {
try { 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) { } catch (IOException e) {
throw new UnsupportedOperationException("Could not read from file '" + file + "'", e); throw new UnsupportedOperationException("Could not read from file '" + file + "'", e);
} }
@ -48,7 +47,7 @@ public final class FileUtils {
public static List<String> readLinesFromFile(String file) { public static List<String> readLinesFromFile(String file) {
try { try {
return Files.readAllLines(Paths.get(file), Charsets.UTF_8); return Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
throw new UnsupportedOperationException("Could not read from file '" + file + "'", e); throw new UnsupportedOperationException("Could not read from file '" + file + "'", e);
} }