Update Injector and create injectable object factory
- Using e.g. Factory<Converter> instead of the injector directly makes its purpose more specific and disallows any future abuse of the injector's functions
This commit is contained in:
@@ -6,6 +6,7 @@ import fr.xephi.authme.command.TestCommandsUtil.TestLoginCommand;
|
||||
import fr.xephi.authme.command.TestCommandsUtil.TestRegisterCommand;
|
||||
import fr.xephi.authme.command.TestCommandsUtil.TestUnregisterCommand;
|
||||
import fr.xephi.authme.command.help.HelpProvider;
|
||||
import fr.xephi.authme.initialization.factory.Factory;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.message.Messages;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
@@ -56,7 +57,7 @@ public class CommandHandlerTest {
|
||||
private CommandHandler handler;
|
||||
|
||||
@Mock
|
||||
private Injector injector;
|
||||
private Factory<ExecutableCommand> commandFactory;
|
||||
@Mock
|
||||
private CommandMapper commandMapper;
|
||||
@Mock
|
||||
@@ -75,7 +76,7 @@ public class CommandHandlerTest {
|
||||
ExecutableCommand.class, TestLoginCommand.class, TestRegisterCommand.class, TestUnregisterCommand.class));
|
||||
setInjectorToMockExecutableCommandClasses();
|
||||
|
||||
handler = new CommandHandler(injector, commandMapper, permissionsManager, messages, helpProvider);
|
||||
handler = new CommandHandler(commandFactory, commandMapper, permissionsManager, messages, helpProvider);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,7 +87,7 @@ public class CommandHandlerTest {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void setInjectorToMockExecutableCommandClasses() {
|
||||
given(injector.newInstance(any(Class.class))).willAnswer(new Answer<Object>() {
|
||||
given(commandFactory.newInstance(any(Class.class))).willAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
Class<?> clazz = invocation.getArgument(0);
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.datasource.converter.Converter;
|
||||
import fr.xephi.authme.initialization.factory.Factory;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.CommonService;
|
||||
@@ -48,7 +48,7 @@ public class ConverterCommandTest {
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Mock
|
||||
private Injector injector;
|
||||
private Factory<Converter> converterFactory;
|
||||
|
||||
@BeforeClass
|
||||
public static void initLogger() {
|
||||
@@ -66,7 +66,7 @@ public class ConverterCommandTest {
|
||||
// then
|
||||
verify(sender).sendMessage(argThat(containsString("Converter does not exist")));
|
||||
verifyNoMoreInteractions(commandService);
|
||||
verifyZeroInteractions(injector);
|
||||
verifyZeroInteractions(converterFactory);
|
||||
verifyZeroInteractions(bukkitService);
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ public class ConverterCommandTest {
|
||||
// then
|
||||
verify(converter).execute(sender);
|
||||
verifyNoMoreInteractions(converter);
|
||||
verify(injector).newInstance(converterClass);
|
||||
verifyNoMoreInteractions(injector);
|
||||
verify(converterFactory).newInstance(converterClass);
|
||||
verifyNoMoreInteractions(converterFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,14 +120,14 @@ public class ConverterCommandTest {
|
||||
// then
|
||||
verify(converter).execute(sender);
|
||||
verifyNoMoreInteractions(converter);
|
||||
verify(injector).newInstance(converterClass);
|
||||
verifyNoMoreInteractions(injector);
|
||||
verify(converterFactory).newInstance(converterClass);
|
||||
verifyNoMoreInteractions(converterFactory);
|
||||
verify(commandService).send(sender, MessageKey.ERROR);
|
||||
}
|
||||
|
||||
private <T extends Converter> T createMockReturnedByInjector(Class<T> clazz) {
|
||||
T converter = mock(clazz);
|
||||
given(injector.newInstance(clazz)).willReturn(converter);
|
||||
given(converterFactory.newInstance(clazz)).willReturn(converter);
|
||||
return converter;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user