Injector - disallow static PostConstruct methods, add more tests
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
@@ -18,6 +19,7 @@ import org.junit.Test;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -47,12 +49,18 @@ public class AuthMeServiceInitializerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForInvalidPackage() {
|
||||
// given / when / then
|
||||
initializer.get(InvalidClass.class);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForUnregisteredPrimitiveType() {
|
||||
// given / when / then
|
||||
initializer.get(int.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldPassValueByAnnotation() {
|
||||
// given
|
||||
@@ -169,7 +177,19 @@ public class AuthMeServiceInitializerTest {
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForInvalidPostConstructMethod() {
|
||||
// given / when / then
|
||||
initializer.get(InvalidPostConstruct.class);
|
||||
initializer.get(InvalidPostConstruct.WithParams.class);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForStaticPostConstructMethod() {
|
||||
// given / when / then
|
||||
initializer.get(InvalidPostConstruct.Static.class);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldForwardExceptionFromPostConstruct() {
|
||||
// given / when / then
|
||||
initializer.get(InvalidPostConstruct.ThrowsException.class);
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
@@ -191,4 +211,26 @@ public class AuthMeServiceInitializerTest {
|
||||
assertThat(cwad.getAbstractDependency() == concrete, equalTo(true));
|
||||
assertThat(cwad.getAlphaService(), not(nullValue()));
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void shouldThrowForAlreadyRegisteredClass() {
|
||||
// given
|
||||
initializer.register(new BetaManager());
|
||||
|
||||
// when / then
|
||||
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)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
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.FieldInjectionWithAnnotations;
|
||||
import fr.xephi.authme.initialization.samples.GammaService;
|
||||
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.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link FieldInjection}.
|
||||
*/
|
||||
public class FieldInjectionTest {
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,4 +17,14 @@ public class AlphaService {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ public class GammaService {
|
||||
private AlphaService alphaService;
|
||||
|
||||
@Inject
|
||||
GammaService(AlphaService alphaService) {
|
||||
public GammaService(AlphaService alphaService) {
|
||||
this.alphaService = alphaService;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,12 +8,35 @@ import javax.inject.Inject;
|
||||
*/
|
||||
public class InvalidPostConstruct {
|
||||
|
||||
@Inject
|
||||
private AlphaService alphaService;
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
public static final class WithParams {
|
||||
@Inject
|
||||
private AlphaService alphaService;
|
||||
@Inject
|
||||
private ProvidedClass providedClass;
|
||||
|
||||
@PostConstruct
|
||||
public void invalidPostConstr(BetaManager betaManager) {
|
||||
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 {
|
||||
@PostConstruct
|
||||
public void throwingPostConstruct() {
|
||||
throw new IllegalStateException("Exception in post construct");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user