Search in sources :

Example 1 with NinjaProperties

use of ninja.utils.NinjaProperties 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);
}
Also used : NinjaProperties(ninja.utils.NinjaProperties) Method(java.lang.reflect.Method) AbstractModule(com.google.inject.AbstractModule)

Example 2 with NinjaProperties

use of ninja.utils.NinjaProperties in project ninja by ninjaframework.

the class ReverseRouterTest method before.

@Before
@SuppressWarnings("Convert2Lambda")
public void before() {
    this.ninjaProperties = mock(NinjaProperties.class);
    this.injector = mock(Injector.class);
    this.testControllerProvider = mock(Provider.class);
    this.ninjaBaseDirectoryResolver = new NinjaBaseDirectoryResolver(ninjaProperties);
    when(testControllerProvider.get()).thenReturn(new TestController());
    when(injector.getProvider(TestController.class)).thenReturn(testControllerProvider);
    when(injector.getInstance(ParamParsers.class)).thenReturn(new ParamParsers(Collections.emptySet()));
    Provider<RouteBuilderImpl> routeBuilderImplProvider = mock(Provider.class);
    when(routeBuilderImplProvider.get()).thenAnswer((invocation) -> new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver));
    router = new RouterImpl(injector, ninjaProperties, routeBuilderImplProvider);
    reverseRouter = new ReverseRouter(ninjaProperties, router);
    router.GET().route("/home").with(TestController::home);
    router.GET().route("/user/{email}/{id: .*}").with(TestController::user);
    router.GET().route("/u{userId: .*}/entries/{entryId: .*}").with(TestController::entry);
    // second route to index should not break reverse routing matching the first
    router.GET().route("/home/index").with(TestController::index);
    router.compileRoutes();
}
Also used : NinjaBaseDirectoryResolver(ninja.utils.NinjaBaseDirectoryResolver) ParamParsers(ninja.params.ParamParsers) Injector(com.google.inject.Injector) NinjaProperties(ninja.utils.NinjaProperties) Provider(com.google.inject.Provider) Before(org.junit.Before)

Example 3 with NinjaProperties

use of ninja.utils.NinjaProperties in project ninja by ninjaframework.

the class ControllerMethodInvokerWithDeprecatedValidationTest 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() {

        @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);
}
Also used : NinjaProperties(ninja.utils.NinjaProperties) Method(java.lang.reflect.Method) AbstractModule(com.google.inject.AbstractModule)

Example 4 with NinjaProperties

use of ninja.utils.NinjaProperties in project ninja by ninjaframework.

the class NinjaServletListenerTest method testCreatingInjectorWithoutContextAndOrPropertiesWorks.

@Test
public void testCreatingInjectorWithoutContextAndOrPropertiesWorks() {
    NinjaServletListener ninjaServletListener = new NinjaServletListener();
    ninjaServletListener.contextInitialized(servletContextEvent);
    Injector injector = ninjaServletListener.getInjector();
    NinjaProperties ninjaProperties = injector.getInstance(NinjaProperties.class);
    // make sure we are using the context path from the serveltcontext here
    assertThat(ninjaProperties.getContextPath(), equalTo(CONTEXT_PATH));
}
Also used : Injector(com.google.inject.Injector) NinjaProperties(ninja.utils.NinjaProperties) Test(org.junit.Test)

Example 5 with NinjaProperties

use of ninja.utils.NinjaProperties in project ninja by ninjaframework.

the class NinjaServletListenerTest method testCreatingInjectorWithCustomNinjaPropertiesWorks.

@Test
public void testCreatingInjectorWithCustomNinjaPropertiesWorks() {
    // setup stuff
    NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test);
    ninjaProperties.setProperty("key!", "value!");
    NinjaServletListener ninjaServletListener = new NinjaServletListener();
    ninjaServletListener.setNinjaProperties(ninjaProperties);
    // start the injector:
    ninjaServletListener.contextInitialized(servletContextEvent);
    // test stuff
    Injector injector = ninjaServletListener.getInjector();
    NinjaProperties ninjaPropertiesFromServer = injector.getInstance(NinjaProperties.class);
    assertThat(ninjaPropertiesFromServer.get("key!"), equalTo("value!"));
    // make sure we are using the context path from the serveltcontext here
    assertThat(ninjaProperties.getContextPath(), equalTo(CONTEXT_PATH));
}
Also used : NinjaPropertiesImpl(ninja.utils.NinjaPropertiesImpl) Injector(com.google.inject.Injector) NinjaProperties(ninja.utils.NinjaProperties) Test(org.junit.Test)

Aggregations

NinjaProperties (ninja.utils.NinjaProperties)6 AbstractModule (com.google.inject.AbstractModule)3 Injector (com.google.inject.Injector)3 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 Provider (com.google.inject.Provider)1 Singleton (com.google.inject.Singleton)1 FrameworkModule (ninja.conf.FrameworkModule)1 NinjaBaseModule (ninja.conf.NinjaBaseModule)1 NinjaClassicModule (ninja.conf.NinjaClassicModule)1 ParamParsers (ninja.params.ParamParsers)1 NinjaBaseDirectoryResolver (ninja.utils.NinjaBaseDirectoryResolver)1 NinjaPropertiesImpl (ninja.utils.NinjaPropertiesImpl)1 Before (org.junit.Before)1