#835 Implement reloading with external injector

This commit is contained in:
ljacqu 2016-07-18 19:39:55 +02:00
parent 9a91156000
commit 3334e2a160
2 changed files with 46 additions and 7 deletions

View File

@ -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<Reloadable> reloadables = injector.retrieveAllOfType(Reloadable.class);
for (Reloadable reloadable : reloadables) {
reloadable.reload();
}
Collection<SettingsDependent> settingsDependents = injector.retrieveAllOfType(SettingsDependent.class);
for (SettingsDependent dependent : settingsDependents) {
dependent.reload(settings);
}
}
}

View File

@ -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<Reloadable> reloadables = Arrays.asList(
mock(Reloadable.class), mock(Reloadable.class), mock(Reloadable.class));
List<SettingsDependent> 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.<String>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<Reloadable>());
given(injector.retrieveAllOfType(SettingsDependent.class)).willReturn(new ArrayList<SettingsDependent>());
// when
command.executeCommand(sender, Collections.<String>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<Reloadable> reloadables, List<SettingsDependent> dependents) {
for (Reloadable reloadable : reloadables) {
verify(reloadable).reload();
}
for (SettingsDependent dependent : dependents) {
verify(dependent).reload(settings);
}
}
}