Search in sources :

Example 1 with ServletContextImpl

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());
}
Also used : ServletContextImpl(org.apache.felix.http.base.internal.service.ServletContextImpl) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 2 with ServletContextImpl

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");
}
Also used : ServletContextImpl(org.apache.felix.http.base.internal.service.ServletContextImpl) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 3 with ServletContextImpl

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"));
}
Also used : ServletContextImpl(org.apache.felix.http.base.internal.service.ServletContextImpl) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 4 with ServletContextImpl

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);
}
Also used : ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) HandlerRegistry(org.apache.felix.http.base.internal.registry.HandlerRegistry) PerContextHandlerRegistry(org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry) ListenerInfo(org.apache.felix.http.base.internal.runtime.ListenerInfo) Bundle(org.osgi.framework.Bundle) ServletContextImpl(org.apache.felix.http.base.internal.service.ServletContextImpl) HttpContext(org.osgi.service.http.HttpContext) ServletContext(javax.servlet.ServletContext) ServletContextAttributeListener(javax.servlet.ServletContextAttributeListener) ListenerHandler(org.apache.felix.http.base.internal.handler.ListenerHandler) EventListenerRegistry(org.apache.felix.http.base.internal.registry.EventListenerRegistry) Before(org.junit.Before)

Example 5 with ServletContextImpl

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());
}
Also used : ServletContextImpl(org.apache.felix.http.base.internal.service.ServletContextImpl) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Aggregations

ServletContext (javax.servlet.ServletContext)5 ServletContextImpl (org.apache.felix.http.base.internal.service.ServletContextImpl)5 Test (org.junit.Test)4 ServletContextAttributeListener (javax.servlet.ServletContextAttributeListener)1 ListenerHandler (org.apache.felix.http.base.internal.handler.ListenerHandler)1 EventListenerRegistry (org.apache.felix.http.base.internal.registry.EventListenerRegistry)1 HandlerRegistry (org.apache.felix.http.base.internal.registry.HandlerRegistry)1 PerContextHandlerRegistry (org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry)1 ListenerInfo (org.apache.felix.http.base.internal.runtime.ListenerInfo)1 Before (org.junit.Before)1 Bundle (org.osgi.framework.Bundle)1 HttpContext (org.osgi.service.http.HttpContext)1