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

View File

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

View File

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

View File

@ -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<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.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<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.get(0), containsString("[INFO] Info text"));
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.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<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
private final Map<String, Boolean> missingKeys = new HashMap<>();
private final List<MissingKey> missingKeys = new ArrayList<>();
private final Multimap<String, String> missingTags = HashMultimap.create();
/**
@ -58,7 +56,7 @@ public class MessageFileVerifier {
*
* @return The list of missing keys in the file
*/
public Map<String, Boolean> getMissingKeys() {
public List<MissingKey> 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<String> fileLines = new ArrayList<>(
Arrays.asList(FileUtils.readFromFile(messagesFile).split("\\n")));
List<String> keysToAdd = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : missingKeys.entrySet()) {
List<MissingKey> 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) {

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.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<String, Boolean> missingKeys = verifier.getMissingKeys();
List<MissingKey> missingKeys = verifier.getMissingKeys();
if (!missingKeys.isEmpty()) {
System.out.println(" Missing keys: " + missingKeys.keySet());
System.out.println(" Missing keys: " + missingKeys);
}
Set<String> unknownKeys = verifier.getUnknownKeys();
@ -94,13 +95,13 @@ public final class VerifyMessagesTask implements ToolTask {
}
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()) {
verifier.addMissingKeys(defaultMessages);
List<String> addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys);
List<String> addedKeys = getMissingKeysWithAdded(missingKeys, true);
System.out.println(" Added missing keys " + addedKeys);
List<String> unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys);
List<String> 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 <K, V> List<K> getKeysWithValue(V value, Map<K, V> map) {
List<K> result = new ArrayList<>();
for (Map.Entry<K, V> entry : map.entrySet()) {
if (value.equals(entry.getValue())) {
result.add(entry.getKey());
}
}
return result;
private static List<String> getMissingKeysWithAdded(List<MissingKey> missingKeys, boolean wasAdded) {
return missingKeys.stream()
.filter(e -> e.getWasAdded() == wasAdded)
.map(MissingKey::getKey)
.collect(Collectors.toList());
}
private static List<File> getMessagesFiles() {

View File

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

View File

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