#835 Replace injector classes with ones from ch.jalu.injector project
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
package fr.xephi.authme;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import ch.jalu.injector.InjectorBuilder;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.api.NewAPI;
|
||||
import fr.xephi.authme.command.CommandHandler;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.initialization.AuthMeServiceInitializer;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.listener.AuthMeBlockListener;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
@@ -42,7 +43,7 @@ import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Integration test verifying that all services can be initialized in {@link AuthMe}
|
||||
* with the {@link AuthMeServiceInitializer}.
|
||||
* with the {@link Injector}.
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AuthMeInitializationTest {
|
||||
@@ -97,29 +98,29 @@ public class AuthMeInitializationTest {
|
||||
// We only require it right now because of usages of AuthMe#getInstance()
|
||||
ReflectionTestUtils.setField(AuthMe.class, null, "plugin", authMe);
|
||||
|
||||
AuthMeServiceInitializer initializer = new AuthMeServiceInitializer("fr.xephi.authme");
|
||||
initializer.provide(DataFolder.class, dataFolder);
|
||||
initializer.register(Server.class, server);
|
||||
initializer.register(PluginManager.class, pluginManager);
|
||||
Injector injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme").create();
|
||||
injector.provide(DataFolder.class, dataFolder);
|
||||
injector.register(Server.class, server);
|
||||
injector.register(PluginManager.class, pluginManager);
|
||||
|
||||
initializer.register(AuthMe.class, authMe);
|
||||
initializer.register(NewSetting.class, settings);
|
||||
initializer.register(DataSource.class, mock(DataSource.class));
|
||||
injector.register(AuthMe.class, authMe);
|
||||
injector.register(NewSetting.class, settings);
|
||||
injector.register(DataSource.class, mock(DataSource.class));
|
||||
|
||||
// when
|
||||
authMe.instantiateServices(initializer);
|
||||
authMe.registerEventListeners(initializer);
|
||||
authMe.instantiateServices(injector);
|
||||
authMe.registerEventListeners(injector);
|
||||
|
||||
// then
|
||||
// Take a few samples and ensure that they are not null
|
||||
assertThat(initializer.getIfAvailable(AuthMeBlockListener.class), not(nullValue()));
|
||||
assertThat(initializer.getIfAvailable(CommandHandler.class), not(nullValue()));
|
||||
assertThat(initializer.getIfAvailable(Management.class), not(nullValue()));
|
||||
assertThat(initializer.getIfAvailable(NewAPI.class), not(nullValue()));
|
||||
assertThat(initializer.getIfAvailable(PasswordSecurity.class), not(nullValue()));
|
||||
assertThat(initializer.getIfAvailable(PermissionsManager.class), not(nullValue()));
|
||||
assertThat(initializer.getIfAvailable(ProcessSyncPlayerLogin.class), not(nullValue()));
|
||||
assertThat(initializer.getIfAvailable(PurgeService.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(AuthMeBlockListener.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(CommandHandler.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(Management.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(NewAPI.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(PasswordSecurity.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(PermissionsManager.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(ProcessSyncPlayerLogin.class), not(nullValue()));
|
||||
assertThat(injector.getIfAvailable(PurgeService.class), not(nullValue()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package fr.xephi.authme.cache.backup;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.cache.limbo.PlayerData;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.runner.BeforeInjecting;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import fr.xephi.authme.settings.SpawnLoader;
|
||||
import fr.xephi.authme.util.BukkitService;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import com.google.common.collect.Sets;
|
||||
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.AuthMeServiceInitializer;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.runner.BeforeInjecting;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -56,7 +56,7 @@ public class CommandHandlerTest {
|
||||
private CommandHandler handler;
|
||||
|
||||
@Mock
|
||||
private AuthMeServiceInitializer initializer;
|
||||
private Injector injector;
|
||||
@Mock
|
||||
private CommandMapper commandMapper;
|
||||
@Mock
|
||||
@@ -75,14 +75,14 @@ public class CommandHandlerTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the initializer return a mock when {@link AuthMeServiceInitializer#newInstance(Class)} is invoked
|
||||
* with (a child of) ExecutableCommand.class. The mocks the initializer creates are stored in {@link #mockedCommands}.
|
||||
* Makes the injector return a mock when {@link Injector#newInstance(Class)} is invoked
|
||||
* with (a child of) ExecutableCommand.class. The mocks the injector creates are stored in {@link #mockedCommands}.
|
||||
* <p>
|
||||
* The {@link CommandMapper} is mocked in {@link #initializeCommandMapper()} to return certain test classes.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void setInjectorToMockExecutableCommandClasses() {
|
||||
given(initializer.newInstance(any(Class.class))).willAnswer(new Answer<Object>() {
|
||||
given(injector.newInstance(any(Class.class))).willAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
Class<?> clazz = (Class<?>) invocation.getArguments()[0];
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package fr.xephi.authme.command;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
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.executable.HelpCommand;
|
||||
import fr.xephi.authme.permission.PermissionNode;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.runner.BeforeInjecting;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.converter.RakamakConverter;
|
||||
import fr.xephi.authme.initialization.AuthMeServiceInitializer;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.util.BukkitService;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@@ -43,7 +43,7 @@ public class ConverterCommandTest {
|
||||
private BukkitService bukkitService;
|
||||
|
||||
@Mock
|
||||
private AuthMeServiceInitializer initializer;
|
||||
private Injector injector;
|
||||
|
||||
@Test
|
||||
public void shouldHandleUnknownConversionType() {
|
||||
@@ -56,7 +56,7 @@ public class ConverterCommandTest {
|
||||
// then
|
||||
verify(commandService).send(sender, MessageKey.ERROR);
|
||||
verifyNoMoreInteractions(commandService);
|
||||
verifyZeroInteractions(initializer);
|
||||
verifyZeroInteractions(injector);
|
||||
verifyZeroInteractions(bukkitService);
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ public class ConverterCommandTest {
|
||||
// given
|
||||
ConverterCommand.ConvertType type = ConverterCommand.ConvertType.RAKAMAK;
|
||||
RakamakConverter converter = mock(RakamakConverter.class);
|
||||
given(initializer.newInstance(RakamakConverter.class)).willReturn(converter);
|
||||
given(injector.newInstance(RakamakConverter.class)).willReturn(converter);
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
|
||||
// when
|
||||
@@ -97,8 +97,8 @@ public class ConverterCommandTest {
|
||||
// then
|
||||
verify(converter).execute(sender);
|
||||
verifyNoMoreInteractions(converter);
|
||||
verify(initializer).newInstance(type.getConverterClass());
|
||||
verifyNoMoreInteractions(initializer);
|
||||
verify(injector).newInstance(type.getConverterClass());
|
||||
verifyNoMoreInteractions(injector);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package fr.xephi.authme.command.executable.authme;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
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.AuthMeServiceInitializer;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
@@ -25,7 +25,6 @@ import static org.hamcrest.Matchers.containsString;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
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.verify;
|
||||
|
||||
@@ -42,7 +41,7 @@ public class ReloadCommandTest {
|
||||
private AuthMe authMe;
|
||||
|
||||
@Mock
|
||||
private AuthMeServiceInitializer initializer;
|
||||
private Injector injector;
|
||||
|
||||
@Mock
|
||||
private NewSetting settings;
|
||||
@@ -77,7 +76,7 @@ public class ReloadCommandTest {
|
||||
|
||||
// then
|
||||
verify(settings).reload();
|
||||
verify(initializer).performReloadOnServices();
|
||||
// FIXME #835 verify(injector).performReloadOnServices();
|
||||
verify(commandService).send(sender, MessageKey.CONFIG_RELOAD_SUCCESS);
|
||||
}
|
||||
|
||||
@@ -85,7 +84,7 @@ public class ReloadCommandTest {
|
||||
public void shouldHandleReloadError() {
|
||||
// given
|
||||
CommandSender sender = mock(CommandSender.class);
|
||||
doThrow(IllegalStateException.class).when(initializer).performReloadOnServices();
|
||||
// FIXME #835 doThrow(IllegalStateException.class).when(injector).performReloadOnServices();
|
||||
given(settings.getProperty(DatabaseSettings.BACKEND)).willReturn(DataSourceType.MYSQL);
|
||||
given(dataSource.getType()).willReturn(DataSourceType.MYSQL);
|
||||
|
||||
@@ -94,7 +93,7 @@ public class ReloadCommandTest {
|
||||
|
||||
// then
|
||||
verify(settings).reload();
|
||||
verify(initializer).performReloadOnServices();
|
||||
// FIXME #835 verify(injector).performReloadOnServices();
|
||||
verify(sender).sendMessage(matches("Error occurred.*"));
|
||||
verify(authMe).stopOrUnload();
|
||||
}
|
||||
@@ -111,7 +110,7 @@ public class ReloadCommandTest {
|
||||
|
||||
// then
|
||||
verify(settings).reload();
|
||||
verify(initializer).performReloadOnServices();
|
||||
// FIXME #835 verify(injector).performReloadOnServices();
|
||||
verify(sender).sendMessage(argThat(containsString("cannot change database type")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package fr.xephi.authme.converter;
|
||||
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.ConverterSettings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@@ -1,340 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.initialization.samples.AlphaService;
|
||||
import fr.xephi.authme.initialization.samples.BadFieldInjection;
|
||||
import fr.xephi.authme.initialization.samples.BetaManager;
|
||||
import fr.xephi.authme.initialization.samples.CircularClasses;
|
||||
import fr.xephi.authme.initialization.samples.ClassWithAbstractDependency;
|
||||
import fr.xephi.authme.initialization.samples.ClassWithAnnotations;
|
||||
import fr.xephi.authme.initialization.samples.Duration;
|
||||
import fr.xephi.authme.initialization.samples.FieldInjectionWithAnnotations;
|
||||
import fr.xephi.authme.initialization.samples.GammaService;
|
||||
import fr.xephi.authme.initialization.samples.InstantiationFallbackClasses;
|
||||
import fr.xephi.authme.initialization.samples.InvalidClass;
|
||||
import fr.xephi.authme.initialization.samples.InvalidPostConstruct;
|
||||
import fr.xephi.authme.initialization.samples.InvalidStaticFieldInjection;
|
||||
import fr.xephi.authme.initialization.samples.PostConstructTestClass;
|
||||
import fr.xephi.authme.initialization.samples.ProvidedClass;
|
||||
import fr.xephi.authme.initialization.samples.Size;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link AuthMeServiceInitializer}.
|
||||
*/
|
||||
public class AuthMeServiceInitializerTest {
|
||||
|
||||
private static final String ALLOWED_PACKAGE = "fr.xephi.authme.initialization";
|
||||
|
||||
private AuthMeServiceInitializer initializer;
|
||||
|
||||
// As we test many cases that throw exceptions, we use JUnit's ExpectedException Rule
|
||||
// to make sure that we receive the exception we expect
|
||||
@Rule
|
||||
public ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setInitializer() {
|
||||
initializer = new AuthMeServiceInitializer(ALLOWED_PACKAGE);
|
||||
initializer.register(ProvidedClass.class, new ProvidedClass(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInitializeElements() {
|
||||
// given / when
|
||||
BetaManager betaManager = initializer.get(BetaManager.class);
|
||||
|
||||
// then
|
||||
assertThat(betaManager, not(nullValue()));
|
||||
for (Object o : betaManager.getDependencies()) {
|
||||
assertThat(o, not(nullValue()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForInvalidPackage() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("outside of the allowed packages");
|
||||
initializer.get(InvalidClass.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForUnregisteredPrimitiveType() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("Primitive types must be provided");
|
||||
initializer.get(int.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPassValueByAnnotation() {
|
||||
// given
|
||||
int size = 12;
|
||||
long duration = -15482L;
|
||||
initializer.provide(Size.class, size);
|
||||
initializer.provide(Duration.class, duration);
|
||||
|
||||
// when
|
||||
ClassWithAnnotations object = initializer.get(ClassWithAnnotations.class);
|
||||
|
||||
// then
|
||||
assertThat(object, not(nullValue()));
|
||||
assertThat(object.getSize(), equalTo(size));
|
||||
assertThat(object.getDuration(), equalTo(duration));
|
||||
// some sample check to make sure we only have one instance of GammaService
|
||||
assertThat(object.getGammaService(), equalTo(initializer.get(BetaManager.class).getDependencies()[1]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRecognizeCircularReferences() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("Found cyclic dependency");
|
||||
initializer.get(CircularClasses.Circular3.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForUnregisteredAnnotation() {
|
||||
// given
|
||||
initializer.provide(Size.class, 4523);
|
||||
|
||||
// when / then
|
||||
expectRuntimeExceptionWith("must be registered beforehand");
|
||||
initializer.get(ClassWithAnnotations.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForFieldInjectionWithoutNoArgsConstructor() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("Did not find injection method");
|
||||
initializer.get(BadFieldInjection.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInjectFieldsWithAnnotationsProperly() {
|
||||
// given
|
||||
initializer.provide(Size.class, 2809375);
|
||||
initializer.provide(Duration.class, 13095L);
|
||||
|
||||
// when
|
||||
FieldInjectionWithAnnotations result = initializer.get(FieldInjectionWithAnnotations.class);
|
||||
|
||||
// then
|
||||
assertThat(result.getSize(), equalTo(2809375));
|
||||
assertThat(result.getDuration(), equalTo(13095L));
|
||||
assertThat(result.getBetaManager(), not(nullValue()));
|
||||
assertThat(result.getClassWithAnnotations(), not(nullValue()));
|
||||
assertThat(result.getClassWithAnnotations().getGammaService(),
|
||||
equalTo(result.getBetaManager().getDependencies()[1]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForAnnotationAsKey() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("Cannot retrieve annotated elements in this way");
|
||||
initializer.get(Size.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForSecondRegistration() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("There is already an object present");
|
||||
initializer.register(ProvidedClass.class, new ProvidedClass(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForSecondAnnotationRegistration() {
|
||||
// given
|
||||
initializer.provide(Size.class, 12);
|
||||
|
||||
// when / then
|
||||
expectRuntimeExceptionWith("already registered");
|
||||
initializer.provide(Size.class, -8);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForNullValueAssociatedToAnnotation() {
|
||||
// given / when / then
|
||||
expectedException.expect(NullPointerException.class);
|
||||
initializer.provide(Duration.class, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForRegisterWithNull() {
|
||||
// given / when / then
|
||||
expectedException.expect(NullPointerException.class);
|
||||
initializer.register(String.class, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldExecutePostConstructMethod() {
|
||||
// given
|
||||
initializer.provide(Size.class, 15123);
|
||||
|
||||
// when
|
||||
PostConstructTestClass testClass = initializer.get(PostConstructTestClass.class);
|
||||
|
||||
// then
|
||||
assertThat(testClass.wasPostConstructCalled(), equalTo(true));
|
||||
assertThat(testClass.getBetaManager(), not(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForInvalidPostConstructMethod() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("@PostConstruct method may not be static or have any parameters");
|
||||
initializer.get(InvalidPostConstruct.WithParams.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForStaticPostConstructMethod() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("@PostConstruct method may not be static or have any parameters");
|
||||
initializer.get(InvalidPostConstruct.Static.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldForwardExceptionFromPostConstruct() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("Error executing @PostConstruct method");
|
||||
initializer.get(InvalidPostConstruct.ThrowsException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForMultiplePostConstructMethods() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("Multiple methods with @PostConstruct");
|
||||
initializer.get(InvalidPostConstruct.MultiplePostConstructs.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForPostConstructNotReturningVoid() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("@PostConstruct method must have return type void");
|
||||
initializer.get(InvalidPostConstruct.NotVoidReturnType.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForAbstractNonRegisteredDependency() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("cannot be instantiated");
|
||||
initializer.get(ClassWithAbstractDependency.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInstantiateWithImplementationOfAbstractDependency() {
|
||||
// given
|
||||
ClassWithAbstractDependency.ConcreteDependency concrete = new ClassWithAbstractDependency.ConcreteDependency();
|
||||
initializer.register(ClassWithAbstractDependency.AbstractDependency.class, concrete);
|
||||
|
||||
// when
|
||||
ClassWithAbstractDependency cwad = initializer.get(ClassWithAbstractDependency.class);
|
||||
|
||||
// then
|
||||
assertThat(cwad.getAbstractDependency() == concrete, equalTo(true));
|
||||
assertThat(cwad.getAlphaService(), not(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForAlreadyRegisteredClass() {
|
||||
// given
|
||||
initializer.register(BetaManager.class, new BetaManager());
|
||||
|
||||
// when / then
|
||||
expectRuntimeExceptionWith("There is already an object present");
|
||||
initializer.register(BetaManager.class, new BetaManager());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateNewUntrackedInstance() {
|
||||
// given / when
|
||||
AlphaService singletonScoped = initializer.get(AlphaService.class);
|
||||
AlphaService requestScoped = initializer.newInstance(AlphaService.class);
|
||||
|
||||
// then
|
||||
assertThat(singletonScoped.getProvidedClass(), not(nullValue()));
|
||||
assertThat(singletonScoped.getProvidedClass(), equalTo(requestScoped.getProvidedClass()));
|
||||
assertThat(singletonScoped, not(sameInstance(requestScoped)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForStaticFieldInjection() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("is static but annotated with @Inject");
|
||||
initializer.newInstance(InvalidStaticFieldInjection.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFallbackToSimpleInstantiationForPlainClass() {
|
||||
// given / when
|
||||
InstantiationFallbackClasses.HasFallbackDependency result =
|
||||
initializer.get(InstantiationFallbackClasses.HasFallbackDependency.class);
|
||||
|
||||
// then
|
||||
assertThat(result, not(nullValue()));
|
||||
assertThat(result.getGammaService(), not(nullValue()));
|
||||
assertThat(result.getFallbackDependency(), not(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPerformReloadOnApplicableInstances() {
|
||||
// given
|
||||
initializer.provide(Size.class, 12);
|
||||
initializer.provide(Duration.class, -113L);
|
||||
initializer.register(NewSetting.class, mock(NewSetting.class));
|
||||
|
||||
GammaService gammaService = initializer.get(GammaService.class);
|
||||
PostConstructTestClass postConstructTestClass = initializer.get(PostConstructTestClass.class);
|
||||
ProvidedClass providedClass = initializer.get(ProvidedClass.class);
|
||||
initializer.get(ClassWithAnnotations.class);
|
||||
// Assert that no class was somehow reloaded at initialization
|
||||
assertThat(gammaService.getWasReloaded() || postConstructTestClass.getWasReloaded()
|
||||
|| providedClass.getWasReloaded(), equalTo(false));
|
||||
|
||||
// when
|
||||
initializer.performReloadOnServices();
|
||||
|
||||
// then
|
||||
assertThat(gammaService.getWasReloaded(), equalTo(true));
|
||||
assertThat(postConstructTestClass.getWasReloaded(), equalTo(true));
|
||||
assertThat(providedClass.getWasReloaded(), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldThrowForNullSetting() {
|
||||
// given / when / then
|
||||
expectRuntimeExceptionWith("Settings instance is null");
|
||||
initializer.performReloadOnServices();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRetrieveExistingInstancesOnly() {
|
||||
// given
|
||||
initializer.get(GammaService.class);
|
||||
|
||||
// when
|
||||
AlphaService alphaService = initializer.getIfAvailable(AlphaService.class);
|
||||
BetaManager betaManager = initializer.getIfAvailable(BetaManager.class);
|
||||
|
||||
// then
|
||||
// was initialized because is dependency of GammaService
|
||||
assertThat(alphaService, not(nullValue()));
|
||||
// nothing caused this to be initialized
|
||||
assertThat(betaManager, nullValue());
|
||||
}
|
||||
|
||||
private void expectRuntimeExceptionWith(String message) {
|
||||
expectedException.expect(RuntimeException.class);
|
||||
expectedException.expectMessage(containsString(message));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.initialization.samples.AlphaService;
|
||||
import fr.xephi.authme.initialization.samples.BetaManager;
|
||||
import fr.xephi.authme.initialization.samples.ClassWithAnnotations;
|
||||
import fr.xephi.authme.initialization.samples.Duration;
|
||||
import fr.xephi.authme.initialization.samples.GammaService;
|
||||
import fr.xephi.authme.initialization.samples.InvalidClass;
|
||||
import fr.xephi.authme.initialization.samples.ProvidedClass;
|
||||
import fr.xephi.authme.initialization.samples.Size;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link ConstructorInjection}.
|
||||
*/
|
||||
public class ConstructorInjectionTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void shouldReturnDependencies() {
|
||||
// given
|
||||
Injection<ClassWithAnnotations> injection = ConstructorInjection.provide(ClassWithAnnotations.class).get();
|
||||
|
||||
// when
|
||||
Class<?>[] dependencies = injection.getDependencies();
|
||||
Class<?>[] annotations = injection.getDependencyAnnotations();
|
||||
|
||||
// then
|
||||
assertThat(dependencies, arrayContaining(int.class, GammaService.class, long.class));
|
||||
assertThat(annotations, arrayContaining((Class<?>) Size.class, null, Duration.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInstantiate() {
|
||||
// given
|
||||
GammaService gammaService = new GammaService(
|
||||
AlphaService.newInstance(new ProvidedClass("")));
|
||||
Injection<ClassWithAnnotations> injection = ConstructorInjection.provide(ClassWithAnnotations.class).get();
|
||||
|
||||
// when
|
||||
ClassWithAnnotations instance = injection.instantiateWith(-112, gammaService, 19L);
|
||||
|
||||
// then
|
||||
assertThat(instance, not(nullValue()));
|
||||
assertThat(instance.getSize(), equalTo(-112));
|
||||
assertThat(instance.getGammaService(), equalTo(gammaService));
|
||||
assertThat(instance.getDuration(), equalTo(19L));
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void shouldThrowForNullValue() {
|
||||
// given
|
||||
Injection<ClassWithAnnotations> injection = ConstructorInjection.provide(ClassWithAnnotations.class).get();
|
||||
|
||||
// when / then
|
||||
injection.instantiateWith(-112, null, 12L);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowUponInstantiationError() {
|
||||
// given
|
||||
AlphaService alphaService = AlphaService.newInstance(new ProvidedClass(""));
|
||||
Injection<InvalidClass> injection = ConstructorInjection.provide(InvalidClass.class).get();
|
||||
|
||||
// when
|
||||
injection.instantiateWith(alphaService, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForNoConstructorInjection() {
|
||||
// given / when
|
||||
Injection<BetaManager> injection = ConstructorInjection.provide(BetaManager.class).get();
|
||||
|
||||
// then
|
||||
assertThat(injection, nullValue());
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.initialization.samples.AlphaService;
|
||||
import fr.xephi.authme.initialization.samples.BadFieldInjection;
|
||||
import fr.xephi.authme.initialization.samples.BetaManager;
|
||||
import fr.xephi.authme.initialization.samples.ClassWithAnnotations;
|
||||
import fr.xephi.authme.initialization.samples.Duration;
|
||||
import fr.xephi.authme.initialization.samples.FieldInjectionWithAnnotations;
|
||||
import fr.xephi.authme.initialization.samples.GammaService;
|
||||
import fr.xephi.authme.initialization.samples.InvalidStaticFieldInjection;
|
||||
import fr.xephi.authme.initialization.samples.ProvidedClass;
|
||||
import fr.xephi.authme.initialization.samples.Size;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.hamcrest.Matchers.arrayContaining;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link FieldInjection}.
|
||||
*/
|
||||
|
||||
public class FieldInjectionTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void shouldReturnDependencies() {
|
||||
// given
|
||||
FieldInjection<FieldInjectionWithAnnotations> injection =
|
||||
FieldInjection.provide(FieldInjectionWithAnnotations.class).get();
|
||||
|
||||
// when
|
||||
Class<?>[] dependencies = injection.getDependencies();
|
||||
Class<?>[] annotations = injection.getDependencyAnnotations();
|
||||
|
||||
// then
|
||||
assertThat(dependencies, arrayContaining(BetaManager.class, int.class, long.class, ClassWithAnnotations.class));
|
||||
assertThat(annotations, arrayContaining((Class<?>) null, Size.class, Duration.class, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInstantiateClass() {
|
||||
// given
|
||||
FieldInjection<BetaManager> injection = FieldInjection.provide(BetaManager.class).get();
|
||||
ProvidedClass providedClass = new ProvidedClass("");
|
||||
AlphaService alphaService = AlphaService.newInstance(providedClass);
|
||||
GammaService gammaService = new GammaService(alphaService);
|
||||
|
||||
// when
|
||||
BetaManager betaManager = injection.instantiateWith(providedClass, gammaService, alphaService);
|
||||
|
||||
// then
|
||||
assertThat(betaManager, not(nullValue()));
|
||||
assertThat(betaManager.getDependencies(), arrayContaining(providedClass, gammaService, alphaService));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldProvideNullForImpossibleFieldInjection() {
|
||||
// given / when
|
||||
FieldInjection<BadFieldInjection> injection = FieldInjection.provide(BadFieldInjection.class).get();
|
||||
|
||||
// then
|
||||
assertThat(injection, nullValue());
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldForwardExceptionDuringInstantiation() {
|
||||
// given
|
||||
FieldInjection<ThrowingConstructor> injection = FieldInjection.provide(ThrowingConstructor.class).get();
|
||||
|
||||
// when / when
|
||||
injection.instantiateWith(new ProvidedClass(""));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForInvalidFieldValue() {
|
||||
// given
|
||||
ProvidedClass providedClass = new ProvidedClass("");
|
||||
AlphaService alphaService = AlphaService.newInstance(providedClass);
|
||||
GammaService gammaService = new GammaService(alphaService);
|
||||
FieldInjection<BetaManager> injection = FieldInjection.provide(BetaManager.class).get();
|
||||
|
||||
// when / then
|
||||
// Correct order is provided, gamma, alpha
|
||||
injection.instantiateWith(providedClass, alphaService, gammaService);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void shouldThrowForNullValue() {
|
||||
// given
|
||||
ProvidedClass providedClass = new ProvidedClass("");
|
||||
AlphaService alphaService = AlphaService.newInstance(providedClass);
|
||||
FieldInjection<BetaManager> injection = FieldInjection.provide(BetaManager.class).get();
|
||||
|
||||
// when / then
|
||||
// Correct order is provided, gamma, alpha
|
||||
injection.instantiateWith(providedClass, null, alphaService);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForStaticFieldInjection() {
|
||||
// given / when / then
|
||||
FieldInjection.provide(InvalidStaticFieldInjection.class).get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotReturnFieldInjectionForZeroInjectFields() {
|
||||
// given / when
|
||||
Injection<NoInjectionClass> injection = FieldInjection.provide(NoInjectionClass.class).get();
|
||||
|
||||
// then
|
||||
assertThat(injection, nullValue());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class ThrowingConstructor {
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
public ThrowingConstructor() {
|
||||
throw new UnsupportedOperationException("Exception in constructor");
|
||||
}
|
||||
}
|
||||
|
||||
private static class NoInjectionClass {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.initialization.samples.GammaService;
|
||||
import fr.xephi.authme.initialization.samples.InstantiationFallbackClasses;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link InstantiationFallback}.
|
||||
*/
|
||||
public class InstantiationFallbackTest {
|
||||
|
||||
@Test
|
||||
public void shouldInstantiateClass() {
|
||||
// given
|
||||
Injection<InstantiationFallbackClasses.FallbackClass> instantiation =
|
||||
InstantiationFallback.provide(InstantiationFallbackClasses.FallbackClass.class).get();
|
||||
|
||||
// when
|
||||
InstantiationFallbackClasses.FallbackClass result = instantiation.instantiateWith();
|
||||
|
||||
// then
|
||||
assertThat(result, not(nullValue()));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowIfArgumentsAreSupplied() {
|
||||
// given
|
||||
Injection<InstantiationFallbackClasses.FallbackClass> instantiation =
|
||||
InstantiationFallback.provide(InstantiationFallbackClasses.FallbackClass.class).get();
|
||||
|
||||
// when / then
|
||||
instantiation.instantiateWith("some argument");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForClassWithInjectMethod() {
|
||||
// given / when
|
||||
Injection<InstantiationFallbackClasses.InvalidInjectOnMethodClass> instantiation =
|
||||
InstantiationFallback.provide(InstantiationFallbackClasses.InvalidInjectOnMethodClass.class).get();
|
||||
|
||||
// then
|
||||
assertThat(instantiation, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForMissingNoArgsConstructor() {
|
||||
// given / when
|
||||
Injection<InstantiationFallbackClasses.InvalidFallbackClass> instantiation =
|
||||
InstantiationFallback.provide(InstantiationFallbackClasses.InvalidFallbackClass.class).get();
|
||||
|
||||
// then
|
||||
assertThat(instantiation, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForDifferentInjectionType() {
|
||||
// given / when
|
||||
Injection<GammaService> instantiation = InstantiationFallback.provide(GammaService.class).get();
|
||||
|
||||
// then
|
||||
assertThat(instantiation, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForClassWithPostConstruct() {
|
||||
// given / when
|
||||
Injection<InstantiationFallbackClasses.ClassWithPostConstruct> instantiation =
|
||||
InstantiationFallback.provide(InstantiationFallbackClasses.ClassWithPostConstruct.class).get();
|
||||
|
||||
// then
|
||||
assertThat(instantiation, nullValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample - class with dependency to ProvidedClass.
|
||||
*/
|
||||
public class AlphaService {
|
||||
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@Inject
|
||||
AlphaService(ProvidedClass providedClass) {
|
||||
this.providedClass = providedClass;
|
||||
}
|
||||
|
||||
public ProvidedClass getProvidedClass() {
|
||||
return providedClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance (for instantiations in tests).
|
||||
*
|
||||
* @param providedClass .
|
||||
* @return created instance
|
||||
*/
|
||||
public static AlphaService newInstance(ProvidedClass providedClass) {
|
||||
return new AlphaService(providedClass);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample class with invalid field injection (requires default constructor).
|
||||
*/
|
||||
public class BadFieldInjection {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Inject
|
||||
private AlphaService alphaService;
|
||||
|
||||
public BadFieldInjection(BetaManager betaManager) {
|
||||
throw new IllegalStateException("Should never be called");
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample - depends on Provided, alpha and gamma.
|
||||
*/
|
||||
public class BetaManager {
|
||||
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
@Inject
|
||||
private GammaService gammaService;
|
||||
@Inject
|
||||
private AlphaService alphaService;
|
||||
|
||||
public Object[] getDependencies() {
|
||||
return new Object[]{providedClass, gammaService, alphaService};
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Classes with circular dependencies.
|
||||
*/
|
||||
public abstract class CircularClasses {
|
||||
|
||||
public static final class Circular1 {
|
||||
@Inject
|
||||
public Circular1(AlphaService alphaService, Circular3 circular3) {
|
||||
// --
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Circular2 {
|
||||
@Inject
|
||||
public Circular2(Circular1 circular1) {
|
||||
// --
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Circular3 {
|
||||
@Inject
|
||||
public Circular3(Circular2 circular2, BetaManager betaManager) {
|
||||
// --
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Test with an abstract class declared as dependency.
|
||||
*/
|
||||
public class ClassWithAbstractDependency {
|
||||
|
||||
private final AlphaService alphaService;
|
||||
private final AbstractDependency abstractDependency;
|
||||
|
||||
@Inject
|
||||
public ClassWithAbstractDependency(AlphaService as, AbstractDependency ad) {
|
||||
this.alphaService = as;
|
||||
this.abstractDependency = ad;
|
||||
}
|
||||
|
||||
public AlphaService getAlphaService() {
|
||||
return alphaService;
|
||||
}
|
||||
|
||||
public AbstractDependency getAbstractDependency() {
|
||||
return abstractDependency;
|
||||
}
|
||||
|
||||
public static abstract class AbstractDependency {
|
||||
}
|
||||
|
||||
public static final class ConcreteDependency extends AbstractDependency {
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class ClassWithAnnotations {
|
||||
|
||||
private int size;
|
||||
private GammaService gammaService;
|
||||
private long duration;
|
||||
|
||||
@Inject
|
||||
ClassWithAnnotations(@Size int size, GammaService gammaService, @Duration long duration) {
|
||||
this.size = size;
|
||||
this.gammaService = gammaService;
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public GammaService getGammaService() {
|
||||
return gammaService;
|
||||
}
|
||||
|
||||
public long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Sample annotation.
|
||||
*/
|
||||
@Target({ElementType.PARAMETER, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Duration {
|
||||
}
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample - field injection, including custom annotations.
|
||||
*/
|
||||
public class FieldInjectionWithAnnotations {
|
||||
|
||||
@Inject
|
||||
private BetaManager betaManager;
|
||||
@Inject
|
||||
@Size
|
||||
private int size;
|
||||
@Duration
|
||||
@Inject
|
||||
private long duration;
|
||||
@Inject
|
||||
protected ClassWithAnnotations classWithAnnotations;
|
||||
|
||||
FieldInjectionWithAnnotations() {
|
||||
}
|
||||
|
||||
public BetaManager getBetaManager() {
|
||||
return betaManager;
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public ClassWithAnnotations getClassWithAnnotations() {
|
||||
return classWithAnnotations;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample - class dependent on alpha service.
|
||||
*/
|
||||
public class GammaService implements Reloadable {
|
||||
|
||||
private AlphaService alphaService;
|
||||
private boolean wasReloaded;
|
||||
|
||||
@Inject
|
||||
public GammaService(AlphaService alphaService) {
|
||||
this.alphaService = alphaService;
|
||||
}
|
||||
|
||||
public AlphaService getAlphaService() {
|
||||
return alphaService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
wasReloaded = true;
|
||||
}
|
||||
|
||||
public boolean getWasReloaded() {
|
||||
return wasReloaded;
|
||||
}
|
||||
}
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample class - tests various situations for the instantiation fallback.
|
||||
*/
|
||||
public abstract class InstantiationFallbackClasses {
|
||||
|
||||
public static final class FallbackClass {
|
||||
// No @Inject annotations, public no-args constructor
|
||||
}
|
||||
|
||||
public static final class HasFallbackDependency {
|
||||
@Inject
|
||||
private FallbackClass fallbackClass;
|
||||
|
||||
@Inject
|
||||
private GammaService gammaService;
|
||||
|
||||
public GammaService getGammaService() {
|
||||
return gammaService;
|
||||
}
|
||||
|
||||
public FallbackClass getFallbackDependency() {
|
||||
return fallbackClass;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class InvalidFallbackClass {
|
||||
private InvalidFallbackClass() {
|
||||
// no-args constructor must be public for fallback instantiation
|
||||
}
|
||||
}
|
||||
|
||||
public static final class InvalidInjectOnMethodClass {
|
||||
// We don't support method injection but this should still be detected and an exception returned
|
||||
// Only use instantiation fallback if we're sure there isn't some sort of misconfiguration
|
||||
@Inject
|
||||
public void setGammaService(GammaService gammaService) {
|
||||
// --
|
||||
}
|
||||
}
|
||||
|
||||
// Class with @PostConstruct method should never be instantiated by instantiation fallback
|
||||
public static final class ClassWithPostConstruct {
|
||||
@PostConstruct
|
||||
public void postConstructMethod() {
|
||||
// --
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample - invalid class, since Integer parameter type is outside of the allowed package and not annotated.
|
||||
*/
|
||||
public class InvalidClass {
|
||||
|
||||
@Inject
|
||||
public InvalidClass(AlphaService alphaService, Integer i) {
|
||||
throw new IllegalStateException("Should never be called");
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Class with invalid @PostConstruct method.
|
||||
*/
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class InvalidPostConstruct {
|
||||
|
||||
public static final class WithParams {
|
||||
@Inject
|
||||
private AlphaService alphaService;
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
WithParams() { }
|
||||
|
||||
@PostConstruct
|
||||
public void invalidPostConstr(BetaManager betaManager) {
|
||||
}
|
||||
}
|
||||
|
||||
public static final class Static {
|
||||
@Inject
|
||||
Static(BetaManager betaManager) {
|
||||
// --
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public static void invalidMethod() {
|
||||
// --
|
||||
}
|
||||
}
|
||||
|
||||
public static final class ThrowsException {
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@PostConstruct
|
||||
public void throwingPostConstruct() {
|
||||
throw new IllegalStateException("Exception in post construct");
|
||||
}
|
||||
}
|
||||
|
||||
public static final class NotVoidReturnType {
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@PostConstruct
|
||||
public int returnsInt() {
|
||||
return 42;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class MultiplePostConstructs {
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct1() {
|
||||
// --
|
||||
}
|
||||
@PostConstruct
|
||||
public void postConstruct2() {
|
||||
// --
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample class - attempted field injection on a static member.
|
||||
*/
|
||||
public class InvalidStaticFieldInjection {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
@Inject
|
||||
protected static AlphaService alphaService;
|
||||
|
||||
InvalidStaticFieldInjection() { }
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import fr.xephi.authme.initialization.SettingsDependent;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample class for testing the execution of @PostConstruct methods.
|
||||
*/
|
||||
public class PostConstructTestClass implements SettingsDependent {
|
||||
|
||||
@Inject
|
||||
@Size
|
||||
private int size;
|
||||
@Inject
|
||||
private BetaManager betaManager;
|
||||
private boolean wasPostConstructCalled = false;
|
||||
private boolean wasReloaded = false;
|
||||
|
||||
@PostConstruct
|
||||
public void postConstructMethod() {
|
||||
wasPostConstructCalled = true;
|
||||
}
|
||||
|
||||
public boolean wasPostConstructCalled() {
|
||||
return wasPostConstructCalled;
|
||||
}
|
||||
|
||||
public BetaManager getBetaManager() {
|
||||
return betaManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(NewSetting settings) {
|
||||
if (settings != null) {
|
||||
wasReloaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getWasReloaded() {
|
||||
return wasReloaded;
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import fr.xephi.authme.initialization.Reloadable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Sample - class that is always provided to the initializer beforehand.
|
||||
*/
|
||||
public class ProvidedClass implements Reloadable {
|
||||
|
||||
private boolean wasReloaded = false;
|
||||
|
||||
@Inject
|
||||
public ProvidedClass() {
|
||||
throw new IllegalStateException("Should never be called (tests always provide this class)");
|
||||
}
|
||||
|
||||
public ProvidedClass(String manualConstructor) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload() {
|
||||
wasReloaded = true;
|
||||
}
|
||||
|
||||
public boolean getWasReloaded() {
|
||||
return wasReloaded;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package fr.xephi.authme.initialization.samples;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Sample annotation.
|
||||
*/
|
||||
@Target({ElementType.PARAMETER, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Size {
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package fr.xephi.authme.listener;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.hooks.PluginHooks;
|
||||
import fr.xephi.authme.runner.BeforeInjecting;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Marks methods to run before {@link InjectDelayed} fields are instantiated.
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface BeforeInjecting {
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.initialization.Injection;
|
||||
import fr.xephi.authme.initialization.InjectionHelper;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.FrameworkField;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.junit.runners.model.Statement;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Custom JUnit runner which adds support for {@link InjectDelayed} and {@link BeforeInjecting}.
|
||||
* This runner also initializes fields with Mockito's {@link Mock}, {@link org.mockito.Spy} and
|
||||
* {@link org.mockito.InjectMocks}.
|
||||
* <p>
|
||||
* Mockito's {@link Mock} and {@link org.mockito.InjectMocks} are initialized <i>before</i>
|
||||
* {@link org.junit.Before} methods are run. This leaves no possibility to initialize some mock
|
||||
* behavior before {@link org.mockito.InjectMocks} fields get instantiated.
|
||||
* <p>
|
||||
* The runner fills this gap by introducing {@link BeforeInjecting}. At the time these methods
|
||||
* are run Mockito's annotation will have taken effect but not {@link InjectDelayed}. Fields with
|
||||
* this annotation are initialized after {@link BeforeInjecting} methods have been run.
|
||||
* <p>
|
||||
* Additionally, after a field annotated with {@link InjectDelayed} has been initialized, its
|
||||
* {@link javax.annotation.PostConstruct} method will be invoked, if available.
|
||||
* <p>
|
||||
* Important: It is required to declare all dependencies of classes annotated with
|
||||
* {@link InjectDelayed} as {@link Mock} fields. If a dependency is missing, an exception
|
||||
* will be thrown.
|
||||
*/
|
||||
public class DelayedInjectionRunner extends BlockJUnit4ClassRunner {
|
||||
|
||||
public DelayedInjectionRunner(Class<?> clazz) throws InitializationError {
|
||||
super(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
|
||||
// Initialize all Mockito annotations
|
||||
MockitoAnnotations.initMocks(target);
|
||||
|
||||
// Call chain normally: let parent handle @Before methods.
|
||||
// Note that the chain of statements will be run from the end to the start,
|
||||
// so @Before will be run AFTER our custom statements below
|
||||
statement = super.withBefores(method, target, statement);
|
||||
|
||||
// Add support for @BeforeInjecting and @InjectDelayed (again, reverse order)
|
||||
statement = withDelayedInjects(target, statement);
|
||||
return withBeforeInjectings(target, statement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(final RunNotifier notifier) {
|
||||
// add listener that validates framework usage at the end of each test
|
||||
notifier.addListener(new DelayedInjectionRunnerValidator(notifier, getTestClass()));
|
||||
super.run(notifier);
|
||||
}
|
||||
|
||||
/* Adds a Statement to the chain if @BeforeInjecting methods are present. */
|
||||
private Statement withBeforeInjectings(Object target, Statement statement) {
|
||||
List<FrameworkMethod> beforeInjectings = getTestClass().getAnnotatedMethods(BeforeInjecting.class);
|
||||
return beforeInjectings.isEmpty()
|
||||
? statement
|
||||
: new RunBeforeInjectings(statement, beforeInjectings, target);
|
||||
}
|
||||
|
||||
/*
|
||||
* Adds a Statement to the chain if @InjectDelayed methods are present.
|
||||
* If fields have been found, the injection for the type is resolved and stored with the necessary dependencies.
|
||||
*/
|
||||
private Statement withDelayedInjects(Object target, Statement statement) {
|
||||
List<FrameworkField> delayedFields = getTestClass().getAnnotatedFields(InjectDelayed.class);
|
||||
if (delayedFields.isEmpty()) {
|
||||
return statement;
|
||||
}
|
||||
|
||||
List<PendingInjection> pendingInjections = new ArrayList<>(delayedFields.size());
|
||||
for (FrameworkField field : delayedFields) {
|
||||
pendingInjections.add(new PendingInjection(field.getField(), getInjection(field)));
|
||||
}
|
||||
InjectionResolver injectionResolver = new InjectionResolver(getTestClass(), target);
|
||||
return new RunDelayedInjects(statement, pendingInjections, target, injectionResolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the injection method for the given field's type and ensures an injection method has been found.
|
||||
*
|
||||
* @param field the field to get the injection for
|
||||
* @return the injection
|
||||
*/
|
||||
private static Injection<?> getInjection(FrameworkField field) {
|
||||
final Injection<?> injection = InjectionHelper.getInjection(field.getType());
|
||||
if (injection == null) {
|
||||
throw new IllegalStateException("No injection method available for field '" + field.getName() + "'");
|
||||
}
|
||||
return injection;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runner.notification.Failure;
|
||||
import org.junit.runner.notification.RunListener;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.model.TestClass;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
/**
|
||||
* Validates that {@link DelayedInjectionRunner} is used as intended.
|
||||
*/
|
||||
class DelayedInjectionRunnerValidator extends RunListener {
|
||||
|
||||
private final RunNotifier notifier;
|
||||
private final TestClass testClass;
|
||||
|
||||
public DelayedInjectionRunnerValidator(RunNotifier notifier, TestClass testClass) {
|
||||
this.notifier = notifier;
|
||||
this.testClass = testClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testFinished(Description description) throws Exception {
|
||||
try {
|
||||
Mockito.validateMockitoUsage();
|
||||
if (!testClass.getAnnotatedFields(InjectMocks.class).isEmpty()) {
|
||||
throw new IllegalStateException("Do not use @InjectMocks with the DelayedInjectionRunner:" +
|
||||
" use @InjectDelayed or change runner");
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
notifier.fireTestFailure(new Failure(description, t));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Marks fields to instantiate with mocks after {@link BeforeInjecting} methods.
|
||||
*
|
||||
* @see DelayedInjectionRunner
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface InjectDelayed {
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.initialization.Injection;
|
||||
import fr.xephi.authme.initialization.InjectionHelper;
|
||||
import org.junit.runners.model.FrameworkField;
|
||||
import org.junit.runners.model.TestClass;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Resolves the dependencies of an injection based on the provided {@link TestClass} and {@link #target target}.
|
||||
*/
|
||||
class InjectionResolver {
|
||||
|
||||
private final TestClass testClass;
|
||||
private final Object target;
|
||||
private final Map<Class<?>, Object> mocksByType;
|
||||
|
||||
public InjectionResolver(TestClass testClass, Object target) {
|
||||
this.testClass = testClass;
|
||||
this.target = target;
|
||||
this.mocksByType = gatherAvailableMocks();
|
||||
}
|
||||
|
||||
public Object instantiate(Injection<?> injection) {
|
||||
Object[] dependencies = resolveDependencies(injection);
|
||||
Object object = injection.instantiateWith(dependencies);
|
||||
executePostConstructMethod(object);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all objects for the given list of dependencies, retrieved from the given
|
||||
* target's {@link Mock} fields.
|
||||
*
|
||||
* @param injection the injection whose dependencies to gather
|
||||
* @return the resolved dependencies
|
||||
*/
|
||||
private Object[] resolveDependencies(Injection<?> injection) {
|
||||
final Class<?>[] dependencies = injection.getDependencies();
|
||||
final Class<?>[] annotations = injection.getDependencyAnnotations();
|
||||
Object[] resolvedValues = new Object[dependencies.length];
|
||||
for (int i = 0; i < dependencies.length; ++i) {
|
||||
Object dependency = (annotations[i] == null)
|
||||
? resolveDependency(dependencies[i])
|
||||
: resolveAnnotation(annotations[i]);
|
||||
resolvedValues[i] = dependency;
|
||||
}
|
||||
return resolvedValues;
|
||||
}
|
||||
|
||||
private Object resolveDependency(Class<?> clazz) {
|
||||
Object o = mocksByType.get(clazz);
|
||||
if (o == null) {
|
||||
throw new IllegalStateException("No mock found for '" + clazz + "'. "
|
||||
+ "All dependencies of @InjectDelayed must be provided as @Mock fields");
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
private Object resolveAnnotation(Class<?> clazz) {
|
||||
Class<? extends Annotation> annotation = (Class<? extends Annotation>) clazz;
|
||||
List<FrameworkField> matches = testClass.getAnnotatedFields(annotation);
|
||||
if (matches.isEmpty()) {
|
||||
throw new IllegalStateException("No field found with @" + annotation.getSimpleName() + " in test class,"
|
||||
+ "but a dependency in an @InjectDelayed field is using it");
|
||||
} else if (matches.size() > 1) {
|
||||
throw new IllegalStateException("You cannot have multiple fields with @" + annotation.getSimpleName());
|
||||
}
|
||||
return ReflectionTestUtils.getFieldValue(matches.get(0).getField(), target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the class' PostConstruct method if available. Validates that all rules for
|
||||
* {@link javax.annotation.PostConstruct} are met.
|
||||
*
|
||||
* @param object the object whose PostConstruct method should be run, if available
|
||||
* @see InjectionHelper#getAndValidatePostConstructMethod
|
||||
*/
|
||||
private static void executePostConstructMethod(Object object) {
|
||||
Method postConstructMethod = InjectionHelper.getAndValidatePostConstructMethod(object.getClass());
|
||||
if (postConstructMethod != null) {
|
||||
ReflectionTestUtils.invokeMethod(postConstructMethod, object);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<Class<?>, Object> gatherAvailableMocks() {
|
||||
List<FrameworkField> availableMocks = testClass.getAnnotatedFields(Mock.class);
|
||||
Map<Class<?>, Object> mocksByType = new HashMap<>();
|
||||
for (FrameworkField frameworkField : availableMocks) {
|
||||
Field field = frameworkField.getField();
|
||||
Object fieldValue = ReflectionTestUtils.getFieldValue(field, target);
|
||||
mocksByType.put(field.getType(), fieldValue);
|
||||
}
|
||||
return mocksByType;
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.initialization.Injection;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* Contains an injection and the field it's for.
|
||||
*/
|
||||
class PendingInjection {
|
||||
|
||||
private final Field field;
|
||||
private final Injection<?> injection;
|
||||
|
||||
public PendingInjection(Field field, Injection<?> injection) {
|
||||
this.field = field;
|
||||
this.injection = injection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the injection to perform.
|
||||
*
|
||||
* @return the injection
|
||||
*/
|
||||
public Injection<?> getInjection() {
|
||||
return injection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the field the constructed object should be assigned to.
|
||||
*
|
||||
* @return the field in the test class
|
||||
*/
|
||||
public Field getField() {
|
||||
return field;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Statement for running {@link BeforeInjecting} methods. Such methods are run
|
||||
* after Mockito's @Mock, @Spy and @InjectMocks have taken effect,
|
||||
* but before {@link InjectDelayed} fields are handled.
|
||||
*/
|
||||
class RunBeforeInjectings extends Statement {
|
||||
|
||||
private final Statement next;
|
||||
private final List<FrameworkMethod> beforeInjectings;
|
||||
private final Object target;
|
||||
|
||||
public RunBeforeInjectings(Statement next, List<FrameworkMethod> beforeInjectings, Object target) {
|
||||
this.next = next;
|
||||
this.beforeInjectings = beforeInjectings;
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
for (FrameworkMethod method : beforeInjectings) {
|
||||
ReflectionTestUtils.invokeMethod(method.getMethod(), target);
|
||||
}
|
||||
next.evaluate();
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package fr.xephi.authme.runner;
|
||||
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Statement for initializing {@link InjectDelayed} fields. These fields are
|
||||
* constructed after {@link BeforeInjecting} and before JUnit's @Before.
|
||||
*/
|
||||
class RunDelayedInjects extends Statement {
|
||||
|
||||
private final Statement next;
|
||||
private final Object target;
|
||||
private List<PendingInjection> pendingInjections;
|
||||
private InjectionResolver injectionResolver;
|
||||
|
||||
public RunDelayedInjects(Statement next, List<PendingInjection> pendingInjections, Object target,
|
||||
InjectionResolver injectionResolver) {
|
||||
this.next = next;
|
||||
this.pendingInjections = pendingInjections;
|
||||
this.target = target;
|
||||
this.injectionResolver = injectionResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
for (PendingInjection pendingInjection : pendingInjections) {
|
||||
if (ReflectionTestUtils.getFieldValue(pendingInjection.getField(), target) != null) {
|
||||
throw new IllegalStateException("Field with @InjectDelayed must be null on startup");
|
||||
}
|
||||
Object object = injectionResolver.instantiate(pendingInjection.getInjection());
|
||||
ReflectionTestUtils.setField(pendingInjection.getField(), target, object);
|
||||
}
|
||||
this.pendingInjections = null;
|
||||
this.injectionResolver = null;
|
||||
next.evaluate();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package fr.xephi.authme.security;
|
||||
|
||||
import fr.xephi.authme.initialization.AuthMeServiceInitializer;
|
||||
import ch.jalu.injector.Injector;
|
||||
import ch.jalu.injector.InjectorBuilder;
|
||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
@@ -24,15 +25,15 @@ import static org.mockito.Mockito.mock;
|
||||
*/
|
||||
public class HashAlgorithmIntegrationTest {
|
||||
|
||||
private static AuthMeServiceInitializer initializer;
|
||||
private static Injector injector;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpConfigAndInjector() {
|
||||
NewSetting settings = mock(NewSetting.class);
|
||||
given(settings.getProperty(HooksSettings.BCRYPT_LOG2_ROUND)).willReturn(8);
|
||||
given(settings.getProperty(SecuritySettings.DOUBLE_MD5_SALT_LENGTH)).willReturn(16);
|
||||
initializer = new AuthMeServiceInitializer();
|
||||
initializer.register(NewSetting.class, settings);
|
||||
injector = new InjectorBuilder().addDefaultHandlers("fr.xephi.authme").create();
|
||||
injector.register(NewSetting.class, settings);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -55,7 +56,7 @@ public class HashAlgorithmIntegrationTest {
|
||||
// given / when / then
|
||||
for (HashAlgorithm algorithm : HashAlgorithm.values()) {
|
||||
if (!HashAlgorithm.CUSTOM.equals(algorithm) && !HashAlgorithm.PLAINTEXT.equals(algorithm)) {
|
||||
EncryptionMethod method = initializer.newInstance(algorithm.getClazz());
|
||||
EncryptionMethod method = injector.newInstance(algorithm.getClazz());
|
||||
HashedPassword hashedPassword = method.computeHash("pwd", "name");
|
||||
assertThat("Salt should not be null if method.hasSeparateSalt(), and vice versa. Method: '"
|
||||
+ method + "'", StringUtils.isEmpty(hashedPassword.getSalt()), equalTo(!method.hasSeparateSalt()));
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package fr.xephi.authme.security;
|
||||
|
||||
import ch.jalu.injector.Injector;
|
||||
import ch.jalu.injector.InjectorBuilder;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.events.PasswordEncryptionEvent;
|
||||
import fr.xephi.authme.initialization.AuthMeServiceInitializer;
|
||||
import fr.xephi.authme.security.crypts.EncryptionMethod;
|
||||
import fr.xephi.authme.security.crypts.HashedPassword;
|
||||
import fr.xephi.authme.security.crypts.JOOMLA;
|
||||
@@ -41,7 +42,7 @@ import static org.mockito.Mockito.verify;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class PasswordSecurityTest {
|
||||
|
||||
private AuthMeServiceInitializer initializer;
|
||||
private Injector injector;
|
||||
|
||||
@Mock
|
||||
private NewSetting settings;
|
||||
@@ -79,10 +80,10 @@ public class PasswordSecurityTest {
|
||||
return null;
|
||||
}
|
||||
}).when(pluginManager).callEvent(any(Event.class));
|
||||
initializer = new AuthMeServiceInitializer();
|
||||
initializer.register(NewSetting.class, settings);
|
||||
initializer.register(DataSource.class, dataSource);
|
||||
initializer.register(PluginManager.class, pluginManager);
|
||||
injector = new InjectorBuilder().addDefaultHandlers("!! impossible package !!").create();
|
||||
injector.register(NewSetting.class, settings);
|
||||
injector.register(DataSource.class, dataSource);
|
||||
injector.register(PluginManager.class, pluginManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,7 +98,7 @@ public class PasswordSecurityTest {
|
||||
given(dataSource.getPassword(playerName)).willReturn(password);
|
||||
given(method.comparePassword(clearTextPass, password, playerLowerCase)).willReturn(true);
|
||||
initSettings(HashAlgorithm.BCRYPT, false);
|
||||
PasswordSecurity security = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity security = injector.newInstance(PasswordSecurity.class);
|
||||
|
||||
// when
|
||||
boolean result = security.comparePassword(clearTextPass, playerName);
|
||||
@@ -120,7 +121,7 @@ public class PasswordSecurityTest {
|
||||
given(dataSource.getPassword(playerName)).willReturn(password);
|
||||
given(method.comparePassword(clearTextPass, password, playerLowerCase)).willReturn(false);
|
||||
initSettings(HashAlgorithm.CUSTOM, false);
|
||||
PasswordSecurity security = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity security = injector.newInstance(PasswordSecurity.class);
|
||||
|
||||
// when
|
||||
boolean result = security.comparePassword(clearTextPass, playerName);
|
||||
@@ -140,7 +141,7 @@ public class PasswordSecurityTest {
|
||||
|
||||
given(dataSource.getPassword(playerName)).willReturn(null);
|
||||
initSettings(HashAlgorithm.MD5, false);
|
||||
PasswordSecurity security = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity security = injector.newInstance(PasswordSecurity.class);
|
||||
|
||||
// when
|
||||
boolean result = security.comparePassword(clearTextPass, playerName);
|
||||
@@ -168,7 +169,7 @@ public class PasswordSecurityTest {
|
||||
given(method.comparePassword(clearTextPass, password, playerLowerCase)).willReturn(false);
|
||||
given(method.computeHash(clearTextPass, playerLowerCase)).willReturn(newPassword);
|
||||
initSettings(HashAlgorithm.MD5, true);
|
||||
PasswordSecurity security = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity security = injector.newInstance(PasswordSecurity.class);
|
||||
|
||||
// when
|
||||
boolean result = security.comparePassword(clearTextPass, playerName);
|
||||
@@ -193,7 +194,7 @@ public class PasswordSecurityTest {
|
||||
given(dataSource.getPassword(playerName)).willReturn(password);
|
||||
given(method.comparePassword(clearTextPass, password, playerName)).willReturn(false);
|
||||
initSettings(HashAlgorithm.MD5, true);
|
||||
PasswordSecurity security = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity security = injector.newInstance(PasswordSecurity.class);
|
||||
|
||||
// when
|
||||
boolean result = security.comparePassword(clearTextPass, playerName);
|
||||
@@ -212,7 +213,7 @@ public class PasswordSecurityTest {
|
||||
HashedPassword hashedPassword = new HashedPassword("$T$est#Hash", "__someSalt__");
|
||||
given(method.computeHash(password, usernameLowerCase)).willReturn(hashedPassword);
|
||||
initSettings(HashAlgorithm.JOOMLA, true);
|
||||
PasswordSecurity security = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity security = injector.newInstance(PasswordSecurity.class);
|
||||
|
||||
// when
|
||||
HashedPassword result = security.computeHash(password, username);
|
||||
@@ -235,7 +236,7 @@ public class PasswordSecurityTest {
|
||||
given(method.computeHash(password, username)).willReturn(hashedPassword);
|
||||
given(method.hasSeparateSalt()).willReturn(true);
|
||||
initSettings(HashAlgorithm.XAUTH, false);
|
||||
PasswordSecurity security = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity security = injector.newInstance(PasswordSecurity.class);
|
||||
|
||||
// when
|
||||
boolean result = security.comparePassword(password, hashedPassword, username);
|
||||
@@ -251,7 +252,7 @@ public class PasswordSecurityTest {
|
||||
public void shouldReloadSettings() {
|
||||
// given
|
||||
initSettings(HashAlgorithm.BCRYPT, false);
|
||||
PasswordSecurity passwordSecurity = initializer.newInstance(PasswordSecurity.class);
|
||||
PasswordSecurity passwordSecurity = injector.newInstance(PasswordSecurity.class);
|
||||
given(settings.getProperty(SecuritySettings.PASSWORD_HASH)).willReturn(HashAlgorithm.MD5);
|
||||
given(settings.getProperty(SecuritySettings.SUPPORT_OLD_PASSWORD_HASH)).willReturn(true);
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import com.google.common.io.Files;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.hooks.PluginHooks;
|
||||
import fr.xephi.authme.initialization.DataFolder;
|
||||
import fr.xephi.authme.runner.BeforeInjecting;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package fr.xephi.authme.task;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import fr.xephi.authme.ReflectionTestUtils;
|
||||
import fr.xephi.authme.TestHelper;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.hooks.PluginHooks;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PlayerStatePermission;
|
||||
import fr.xephi.authme.runner.BeforeInjecting;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.PurgeSettings;
|
||||
import fr.xephi.authme.util.BukkitService;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import ch.jalu.injector.testing.BeforeInjecting;
|
||||
import ch.jalu.injector.testing.DelayedInjectionRunner;
|
||||
import ch.jalu.injector.testing.InjectDelayed;
|
||||
import com.google.common.base.Strings;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.permission.PermissionsManager;
|
||||
import fr.xephi.authme.permission.PlayerStatePermission;
|
||||
import fr.xephi.authme.runner.BeforeInjecting;
|
||||
import fr.xephi.authme.runner.DelayedInjectionRunner;
|
||||
import fr.xephi.authme.runner.InjectDelayed;
|
||||
import fr.xephi.authme.settings.NewSetting;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
|
||||
Reference in New Issue
Block a user