Search in sources :

Example 1 with MockHttpSession

use of com.mockrunner.mock.web.MockHttpSession in project geode by apache.

the class CommonTests method testSessionAttributeListener1.

/**
   * Test that Session Attribute events get triggered
   */
@Test
public void testSessionAttributeListener1() throws Exception {
    AbstractListener listener = new HttpSessionAttributeListenerImpl();
    RendezvousManager.registerListener(listener);
    listener.setLatch(3);
    doFilter();
    // Ugh
    MockHttpSession session = (MockHttpSession) ((GemfireHttpSession) ((HttpServletRequest) getFilteredRequest()).getSession()).getNativeSession();
    session.addAttributeListener((HttpSessionAttributeListener) listener);
    session.setAttribute("foo", "bar");
    session.setAttribute("foo", "baz");
    session.setAttribute("foo", null);
    assertTrue("Event timeout", listener.await(1, TimeUnit.SECONDS));
    assertEquals(ListenerEventType.SESSION_ATTRIBUTE_ADDED, listener.getEvents().get(0));
    assertEquals(ListenerEventType.SESSION_ATTRIBUTE_REPLACED, listener.getEvents().get(1));
    assertEquals(ListenerEventType.SESSION_ATTRIBUTE_REMOVED, listener.getEvents().get(2));
}
Also used : MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) Test(org.junit.Test)

Example 2 with MockHttpSession

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

the class HessianServiceTest method testGetSession.

@Test
public void testGetSession() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put(Constants.SERVER_ROP_EVENT_BRIDGE_FACTORY_PROPERTY, MockEventBridgeFactory.class.getName());
    ObjectContextFactory factory = new ObjectContextFactory() {

        public ObjectContext createContext(DataChannel parent) {
            return null;
        }

        public ObjectContext createContext() {
            return null;
        }
    };
    HessianService service = new HessianService(factory, map);
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = new MockHttpSession();
    request.setSession(session);
    // for some reason need to call this to get session activated in the
    // mock request
    request.getSession();
    try {
        ServiceContext.begin(request, null, null, null);
        assertSame(session, service.getSession(false));
    } finally {
        ServiceContext.end();
    }
}
Also used : DataChannel(org.apache.cayenne.DataChannel) HashMap(java.util.HashMap) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockEventBridgeFactory(org.apache.cayenne.event.MockEventBridgeFactory) Test(org.junit.Test)

Example 3 with MockHttpSession

use of com.mockrunner.mock.web.MockHttpSession 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)3 MockHttpSession (com.mockrunner.mock.web.MockHttpSession)3 Test (org.junit.Test)3 DataChannel (org.apache.cayenne.DataChannel)2 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)2 MockHttpServletResponse (com.mockrunner.mock.web.MockHttpServletResponse)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 BaseContext (org.apache.cayenne.BaseContext)1 MockDataChannel (org.apache.cayenne.MockDataChannel)1 ObjectContext (org.apache.cayenne.ObjectContext)1 DIBootstrap (org.apache.cayenne.di.DIBootstrap)1 Injector (org.apache.cayenne.di.Injector)1 Module (org.apache.cayenne.di.Module)1 MockEventBridgeFactory (org.apache.cayenne.event.MockEventBridgeFactory)1 Assert (org.junit.Assert)1 Mockito.mock (org.mockito.Mockito.mock)1