From 3334e2a160d064e6957a99a619973e60e199c669 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Mon, 18 Jul 2016 19:39:55 +0200 Subject: [PATCH] #835 Implement reloading with external injector --- .../executable/authme/ReloadCommand.java | 17 ++++++++- .../executable/authme/ReloadCommandTest.java | 36 +++++++++++++++---- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java index ed95d21da..1a7d1a691 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java @@ -6,12 +6,15 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.datasource.DataSource; +import fr.xephi.authme.initialization.Reloadable; +import fr.xephi.authme.initialization.SettingsDependent; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.properties.DatabaseSettings; import org.bukkit.command.CommandSender; import javax.inject.Inject; +import java.util.Collection; import java.util.List; /** @@ -44,7 +47,7 @@ public class ReloadCommand implements ExecutableCommand { ConsoleLogger.info("Note: cannot change database type during /authme reload"); sender.sendMessage("Note: cannot change database type during /authme reload"); } - // FIXME #835: Add replacement for injector.performReloadOnServices(); + performReloadOnServices(); commandService.send(sender, MessageKey.CONFIG_RELOAD_SUCCESS); } catch (Exception e) { sender.sendMessage("Error occurred during reload of AuthMe: aborting"); @@ -52,4 +55,16 @@ public class ReloadCommand implements ExecutableCommand { plugin.stopOrUnload(); } } + + private void performReloadOnServices() { + Collection reloadables = injector.retrieveAllOfType(Reloadable.class); + for (Reloadable reloadable : reloadables) { + reloadable.reload(); + } + + Collection settingsDependents = injector.retrieveAllOfType(SettingsDependent.class); + for (SettingsDependent dependent : settingsDependents) { + dependent.reload(settings); + } + } } diff --git a/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java b/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java index 9067674e7..f21d34081 100644 --- a/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java +++ b/src/test/java/fr/xephi/authme/command/executable/authme/ReloadCommandTest.java @@ -6,6 +6,8 @@ import fr.xephi.authme.TestHelper; import fr.xephi.authme.command.CommandService; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSourceType; +import fr.xephi.authme.initialization.Reloadable; +import fr.xephi.authme.initialization.SettingsDependent; import fr.xephi.authme.output.MessageKey; import fr.xephi.authme.settings.NewSetting; import fr.xephi.authme.settings.properties.DatabaseSettings; @@ -19,13 +21,18 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import static org.hamcrest.Matchers.containsString; import static org.mockito.BDDMockito.given; +import static org.mockito.Matchers.any; import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.matches; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -70,13 +77,19 @@ public class ReloadCommandTest { CommandSender sender = mock(CommandSender.class); given(settings.getProperty(DatabaseSettings.BACKEND)).willReturn(DataSourceType.MYSQL); given(dataSource.getType()).willReturn(DataSourceType.MYSQL); + List reloadables = Arrays.asList( + mock(Reloadable.class), mock(Reloadable.class), mock(Reloadable.class)); + List dependents = Arrays.asList( + mock(SettingsDependent.class), mock(SettingsDependent.class)); + given(injector.retrieveAllOfType(Reloadable.class)).willReturn(reloadables); + given(injector.retrieveAllOfType(SettingsDependent.class)).willReturn(dependents); // when command.executeCommand(sender, Collections.emptyList()); // then verify(settings).reload(); - // FIXME #835 verify(injector).performReloadOnServices(); + verifyReloadingCalls(reloadables, dependents); verify(commandService).send(sender, MessageKey.CONFIG_RELOAD_SUCCESS); } @@ -84,7 +97,7 @@ public class ReloadCommandTest { public void shouldHandleReloadError() { // given CommandSender sender = mock(CommandSender.class); - // FIXME #835 doThrow(IllegalStateException.class).when(injector).performReloadOnServices(); + doThrow(IllegalStateException.class).when(injector).retrieveAllOfType(Reloadable.class); given(settings.getProperty(DatabaseSettings.BACKEND)).willReturn(DataSourceType.MYSQL); given(dataSource.getType()).willReturn(DataSourceType.MYSQL); @@ -93,8 +106,8 @@ public class ReloadCommandTest { // then verify(settings).reload(); - // FIXME #835 verify(injector).performReloadOnServices(); - verify(sender).sendMessage(matches("Error occurred.*")); + verify(injector).retrieveAllOfType(Reloadable.class); + verify(sender).sendMessage(argThat(containsString("Error occurred"))); verify(authMe).stopOrUnload(); } @@ -104,13 +117,24 @@ public class ReloadCommandTest { CommandSender sender = mock(CommandSender.class); given(settings.getProperty(DatabaseSettings.BACKEND)).willReturn(DataSourceType.MYSQL); given(dataSource.getType()).willReturn(DataSourceType.SQLITE); + given(injector.retrieveAllOfType(Reloadable.class)).willReturn(new ArrayList()); + given(injector.retrieveAllOfType(SettingsDependent.class)).willReturn(new ArrayList()); // when command.executeCommand(sender, Collections.emptyList()); // then verify(settings).reload(); - // FIXME #835 verify(injector).performReloadOnServices(); + verify(injector, times(2)).retrieveAllOfType(any(Class.class)); verify(sender).sendMessage(argThat(containsString("cannot change database type"))); } + + private void verifyReloadingCalls(List reloadables, List dependents) { + for (Reloadable reloadable : reloadables) { + verify(reloadable).reload(); + } + for (SettingsDependent dependent : dependents) { + verify(dependent).reload(settings); + } + } }