Search in sources :

Example 1 with MockServletContext

use of org.apache.wicket.protocol.http.mock.MockServletContext in project wicket by apache.

the class AbstractInjectorTest method before.

@Before
public void before() {
    app.setServletContext(new MockServletContext(app, null));
    ThreadContext.setApplication(app);
    app.setName(getClass().getName());
    app.initApplication();
    Session session = new WebSession(new MockWebRequest(Url.parse("/")));
    app.getSessionStore().bind(null, session);
    ThreadContext.setSession(session);
    GuiceComponentInjector injector = new GuiceComponentInjector(app, new Module() {

        @Override
        public void configure(final Binder binder) {
            binder.bind(ITestService.class).to(TestService.class);
            binder.bind(ITestService.class).annotatedWith(Red.class).to(TestServiceRed.class);
            binder.bind(ITestService.class).annotatedWith(Blue.class).to(TestServiceBlue.class);
            binder.bind(new TypeLiteral<Map<String, String>>() {
            }).toProvider(new Provider<Map<String, String>>() {

                @Override
                public Map<String, String> get() {
                    Map<String, String> strings = new HashMap<>();
                    strings.put(ITestService.RESULT, ITestService.RESULT);
                    return strings;
                }
            });
            binder.bind(String.class).annotatedWith(Names.named("named1")).toInstance("NAMED_1");
            binder.bind(String.class).annotatedWith(Names.named("named2")).toInstance("NAMED_2");
            binder.bind(String.class).annotatedWith(new Jsr330Named("named1")).toInstance("NAMED_1");
            binder.bind(String.class).annotatedWith(new Jsr330Named("named2")).toInstance("NAMED_2");
            binder.bind(EvilTestService.class).toInstance(new EvilTestService("evil123", 5));
        }
    });
    app.getComponentInstantiationListeners().add(injector);
}
Also used : HashMap(java.util.HashMap) MockServletContext(org.apache.wicket.protocol.http.mock.MockServletContext) Provider(com.google.inject.Provider) Binder(com.google.inject.Binder) WebSession(org.apache.wicket.protocol.http.WebSession) MockWebRequest(org.apache.wicket.mock.MockWebRequest) Module(com.google.inject.Module) HashMap(java.util.HashMap) Map(java.util.Map) WebSession(org.apache.wicket.protocol.http.WebSession) Session(org.apache.wicket.Session) Before(org.junit.Before)

Example 2 with MockServletContext

use of org.apache.wicket.protocol.http.mock.MockServletContext in project openmeetings by apache.

the class ApplicationHelper method createApp.

private static WebApplication createApp(WebApplication _app) {
    WebApplication app = _app;
    if (app == null) {
        // This is the case for non-web-app applications (command line admin)
        try {
            app = (WebApplication) OpenmeetingsVariables.getAppClass().newInstance();
            // temporary name for temporary application
            app.setName("--dumb--");
            ServletContext sc = new MockServletContext(app, null);
            XmlWebApplicationContext xmlContext = new XmlWebApplicationContext();
            xmlContext.setConfigLocation("classpath:applicationContext.xml");
            xmlContext.setServletContext(sc);
            xmlContext.refresh();
            sc.setAttribute(ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, xmlContext);
            app = xmlContext.getBean(WebApplication.class);
            app.setName(getWicketApplicationName());
            app.setServletContext(sc);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            log.error("Failed to create Application");
        }
    }
    return app;
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) MockServletContext(org.apache.wicket.protocol.http.mock.MockServletContext) ServletContext(javax.servlet.ServletContext) WebApplication(org.apache.wicket.protocol.http.WebApplication) MockServletContext(org.apache.wicket.protocol.http.mock.MockServletContext)

Example 3 with MockServletContext

use of org.apache.wicket.protocol.http.mock.MockServletContext in project openmeetings by apache.

the class ApplicationHelper method initApp.

private static WebApplication initApp(WebApplication app) {
    if (app != null) {
        try {
            app.getServletContext();
        } catch (IllegalStateException e) {
            app.setServletContext(new MockServletContext(app, null));
        }
        app.setConfigurationType(RuntimeConfigurationType.DEPLOYMENT);
        OMContextListener omcl = new OMContextListener();
        omcl.contextInitialized(new ServletContextEvent(app.getServletContext()));
        ThreadContext.setApplication(app);
        app.initApplication();
    }
    return app;
}
Also used : OMContextListener(org.apache.openmeetings.util.OMContextListener) MockServletContext(org.apache.wicket.protocol.http.mock.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent)

Example 4 with MockServletContext

use of org.apache.wicket.protocol.http.mock.MockServletContext in project wicket by apache.

the class ComponentRenderer method initApplication.

private void initApplication() {
    if (application instanceof WebApplication) {
        WebApplication webApplication = (WebApplication) application;
        // WebApplication requires a servlet context
        webApplication.setServletContext(new MockServletContext(application, null));
    }
    application.setName("ComponentRenderer[" + System.identityHashCode(ComponentRenderer.this) + "]");
    application.initApplication();
}
Also used : WebApplication(org.apache.wicket.protocol.http.WebApplication) MockServletContext(org.apache.wicket.protocol.http.mock.MockServletContext)

Example 5 with MockServletContext

use of org.apache.wicket.protocol.http.mock.MockServletContext in project wicket by apache.

the class ApplicationSettingsTest method testLocalizer.

/**
 */
@Test
public void testLocalizer() {
    MockApplication dummy = new MockApplication();
    dummy.setName("test-app");
    dummy.setServletContext(new MockServletContext(dummy, ""));
    ThreadContext.setApplication(dummy);
    dummy.initApplication();
    Localizer localizer = dummy.getResourceSettings().getLocalizer();
    Assert.assertNotNull("Localizer should be available", localizer);
    dummy.internalDestroy();
}
Also used : MockApplication(org.apache.wicket.mock.MockApplication) MockServletContext(org.apache.wicket.protocol.http.mock.MockServletContext) Test(org.junit.Test)

Aggregations

MockServletContext (org.apache.wicket.protocol.http.mock.MockServletContext)7 MockApplication (org.apache.wicket.mock.MockApplication)2 MockWebRequest (org.apache.wicket.mock.MockWebRequest)2 WebApplication (org.apache.wicket.protocol.http.WebApplication)2 Before (org.junit.Before)2 Test (org.junit.Test)2 Binder (com.google.inject.Binder)1 Module (com.google.inject.Module)1 Provider (com.google.inject.Provider)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1 ServletContextEvent (javax.servlet.ServletContextEvent)1 OMContextListener (org.apache.openmeetings.util.OMContextListener)1 Session (org.apache.wicket.Session)1 WebSession (org.apache.wicket.protocol.http.WebSession)1 IRequestHandler (org.apache.wicket.request.IRequestHandler)1 Url (org.apache.wicket.request.Url)1 DummyApplication (org.apache.wicket.resource.DummyApplication)1