diff --git a/pom.xml b/pom.xml index 101ec1198..d6a65cba5 100644 --- a/pom.xml +++ b/pom.xml @@ -873,7 +873,7 @@ com.github.authme configme - 0.1 + 0.2 compile true diff --git a/src/main/java/fr/xephi/authme/initialization/Initializer.java b/src/main/java/fr/xephi/authme/initialization/Initializer.java index a7bb4c28c..eaac2abc9 100644 --- a/src/main/java/fr/xephi/authme/initialization/Initializer.java +++ b/src/main/java/fr/xephi/authme/initialization/Initializer.java @@ -1,6 +1,6 @@ package fr.xephi.authme.initialization; -import com.github.authme.configme.knownproperties.PropertyEntry; +import com.github.authme.configme.knownproperties.ConfigurationData; import com.github.authme.configme.resource.PropertyResource; import com.github.authme.configme.resource.YamlFileResource; import fr.xephi.authme.AuthMe; @@ -12,19 +12,19 @@ import fr.xephi.authme.datasource.DataSourceType; import fr.xephi.authme.datasource.FlatFile; import fr.xephi.authme.datasource.MySQL; import fr.xephi.authme.datasource.SQLite; -import fr.xephi.authme.output.ConsoleFilter; -import fr.xephi.authme.output.Log4JFilter; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.Messages; +import fr.xephi.authme.output.ConsoleFilter; +import fr.xephi.authme.output.Log4JFilter; +import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.service.MigrationService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.SettingsMigrationService; import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever; import fr.xephi.authme.settings.properties.DatabaseSettings; import fr.xephi.authme.settings.properties.EmailSettings; import fr.xephi.authme.settings.properties.SecuritySettings; -import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.util.FileUtils; -import fr.xephi.authme.service.MigrationService; import fr.xephi.authme.util.StringUtils; import org.apache.logging.log4j.LogManager; import org.bukkit.Bukkit; @@ -33,11 +33,10 @@ import org.bukkit.entity.Player; import java.io.File; import java.io.IOException; import java.sql.SQLException; -import java.util.List; import java.util.logging.Logger; -import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS; import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE; +import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS; /** * Initializes various services. @@ -62,13 +61,12 @@ public class Initializer { * @return the settings instance, or null if it could not be constructed */ public static Settings createSettings(AuthMe authMe) throws Exception { - SettingsMigrationService migrationService = new SettingsMigrationService(authMe.getDataFolder()); - List knownProperties = AuthMeSettingsRetriever.getAllPropertyFields(); - File configFile = new File(authMe.getDataFolder(), "config.yml"); if (FileUtils.copyFileFromResource(configFile, "config.yml")) { PropertyResource resource = new YamlFileResource(configFile); - return new Settings(authMe.getDataFolder(), resource, migrationService, knownProperties); + SettingsMigrationService migrationService = new SettingsMigrationService(authMe.getDataFolder()); + ConfigurationData configurationData = AuthMeSettingsRetriever.buildConfigurationData(); + return new Settings(authMe.getDataFolder(), resource, migrationService, configurationData); } throw new Exception("Could not copy config.yml from JAR to plugin folder"); } diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index e2ebd0aa6..fb4c06246 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -1,7 +1,7 @@ package fr.xephi.authme.settings; import com.github.authme.configme.SettingsManager; -import com.github.authme.configme.knownproperties.PropertyEntry; +import com.github.authme.configme.knownproperties.ConfigurationData; import com.github.authme.configme.migration.MigrationService; import com.github.authme.configme.resource.PropertyResource; import com.google.common.io.Files; @@ -10,7 +10,6 @@ import fr.xephi.authme.ConsoleLogger; 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; @@ -30,11 +29,11 @@ public class Settings extends SettingsManager { * @param pluginFolder the AuthMe plugin folder * @param resource the property resource to read and write properties to * @param migrationService migration service to check the settings file with - * @param knownProperties collection of all available settings + * @param configurationData configuration data (properties and comments) */ public Settings(File pluginFolder, PropertyResource resource, MigrationService migrationService, - List knownProperties) { - super(resource, migrationService, knownProperties); + ConfigurationData configurationData) { + super(resource, migrationService, configurationData); this.pluginFolder = pluginFolder; loadSettingsFromFiles(); } diff --git a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java index 3ef6259b7..30163bf0f 100644 --- a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java +++ b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java @@ -1,6 +1,5 @@ package fr.xephi.authme.settings; -import com.github.authme.configme.knownproperties.PropertyEntry; import com.github.authme.configme.migration.PlainMigrationService; import com.github.authme.configme.properties.Property; import com.github.authme.configme.resource.PropertyResource; @@ -35,7 +34,7 @@ public class SettingsMigrationService extends PlainMigrationService { } @Override - protected boolean performMigrations(PropertyResource resource, List knownProperties) { + protected boolean performMigrations(PropertyResource resource, List> properties) { boolean changes = false; if ("[a-zA-Z0-9_?]*".equals(resource.getString(ALLOWED_NICKNAME_CHARACTERS.getPath()))) { resource.setValue(ALLOWED_NICKNAME_CHARACTERS.getPath(), "[a-zA-Z0-9_]*"); diff --git a/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java b/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java index 54b3e6e58..80bd02cfd 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java +++ b/src/main/java/fr/xephi/authme/settings/properties/AuthMeSettingsRetriever.java @@ -1,12 +1,10 @@ package fr.xephi.authme.settings.properties; import com.github.authme.configme.SettingsHolder; -import com.github.authme.configme.knownproperties.PropertyEntry; -import com.github.authme.configme.knownproperties.PropertyFieldsCollector; +import com.github.authme.configme.knownproperties.ConfigurationData; +import com.github.authme.configme.knownproperties.ConfigurationDataBuilder; import com.github.authme.configme.properties.Property; -import java.util.List; - /** * Utility class responsible for retrieving all {@link Property} fields * from {@link SettingsHolder} implementations via reflection. @@ -17,12 +15,12 @@ public final class AuthMeSettingsRetriever { } /** - * Constructs a list with all property fields in AuthMe {@link SettingsHolder} classes. + * Builds the configuration data for all property fields in AuthMe {@link SettingsHolder} classes. * - * @return list of all known properties + * @return configuration data */ - public static List getAllPropertyFields() { - return PropertyFieldsCollector.getAllProperties( + public static ConfigurationData buildConfigurationData() { + return ConfigurationDataBuilder.collectData( DatabaseSettings.class, ConverterSettings.class, PluginSettings.class, RestrictionSettings.class, EmailSettings.class, HooksSettings.class, ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class, diff --git a/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java b/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java index fc504f439..ba7f76068 100644 --- a/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java +++ b/src/test/java/fr/xephi/authme/AuthMeInitializationTest.java @@ -13,9 +13,9 @@ import fr.xephi.authme.permission.PermissionsManager; import fr.xephi.authme.process.Management; import fr.xephi.authme.process.login.ProcessSyncPlayerLogin; import fr.xephi.authme.security.PasswordSecurity; +import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.task.purge.PurgeService; -import fr.xephi.authme.service.BukkitService; import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.plugin.PluginDescriptionFile; @@ -36,7 +36,7 @@ import java.io.IOException; import java.util.logging.Logger; import static fr.xephi.authme.settings.TestSettingsMigrationServices.alwaysFulfilled; -import static fr.xephi.authme.settings.properties.AuthMeSettingsRetriever.getAllPropertyFields; +import static fr.xephi.authme.settings.properties.AuthMeSettingsRetriever.buildConfigurationData; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; @@ -95,7 +95,7 @@ public class AuthMeInitializationTest { public void shouldInitializeAllServices() { // given Settings settings = - new Settings(dataFolder, mock(PropertyResource.class), alwaysFulfilled(), getAllPropertyFields()); + new Settings(dataFolder, mock(PropertyResource.class), alwaysFulfilled(), buildConfigurationData()); Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme").create(); injector.provide(DataFolder.class, dataFolder); diff --git a/src/test/java/fr/xephi/authme/listener/EntityListenerTest.java b/src/test/java/fr/xephi/authme/listener/EntityListenerTest.java index 0b7b00845..1f36fa8d4 100644 --- a/src/test/java/fr/xephi/authme/listener/EntityListenerTest.java +++ b/src/test/java/fr/xephi/authme/listener/EntityListenerTest.java @@ -18,7 +18,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import static fr.xephi.authme.listener.ListenerTestUtils.checkEventIsCanceledForUnauthed; +import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Mockito.mock; @@ -41,65 +41,12 @@ public class EntityListenerTest { @Test public void shouldHandleSimpleEvents() { - checkEventIsCanceledForUnauthed(listener, listenerService, EntityTargetEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, FoodLevelChangeEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, EntityShootBowEvent.class); - } - - @Test - public void shouldCancelEntityInteractEvent() { - // given - EntityInteractEvent event = mock(EntityInteractEvent.class); - given(listenerService.shouldCancelEvent(event)).willReturn(true); - - // when - listener.onLowestEntityInteract(event); - - // then - verify(listenerService).shouldCancelEvent(event); - verify(event).setCancelled(true); - } - - @Test - public void shouldNotCancelEntityInteractEvent() { - // given - EntityInteractEvent event = mock(EntityInteractEvent.class); - given(listenerService.shouldCancelEvent(event)).willReturn(false); - - // when - listener.onLowestEntityInteract(event); - - // then - verify(listenerService).shouldCancelEvent(event); - verifyZeroInteractions(event); - } - - @Test - public void shouldCancelEntityInteractEventHighest() { - // given - EntityInteractEvent event = mock(EntityInteractEvent.class); - given(listenerService.shouldCancelEvent(event)).willReturn(true); - - // when - listener.onEntityInteract(event); - - // then - verify(listenerService).shouldCancelEvent(event); - verify(event).setCancelled(true); - } - - @Test - public void shouldNotCancelEntityInteractEventHighest() { - // given - EntityInteractEvent event = mock(EntityInteractEvent.class); - given(listenerService.shouldCancelEvent(event)).willReturn(false); - - // when - listener.onEntityInteract(event); - - // then - verify(listenerService).shouldCancelEvent(event); - verifyZeroInteractions(event); + withServiceMock(listenerService) + .check(listener::onEntityTarget, EntityTargetEvent.class) + .check(listener::onFoodLevelChange, FoodLevelChangeEvent.class) + .check(listener::onShoot, EntityShootBowEvent.class) + .check(listener::onEntityInteract, EntityInteractEvent.class) + .check(listener::onLowestEntityInteract, EntityInteractEvent.class); } @Test diff --git a/src/test/java/fr/xephi/authme/listener/ListenerTestUtils.java b/src/test/java/fr/xephi/authme/listener/EventCancelVerifier.java similarity index 51% rename from src/test/java/fr/xephi/authme/listener/ListenerTestUtils.java rename to src/test/java/fr/xephi/authme/listener/EventCancelVerifier.java index cdc07cce8..f74dc24bd 100644 --- a/src/test/java/fr/xephi/authme/listener/ListenerTestUtils.java +++ b/src/test/java/fr/xephi/authme/listener/EventCancelVerifier.java @@ -1,14 +1,11 @@ package fr.xephi.authme.listener; -import fr.xephi.authme.ReflectionTestUtils; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityEvent; import org.bukkit.event.player.PlayerEvent; -import java.lang.reflect.Method; +import java.util.function.Consumer; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -16,11 +13,25 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; /** - * Utilities for testing AuthMe listener classes. + * Tests simple listener methods that should cancel an event when the listener service says so. */ -public final class ListenerTestUtils { +public final class EventCancelVerifier { - private ListenerTestUtils() { + private final ListenerService listenerService; + + private EventCancelVerifier(ListenerService listenerService) { + this.listenerService = listenerService; + } + + /** + * Creates a new verifier that uses the given ListenerService mock (needs to be the same instance + * as used in the listener class to test). + * + * @param listenerService the listener service mock + * @return new verifier + */ + public static EventCancelVerifier withServiceMock(ListenerService listenerService) { + return new EventCancelVerifier(listenerService); } /** @@ -29,25 +40,23 @@ public final class ListenerTestUtils { * canceled when the service says so and the other way around. Do not use this * method if the handler method has additional behavior. * - * - * @param listener the listener to test - * @param listenerService the listener service mock + * @param listenerMethod the listener method to test * @param clazz the event class to test the handler method for * @param the event type + * @return the verifier (for chaining of methods) */ - public static - void checkEventIsCanceledForUnauthed(Listener listener, ListenerService listenerService, Class clazz) { - Method handlerMethod = findMethod(listener, clazz); - + public EventCancelVerifier check(Consumer listenerMethod, Class clazz) { T event = mock(clazz); mockShouldCancel(true, listenerService, event); - ReflectionTestUtils.invokeMethod(handlerMethod, listener, event); + listenerMethod.accept(event); verify(event).setCancelled(true); event = mock(clazz); mockShouldCancel(false, listenerService, event); - ReflectionTestUtils.invokeMethod(handlerMethod, listener, event); + listenerMethod.accept(event); verifyZeroInteractions(event); + + return this; } /** @@ -67,33 +76,4 @@ public final class ListenerTestUtils { throw new IllegalStateException("Found event with unsupported type: " + event.getClass()); } } - - /** - * Returns the method in the listener that takes the given event type as parameter. - * - * @param listener the listener to scan - * @param paramType the event type - * @return the mapped method - * @throws IllegalStateException if there is not exactly one method with the given event type as parameter - */ - private static Method findMethod(Listener listener, Class paramType) { - Method matchingMethod = null; - for (Method method : listener.getClass().getMethods()) { - if (method.isAnnotationPresent(EventHandler.class)) { - Class[] parameters = method.getParameterTypes(); - if (parameters.length == 1 && parameters[0] == paramType) { - if (matchingMethod == null) { - matchingMethod = method; - } else { - throw new IllegalStateException("Found multiple eligible methods for " + paramType); - } - } - } - } - if (matchingMethod == null) { - throw new IllegalStateException("Found no matching method for " + paramType); - } - return matchingMethod; - } - } diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListener16Test.java b/src/test/java/fr/xephi/authme/listener/PlayerListener16Test.java index 980f9dabb..c60b8abff 100644 --- a/src/test/java/fr/xephi/authme/listener/PlayerListener16Test.java +++ b/src/test/java/fr/xephi/authme/listener/PlayerListener16Test.java @@ -7,6 +7,8 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock; + /** * Test for {@link PlayerListener16}. */ @@ -21,7 +23,8 @@ public class PlayerListener16Test { @Test public void shouldCancelEvent() { - ListenerTestUtils.checkEventIsCanceledForUnauthed(listener, listenerService, PlayerEditBookEvent.class); + withServiceMock(listenerService) + .check(listener::onPlayerEditBook, PlayerEditBookEvent.class); } } diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListener18Test.java b/src/test/java/fr/xephi/authme/listener/PlayerListener18Test.java index 79466a31d..9b93a270b 100644 --- a/src/test/java/fr/xephi/authme/listener/PlayerListener18Test.java +++ b/src/test/java/fr/xephi/authme/listener/PlayerListener18Test.java @@ -7,6 +7,8 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock; + /** * Test for {@link PlayerListener18}. */ @@ -21,7 +23,8 @@ public class PlayerListener18Test { @Test public void shouldCancelEvent() { - ListenerTestUtils.checkEventIsCanceledForUnauthed(listener, listenerService, PlayerInteractAtEntityEvent.class); + withServiceMock(listenerService) + .check(listener::onPlayerInteractAtEntity, PlayerInteractAtEntityEvent.class); } } diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java b/src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java index 8fcd39b68..50dd179dc 100644 --- a/src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java +++ b/src/test/java/fr/xephi/authme/listener/PlayerListener19Test.java @@ -7,6 +7,8 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock; + /** * Test for {@link PlayerListener19}. */ @@ -21,7 +23,8 @@ public class PlayerListener19Test { @Test public void shouldCancelEvent() { - ListenerTestUtils.checkEventIsCanceledForUnauthed(listener, listenerService, PlayerSwapHandItemsEvent.class); + withServiceMock(listenerService) + .check(listener::onPlayerSwapHandItems, PlayerSwapHandItemsEvent.class); } } diff --git a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java index 74f9d5083..15bf4c6ee 100644 --- a/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java +++ b/src/test/java/fr/xephi/authme/listener/PlayerListenerTest.java @@ -1,18 +1,18 @@ package fr.xephi.authme.listener; -import fr.xephi.authme.service.AntiBotService; import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.Messages; import fr.xephi.authme.process.Management; +import fr.xephi.authme.service.AntiBotService; +import fr.xephi.authme.service.BukkitService; +import fr.xephi.authme.service.TeleportationService; +import fr.xephi.authme.service.ValidationService; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.SpawnLoader; import fr.xephi.authme.settings.properties.HooksSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; -import fr.xephi.authme.service.BukkitService; -import fr.xephi.authme.service.TeleportationService; -import fr.xephi.authme.service.ValidationService; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.World; @@ -45,7 +45,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; -import static fr.xephi.authme.listener.ListenerTestUtils.checkEventIsCanceledForUnauthed; +import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; @@ -153,15 +153,16 @@ public class PlayerListenerTest { @Test public void shouldHandleSimpleCancelableEvents() { - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerShearEntityEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerFishEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerBedEnterEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerDropItemEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, EntityDamageByEntityEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerItemConsumeEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerInteractEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerPickupItemEvent.class); - checkEventIsCanceledForUnauthed(listener, listenerService, PlayerInteractEntityEvent.class); + withServiceMock(listenerService) + .check(listener::onPlayerShear, PlayerShearEntityEvent.class) + .check(listener::onPlayerFish, PlayerFishEvent.class) + .check(listener::onPlayerBedEnter, PlayerBedEnterEvent.class) + .check(listener::onPlayerDropItem, PlayerDropItemEvent.class) + .check(listener::onPlayerHitPlayerEvent, EntityDamageByEntityEvent.class) + .check(listener::onPlayerConsumeItem, PlayerItemConsumeEvent.class) + .check(listener::onPlayerInteract, PlayerInteractEvent.class) + .check(listener::onPlayerPickupItem, PlayerPickupItemEvent.class) + .check(listener::onPlayerInteractEntity, PlayerInteractEntityEvent.class); } @Test diff --git a/src/test/java/fr/xephi/authme/listener/ServerListenerTest.java b/src/test/java/fr/xephi/authme/listener/ServerListenerTest.java index 266a437ff..d40683c37 100644 --- a/src/test/java/fr/xephi/authme/listener/ServerListenerTest.java +++ b/src/test/java/fr/xephi/authme/listener/ServerListenerTest.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; /** * Test for {@link ServerListener}. @@ -56,82 +57,22 @@ public class ServerListenerTest { @Test public void shouldForwardPluginNameOnEnable() { - checkEnableHandling(ESSENTIALS, new Runnable() { - @Override - public void run() { - verify(pluginHookService).tryHookToEssentials(); - } - }); - checkEnableHandling(ESSENTIALS_SPAWN, new Runnable() { - @Override - public void run() { - verify(spawnLoader).loadEssentialsSpawn(); - } - }); - checkEnableHandling(MULTIVERSE, new Runnable() { - @Override - public void run() { - verify(pluginHookService).tryHookToMultiverse(); - } - }); - checkEnableHandling(COMBAT_TAG, new Runnable() { - @Override - public void run() { - verify(pluginHookService).tryHookToCombatPlus(); - } - }); - checkEnableHandling(PROTOCOL_LIB, new Runnable() { - @Override - public void run() { - verify(protocolLibService).setup(); - } - }); - checkEnableHandling("UnknownPlugin", new Runnable() { - @Override - public void run() { - // nothing - } - }); + checkEnableHandling(ESSENTIALS, () -> verify(pluginHookService).tryHookToEssentials()); + checkEnableHandling(ESSENTIALS_SPAWN, () -> verify(spawnLoader).loadEssentialsSpawn()); + checkEnableHandling(MULTIVERSE, () -> verify(pluginHookService).tryHookToMultiverse()); + checkEnableHandling(COMBAT_TAG, () -> verify(pluginHookService).tryHookToCombatPlus()); + checkEnableHandling(PROTOCOL_LIB, () -> verify(protocolLibService).setup()); + checkEnableHandling("UnknownPlugin", () -> verifyZeroInteractions(pluginHookService, spawnLoader)); } @Test public void shouldForwardPluginNameOnDisable() { - checkDisableHandling(ESSENTIALS, new Runnable() { - @Override - public void run() { - verify(pluginHookService).unhookEssentials(); - } - }); - checkDisableHandling(ESSENTIALS_SPAWN, new Runnable() { - @Override - public void run() { - verify(spawnLoader).unloadEssentialsSpawn(); - } - }); - checkDisableHandling(MULTIVERSE, new Runnable() { - @Override - public void run() { - verify(pluginHookService).unhookMultiverse(); - } - }); - checkDisableHandling(COMBAT_TAG, new Runnable() { - @Override - public void run() { - verify(pluginHookService).unhookCombatPlus(); - } - }); - checkDisableHandling(PROTOCOL_LIB, new Runnable() { - @Override - public void run() { - verify(protocolLibService).disable(); - } - }); - checkDisableHandling("UnknownPlugin", new Runnable() { - @Override - public void run() { - // nothing - } - }); + checkDisableHandling(ESSENTIALS, () -> verify(pluginHookService).unhookEssentials()); + checkDisableHandling(ESSENTIALS_SPAWN, () -> verify(spawnLoader).unloadEssentialsSpawn()); + checkDisableHandling(MULTIVERSE, () -> verify(pluginHookService).unhookMultiverse()); + checkDisableHandling(COMBAT_TAG, () -> verify(pluginHookService).unhookCombatPlus()); + checkDisableHandling(PROTOCOL_LIB, () -> verify(protocolLibService).disable()); + checkDisableHandling("UnknownPlugin", () -> verifyZeroInteractions(pluginHookService, spawnLoader)); } @Test diff --git a/src/test/java/fr/xephi/authme/settings/ConfigFileConsistencyTest.java b/src/test/java/fr/xephi/authme/settings/ConfigFileConsistencyTest.java index 768dd98ed..ab4031e25 100644 --- a/src/test/java/fr/xephi/authme/settings/ConfigFileConsistencyTest.java +++ b/src/test/java/fr/xephi/authme/settings/ConfigFileConsistencyTest.java @@ -1,6 +1,6 @@ package fr.xephi.authme.settings; -import com.github.authme.configme.knownproperties.PropertyEntry; +import com.github.authme.configme.knownproperties.ConfigurationData; import com.github.authme.configme.migration.MigrationService; import com.github.authme.configme.migration.PlainMigrationService; import com.github.authme.configme.properties.Property; @@ -16,10 +16,10 @@ import org.junit.Test; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; @@ -42,7 +42,8 @@ public class ConfigFileConsistencyTest { MigrationService migration = new PlainMigrationService(); // when - boolean result = migration.checkAndMigrate(resource, AuthMeSettingsRetriever.getAllPropertyFields()); + boolean result = migration.checkAndMigrate( + resource, AuthMeSettingsRetriever.buildConfigurationData().getProperties()); // then if (result) { @@ -86,23 +87,19 @@ public class ConfigFileConsistencyTest { // given File configFile = TestHelper.getJarFile(CONFIG_FILE); PropertyResource resource = new YamlFileResource(configFile); - List knownProperties = AuthMeSettingsRetriever.getAllPropertyFields(); + ConfigurationData configurationData = AuthMeSettingsRetriever.buildConfigurationData(); // when / then - for (PropertyEntry propertyEntry : knownProperties) { - Property property = propertyEntry.getProperty(); + for (Property property : configurationData.getProperties()) { assertThat("Default value of '" + property.getPath() + "' in config.yml should be the same as in Property", property.getValue(resource).equals(property.getDefaultValue()), equalTo(true)); } } private static Set getAllKnownPropertyPaths() { - List knownProperties = AuthMeSettingsRetriever.getAllPropertyFields(); - Set paths = new HashSet<>(knownProperties.size()); - for (PropertyEntry propertyEntry : knownProperties) { - paths.add(propertyEntry.getProperty().getPath()); - } - return paths; + return AuthMeSettingsRetriever.buildConfigurationData() + .getProperties().stream() + .map(Property::getPath) + .collect(Collectors.toSet()); } - } diff --git a/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java b/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java index f29bcc49b..9a6441979 100644 --- a/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/settings/SettingsIntegrationTest.java @@ -1,7 +1,7 @@ package fr.xephi.authme.settings; -import com.github.authme.configme.knownproperties.PropertyEntry; -import com.github.authme.configme.knownproperties.PropertyFieldsCollector; +import com.github.authme.configme.knownproperties.ConfigurationData; +import com.github.authme.configme.knownproperties.ConfigurationDataBuilder; import com.github.authme.configme.migration.PlainMigrationService; import com.github.authme.configme.properties.Property; import com.github.authme.configme.resource.PropertyResource; @@ -21,7 +21,6 @@ import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.Collections; -import java.util.List; import java.util.Map; import static fr.xephi.authme.TestHelper.getJarFile; @@ -38,8 +37,8 @@ public class SettingsIntegrationTest { /** File name of the sample config missing certain {@link TestConfiguration} values. */ private static final String INCOMPLETE_FILE = TestHelper.PROJECT_ROOT + "settings/config-incomplete-sample.yml"; - private static List knownProperties = - PropertyFieldsCollector.getAllProperties(TestConfiguration.class); + private static ConfigurationData CONFIG_DATA = + ConfigurationDataBuilder.collectData(TestConfiguration.class); @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -65,7 +64,7 @@ public class SettingsIntegrationTest { // when / then Settings settings = new Settings(testPluginFolder, resource, - new PlainMigrationService(), knownProperties); + new PlainMigrationService(), CONFIG_DATA); Map, Object> expectedValues = ImmutableMap., Object>builder() .put(TestConfiguration.DURATION_IN_SECONDS, 22) .put(TestConfiguration.SYSTEM_NAME, "Custom sys name") @@ -91,14 +90,14 @@ public class SettingsIntegrationTest { File file = copyFileFromResources(INCOMPLETE_FILE); PropertyResource resource = new YamlFileResource(file); // Expectation: File is rewritten to since it does not have all configurations - new Settings(testPluginFolder, resource, new PlainMigrationService(), knownProperties); + new Settings(testPluginFolder, resource, new PlainMigrationService(), CONFIG_DATA); // Load the settings again -> checks that what we wrote can be loaded again resource = new YamlFileResource(file); // then Settings settings = new Settings(testPluginFolder, resource, - new PlainMigrationService(), knownProperties); + new PlainMigrationService(), CONFIG_DATA); Map, Object> expectedValues = ImmutableMap., Object>builder() .put(TestConfiguration.DURATION_IN_SECONDS, 22) .put(TestConfiguration.SYSTEM_NAME, "[TestDefaultValue]") @@ -123,7 +122,7 @@ public class SettingsIntegrationTest { File configFile = temporaryFolder.newFile(); PropertyResource resource = new YamlFileResource(configFile); Settings settings = new Settings(testPluginFolder, resource, - TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); + TestSettingsMigrationServices.alwaysFulfilled(), CONFIG_DATA); // when assertThat(settings.getProperty(TestConfiguration.RATIO_ORDER), equalTo(TestEnum.SECOND)); // default value diff --git a/src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java b/src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java index bc2284b8e..93325110a 100644 --- a/src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java +++ b/src/test/java/fr/xephi/authme/settings/SettingsMigrationServiceTest.java @@ -1,6 +1,6 @@ package fr.xephi.authme.settings; -import com.github.authme.configme.knownproperties.PropertyEntry; +import com.github.authme.configme.knownproperties.ConfigurationData; import com.github.authme.configme.resource.PropertyResource; import com.github.authme.configme.resource.YamlFileResource; import com.google.common.io.Files; @@ -12,7 +12,6 @@ import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; -import java.util.List; import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.equalTo; @@ -41,12 +40,12 @@ public class SettingsMigrationServiceTest { // given copyConfigToTestFolder(); PropertyResource resource = new YamlFileResource(configTestFile); - List propertyMap = AuthMeSettingsRetriever.getAllPropertyFields(); + ConfigurationData configurationData = AuthMeSettingsRetriever.buildConfigurationData(); assumeThat(testFolder.listFiles(), arrayWithSize(1)); SettingsMigrationService migrationService = new SettingsMigrationService(testFolder); // when - boolean result = migrationService.checkAndMigrate(resource, propertyMap); + boolean result = migrationService.checkAndMigrate(resource, configurationData.getProperties()); // then assertThat(result, equalTo(false)); diff --git a/src/test/java/fr/xephi/authme/settings/SettingsTest.java b/src/test/java/fr/xephi/authme/settings/SettingsTest.java index 8dc2316f5..e839bb39f 100644 --- a/src/test/java/fr/xephi/authme/settings/SettingsTest.java +++ b/src/test/java/fr/xephi/authme/settings/SettingsTest.java @@ -1,7 +1,7 @@ package fr.xephi.authme.settings; -import com.github.authme.configme.knownproperties.PropertyEntry; -import com.github.authme.configme.knownproperties.PropertyFieldsCollector; +import com.github.authme.configme.knownproperties.ConfigurationData; +import com.github.authme.configme.knownproperties.ConfigurationDataBuilder; import com.github.authme.configme.resource.PropertyResource; import fr.xephi.authme.TestHelper; import fr.xephi.authme.settings.properties.RegistrationSettings; @@ -15,7 +15,6 @@ import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; import java.nio.file.Files; -import java.util.List; import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.arrayWithSize; @@ -29,8 +28,8 @@ import static org.mockito.Mockito.mock; */ public class SettingsTest { - private static final List knownProperties = - PropertyFieldsCollector.getAllProperties(TestConfiguration.class); + private static final ConfigurationData CONFIG_DATA = + ConfigurationDataBuilder.collectData(TestConfiguration.class); @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -57,7 +56,7 @@ public class SettingsTest { PropertyResource resource = mock(PropertyResource.class); given(resource.getBoolean(RegistrationSettings.USE_WELCOME_MESSAGE.getPath())).willReturn(true); Settings settings = new Settings(testPluginFolder, resource, - TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); + TestSettingsMigrationServices.alwaysFulfilled(), CONFIG_DATA); // when String[] result = settings.getWelcomeMessage(); @@ -77,7 +76,7 @@ public class SettingsTest { PropertyResource resource = mock(PropertyResource.class); Settings settings = new Settings(testPluginFolder, resource, - TestSettingsMigrationServices.alwaysFulfilled(), knownProperties); + TestSettingsMigrationServices.alwaysFulfilled(), CONFIG_DATA); // when String result = settings.getPasswordEmailMessage();