#1467 Rearrange old keys migration so no entries get lost

- 'error' was an old entry but now we have multiple entries under 'error' (which is now a section), so we need to ensure that we migrate the old 'error' entry before the migration sets anything under that path
This commit is contained in:
ljacqu
2018-02-02 18:53:08 +01:00
parent f44353ed4c
commit 1d6d9eb764
34 changed files with 154 additions and 105 deletions
@@ -108,7 +108,7 @@ public class MessageUpdaterTest {
.collect(Collectors.toSet());
// when
Set<String> messageKeysFromConfigData = MessageUpdater.CONFIGURATION_DATA.getProperties().stream()
Set<String> messageKeysFromConfigData = MessageUpdater.getConfigurationData().getProperties().stream()
.map(Property::getPath)
.collect(Collectors.toSet());
@@ -125,7 +125,7 @@ public class MessageUpdaterTest {
// when
Map<String, String[]> comments = ReflectionTestUtils.getFieldValue(
ConfigurationData.class, MessageUpdater.CONFIGURATION_DATA, "sectionComments");
ConfigurationData.class, MessageUpdater.getConfigurationData(), "sectionComments");
// then
assertThat(comments.keySet(), equalTo(rootPaths));
@@ -0,0 +1,41 @@
package fr.xephi.authme.message.updater;
import fr.xephi.authme.message.MessageKey;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.Assert.fail;
/**
* Test for {@link OldMessageKeysMigrater}.
*/
public class OldMessageKeysMigraterTest {
@Test
public void shouldHasOldKeysThatAreNewParentsFirstInMap() {
// given
Set<String> parentPaths = collectParentPathsFromMessageKeys();
Set<String> encounteredParents = new HashSet<>();
// when / then
for (Map.Entry<MessageKey, String> entry : OldMessageKeysMigrater.KEYS_TO_OLD_PATH.entrySet()) {
if (parentPaths.contains(entry.getValue()) && encounteredParents.contains(entry.getValue())) {
fail("Entry migrating old path '" + entry.getValue()
+ "' should come before any new paths with it as parent");
}
String parent = entry.getKey().getKey().split("\\.")[0];
encounteredParents.add(parent);
}
}
private Set<String> collectParentPathsFromMessageKeys() {
return Arrays.stream(MessageKey.values())
.map(mk -> mk.getKey().split("\\.")[0])
.collect(Collectors.toSet());
}
}
@@ -25,7 +25,7 @@ import java.util.regex.Pattern;
* were at the top of the file and to separate comments by new lines (which ConfigMe
* currently doesn't support).
*/
public class MessagesFileWriter {
public final class MessagesFileWriter {
/** Marker used inside a text to signal that it should be a comment later on. */
private static final String COMMENT_MARKER = "::COMMENT::";
@@ -52,7 +52,7 @@ public class MessagesFileWriter {
// Create property resource with new defaults, save with ConfigMe for proper sections & comments
PropertyResource resource = createPropertyResourceWithCommentEntries();
new SettingsManager(resource, null, MessageUpdater.CONFIGURATION_DATA).save();
new SettingsManager(resource, null, MessageUpdater.getConfigurationData()).save();
// Go through the newly saved file and replace texts with comment marker to actual YAML comments
// and add initial comments back to the file
@@ -89,7 +89,7 @@ public class MessagesFileWriter {
* @return the first comment generated by ConfigMe (comment of the first root path)
*/
private static String getFirstCommentByConfigMe() {
ConfigurationData configurationData = MessageUpdater.CONFIGURATION_DATA;
ConfigurationData configurationData = MessageUpdater.getConfigurationData();
String firstRootPath = configurationData.getProperties().get(0).getPath().split("\\.")[0];
return "# " + configurationData.getCommentsForSection(firstRootPath)[0];
}
@@ -100,7 +100,7 @@ public class MessagesFileWriter {
*/
private PropertyResource createPropertyResourceWithCommentEntries() {
YamlFileResource resource = new MigraterYamlFileResource(file);
for (Property<?> property : MessageUpdater.CONFIGURATION_DATA.getProperties()) {
for (Property<?> property : MessageUpdater.getConfigurationData().getProperties()) {
String text = resource.getString(property.getPath());
if (text == null) {
resource.setValue(property.getPath(), COMMENT_MARKER + defaultFile.getString(property.getPath()));
@@ -132,6 +132,7 @@ public class MessagesFileWriter {
newLines.add(line);
}
}
newLines.add(""); // Makes sure file ends with new line
FileIoUtils.writeToFile(file.toPath(), String.join("\n", newLines));
}