From d34c5954b9bf296288dd1d69693968d983f15a2e Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 16 Dec 2018 00:11:35 +0200 Subject: [PATCH] [#844] Quoted String no longer double quoted - 'Something' no longer changed to "'Something'" on config update. - Added failing tests for rest of the problems. --- .../system/settings/changes/ConfigChange.java | 8 +- .../settings/changes/ConfigChangeTest.java | 128 ++++++++++++++++++ 2 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/system/settings/changes/ConfigChangeTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/changes/ConfigChange.java b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/changes/ConfigChange.java index 30b5979fc..5da331893 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/settings/changes/ConfigChange.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/settings/changes/ConfigChange.java @@ -49,8 +49,8 @@ public interface ConfigChange { ConfigNode newNode = config.getConfigNode(newPath); ConfigNode oldNode = config.getConfigNode(oldPath); newNode.copyDefaults(oldNode); - newNode.set(oldNode.getValue()); - super.apply(config); + newNode.set(oldNode.getString()); + removeNode(oldNode); } @Override @@ -97,6 +97,10 @@ public interface ConfigChange { @Override public synchronized void apply(Config config) { ConfigNode node = config.getConfigNode(oldPath); + removeNode(node); + } + + void removeNode(ConfigNode node) { ConfigNode parent = node.getParent(); String key = node.getKey(false); parent.getChildren().remove(key); diff --git a/Plan/common/src/test/java/com/djrapitops/plan/system/settings/changes/ConfigChangeTest.java b/Plan/common/src/test/java/com/djrapitops/plan/system/settings/changes/ConfigChangeTest.java new file mode 100644 index 000000000..d590422a2 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/system/settings/changes/ConfigChangeTest.java @@ -0,0 +1,128 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.system.settings.changes; + +import com.djrapitops.plugin.config.Config; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; +import org.junitpioneer.jupiter.TempDirectory; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.nio.file.Path; +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.*; + +@RunWith(JUnitPlatform.class) +@ExtendWith(MockitoExtension.class) +@ExtendWith(TempDirectory.class) +class ConfigChangeTest { + + private Path temporaryFolder; + + private Config config; + + @BeforeEach + void prepareTemporaryFolder(@TempDirectory.TempDir Path temporaryFolder) { + this.temporaryFolder = temporaryFolder; + } + + private Config prepareConfig(String withValue) { + return new Config(temporaryFolder.resolve("config.yml").toFile(), Collections.singletonList(withValue)); + } + + @Test + void moveChangeRecognizesItHasNotBeenApplied() { + config = prepareConfig("Test: 'value'"); + + assertFalse(new ConfigChange.Moved("Test", "MovedTo").hasBeenApplied(config)); + } + + @Test + void moveChangeRecognizesItHasBeenApplied() { + config = prepareConfig("Test: 'value'"); + + ConfigChange change = new ConfigChange.Moved("Test", "MovedTo"); + change.apply(config); + + assertFalse(config.getChildren().containsKey("Test")); + assertFalse(config.contains("Test"), "Old node was not removed"); + assertTrue(config.contains("MovedTo"), "New node was not created"); + assertTrue(change.hasBeenApplied(config), "Did not recognize it has been applied"); + } + + @Test + void stringSettingWithQuotesIsMovedAsString() { + config = prepareConfig("Test: 'value'"); + + new ConfigChange.Moved("Test", "MovedTo").apply(config); + + assertFalse(config.contains("Test"), "Old node was not removed"); + assertTrue(config.contains("MovedTo"), "New node was not created"); + String result = config.getConfigNode("MovedTo").getValue(); + assertEquals("value", result); + } + + @Test + void stringSettingWithDoubleQuotesIsMovedAsString() { + config = prepareConfig("Test: \"value\""); + + new ConfigChange.Moved("Test", "MovedTo").apply(config); + + assertFalse(config.contains("Test"), "Old node was not removed"); + assertTrue(config.contains("MovedTo"), "New node was not created"); + String result = config.getConfigNode("MovedTo").getValue(); + assertEquals("value", result); + } + + @Test + void stringSettingWithQuotedDoubleQuotesIsMovedAsString() { + config = prepareConfig("Test: '\"value\"'"); + + new ConfigChange.Moved("Test", "MovedTo").apply(config); + + assertFalse(config.contains("Test"), "Old node was not removed"); + assertTrue(config.contains("MovedTo"), "New node was not created"); + String result = config.getConfigNode("MovedTo").getValue(); + assertEquals("\"value\"", result); + } + + @Test + void stringSettingWithQuotedQuotesIsMovedAsString() { + config = prepareConfig("Test: \"'value'\""); + + new ConfigChange.Moved("Test", "MovedTo").apply(config); + + assertFalse(config.contains("Test"), "Old node was not removed"); + assertTrue(config.contains("MovedTo"), "New node was not created"); + String result = config.getConfigNode("MovedTo").getValue(); + assertEquals("'value'", result); + } + + @Test + void removedSettingIsNotPresent() { + config = prepareConfig("Test: \"value\""); + + new ConfigChange.Removed("Test").apply(config); + + assertFalse(config.contains("Test"), "Old node was not removed"); + } + +} \ No newline at end of file