#603 Various setting improvements

- Pass PropertyMap to settings class from the outside
- Fix tests not being reentrant due to real file writes
- Improve Node (internal tree for PropertyMap) interface
- Add code coverage for private constructors
This commit is contained in:
ljacqu
2016-03-14 20:32:32 +01:00
parent 374e2ff292
commit 3522a5b0c0
9 changed files with 161 additions and 60 deletions
+4 -1
View File
@@ -52,6 +52,8 @@ import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.PurgeSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import fr.xephi.authme.util.CollectionUtils;
import fr.xephi.authme.util.FileUtils;
import fr.xephi.authme.util.GeoLiteAPI;
@@ -464,8 +466,9 @@ public class AuthMe extends JavaPlugin {
private NewSetting createNewSetting() {
File configFile = new File(getDataFolder(), "config.yml");
PropertyMap properties = SettingsFieldRetriever.getAllPropertyFields();
return FileUtils.copyFileFromResource(configFile, "config.yml")
? new NewSetting(configFile, getDataFolder())
? new NewSetting(configFile, getDataFolder(), properties)
: null;
}
@@ -6,7 +6,6 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.properties.PluginSettings;
import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.SettingsFieldRetriever;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import fr.xephi.authme.util.CollectionUtils;
import fr.xephi.authme.util.StringUtils;
@@ -33,6 +32,7 @@ public class NewSetting {
private final File pluginFolder;
private final File configFile;
private final PropertyMap propertyMap;
private FileConfiguration configuration;
/** The file with the localized messages based on {@link PluginSettings#MESSAGES_LANGUAGE}. */
private File messagesFile;
@@ -45,10 +45,11 @@ public class NewSetting {
* @param configFile The configuration file
* @param pluginFolder The AuthMe plugin folder
*/
public NewSetting(File configFile, File pluginFolder) {
public NewSetting(File configFile, File pluginFolder, PropertyMap propertyMap) {
this.configuration = YamlConfiguration.loadConfiguration(configFile);
this.configFile = configFile;
this.pluginFolder = pluginFolder;
this.propertyMap = propertyMap;
validateAndLoadOptions();
}
@@ -64,9 +65,10 @@ public class NewSetting {
this.configuration = configuration;
this.configFile = configFile;
this.pluginFolder = new File("");
this.propertyMap = propertyMap;
if (propertyMap != null && SettingsMigrationService.checkAndMigrate(configuration, propertyMap, pluginFolder)) {
save(propertyMap);
if (propertyMap != null) {
validateAndLoadOptions();
}
}
@@ -92,13 +94,6 @@ public class NewSetting {
configuration.set(property.getPath(), value);
}
/**
* Save the config file. Use after migrating one or more settings.
*/
public void save() {
save(SettingsFieldRetriever.getAllPropertyFields());
}
/**
* Return the messages file based on the messages language config.
*
@@ -133,7 +128,10 @@ public class NewSetting {
validateAndLoadOptions();
}
private void save(PropertyMap propertyMap) {
/**
* Save the config file. Use after migrating one or more settings.
*/
public void save() {
try (FileWriter writer = new FileWriter(configFile)) {
Yaml simpleYaml = newYaml(false);
Yaml singleQuoteYaml = newYaml(true);
@@ -186,11 +184,10 @@ public class NewSetting {
}
private void validateAndLoadOptions() {
PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
if (SettingsMigrationService.checkAndMigrate(configuration, propertyMap, pluginFolder)) {
ConsoleLogger.info("Merged new config options");
ConsoleLogger.info("Please check your config.yml file for new settings!");
save(propertyMap);
save();
}
messagesFile = buildMessagesFile();
@@ -12,6 +12,6 @@ import java.lang.annotation.Target;
@Target(ElementType.FIELD)
public @interface Comment {
String[] value();
String[] value();
}
@@ -1,5 +1,7 @@
package fr.xephi.authme.settings.propertymap;
import fr.xephi.authme.ConsoleLogger;
import java.util.ArrayList;
import java.util.List;
@@ -37,14 +39,13 @@ final class Node {
}
/**
* Add a node to the root, creating any intermediary children that don't exist.
* Add a child node, creating any intermediary children that don't exist.
*
* @param root The root to add the path to
* @param fullPath The entire path of the node to add, separate by periods
*/
public static void addNode(Node root, String fullPath) {
public void addNode(String fullPath) {
String[] pathParts = fullPath.split("\\.");
Node parent = root;
Node parent = this;
for (String part : pathParts) {
Node child = parent.getChild(part);
if (child == null) {
@@ -59,17 +60,16 @@ final class Node {
* Compare two nodes by this class' sorting behavior (insertion order).
* Note that this method assumes that both supplied paths exist in the tree.
*
* @param root The root of the tree
* @param fullPath1 The full path to the first node
* @param fullPath2 The full path to the second node
* @return The comparison result, in the same format as {@link Comparable#compareTo}
*/
public static int compare(Node root, String fullPath1, String fullPath2) {
public int compare(String fullPath1, String fullPath2) {
String[] path1 = fullPath1.split("\\.");
String[] path2 = fullPath2.split("\\.");
int commonCount = 0;
Node commonNode = root;
Node commonNode = this;
while (commonCount < path1.length && commonCount < path2.length
&& path1[commonCount].equals(path2[commonCount]) && commonNode != null) {
commonNode = commonNode.getChild(path1[commonCount]);
@@ -77,7 +77,7 @@ final class Node {
}
if (commonNode == null) {
System.err.println("Could not find common node for '" + fullPath1 + "' at index " + commonCount);
ConsoleLogger.showError("Could not find common node for '" + fullPath1 + "' at index " + commonCount);
return fullPath1.compareTo(fullPath2); // fallback
} else if (commonCount >= path1.length || commonCount >= path2.length) {
return Integer.compare(path1.length, path2.length);
@@ -23,12 +23,12 @@ final class PropertyMapComparator implements Comparator<Property> {
* @param property The property that is being added
*/
public void add(Property property) {
Node.addNode(parent, property.getPath());
parent.addNode(property.getPath());
}
@Override
public int compare(Property p1, Property p2) {
return Node.compare(parent, p1.getPath(), p2.getPath());
return parent.compare(p1.getPath(), p2.getPath());
}
}