use of org.apache.felix.http.base.internal.service.ServletContextImpl in project felix by apache.
the class ServletContextImplTest method testGetSharedAttributeNames.
@Test
public void testGetSharedAttributeNames() {
ServletContext globalContext = new MockServletContext();
ServletContext ctx1 = new ServletContextImpl(bundle, globalContext, httpContext, true, contextRegistry);
ServletContext ctx2 = new ServletContextImpl(bundle, globalContext, httpContext, true, contextRegistry);
Enumeration<String> e = ctx1.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
e = ctx2.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
e = globalContext.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
ctx1.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
e = ctx1.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertTrue(e.hasMoreElements());
Assert.assertEquals("key1", e.nextElement());
Assert.assertFalse(e.hasMoreElements());
e = ctx2.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertTrue(e.hasMoreElements());
Assert.assertEquals("key1", e.nextElement());
Assert.assertFalse(e.hasMoreElements());
e = globalContext.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertTrue(e.hasMoreElements());
Assert.assertEquals("key1", e.nextElement());
Assert.assertFalse(e.hasMoreElements());
}
use of org.apache.felix.http.base.internal.service.ServletContextImpl in project felix by apache.
the class ServletContextImplTest method testGetSharedAttribute.
@Test
public void testGetSharedAttribute() {
ServletContext globalContext = new MockServletContext();
ServletContext ctx1 = new ServletContextImpl(bundle, globalContext, httpContext, true, contextRegistry);
ServletContext ctx2 = new ServletContextImpl(bundle, globalContext, httpContext, true, contextRegistry);
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
// Operations on ctx1 and check results
ctx1.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("value1", ctx2.getAttribute("key1"));
Assert.assertEquals("value1", globalContext.getAttribute("key1"));
ctx1.removeAttribute("key1");
this.listener.checkRemoved("key1", "value1");
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
ctx1.setAttribute("key1", null);
this.listener.checkNull();
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
ctx1.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("value1", ctx2.getAttribute("key1"));
Assert.assertEquals("value1", globalContext.getAttribute("key1"));
ctx1.setAttribute("key1", "newValue");
this.listener.checkReplaced("key1", "value1");
Assert.assertEquals("newValue", ctx1.getAttribute("key1"));
Assert.assertEquals("newValue", ctx2.getAttribute("key1"));
Assert.assertEquals("newValue", globalContext.getAttribute("key1"));
ctx1.removeAttribute("key1");
// Operations on ctx2 and check results
ctx2.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("value1", ctx2.getAttribute("key1"));
Assert.assertEquals("value1", globalContext.getAttribute("key1"));
ctx2.removeAttribute("key1");
this.listener.checkRemoved("key1", "value1");
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
ctx2.setAttribute("key1", null);
this.listener.checkNull();
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
ctx2.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("value1", ctx2.getAttribute("key1"));
Assert.assertEquals("value1", globalContext.getAttribute("key1"));
ctx2.setAttribute("key1", "newValue");
this.listener.checkReplaced("key1", "value1");
Assert.assertEquals("newValue", ctx1.getAttribute("key1"));
Assert.assertEquals("newValue", ctx2.getAttribute("key1"));
Assert.assertEquals("newValue", globalContext.getAttribute("key1"));
ctx2.removeAttribute("key1");
// Operations on globalContext and check results
globalContext.setAttribute("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("value1", ctx2.getAttribute("key1"));
Assert.assertEquals("value1", globalContext.getAttribute("key1"));
globalContext.removeAttribute("key1");
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
globalContext.setAttribute("key1", null);
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
globalContext.setAttribute("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("value1", ctx2.getAttribute("key1"));
Assert.assertEquals("value1", globalContext.getAttribute("key1"));
globalContext.setAttribute("key1", "newValue");
Assert.assertEquals("newValue", ctx1.getAttribute("key1"));
Assert.assertEquals("newValue", ctx2.getAttribute("key1"));
Assert.assertEquals("newValue", globalContext.getAttribute("key1"));
globalContext.removeAttribute("key1");
}
use of org.apache.felix.http.base.internal.service.ServletContextImpl in project felix by apache.
the class ServletContextImplTest method testGetUnsharedAttribute.
@Test
public void testGetUnsharedAttribute() {
ServletContext globalContext = new MockServletContext();
ServletContext ctx1 = new ServletContextImpl(bundle, globalContext, httpContext, false, contextRegistry);
ServletContext ctx2 = new ServletContextImpl(bundle, globalContext, httpContext, false, contextRegistry);
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertNull(ctx2.getAttribute("key1"));
Assert.assertNull(globalContext.getAttribute("key1"));
// Operations on ctx1 and check results
ctx2.setAttribute("key1", "ctx2_private_value");
globalContext.setAttribute("key1", "globalContext_private_value");
ctx1.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("ctx2_private_value", ctx2.getAttribute("key1"));
Assert.assertEquals("globalContext_private_value", globalContext.getAttribute("key1"));
ctx1.removeAttribute("key1");
this.listener.checkRemoved("key1", "value1");
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertEquals("ctx2_private_value", ctx2.getAttribute("key1"));
Assert.assertEquals("globalContext_private_value", globalContext.getAttribute("key1"));
ctx1.setAttribute("key1", null);
this.listener.checkNull();
Assert.assertNull(ctx1.getAttribute("key1"));
Assert.assertEquals("ctx2_private_value", ctx2.getAttribute("key1"));
Assert.assertEquals("globalContext_private_value", globalContext.getAttribute("key1"));
ctx1.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
Assert.assertEquals("value1", ctx1.getAttribute("key1"));
Assert.assertEquals("ctx2_private_value", ctx2.getAttribute("key1"));
Assert.assertEquals("globalContext_private_value", globalContext.getAttribute("key1"));
ctx1.setAttribute("key1", "newValue");
this.listener.checkReplaced("key1", "value1");
Assert.assertEquals("newValue", ctx1.getAttribute("key1"));
Assert.assertEquals("ctx2_private_value", ctx2.getAttribute("key1"));
Assert.assertEquals("globalContext_private_value", globalContext.getAttribute("key1"));
}
use of org.apache.felix.http.base.internal.service.ServletContextImpl in project felix by apache.
the class ServletContextImplTest method setUp.
@Before
public void setUp() {
this.bundle = Mockito.mock(Bundle.class);
ServletContext globalContext = new MockServletContext();
this.httpContext = Mockito.mock(HttpContext.class);
this.listener = new AttributeListener();
final HandlerRegistry reg = new HandlerRegistry();
reg.init();
contextRegistry = reg.getRegistry(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID);
final EventListenerRegistry eventReg = contextRegistry.getEventListenerRegistry();
final ListenerInfo info = Mockito.mock(ListenerInfo.class);
when(info.getListenerTypes()).thenReturn(new String[] { ServletContextAttributeListener.class.getName() });
when(info.isListenerType(ServletContextAttributeListener.class.getName())).thenReturn(true);
final ListenerHandler handler = Mockito.mock(ListenerHandler.class);
when(handler.getListenerInfo()).thenReturn(info);
when(handler.getContextServiceId()).thenReturn(HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID);
when(handler.getListener()).thenReturn(listener);
when(handler.init()).thenReturn(-1);
eventReg.addListeners(handler);
this.context = new ServletContextImpl(this.bundle, globalContext, this.httpContext, false, contextRegistry);
}
use of org.apache.felix.http.base.internal.service.ServletContextImpl in project felix by apache.
the class ServletContextImplTest method testGetUnsharedAttributeNames.
@Test
public void testGetUnsharedAttributeNames() {
ServletContext globalContext = new MockServletContext();
ServletContext ctx1 = new ServletContextImpl(bundle, globalContext, httpContext, false, contextRegistry);
ServletContext ctx2 = new ServletContextImpl(bundle, globalContext, httpContext, false, contextRegistry);
Enumeration<String> e = ctx1.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
e = ctx2.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
e = globalContext.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
ctx1.setAttribute("key1", "value1");
this.listener.checkAdded("key1", "value1");
e = ctx1.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertTrue(e.hasMoreElements());
Assert.assertEquals("key1", e.nextElement());
Assert.assertFalse(e.hasMoreElements());
e = ctx2.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
e = globalContext.getAttributeNames();
Assert.assertNotNull(e);
Assert.assertFalse(e.hasMoreElements());
}
Aggregations