#835 Replace injector classes with ones from ch.jalu.injector project
This commit is contained in:
@@ -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 {
|
||||
}
|
||||
Reference in New Issue
Block a user