Search in sources :

Example 6 with MockHttpServletRequest

use of com.mockrunner.mock.web.MockHttpServletRequest in project cayenne by apache.

the class CayenneFilterTest method testDoFilter.

@Test
public void testDoFilter() throws Exception {
    MockFilterConfig config = new MockFilterConfig();
    config.setFilterName("abc");
    config.setInitParameter(WebConfiguration.EXTRA_MODULES_PARAMETER, CayenneFilter_DispatchModule.class.getName());
    MockServletContext context = new MockServletContext();
    config.setupServletContext(context);
    CayenneFilter filter = new CayenneFilter();
    filter.init(config);
    CayenneRuntime runtime = WebUtil.getCayenneRuntime(context);
    CayenneFilter_DispatchRequestHandler handler = (CayenneFilter_DispatchRequestHandler) runtime.getInjector().getInstance(RequestHandler.class);
    assertEquals(0, handler.getStarted());
    assertEquals(0, handler.getEnded());
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    assertEquals(1, handler.getStarted());
    assertEquals(1, handler.getEnded());
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    assertEquals(2, handler.getStarted());
    assertEquals(2, handler.getEnded());
}
Also used : MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) CayenneRuntime(org.apache.cayenne.configuration.CayenneRuntime) MockFilterChain(com.mockrunner.mock.web.MockFilterChain) MockServletContext(com.mockrunner.mock.web.MockServletContext) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) MockFilterConfig(com.mockrunner.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 7 with MockHttpServletRequest

use of com.mockrunner.mock.web.MockHttpServletRequest in project cayenne by apache.

the class ServletContextHandlerTest method testRequestStart_bindContext.

@Test
public void testRequestStart_bindContext() {
    Module module = binder -> {
        binder.bind(DataChannel.class).to(MockDataChannel.class);
        binder.bind(ObjectContextFactory.class).toInstance(new ObjectContextFactory() {

            public ObjectContext createContext(DataChannel parent) {
                return mock(ObjectContext.class);
            }

            public ObjectContext createContext() {
                return mock(ObjectContext.class);
            }
        });
    };
    Injector injector = DIBootstrap.createInjector(module);
    SessionContextRequestHandler handler = new SessionContextRequestHandler();
    injector.injectMembers(handler);
    MockHttpSession session = new MockHttpSession();
    BaseContext.bindThreadObjectContext(null);
    try {
        MockHttpServletRequest request1 = new MockHttpServletRequest();
        MockHttpServletResponse response1 = new MockHttpServletResponse();
        request1.setSession(session);
        handler.requestStart(request1, response1);
        ObjectContext c1 = BaseContext.getThreadObjectContext();
        assertNotNull(c1);
        handler.requestEnd(request1, response1);
        try {
            BaseContext.getThreadObjectContext();
            fail("thread context not null");
        } catch (IllegalStateException e) {
        // expected
        }
        MockHttpServletRequest request2 = new MockHttpServletRequest();
        MockHttpServletResponse response2 = new MockHttpServletResponse();
        request2.setSession(session);
        handler.requestStart(request2, response2);
        ObjectContext c2 = BaseContext.getThreadObjectContext();
        assertSame(c1, c2);
        handler.requestEnd(request2, response2);
        try {
            BaseContext.getThreadObjectContext();
            fail("thread context not null");
        } catch (IllegalStateException e) {
        // expected
        }
        MockHttpServletRequest request3 = new MockHttpServletRequest();
        MockHttpServletResponse response3 = new MockHttpServletResponse();
        request3.setSession(new MockHttpSession());
        handler.requestStart(request3, response3);
        ObjectContext c3 = BaseContext.getThreadObjectContext();
        assertNotNull(c3);
        assertNotSame(c1, c3);
        handler.requestEnd(request3, response3);
        try {
            BaseContext.getThreadObjectContext();
            fail("thread context not null");
        } catch (IllegalStateException e) {
        // expected
        }
    } finally {
        BaseContext.bindThreadObjectContext(null);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) ObjectContext(org.apache.cayenne.ObjectContext) MockDataChannel(org.apache.cayenne.MockDataChannel) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) BaseContext(org.apache.cayenne.BaseContext) DataChannel(org.apache.cayenne.DataChannel) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) MockDataChannel(org.apache.cayenne.MockDataChannel) DataChannel(org.apache.cayenne.DataChannel) Injector(org.apache.cayenne.di.Injector) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) MockDataChannel(org.apache.cayenne.MockDataChannel) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (com.mockrunner.mock.web.MockHttpServletRequest)7 Test (org.junit.Test)7 MockHttpServletResponse (com.mockrunner.mock.web.MockHttpServletResponse)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 MockHttpSession (com.mockrunner.mock.web.MockHttpSession)3 Cookie (javax.servlet.http.Cookie)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 DataChannel (org.apache.cayenne.DataChannel)2 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)2 MockFilterChain (com.mockrunner.mock.web.MockFilterChain)1 MockFilterConfig (com.mockrunner.mock.web.MockFilterConfig)1 MockServletContext (com.mockrunner.mock.web.MockServletContext)1 HashMap (java.util.HashMap)1 BaseContext (org.apache.cayenne.BaseContext)1 MockDataChannel (org.apache.cayenne.MockDataChannel)1 ObjectContext (org.apache.cayenne.ObjectContext)1 CayenneRuntime (org.apache.cayenne.configuration.CayenneRuntime)1 DIBootstrap (org.apache.cayenne.di.DIBootstrap)1 Injector (org.apache.cayenne.di.Injector)1