|
| 1 | +package dev.jorel.commandapi.test; |
| 2 | + |
| 3 | +import dev.jorel.commandapi.config.BukkitConfigurationAdapter; |
| 4 | +import dev.jorel.commandapi.config.CommentedConfigOption; |
| 5 | +import dev.jorel.commandapi.config.CommentedSection; |
| 6 | +import dev.jorel.commandapi.config.ConfigGenerator; |
| 7 | +import dev.jorel.commandapi.config.ConfigurationAdapter; |
| 8 | +import dev.jorel.commandapi.config.DefaultBukkitConfig; |
| 9 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.LinkedHashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +public class ConfigGenerationTests { |
| 20 | + |
| 21 | + private CommentedConfigOption<Boolean> silentLogs; |
| 22 | + private CommentedConfigOption<Boolean> verboseOutputs; |
| 23 | + private CommentedConfigOption<String> missingExecutorImplementation; |
| 24 | + |
| 25 | + private CommentedSection messages; |
| 26 | + |
| 27 | + private ConfigGenerator generator; |
| 28 | + private DefaultBukkitConfig bukkitConfig; |
| 29 | + private BukkitConfigurationAdapter adapter; |
| 30 | + |
| 31 | + @BeforeEach |
| 32 | + public void setup() { |
| 33 | + silentLogs = new CommentedConfigOption<>(new String[]{ |
| 34 | + "Silent logs (default: false)", |
| 35 | + "If \"true\", turns off all logging from the CommandAPI, except for errors." |
| 36 | + }, false); |
| 37 | + verboseOutputs = new CommentedConfigOption<>(new String[]{ |
| 38 | + "Verbose outputs (default: false)", |
| 39 | + "If \"true\", outputs command registration and unregistration logs in the console" |
| 40 | + }, false); |
| 41 | + missingExecutorImplementation = new CommentedConfigOption<>(new String[]{ |
| 42 | + "Missing executor implementation (default: \"This command has no implementations for %s\")", |
| 43 | + "The message to display to senders when a command has no executor. Available", |
| 44 | + "parameters are:", |
| 45 | + " %s - the executor class (lowercase)", |
| 46 | + " %S - the executor class (normal case)" |
| 47 | + }, "This command has no implementations for %s"); |
| 48 | + |
| 49 | + messages = new CommentedSection(new String[]{ |
| 50 | + "Messages", |
| 51 | + "Controls messages that the CommandAPI displays to players" |
| 52 | + }); |
| 53 | + |
| 54 | + Map<String, CommentedConfigOption<?>> options = new LinkedHashMap<>(); |
| 55 | + options.put("silent-logs", silentLogs); |
| 56 | + options.put("verbose-outputs", verboseOutputs); |
| 57 | + options.put("messages.missing-executor-implementation", missingExecutorImplementation); |
| 58 | + |
| 59 | + Map<String, CommentedSection> sections = new LinkedHashMap<>(); |
| 60 | + sections.put("messages", messages); |
| 61 | + |
| 62 | + ConfigurationAdapter<YamlConfiguration> adapter = new BukkitConfigurationAdapter(new YamlConfiguration()); |
| 63 | + bukkitConfig = DefaultBukkitConfig.create(options, sections); |
| 64 | + generator = ConfigGenerator.createNew(bukkitConfig); |
| 65 | + this.adapter = (BukkitConfigurationAdapter) generator.generate(adapter); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterEach |
| 69 | + public void reset() { |
| 70 | + this.silentLogs = null; |
| 71 | + this.verboseOutputs = null; |
| 72 | + this.missingExecutorImplementation = null; |
| 73 | + this.messages = null; |
| 74 | + this.generator = null; |
| 75 | + this.bukkitConfig = null; |
| 76 | + this.adapter = null; |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testDefaultConfigOptionGeneration() { |
| 81 | + validateConfigOptions(Set.of( |
| 82 | + "silent-logs", "verbose-outputs", "messages.missing-executor-implementation" |
| 83 | + ), adapter); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testDefaultConfigOptionCommentGeneration() { |
| 88 | + validateConfigOptionComments(Map.ofEntries( |
| 89 | + Map.entry("silent-logs", silentLogs.comment()), |
| 90 | + Map.entry("verbose-outputs", verboseOutputs.comment()), |
| 91 | + Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()), |
| 92 | + Map.entry("messages", messages.comment()) |
| 93 | + ), adapter); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testConfigOptionAddition() { |
| 98 | + CommentedConfigOption<Boolean> createDispatcherJson = new CommentedConfigOption<>(new String[] { |
| 99 | + "Create dispatcher JSON (default: false)", |
| 100 | + "If \"true\", the CommandAPI creates a command_registration.json file showing the", |
| 101 | + "mapping of registered commands. This is designed to be used by developers -", |
| 102 | + "setting this to \"false\" will improve command registration performance." |
| 103 | + }, false); |
| 104 | + |
| 105 | + bukkitConfig.getAllOptions().put("create-dispatcher-json", createDispatcherJson); |
| 106 | + generator = ConfigGenerator.createNew(bukkitConfig); |
| 107 | + BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter); |
| 108 | + |
| 109 | + validateConfigOptions(Set.of( |
| 110 | + "silent-logs", "verbose-outputs", "messages.missing-executor-implementation", "create-dispatcher-json" |
| 111 | + ), updatedAdapter); |
| 112 | + |
| 113 | + validateConfigOptionComments(Map.ofEntries( |
| 114 | + Map.entry("silent-logs", silentLogs.comment()), |
| 115 | + Map.entry("verbose-outputs", verboseOutputs.comment()), |
| 116 | + Map.entry("messages.missing-executor-implementation", missingExecutorImplementation.comment()), |
| 117 | + Map.entry("create-dispatcher-json", createDispatcherJson.comment()), |
| 118 | + Map.entry("messages", messages.comment()) |
| 119 | + ), updatedAdapter); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testConfigOptionDeletion() { |
| 124 | + bukkitConfig.getAllOptions().remove("silent-logs"); |
| 125 | + generator = ConfigGenerator.createNew(bukkitConfig); |
| 126 | + BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter); |
| 127 | + |
| 128 | + validateConfigOptionsAbsent(Set.of("silent-logs"), updatedAdapter); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void testConfigOptionCommentUpdate() { |
| 133 | + silentLogs = new CommentedConfigOption<>(new String[] { |
| 134 | + "Defines if silent logs should happen" |
| 135 | + }, false); |
| 136 | + |
| 137 | + bukkitConfig.getAllOptions().put("silent-logs", silentLogs); |
| 138 | + generator = ConfigGenerator.createNew(bukkitConfig); |
| 139 | + BukkitConfigurationAdapter updatedAdapter = (BukkitConfigurationAdapter) generator.generate(adapter); |
| 140 | + |
| 141 | + validateConfigOptionComments(Map.ofEntries( |
| 142 | + Map.entry("silent-logs", silentLogs.comment()) |
| 143 | + ), updatedAdapter); |
| 144 | + } |
| 145 | + |
| 146 | + // Test methods |
| 147 | + public void validateConfigOptions(Set<String> options, BukkitConfigurationAdapter adapter) { |
| 148 | + boolean containsAll; |
| 149 | + for (String option : options) { |
| 150 | + containsAll = adapter.contains(option); |
| 151 | + if (!containsAll) { |
| 152 | + throw new IllegalStateException("Config option '" + option + "' does not exist!"); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public void validateConfigOptionComments(Map<String, String[]> comments, BukkitConfigurationAdapter adapter) { |
| 158 | + boolean correctComment; |
| 159 | + for (String option : comments.keySet()) { |
| 160 | + String[] generatedComment = adapter.getComment(option); |
| 161 | + correctComment = Arrays.equals(comments.get(option), generatedComment); |
| 162 | + if (!correctComment) { |
| 163 | + throw new IllegalStateException("Config option comment for option '" + option + "' does not exist or was incorrect!"); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + public void validateConfigOptionsAbsent(Set<String> options, BukkitConfigurationAdapter adapter) { |
| 169 | + boolean isAbsent; |
| 170 | + for (String option : options) { |
| 171 | + isAbsent = !adapter.contains(option); |
| 172 | + if (!isAbsent) { |
| 173 | + throw new IllegalStateException("Config option '" + option + "' does still exist!"); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments