use of com.google.inject.AbstractModule in project ninja by ninjaframework.
the class ControllerMethodInvokerTest method create.
private ControllerMethodInvoker create(String methodName, final Object... toBind) {
Method method = null;
for (Method m : MockController.class.getMethods()) {
if (m.getName().equals(methodName)) {
method = m;
break;
}
}
return ControllerMethodInvoker.build(method, method, Guice.createInjector(new AbstractModule() {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected void configure() {
Multibinder<ParamParser> parsersBinder = Multibinder.newSetBinder(binder(), ParamParser.class);
bind(NinjaProperties.class).toInstance(ninjaProperties);
for (Object o : toBind) {
if (o instanceof Class && ParamParser.class.isAssignableFrom((Class) o)) {
parsersBinder.addBinding().to((Class<? extends ParamParser>) o);
} else {
bind((Class<Object>) o.getClass()).toInstance(o);
}
}
}
}), ninjaProperties);
}
use of com.google.inject.AbstractModule in project ninja by ninjaframework.
the class ControllerMethodInvoker method instantiateComponent.
private static <T> T instantiateComponent(Class<? extends T> argumentExtractor, final Annotation annotation, final Class<?> paramType, Injector injector) {
// Noarg constructor
Constructor noarg = getNoArgConstructor(argumentExtractor);
if (noarg != null) {
try {
return (T) noarg.newInstance();
} catch (Exception e) {
throw new RoutingException(e);
}
}
// Simple case, just takes the annotation
Constructor simple = getSingleArgConstructor(argumentExtractor, annotation.annotationType());
if (simple != null) {
try {
return (T) simple.newInstance(annotation);
} catch (Exception e) {
throw new RoutingException(e);
}
}
// Simple case, just takes the parsed class
Constructor simpleClass = getSingleArgConstructor(argumentExtractor, Class.class);
if (simpleClass != null) {
try {
return (T) simpleClass.newInstance(paramType);
} catch (Exception e) {
throw new RoutingException(e);
}
}
// Complex case, use Guice. Create a child injector with the annotation in it.
return injector.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
bind((Class<Annotation>) annotation.annotationType()).toInstance(annotation);
bind(ArgumentClassHolder.class).toInstance(new ArgumentClassHolder(paramType));
}
}).getInstance(argumentExtractor);
}
use of com.google.inject.AbstractModule in project ninja by ninjaframework.
the class BodyParserEnginePostTest method setUp.
@Before
public void setUp() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(NinjaProperties.class).toInstance(new NinjaPropertiesImpl(NinjaMode.test));
Multibinder<ParamParser> parsersBinder = Multibinder.newSetBinder(binder(), ParamParser.class);
parsersBinder.addBinding().to(NeedingInjectionParamParser.class);
}
});
validation = new ValidationImpl();
Mockito.when(this.context.getValidation()).thenReturn(this.validation);
bodyParserEnginePost = injector.getInstance(BodyParserEnginePost.class);
}
use of com.google.inject.AbstractModule in project ninja by ninjaframework.
the class LifecycleSupportTest method serviceShouldBeStartedIfExplicitlyBoundAsSingleton.
@Test
public void serviceShouldBeStartedIfExplicitlyBoundAsSingleton() {
Injector injector = createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(MockService.class).toInstance(new MockService());
}
});
start(injector);
assertThat(MockService.started, equalTo(1));
}
use of com.google.inject.AbstractModule in project ninja by ninjaframework.
the class LifecycleSupportTest method serviceShouldNotBeStartedIfExplicitlyBoundAndNotSingleton.
@Test
public void serviceShouldNotBeStartedIfExplicitlyBoundAndNotSingleton() {
Injector injector = createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(MockService.class);
}
});
start(injector);
assertThat(MockService.started, equalTo(0));
}
Aggregations