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));
}
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();
}
}
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);
}
}
Aggregations