felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1669088 [3/3] - in /felix/trunk/http: base/src/main/java/org/apache/felix/http/base/internal/handler/ base/src/main/java/org/apache/felix/http/base/internal/runtime/ base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/ base/...
Date Wed, 25 Mar 2015 10:28:10 GMT
Modified: felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilderTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilderTest.java?rev=1669088&r1=1669087&r2=1669088&view=diff
==============================================================================
--- felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilderTest.java (original)
+++ felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilderTest.java Wed Mar 25 10:28:08 2015
@@ -58,10 +58,8 @@ import javax.servlet.ServletContext;
 import org.apache.felix.http.base.internal.context.ExtServletContext;
 import org.apache.felix.http.base.internal.handler.FilterHandler;
 import org.apache.felix.http.base.internal.handler.ServletHandler;
+import org.apache.felix.http.base.internal.runtime.AbstractInfo;
 import org.apache.felix.http.base.internal.runtime.FilterInfo;
-import org.apache.felix.http.base.internal.runtime.HandlerRuntime;
-import org.apache.felix.http.base.internal.runtime.HandlerRuntime.ErrorPage;
-import org.apache.felix.http.base.internal.runtime.RegistryRuntime;
 import org.apache.felix.http.base.internal.runtime.ServletContextHelperInfo;
 import org.apache.felix.http.base.internal.runtime.ServletInfo;
 import org.apache.felix.http.base.internal.whiteboard.ContextHandler;
@@ -85,6 +83,7 @@ import org.osgi.service.http.runtime.dto
 import org.osgi.service.http.runtime.dto.RuntimeDTO;
 import org.osgi.service.http.runtime.dto.ServletContextDTO;
 import org.osgi.service.http.runtime.dto.ServletDTO;
+import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 
 @RunWith(MockitoJUnitRunner.class)
 public class RuntimeDTOBuilderTest
@@ -95,6 +94,8 @@ public class RuntimeDTOBuilderTest
     private static final Long ID_0 = -ID_COUNTER.incrementAndGet();
     private static final Long ID_A = ID_COUNTER.incrementAndGet();
     private static final Long ID_B = ID_COUNTER.incrementAndGet();
+    private static final Long ID_C = ID_COUNTER.incrementAndGet();
+    private static final Long ID_D = ID_COUNTER.incrementAndGet();
 
     private static final Long ID_LISTENER_1 = ID_COUNTER.incrementAndGet();
     private static final Long ID_LISTENER_2 = ID_COUNTER.incrementAndGet();
@@ -111,6 +112,15 @@ public class RuntimeDTOBuilderTest
                 }
             };
 
+    @SuppressWarnings("serial")
+    private static final Map<String, Object> RUNTIME_ATTRIBUTES = new HashMap<String, Object>()
+    {
+        {
+            put("attr_1", "val_1");
+            put("attr_2", "val_2");
+        }
+    };
+
     @Mock private Bundle bundle;
 
     @Mock private DTO testDTO;
@@ -118,10 +128,14 @@ public class RuntimeDTOBuilderTest
     @Mock private ExtServletContext context_0;
     @Mock private ExtServletContext context_A;
     @Mock private ExtServletContext context_B;
+    @Mock private ExtServletContext context_C;
+    @Mock private ExtServletContext context_D;
 
     @Mock private ServiceReference<?> listener_1;
     @Mock private ServiceReference<?> listener_2;
 
+    @Mock private ServiceReference<Object> resource;
+
     private RegistryRuntime registry;
     private Map<String, Object> runtimeAttributes;
 	private ListenerRegistry listenerRegistry;
@@ -130,11 +144,11 @@ public class RuntimeDTOBuilderTest
     public void setUp()
     {
         registry = null;
-        runtimeAttributes = Collections.emptyMap();
+        runtimeAttributes = RUNTIME_ATTRIBUTES;
         listenerRegistry = new ListenerRegistry(bundle);
     }
 
-	public ContextHandler setupContext(ServletContext context, String name, long serviceId)
+    public ServletContextHelperRuntime setupContext(ServletContext context, String name, long serviceId)
     {
         when(context.getServletContextName()).thenReturn(name);
 
@@ -146,13 +160,11 @@ public class RuntimeDTOBuilderTest
         when(context.getInitParameter("param_1")).thenReturn("init_val_1");
         when(context.getInitParameter("param_2")).thenReturn("init_val_2");
 
-        Map<String, String> initParemters = new HashMap<String, String>();
-        initParemters.put("param_1", "init_val_1");
-        initParemters.put("param_2", "init_val_2");
-        ServletContextHelperInfo contextInfo = createContextInfo(0, serviceId, name, path, initParemters);
+        Map<String, String> initParameters = createInitParameterMap();
+        ServletContextHelperInfo contextInfo = createContextInfo(0, serviceId, name, path, initParameters);
 
         PerContextEventListener eventListener = listenerRegistry.addContext(contextInfo);
-        ContextHandler contextHandler = new ContextHandler(contextInfo, context, eventListener, bundle);
+        ServletContextHelperRuntime contextHandler = new ContextHandler(contextInfo, context, eventListener, bundle);
 
         ServletContext sharedContext = contextHandler.getSharedContext();
         sharedContext.setAttribute("intAttr", 1);
@@ -163,12 +175,21 @@ public class RuntimeDTOBuilderTest
         return contextHandler;
     }
 
+    private Map<String, String> createInitParameterMap()
+    {
+        Map<String, String> initParameters = new HashMap<String, String>();
+        initParameters.put("param_1", "init_val_1");
+        initParameters.put("param_2", "init_val_2");
+        return initParameters;
+    }
+
     @SuppressWarnings("unchecked")
     public Map<Long, Collection<ServiceReference<?>>> setupListeners()
     {
         Map<Long, Collection<ServiceReference<?>>> listenerRuntimes = new HashMap<Long, Collection<ServiceReference<?>>>();
+
         listenerRuntimes.put(ID_0, asList(listener_1, listener_2));
-        listenerRuntimes.put(ID_A, Arrays.<ServiceReference<?>> asList(listener_1));
+        listenerRuntimes.put(ID_A, Arrays.<ServiceReference<?>>asList(listener_1));
         listenerRuntimes.put(ID_B, asList(listener_1, listener_2));
 
         when(listener_1.getProperty(Constants.SERVICE_ID)).thenReturn(ID_LISTENER_1);
@@ -182,50 +203,65 @@ public class RuntimeDTOBuilderTest
         return listenerRuntimes;
     }
 
-    public void setupRegistry(List<ContextHandler> contexts,
-            List<HandlerRuntime> contextRuntimes,
-            Map<Long, Collection<ServiceReference<?>>> listenerRuntimes)
+    public void setupResource()
     {
-        registry = new RegistryRuntime(contexts, contextRuntimes, listenerRuntimes, Collections.EMPTY_SET);
+        when(resource.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN)).thenReturn(new String[] { "/" });
+        when(resource.getProperty(HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PREFIX)).thenReturn("prefix");
+    }
+
+    public void setupRegistry(List<ServletContextHelperRuntime> contexts,
+            List<ContextRuntime> contextRuntimes,
+            Map<Long, Collection<ServiceReference<?>>> listenerRuntimes,
+            FailureRuntime failures)
+    {
+        registry = new RegistryRuntime(contexts, contextRuntimes, listenerRuntimes, failures);
     }
 
     @Test
     public void buildRuntimeDTO()
     {
-        ContextHandler contextHelper_0 = setupContext(context_0, "0", ID_0);
-        ContextHandler contextHelper_A = setupContext(context_A, "A", ID_A);
-        ContextHandler contextHelper_B = setupContext(context_B, "B", ID_B);
-
-        List<ServletHandler> servlets_0 = asList(createTestServlet("1", context_0));
-        List<FilterHandler> filters_0 = asList(createTestFilter("1", context_0));
-        List<ServletHandler> resources_0 = asList(createTestServlet("1", context_0));
-        List<ErrorPage> errorPages_0 = asList(createErrorPage("E_1", context_0));
-        HandlerRuntime contextRuntime_0 = new HandlerRuntime(servlets_0, filters_0, resources_0, errorPages_0, ID_0);
-
-        List<ServletHandler> servlets_A = asList(createTestServlet("A_1", context_A));
-        List<FilterHandler> filters_A = asList(createTestFilter("A_1", context_A));
-        List<ServletHandler> resources_A = asList(createTestServlet("A_1", context_A));
-        List<ErrorPage> errorPages_A = asList(createErrorPage("E_A_1", context_A));
-        HandlerRuntime contextRuntime_A = new HandlerRuntime(servlets_A, filters_A, resources_A, errorPages_A, ID_A);
+        ServletContextHelperRuntime contextHelper_0 = setupContext(context_0, "0", ID_0);
+        ServletContextHelperRuntime contextHelper_A = setupContext(context_A, "A", ID_A);
+        ServletContextHelperRuntime contextHelper_B = setupContext(context_B, "B", ID_B);
+
+        List<ServletRuntime> servlets_0 = asList(createTestServlet("1", context_0));
+        List<FilterRuntime> filters_0 = asList(createTestFilter("1", context_0));
+        List<ServletRuntime> resources_0 = asList(createTestServlet("1", context_0));
+        List<ErrorPageRuntime> errorPages_0 = asList(createErrorPage("E_1", context_0));
+        ContextRuntime contextRuntime_0 = new ContextRuntime(servlets_0, filters_0, resources_0, errorPages_0, ID_0);
+
+        List<ServletRuntime> servlets_A = asList(createTestServlet("A_1", context_A));
+        List<FilterRuntime> filters_A = asList(createTestFilter("A_1", context_A));
+        List<ServletRuntime> resources_A = asList(createTestServlet("A_1", context_A));
+        List<ErrorPageRuntime> errorPages_A = asList(createErrorPage("E_A_1", context_A));
+        ContextRuntime contextRuntime_A = new ContextRuntime(servlets_A, filters_A, resources_A, errorPages_A, ID_A);
 
-        List<ServletHandler> servlets_B = asList(createTestServletWithServiceId("B_1", context_B),
+        List<ServletRuntime> servlets_B = asList(createTestServletWithServiceId("B_1", context_B),
                 createTestServletWithServiceId("B_2", context_B));
-        List<FilterHandler> filters_B = asList(createTestFilterWithServiceId("B_1", context_B),
+        List<FilterRuntime> filters_B = asList(createTestFilterWithServiceId("B_1", context_B),
                 createTestFilterWithServiceId("B_2", context_B));
-        List<ServletHandler> resources_B = asList(createTestServletWithServiceId("B_1", context_B),
+        List<ServletRuntime> resources_B = asList(createTestServletWithServiceId("B_1", context_B),
                 createTestServletWithServiceId("B_2", context_B));
-        List<ErrorPage> errorPages_B = asList(createErrorPageWithServiceId("E_B_1", context_B),
+        List<ErrorPageRuntime> errorPages_B = asList(createErrorPageWithServiceId("E_B_1", context_B),
                 createErrorPageWithServiceId("E_B_2", context_B));
-        HandlerRuntime contextRuntime_B = new HandlerRuntime(servlets_B, filters_B, resources_B, errorPages_B, ID_B);
+        ContextRuntime contextRuntime_B = new ContextRuntime(servlets_B, filters_B, resources_B, errorPages_B, ID_B);
 
         Map<Long, Collection<ServiceReference<?>>> listenerRuntimes = setupListeners();
 
         setupRegistry(asList(contextHelper_0, contextHelper_A, contextHelper_B),
                 asList(contextRuntime_0, contextRuntime_A, contextRuntime_B),
-                listenerRuntimes);
+                listenerRuntimes,
+                FailureRuntime.empty());
 
         RuntimeDTO runtimeDTO = new RuntimeDTOBuilder(registry, runtimeAttributes).build();
 
+        assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
+        assertEquals(0, runtimeDTO.failedFilterDTOs.length);
+        assertEquals(0, runtimeDTO.failedListenerDTOs.length);
+        assertEquals(0, runtimeDTO.failedResourceDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+
         assertServletContextDTOs(runtimeDTO);
     }
 
@@ -522,7 +558,7 @@ public class RuntimeDTOBuilderTest
 
     @Test
     public void nullValuesInEntities() {
-        ContextHandler contextHandler = setupContext(context_0, "0", ID_0);
+        ServletContextHelperRuntime contextHandler = setupContext(context_0, "0", ID_0);
 
         ServletInfo servletInfo = createServletInfo(0,
                 ID_COUNTER.incrementAndGet(),
@@ -532,7 +568,7 @@ public class RuntimeDTOBuilderTest
                 true,
                 Collections.<String, String>emptyMap());
         Servlet servlet = mock(Servlet.class);
-        ServletHandler servletHandler = new ServletHandler(null, context_0, servletInfo, servlet);
+        ServletRuntime servletHandler = new ServletHandler(null, context_0, servletInfo, servlet);
         when(servlet.getServletInfo()).thenReturn("info_0");
 
         FilterInfo filterInfo = createFilterInfo(0,
@@ -544,7 +580,7 @@ public class RuntimeDTOBuilderTest
                 true,
                 null,
                 Collections.<String, String>emptyMap());
-        FilterHandler filterHandler = new FilterHandler(null, context_0, mock(Filter.class), filterInfo);
+        FilterRuntime filterHandler = new FilterHandler(null, context_0, mock(Filter.class), filterInfo);
 
         ServletInfo resourceInfo = createServletInfo(0,
                 ID_COUNTER.incrementAndGet(),
@@ -554,15 +590,16 @@ public class RuntimeDTOBuilderTest
                 true,
                 Collections.<String, String>emptyMap());
         Servlet resource = mock(Servlet.class);
-        ServletHandler resourceHandler = new ServletHandler(null, context_0, resourceInfo, resource);
+        ServletRuntime resourceHandler = new ServletHandler(null, context_0, resourceInfo, resource);
 
-        HandlerRuntime contextRuntime = new HandlerRuntime(asList(servletHandler),
+        ContextRuntime contextRuntime = new ContextRuntime(asList(servletHandler),
                 asList(filterHandler),
                 asList(resourceHandler),
-                Collections.<ErrorPage>emptyList(),
+                Collections.<ErrorPageRuntime>emptyList(),
                 ID_0);
         setupRegistry(asList(contextHandler), asList(contextRuntime),
-                Collections.<Long, Collection<ServiceReference<?>>>emptyMap());
+                Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
+                FailureRuntime.empty());
 
         RuntimeDTO runtimeDTO = new RuntimeDTOBuilder(registry, runtimeAttributes).build();
 
@@ -579,12 +616,13 @@ public class RuntimeDTOBuilderTest
 
     @Test
     public void contextWithNoEntities() {
-        ContextHandler contextHandler_0 = setupContext(context_0, "0", ID_0);
-        ContextHandler contextHandler_A = setupContext(context_A, "A", ID_A);
+        ServletContextHelperRuntime contextHandler_0 = setupContext(context_0, "0", ID_0);
+        ServletContextHelperRuntime contextHandler_A = setupContext(context_A, "A", ID_A);
 
         setupRegistry(asList(contextHandler_0, contextHandler_A),
-                asList(HandlerRuntime.empty(ID_0), HandlerRuntime.empty(ID_A)),
-                Collections.<Long, Collection<ServiceReference<?>>>emptyMap());
+                asList(ContextRuntime.empty(ID_0), ContextRuntime.empty(ID_A)),
+                Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
+                FailureRuntime.empty());
 
         RuntimeDTO runtimeDTO = new RuntimeDTOBuilder(registry, runtimeAttributes).build();
 
@@ -601,7 +639,7 @@ public class RuntimeDTOBuilderTest
         expectedException.expect(IllegalArgumentException.class);
         expectedException.expectMessage("patterns");
 
-        ContextHandler contextHandler = setupContext(context_0, "0", ID_0);
+        ServletContextHelperRuntime contextHandler = setupContext(context_0, "0", ID_0);
 
         ServletInfo servletInfo = createServletInfo(0,
                 ID_COUNTER.incrementAndGet(),
@@ -611,17 +649,144 @@ public class RuntimeDTOBuilderTest
                 true,
                 Collections.<String, String>emptyMap());
         Servlet servlet = mock(Servlet.class);
-        ServletHandler servletHandler = new ServletHandler(null, context_0, servletInfo, servlet);
+        ServletRuntime servletHandler = new ServletHandler(null, context_0, servletInfo, servlet);
         when(servlet.getServletInfo()).thenReturn("info_0");
 
-        HandlerRuntime contextRuntime = new HandlerRuntime(asList(servletHandler),
-                Collections.<FilterHandler>emptyList(),
-                Collections.<ServletHandler>emptyList(),
-                Collections.<ErrorPage>emptyList(),
+        ContextRuntime contextRuntime = new ContextRuntime(asList(servletHandler),
+                Collections.<FilterRuntime>emptyList(),
+                Collections.<ServletRuntime>emptyList(),
+                Collections.<ErrorPageRuntime>emptyList(),
                 ID_0);
         setupRegistry(asList(contextHandler), asList(contextRuntime),
-                Collections.<Long, Collection<ServiceReference<?>>> emptyMap());
+                Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
+                FailureRuntime.empty());
 
         new RuntimeDTOBuilder(registry, runtimeAttributes).build();
     }
+
+    public FailureRuntime setupFailures()
+    {
+        Map<AbstractInfo<?>, Integer> serviceFailures = new HashMap<AbstractInfo<?>, Integer>();
+
+        ServletContextHelperInfo contextInfo = createContextInfo(0,
+                ID_C,
+                "context_failure_1",
+                "/",
+                createInitParameterMap());
+        serviceFailures.put(contextInfo, 1);
+
+        contextInfo = createContextInfo(0,
+                ID_D,
+                "context_failure_2",
+                "/",
+                createInitParameterMap());
+        serviceFailures.put(contextInfo, 2);
+
+        ServletInfo servletInfo = createServletInfo(0, ID_COUNTER.incrementAndGet(),
+                "servlet_failure_1",
+                new String[] {"/"},
+                null,
+                false,
+                createInitParameterMap());
+        serviceFailures.put(servletInfo, 3);
+
+        servletInfo = createServletInfo(0, ID_COUNTER.incrementAndGet(),
+                "servlet_failure_2",
+                new String[] {"/"},
+                null,
+                false,
+                createInitParameterMap());
+        serviceFailures.put(servletInfo, 4);
+
+        FilterInfo filterInfo = createFilterInfo(0,
+                ID_COUNTER.incrementAndGet(),
+                "filter_failure_1",
+                new String[] {"/"},
+                null,
+                null,
+                false,
+                null,
+                createInitParameterMap());
+        serviceFailures.put(filterInfo, 5);
+
+        ServletInfo errorPageInfo = createServletInfo(0,
+                ID_COUNTER.incrementAndGet(),
+                "error_failure_1",
+                null,
+                new String[] { "405", "TestException" },
+                false,
+                createInitParameterMap());
+        serviceFailures.put(errorPageInfo, 6);
+
+        ServletInfo invalidErrorPageInfo = createServletInfo(0,
+                ID_COUNTER.incrementAndGet(),
+                "error_failure_2",
+                new String[] { "/" },
+                new String[] { "405", "TestException" },
+                false,
+                createInitParameterMap());
+        serviceFailures.put(invalidErrorPageInfo, 7);
+
+        return FailureRuntime.builder().add(serviceFailures).build();
+    }
+
+    @Test
+    public void testFailureDTOs()
+    {
+        setupRegistry(Collections.<ServletContextHelperRuntime>emptyList(),
+                Collections.<ContextRuntime>emptyList(),
+                Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
+                setupFailures());
+
+        RuntimeDTO runtimeDTO = new RuntimeDTOBuilder(registry, runtimeAttributes).build();
+
+        assertEquals(0, runtimeDTO.servletContextDTOs.length);
+
+        assertEquals(2, runtimeDTO.failedErrorPageDTOs.length);
+        assertEquals(1, runtimeDTO.failedFilterDTOs.length);
+        // ListenerInfo is hard to setup
+        assertEquals(0, runtimeDTO.failedListenerDTOs.length);
+        // ResourceInfo is hard to setup
+        assertEquals(0, runtimeDTO.failedResourceDTOs.length);
+        assertEquals(2, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(2, runtimeDTO.failedServletDTOs.length);
+
+        assertEquals(1, runtimeDTO.failedServletContextDTOs[0].failureReason);
+        assertEquals(2, runtimeDTO.failedServletContextDTOs[1].failureReason);
+        assertEquals(3, runtimeDTO.failedServletDTOs[0].failureReason);
+        assertEquals(4, runtimeDTO.failedServletDTOs[1].failureReason);
+        assertEquals(5, runtimeDTO.failedFilterDTOs[0].failureReason);
+        assertEquals(6, runtimeDTO.failedErrorPageDTOs[0].failureReason);
+        assertEquals(7, runtimeDTO.failedErrorPageDTOs[1].failureReason);
+
+        assertEquals("context_failure_1", runtimeDTO.failedServletContextDTOs[0].name);
+        assertEquals("context_failure_2", runtimeDTO.failedServletContextDTOs[1].name);
+        assertEquals("servlet_failure_1", runtimeDTO.failedServletDTOs[0].name);
+        assertEquals("servlet_failure_2", runtimeDTO.failedServletDTOs[1].name);
+        assertEquals("filter_failure_1", runtimeDTO.failedFilterDTOs[0].name);
+        assertEquals("error_failure_1", runtimeDTO.failedErrorPageDTOs[0].name);
+        assertEquals("error_failure_2", runtimeDTO.failedErrorPageDTOs[1].name);
+
+        assertEquals(ID_C.longValue(), runtimeDTO.failedServletContextDTOs[0].serviceId);
+        assertEquals(ID_D.longValue(), runtimeDTO.failedServletContextDTOs[1].serviceId);
+        assertEquals(0, runtimeDTO.failedServletDTOs[0].servletContextId);
+        assertEquals(0, runtimeDTO.failedServletDTOs[1].servletContextId);
+        assertEquals(0, runtimeDTO.failedFilterDTOs[0].servletContextId);
+        assertEquals(0, runtimeDTO.failedErrorPageDTOs[0].servletContextId);
+        assertEquals(0, runtimeDTO.failedErrorPageDTOs[1].servletContextId);
+
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[0].errorPageDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[0].filterDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[0].listenerDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[0].resourceDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[0].servletDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[1].errorPageDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[1].filterDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[1].listenerDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[1].resourceDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs[1].servletDTOs.length);
+
+        assertTrue(runtimeDTO.failedServletContextDTOs[0].attributes.isEmpty());
+        assertTrue(runtimeDTO.failedServletContextDTOs[1].attributes.isEmpty());
+    }
 }
\ No newline at end of file

Modified: felix/trunk/http/bundle/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/http/bundle/pom.xml?rev=1669088&r1=1669087&r2=1669088&view=diff
==============================================================================
--- felix/trunk/http/bundle/pom.xml (original)
+++ felix/trunk/http/bundle/pom.xml Wed Mar 25 10:28:08 2015
@@ -123,7 +123,7 @@
         <dependency>
             <groupId>${project.groupId}</groupId>
             <artifactId>org.apache.felix.http.jetty</artifactId>
-            <version>3.0.1-SNAPSHOT</version>
+            <version>3.0.3-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>${project.groupId}</groupId>

Modified: felix/trunk/http/itest/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/pom.xml?rev=1669088&r1=1669087&r2=1669088&view=diff
==============================================================================
--- felix/trunk/http/itest/pom.xml (original)
+++ felix/trunk/http/itest/pom.xml Wed Mar 25 10:28:08 2015
@@ -79,8 +79,14 @@
 		</dependency>
 		<dependency>
 			<groupId>org.mockito</groupId>
-			<artifactId>mockito-all</artifactId>
-			<version>1.8.2</version>
+			<artifactId>mockito-core</artifactId>
+			<version>1.10.19</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.objenesis</groupId>
+			<artifactId>objenesis</artifactId>
+			<version>2.1</version>
 			<scope>test</scope>
 		</dependency>
 		<dependency>

Modified: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java?rev=1669088&r1=1669087&r2=1669088&view=diff
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java (original)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java Wed Mar 25 10:28:08 2015
@@ -173,7 +173,7 @@ public abstract class BaseIntegrationTes
         }
     }
 
-    private static final int DEFAULT_TIMEOUT = 10000;
+    protected static final int DEFAULT_TIMEOUT = 10000;
 
     protected static final String ORG_APACHE_FELIX_HTTP_JETTY = "org.apache.felix.http.jetty";
 
@@ -334,6 +334,9 @@ public abstract class BaseIntegrationTes
             mavenBundle("org.apache.felix", ORG_APACHE_FELIX_HTTP_JETTY).versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
             mavenBundle("org.apache.felix", "org.apache.felix.configadmin").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
 
+            mavenBundle("org.mockito", "mockito-core").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
+            mavenBundle("org.objenesis", "objenesis").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
+
             junitBundles(), frameworkStartLevel(START_LEVEL_TEST_BUNDLE), felix());
     }
 
@@ -373,18 +376,37 @@ public abstract class BaseIntegrationTes
      */
     protected <T> T awaitService(String serviceName) throws Exception
     {
-        ServiceTracker<?, ?> tracker = null;
+        ServiceTracker<T, T> tracker = null;
+        tracker = getTracker(serviceName);
+        return tracker.waitForService(DEFAULT_TIMEOUT);
+    }
+
+    /**
+     * Return an array of {@code ServiceReference}s for all services for the
+     * given serviceName
+     * @param serviceName
+     * @return Array of {@code ServiceReference}s or {@code null} if no services
+     *         are being tracked.
+     */
+    protected <T> ServiceReference<T>[] getServiceReferences(String serviceName)
+    {
+        ServiceTracker<T, T> tracker = getTracker(serviceName);
+        return tracker.getServiceReferences();
+    }
+
+    private <T> ServiceTracker<T, T> getTracker(String serviceName)
+    {
         synchronized ( this.trackers )
         {
-            tracker = trackers.get(serviceName);
+            ServiceTracker<?, ?> tracker = trackers.get(serviceName);
             if ( tracker == null )
             {
-                tracker = new ServiceTracker(m_context, serviceName, null);
+                tracker = new ServiceTracker<T, T>(m_context, serviceName, null);
                 trackers.put(serviceName, tracker);
                 tracker.open();
             }
+            return (ServiceTracker<T, T>) tracker;
         }
-        return (T) tracker.waitForService(DEFAULT_TIMEOUT);
     }
 
     protected void configureHttpService(Dictionary<?, ?> props) throws Exception

Modified: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java?rev=1669088&r1=1669087&r2=1669088&view=diff
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java (original)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/EventListenerTest.java Wed Mar 25 10:28:08 2015
@@ -18,8 +18,9 @@
  */
 package org.apache.felix.http.itest;
 
-import static javax.servlet.http.HttpServletResponse.*;
-import static org.junit.Assert.*;
+import static javax.servlet.http.HttpServletResponse.SC_OK;
+import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
+import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.net.URL;

Added: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java?rev=1669088&view=auto
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java (added)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java Wed Mar 25 10:28:08 2015
@@ -0,0 +1,1072 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.itest;
+
+import static java.util.Arrays.asList;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.osgi.framework.Constants.SERVICE_ID;
+import static org.osgi.framework.Constants.SERVICE_RANKING;
+import static org.osgi.service.http.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ENDPOINT_ATTRIBUTE;
+import static org.osgi.service.http.runtime.HttpServiceRuntimeConstants.HTTP_SERVICE_ID_ATTRIBUTE;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_EXCEPTION_ON_INIT;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_VALIDATION_FAILED;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_NAME;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PATTERN;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_RESOURCE_PREFIX;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_ERROR_PAGE;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET;
+
+import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterConfig;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContextAttributeListener;
+import javax.servlet.ServletContextListener;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequestAttributeListener;
+import javax.servlet.ServletRequestListener;
+import javax.servlet.http.HttpSessionAttributeListener;
+import javax.servlet.http.HttpSessionListener;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.context.ServletContextHelper;
+import org.osgi.service.http.runtime.HttpServiceRuntime;
+import org.osgi.service.http.runtime.dto.FailedServletDTO;
+import org.osgi.service.http.runtime.dto.RuntimeDTO;
+import org.osgi.service.http.runtime.dto.ServletContextDTO;
+
+@RunWith(JUnit4TestRunner.class)
+public class HttpServiceRuntimeTest extends BaseIntegrationTest
+{
+    private static final long DEFAULT_SLEEP = 100;
+
+    private void registerServlet(String name, String path) throws InterruptedException
+    {
+        CountDownLatch initLatch = new CountDownLatch(1);
+        registerServlet(name, path, null, initLatch);
+        awaitServiceRegistration(initLatch);
+    }
+
+    private void registerServlet(String name, String path, CountDownLatch initLatch)
+    {
+        registerServlet(name, path, null, initLatch);
+    }
+
+    private void registerServlet(String name, String path, String context, CountDownLatch initLatch)
+    {
+        List<Object> propertyEntries = Arrays.<Object>asList(
+                HTTP_WHITEBOARD_SERVLET_PATTERN, path,
+                HTTP_WHITEBOARD_SERVLET_NAME, name,
+                HTTP_WHITEBOARD_CONTEXT_SELECT, context);
+
+        Dictionary<String, ?> properties = createDictionary(context == null ?
+                propertyEntries.subList(0, 4).toArray() : propertyEntries.toArray());
+
+        m_context.registerService(Servlet.class.getName(), new TestServlet(initLatch, null), properties);
+    }
+
+    private void registerFilter(String name, String path) throws InterruptedException
+    {
+        CountDownLatch initLatch = new CountDownLatch(1);
+        registerFilter(name, path, initLatch);
+        awaitServiceRegistration(initLatch);
+    }
+
+    private void registerFilter(String name, String path, CountDownLatch initLatch)
+    {
+        registerFilter(name, path, null, initLatch);
+    }
+
+    private void registerFilter(String name, String path, String context, CountDownLatch initLatch)
+    {
+        List<Object> propertyEntries = Arrays.<Object>asList(
+                HTTP_WHITEBOARD_FILTER_PATTERN, path,
+                HTTP_WHITEBOARD_FILTER_NAME, name,
+                HTTP_WHITEBOARD_CONTEXT_SELECT, context);
+
+        Dictionary<String, ?> properties = createDictionary(context == null ?
+                propertyEntries.subList(0, 4).toArray() : propertyEntries.toArray());
+
+        m_context.registerService(Filter.class.getName(), new TestFilter(initLatch, null), properties);
+    }
+
+    private void registerResource(String prefix, String path) throws InterruptedException
+    {
+        registerResource(prefix, path, null);
+    }
+
+    private void registerResource(String prefix, String path, String context) throws InterruptedException
+    {
+        List<Object> propertyEntries = Arrays.<Object>asList(
+                HTTP_WHITEBOARD_RESOURCE_PATTERN, path,
+                HTTP_WHITEBOARD_RESOURCE_PREFIX, prefix,
+                HTTP_WHITEBOARD_CONTEXT_SELECT, context);
+
+        Dictionary<String, ?> properties = createDictionary(context == null ?
+                propertyEntries.subList(0, 4).toArray() : propertyEntries.toArray());
+
+        m_context.registerService(TestResource.class.getName(), new TestResource(), properties);
+        awaitServiceRegistration();
+    }
+
+    private void registerErrorPage(String name, List<String> errors) throws InterruptedException
+    {
+        CountDownLatch initLatch = new CountDownLatch(1);
+        registerErrorPage(name, errors, initLatch);
+        awaitServiceRegistration(initLatch);
+    }
+
+    private void registerErrorPage(String name, List<String> errors, CountDownLatch initLatch)
+    {
+        registerErrorPage(name, errors, null, initLatch);
+    }
+
+    private void registerErrorPage(String name, List<String> errors, String context, CountDownLatch initLatch)
+    {
+        List<Object> propertyEntries = Arrays.<Object>asList(
+                HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, errors,
+                HTTP_WHITEBOARD_SERVLET_NAME, name,
+                HTTP_WHITEBOARD_CONTEXT_SELECT, context);
+
+        Dictionary<String, ?> properties = createDictionary(context == null ?
+                propertyEntries.subList(0, 4).toArray() : propertyEntries.toArray());
+
+        m_context.registerService(Servlet.class.getName(), new TestServlet(initLatch, null), properties);
+    }
+
+    private void registerListener(Class<?> listenerClass, boolean useWithWhiteboard) throws InterruptedException
+    {
+        registerListener(listenerClass, useWithWhiteboard, null);
+    }
+
+    private void registerListener(Class<?> listenerClass, boolean useWithWhiteboard, String context) throws InterruptedException
+    {
+        List<Object> propertyEntries = Arrays.<Object>asList(
+                HTTP_WHITEBOARD_LISTENER, useWithWhiteboard ? "true" : "false",
+                HTTP_WHITEBOARD_CONTEXT_SELECT, context);
+
+        Dictionary<String, ?> properties = createDictionary(context == null ?
+                propertyEntries.subList(0, 2).toArray() : propertyEntries.toArray());
+
+        m_context.registerService(listenerClass.getName(), mock(listenerClass), properties);
+        awaitServiceRegistration();
+    }
+
+    private ServiceRegistration<?> registerContext(String name, String path) throws InterruptedException
+    {
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_CONTEXT_NAME, name,
+                HTTP_WHITEBOARD_CONTEXT_PATH, path);
+
+        ServiceRegistration<?> contextRegistration = m_context.registerService(ServletContextHelper.class.getName(), mock(ServletContextHelper.class), properties);
+        awaitServiceRegistration();
+        return contextRegistration;
+    }
+
+    @Before
+    public void awaitServiceRuntime() throws Exception
+    {
+        awaitService(HttpServiceRuntime.class.getName());
+    }
+
+    @Test
+    public void httpRuntimeServiceIsAvailableAfterBundleActivation() throws Exception
+    {
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+        Map<String, String> runtimeDTOAttributes = runtimeDTO.attributes;
+
+        assertNotNull(runtimeDTOAttributes);
+        assertTrue(runtimeDTOAttributes.containsKey(HTTP_SERVICE_ID_ATTRIBUTE));
+        assertTrue(runtimeDTOAttributes.containsKey(HTTP_SERVICE_ENDPOINT_ATTRIBUTE));
+        assertTrue(0 < Integer.valueOf(runtimeDTOAttributes.get(HTTP_SERVICE_ID_ATTRIBUTE)));
+
+        assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
+        assertEquals(0, runtimeDTO.failedFilterDTOs.length);
+        assertEquals(0, runtimeDTO.failedListenerDTOs.length);
+        assertEquals(0, runtimeDTO.failedResourceDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+
+        assertEquals(1, runtimeDTO.servletContextDTOs.length);
+        assertEquals("default", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals(0, runtimeDTO.servletContextDTOs[0].attributes.size());
+
+        // TODO The default context should have a negative service Id
+//        assertTrue(0 > runtimeDTO.servletContextDTOs[0].serviceId);
+        assertEquals("", runtimeDTO.servletContextDTOs[0].contextPath);
+        assertEquals(0, runtimeDTO.servletContextDTOs[0].initParams.size());
+
+        assertEquals(0, runtimeDTO.servletContextDTOs[0].filterDTOs.length);
+        assertEquals(0, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
+        assertEquals(0, runtimeDTO.servletContextDTOs[0].resourceDTOs.length);
+        assertEquals(0, runtimeDTO.servletContextDTOs[0].errorPageDTOs.length);
+        assertEquals(0, runtimeDTO.servletContextDTOs[0].listenerDTOs.length);
+    }
+
+    @Test
+    public void dtosForSuccesfullyRegisteredServlets() throws Exception
+    {
+        //register first servlet
+        registerServlet("testServlet 1", "/servlet_1");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTOWithFirstSerlvet = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithFirstSerlvet.failedServletDTOs.length);
+
+        ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstSerlvet);
+        assertEquals(1, contextDTO.servletDTOs.length);
+        assertEquals("testServlet 1", contextDTO.servletDTOs[0].name);
+
+        //register second servlet
+        registerServlet("testServlet 2", "/servlet_2");
+
+        RuntimeDTO runtimeDTOWithBothSerlvets = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithBothSerlvets.failedServletDTOs.length);
+
+        contextDTO = assertDefaultContext(runtimeDTOWithBothSerlvets);
+        assertEquals(2, contextDTO.servletDTOs.length);
+        assertEquals("testServlet 1", contextDTO.servletDTOs[0].name);
+        assertEquals("testServlet 2", contextDTO.servletDTOs[1].name);
+    }
+
+    @Test
+    public void dtosForSuccesfullyRegisteredFilters() throws Exception
+    {
+        //register first filter
+        registerFilter("testFilter 1", "/servlet_1");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTOWithFirstFilter = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithFirstFilter.failedFilterDTOs.length);
+
+        ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstFilter);
+        assertEquals(1, contextDTO.filterDTOs.length);
+        assertEquals("testFilter 1", contextDTO.filterDTOs[0].name);
+
+        //register second filter
+        registerFilter("testFilter 2", "/servlet_1");
+
+        RuntimeDTO runtimeDTOWithBothFilters = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithBothFilters.failedFilterDTOs.length);
+
+        contextDTO = assertDefaultContext(runtimeDTOWithBothFilters);
+        assertEquals(2, contextDTO.filterDTOs.length);
+        assertEquals("testFilter 1", contextDTO.filterDTOs[0].name);
+        assertEquals("testFilter 2", contextDTO.filterDTOs[1].name);
+    }
+
+    @Test
+    public void dtosForSuccesfullyRegisteredResources() throws Exception
+    {
+        // register first resource service
+        registerResource("/resources", "/resource_1/*");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTOWithFirstResource = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithFirstResource.failedResourceDTOs.length);
+
+        ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstResource);
+        assertEquals(1, contextDTO.resourceDTOs.length);
+        assertEquals("/resources", contextDTO.resourceDTOs[0].prefix);
+        assertArrayEquals(new String[] { "/resource_1/*" }, contextDTO.resourceDTOs[0].patterns);
+
+        // register second resource service
+        registerResource("/resources", "/resource_2/*");
+
+        RuntimeDTO runtimeDTOWithBothResources = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithBothResources.failedResourceDTOs.length);
+
+        contextDTO = assertDefaultContext(runtimeDTOWithBothResources);
+        assertEquals(2, contextDTO.resourceDTOs.length);
+        assertEquals("/resources", contextDTO.resourceDTOs[0].prefix);
+        assertArrayEquals(new String[] { "/resource_1/*" }, contextDTO.resourceDTOs[0].patterns);
+        assertEquals("/resources", contextDTO.resourceDTOs[1].prefix);
+        assertArrayEquals(new String[] { "/resource_2/*" }, contextDTO.resourceDTOs[1].patterns);
+    }
+
+    @Test
+    public void dtosForSuccesfullyRegisteredErrorPages() throws Exception
+    {
+        // register first error page
+        registerErrorPage("error page 1", asList("404", NoSuchElementException.class.getName()));
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTOWithFirstErrorPage = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithFirstErrorPage.failedServletDTOs.length);
+        assertEquals(0, runtimeDTOWithFirstErrorPage.failedErrorPageDTOs.length);
+
+        ServletContextDTO contextDTO = runtimeDTOWithFirstErrorPage.servletContextDTOs[0];
+        assertEquals(1, contextDTO.errorPageDTOs.length);
+        assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
+        assertArrayEquals(new String[] { NoSuchElementException.class.getName() }, contextDTO.errorPageDTOs[0].exceptions);
+        assertArrayEquals(new long[] { 404 }, contextDTO.errorPageDTOs[0].errorCodes);
+
+        // register second error page
+        registerErrorPage("error page 2", asList("500", ServletException.class.getName()));
+
+        RuntimeDTO runtimeDTOWithBothErrorPages = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithBothErrorPages.failedServletDTOs.length);
+        assertEquals(0, runtimeDTOWithBothErrorPages.failedErrorPageDTOs.length);
+
+        contextDTO = assertDefaultContext(runtimeDTOWithBothErrorPages);
+        assertEquals(2, contextDTO.errorPageDTOs.length);
+        assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
+        assertEquals("error page 2", contextDTO.errorPageDTOs[1].name);
+        assertArrayEquals(new String[] { ServletException.class.getName() }, contextDTO.errorPageDTOs[1].exceptions);
+        assertArrayEquals(new long[] { 500 }, contextDTO.errorPageDTOs[1].errorCodes);
+    }
+
+    @Test
+    public void dtosForSuccesfullyRegisteredListeners() throws Exception
+    {
+        // register a servlet context listenere as first listener
+        registerListener(ServletContextListener.class, true);
+        awaitService(ServletContextListener.class.getName());
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTOWithFirstListener = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithFirstListener.failedListenerDTOs.length);
+        assertEquals(1, runtimeDTOWithFirstListener.servletContextDTOs.length);
+
+        ServletContextDTO contextDTO = runtimeDTOWithFirstListener.servletContextDTOs[0];
+        // TODO fix : servlet context listener is only added when registerd before context activation
+        assertEquals(0, contextDTO.listenerDTOs.length);
+        // TODO
+//        assertEquals(ServletContextListener.class.getName(), contextDTO.listenerDTOs[0].types[0]);
+
+        // register all other listener types
+        registerListener(ServletContextAttributeListener.class, true);
+        registerListener(ServletRequestListener.class, true);
+        registerListener(ServletRequestAttributeListener.class, true);
+        registerListener(HttpSessionListener.class, true);
+        registerListener(HttpSessionAttributeListener.class, true);
+
+        awaitService(ServletContextAttributeListener.class.getName());
+        awaitService(ServletRequestListener.class.getName());
+        awaitService(ServletRequestAttributeListener.class.getName());
+        awaitService(HttpSessionListener.class.getName());
+        awaitService(HttpSessionAttributeListener.class.getName());
+
+        RuntimeDTO runtimeDTOWithAllListeners = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithAllListeners.failedListenerDTOs.length);
+        assertEquals(1, runtimeDTOWithAllListeners.servletContextDTOs.length);
+
+        contextDTO = runtimeDTOWithAllListeners.servletContextDTOs[0];
+        // TODO
+        assertEquals(5, contextDTO.listenerDTOs.length);
+//        assertEquals(ServletContextListener.class.getName(), contextDTO.listenerDTOs[0].types[0]);
+        assertEquals(ServletContextAttributeListener.class.getName(), contextDTO.listenerDTOs[0].types[0]);
+        assertEquals(ServletRequestListener.class.getName(), contextDTO.listenerDTOs[1].types[0]);
+        assertEquals(ServletRequestAttributeListener.class.getName(), contextDTO.listenerDTOs[2].types[0]);
+        assertEquals(HttpSessionListener.class.getName(), contextDTO.listenerDTOs[3].types[0]);
+        assertEquals(HttpSessionAttributeListener.class.getName(), contextDTO.listenerDTOs[4].types[0]);
+    }
+
+    @Test
+    public void dtosForSuccesfullyRegisteredContexts() throws Exception
+    {
+        // register first additional context
+        registerContext("contextA", "/contextA");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTOWithAdditionalContext = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithAdditionalContext.failedServletContextDTOs.length);
+        assertEquals(2, runtimeDTOWithAdditionalContext.servletContextDTOs.length);
+
+        // default context is last, as it has the lowest service ranking
+        assertEquals("contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
+        assertEquals("/contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[0].contextPath);
+        assertEquals("default", runtimeDTOWithAdditionalContext.servletContextDTOs[1].name);
+        // TODO should this be "/" ?
+        assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[1].contextPath);
+
+        // register second additional context
+        registerContext("contextB", "/contextB");
+
+        RuntimeDTO runtimeDTOWithAllContexts = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTOWithAllContexts.failedServletContextDTOs.length);
+        assertEquals(3, runtimeDTOWithAllContexts.servletContextDTOs.length);
+
+        // default context is last, as it has the lowest service ranking
+        assertEquals("contextA", runtimeDTOWithAllContexts.servletContextDTOs[0].name);
+        assertEquals("/contextA", runtimeDTOWithAllContexts.servletContextDTOs[0].contextPath);
+        assertEquals("contextB", runtimeDTOWithAllContexts.servletContextDTOs[1].name);
+        assertEquals("/contextB", runtimeDTOWithAllContexts.servletContextDTOs[1].contextPath);
+        assertEquals("default", runtimeDTOWithAllContexts.servletContextDTOs[2].name);
+        assertEquals("", runtimeDTOWithAllContexts.servletContextDTOs[2].contextPath);
+    }
+
+    @Test
+    public void successfulSetup() throws InterruptedException
+    {
+        CountDownLatch initLatch = new CountDownLatch(6);
+
+        registerContext("test-context", "/test-context");
+
+        registerServlet("default servlet", "/default", initLatch);
+        registerFilter("default filter", "/default", initLatch);
+        registerErrorPage("default error page", asList(Exception.class.getName()), initLatch);
+        registerResource("/", "/default/resource");
+        registerListener(ServletRequestListener.class, true);
+
+        registerServlet("context servlet", "/default", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
+        registerFilter("context filter", "/default", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
+        registerErrorPage("context error page", asList("500"), "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
+        registerResource("/", "/test-contextd/resource", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)");
+        registerListener(ServletRequestListener.class, true, "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)");
+
+        awaitServiceRegistration(initLatch);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
+        assertEquals(0, runtimeDTO.failedFilterDTOs.length);
+        assertEquals(0, runtimeDTO.failedListenerDTOs.length);
+        assertEquals(0, runtimeDTO.failedResourceDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+
+        ServletContextDTO defaultContextDTO = runtimeDTO.servletContextDTOs[1];
+        long contextServiceId = defaultContextDTO.serviceId;
+
+        assertEquals(1, defaultContextDTO.servletDTOs.length);
+        assertEquals("default servlet", defaultContextDTO.servletDTOs[0].name);
+        assertEquals(contextServiceId, defaultContextDTO.servletDTOs[0].servletContextId);
+        assertEquals(1, defaultContextDTO.filterDTOs.length);
+        assertEquals("default filter", defaultContextDTO.filterDTOs[0].name);
+        assertEquals(contextServiceId, defaultContextDTO.filterDTOs[0].servletContextId);
+        assertEquals(1, defaultContextDTO.errorPageDTOs.length);
+        assertEquals(Exception.class.getName(), defaultContextDTO.errorPageDTOs[0].exceptions[0]);
+        assertEquals(contextServiceId, defaultContextDTO.errorPageDTOs[0].servletContextId);
+        assertEquals(1, defaultContextDTO.listenerDTOs.length);
+        assertEquals(ServletRequestListener.class.getName(), defaultContextDTO.listenerDTOs[0].types[0]);
+        assertEquals(contextServiceId, defaultContextDTO.listenerDTOs[0].servletContextId);
+
+        ServletContextDTO testContextDTO = runtimeDTO.servletContextDTOs[0];
+        contextServiceId = testContextDTO.serviceId;
+
+        assertEquals(1, testContextDTO.servletDTOs.length);
+        assertEquals("context servlet", testContextDTO.servletDTOs[0].name);
+        assertEquals(contextServiceId, testContextDTO.servletDTOs[0].servletContextId);
+        assertEquals(1, testContextDTO.filterDTOs.length);
+        assertEquals("context filter", testContextDTO.filterDTOs[0].name);
+        assertEquals(contextServiceId, testContextDTO.filterDTOs[0].servletContextId);
+        assertEquals(1, testContextDTO.errorPageDTOs.length);
+        assertEquals(500L, testContextDTO.errorPageDTOs[0].errorCodes[0]);
+        assertEquals(contextServiceId, testContextDTO.errorPageDTOs[0].servletContextId);
+        assertEquals(1, testContextDTO.listenerDTOs.length);
+        assertEquals(ServletRequestListener.class.getName(), testContextDTO.listenerDTOs[0].types[0]);
+        assertEquals(contextServiceId, testContextDTO.listenerDTOs[0].servletContextId);
+    }
+
+    @Test
+    public void exceptionInServletInitAppearsAsFailure() throws ServletException, InterruptedException
+    {
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet",
+                HTTP_WHITEBOARD_SERVLET_NAME, "servlet");
+
+        CountDownLatch initLatch = new CountDownLatch(1);
+
+        @SuppressWarnings("serial")
+        Servlet failingServlet = new TestServlet(initLatch, null) {
+            @Override
+            public void init() throws ServletException
+            {
+                super.init();
+                throw new ServletException();
+            }
+        };
+
+        m_context.registerService(Servlet.class.getName(), failingServlet, properties);
+        awaitServiceRegistration(initLatch);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+        assertEquals(1, runtimeDTO.failedServletDTOs.length);
+        assertEquals("servlet", runtimeDTO.failedServletDTOs[0].name);
+        assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedServletDTOs[0].failureReason);
+    }
+
+    @Test
+    public void exceptionInFilterInitAppearsAsFailure() throws ServletException, InterruptedException
+    {
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_FILTER_PATTERN, "/filter",
+                HTTP_WHITEBOARD_FILTER_NAME, "filter");
+
+        CountDownLatch initLatch = new CountDownLatch(1);
+
+        Filter failingFilter = new TestFilter(initLatch, null) {
+            @Override
+            public void init(FilterConfig config) throws ServletException
+            {
+                super.init(config);
+                throw new ServletException();
+            }
+        };
+
+        m_context.registerService(Filter.class.getName(), failingFilter, properties);
+        awaitServiceRegistration(initLatch);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+        assertEquals(1, runtimeDTO.failedFilterDTOs.length);
+        assertEquals("filter", runtimeDTO.failedFilterDTOs[0].name);
+        assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedFilterDTOs[0].failureReason);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.1 (TODO : exact version)
+    @Test
+    public void hiddenDefaultContextAppearsAsFailure() throws InterruptedException
+    {
+        registerContext("default", "");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+        assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals("default", runtimeDTO.failedServletContextDTOs[0].name);
+        assertDefaultContext(runtimeDTO);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.1
+    @Test
+    public void contextHelperWithDuplicateNameAppearsAsFailure() throws InterruptedException
+    {
+        ServiceRegistration<?> firstContextReg = registerContext("contextA", "/first");
+        registerContext("contextA", "/second");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals("contextA", runtimeDTO.failedServletContextDTOs[0].name);
+        assertEquals("/second", runtimeDTO.failedServletContextDTOs[0].contextPath);
+        assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, runtimeDTO.failedServletContextDTOs[0].failureReason);
+
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+
+        assertEquals("contextA", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("/first", runtimeDTO.servletContextDTOs[0].contextPath);
+
+        firstContextReg.unregister();
+
+        runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
+
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+
+        assertEquals("contextA", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("/second", runtimeDTO.servletContextDTOs[0].contextPath);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.1
+    @Test
+    public void missingContextHelperNameAppearsAsFailure()
+    {
+        Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_CONTEXT_PATH, "");
+
+        m_context.registerService(ServletContextHelper.class.getName(), mock(ServletContextHelper.class), properties);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(null, runtimeDTO.failedServletContextDTOs[0].name);
+        assertEquals(FAILURE_REASON_VALIDATION_FAILED, runtimeDTO.failedServletContextDTOs[0].failureReason);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.1
+    @Test
+    public void invalidContextHelperNameAppearsAsFailure() throws InterruptedException
+    {
+        registerContext("context A", "");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals("context A", runtimeDTO.failedServletContextDTOs[0].name);
+        assertEquals(FAILURE_REASON_VALIDATION_FAILED, runtimeDTO.failedServletContextDTOs[0].failureReason);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.1
+    @Test
+    public void invalidContextHelperPathAppearsAsFailure() throws InterruptedException
+    {
+        registerContext("contextA", "#");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals("#", runtimeDTO.failedServletContextDTOs[0].contextPath);
+        assertEquals(FAILURE_REASON_VALIDATION_FAILED, runtimeDTO.failedServletContextDTOs[0].failureReason);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.3
+    @Test
+    public void selectionOfNonExistingContextHelperAppearsAsFailure() throws InterruptedException
+    {
+        registerServlet("servlet 1", "/", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=contextA)", null);
+        awaitServiceRegistration();
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.failedServletDTOs.length);
+        assertEquals("servlet 1", runtimeDTO.failedServletDTOs[0].name);
+        assertEquals(FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING, runtimeDTO.failedServletDTOs[0].failureReason);
+
+        registerContext("contextA", "/contextA");
+
+        runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+        assertEquals("contextA", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
+        assertEquals("servlet 1", runtimeDTO.servletContextDTOs[0].servletDTOs[0].name);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.3
+    @Test
+    public void differentTargetIsIgnored() throws InterruptedException
+    {
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet",
+                HTTP_WHITEBOARD_SERVLET_NAME, "servlet",
+                HTTP_WHITEBOARD_TARGET, "(org.osgi.service.http.port=8282)");
+
+        m_context.registerService(Servlet.class.getName(), new TestServlet(), properties);
+        awaitServiceRegistration();
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+
+        ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
+        assertEquals(0, defaultContext.servletDTOs.length);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.4
+    @Test
+    public void servletWithoutNameGetsFullyQualifiedName() throws InterruptedException
+    {
+        Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet");
+
+        CountDownLatch initLatch = new CountDownLatch(1);
+        m_context.registerService(Servlet.class.getName(), new TestServlet(initLatch, null), properties);
+        awaitServiceRegistration(initLatch);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+
+        ServletContextDTO defaultContext = assertDefaultContext(serviceRuntime.getRuntimeDTO());
+        assertEquals(1, defaultContext.servletDTOs.length);
+        assertEquals(TestServlet.class.getName(), defaultContext.servletDTOs[0].name);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.4.1
+    @Test
+    public void patternAndErrorPageSpecifiedInvalidAndAppearsAsFailure() throws InterruptedException
+    {
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet",
+                HTTP_WHITEBOARD_SERVLET_NAME, "servlet",
+                HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, asList("400"));
+
+        m_context.registerService(Servlet.class.getName(), new TestServlet(), properties);
+        awaitServiceRegistration();
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        ServletContextDTO defaultContext = assertDefaultContext(serviceRuntime.getRuntimeDTO());
+        assertEquals(0, defaultContext.servletDTOs.length);
+        assertEquals(0, defaultContext.errorPageDTOs.length);
+
+        assertEquals(0, serviceRuntime.getRuntimeDTO().failedServletDTOs.length);
+        assertEquals(1, serviceRuntime.getRuntimeDTO().failedErrorPageDTOs.length);
+        assertEquals("servlet", serviceRuntime.getRuntimeDTO().failedErrorPageDTOs[0].name);
+        assertEquals(FAILURE_REASON_VALIDATION_FAILED, serviceRuntime.getRuntimeDTO().failedErrorPageDTOs[0].failureReason);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.4.1
+    @Test
+    public void multipleServletsForSamePatternChoosenByServiceRankingRules() throws InterruptedException
+    {
+        registerServlet("servlet 1", "/pathcollision");
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+        ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
+        assertEquals(1, defaultContext.servletDTOs.length);
+
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_SERVLET_PATTERN, "/pathcollision",
+                HTTP_WHITEBOARD_SERVLET_NAME, "servlet 2",
+                SERVICE_RANKING, Integer.MAX_VALUE);
+
+        CountDownLatch initLatch = new CountDownLatch(1);
+        CountDownLatch destroyLatch = new CountDownLatch(1);
+        TestServlet testServlet = new TestServlet(initLatch, destroyLatch);
+        ServiceRegistration<?> higherRankingServlet = m_context.registerService(Servlet.class.getName(), testServlet, properties);
+
+        RuntimeDTO runtimeWithShadowedServlet = serviceRuntime.getRuntimeDTO();
+        awaitServiceRegistration(initLatch);
+
+        defaultContext = assertDefaultContext(runtimeWithShadowedServlet);
+        assertEquals(1, defaultContext.servletDTOs.length);
+
+        assertEquals(1, runtimeWithShadowedServlet.failedServletDTOs.length);
+        FailedServletDTO failedServletDTO = runtimeWithShadowedServlet.failedServletDTOs[0];
+        assertEquals("servlet 1", failedServletDTO.name);
+        assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, failedServletDTO.failureReason);
+
+        higherRankingServlet.unregister();
+        awaitServiceRegistration(destroyLatch);
+
+        runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletDTOs.length);
+        defaultContext = assertDefaultContext(runtimeDTO);
+        assertEquals(1, defaultContext.servletDTOs.length);
+        assertEquals("servlet 1", defaultContext.servletDTOs[0].name);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.4.1
+    @Test
+    @Ignore
+    public void multipleErrorPagesForSameExceptionsChoosenByServiceRankingRules()
+    {
+        // TODO
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.4
+    @Test
+    @Ignore
+    public void mulitpleServletsWithSamePatternHttpServiceRegistrationWins()
+    {
+        // TODO
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.7
+    @Test
+    public void invalidListenerPopertyValueAppearsAsFailure() throws Exception
+    {
+        Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_LISTENER, "invalid");
+
+        m_context.registerService(ServletRequestListener.class.getName(), mock(ServletRequestListener.class), properties);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.failedListenerDTOs.length);
+        assertEquals(FAILURE_REASON_VALIDATION_FAILED, runtimeDTO.failedListenerDTOs[0].failureReason);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.8
+    @Test
+    public void contextReplacedWithHigherRankingContext() throws Exception
+    {
+        ServiceRegistration<?> firstContext = registerContext("test-context", "/first");
+        Long firstContextId = (Long) firstContext.getReference().getProperty(Constants.SERVICE_ID);
+
+        CountDownLatch initLatch = new CountDownLatch(1);
+        registerServlet("servlet", "/servlet", "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test-context)", initLatch);
+        awaitServiceRegistration(initLatch);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+        assertEquals(firstContextId.longValue(), runtimeDTO.servletContextDTOs[0].serviceId);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("/first", runtimeDTO.servletContextDTOs[0].contextPath);
+        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+
+        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
+        assertEquals("servlet", runtimeDTO.servletContextDTOs[0].servletDTOs[0].name);
+
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_CONTEXT_NAME, "test-context",
+                HTTP_WHITEBOARD_CONTEXT_PATH, "/second",
+                SERVICE_RANKING, Integer.MAX_VALUE);
+
+        ServiceRegistration<?> secondContext = m_context.registerService(ServletContextHelper.class.getName(), mock(ServletContextHelper.class), properties);
+        Long secondContextId = (Long) secondContext.getReference().getProperty(Constants.SERVICE_ID);
+
+        runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(firstContextId.longValue(), runtimeDTO.failedServletContextDTOs[0].serviceId);
+        assertEquals("test-context", runtimeDTO.failedServletContextDTOs[0].name);
+        assertEquals("/first", runtimeDTO.failedServletContextDTOs[0].contextPath);
+
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+
+        assertEquals(secondContextId.longValue(), runtimeDTO.servletContextDTOs[0].serviceId);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("/second", runtimeDTO.servletContextDTOs[0].contextPath);
+        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+
+        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
+        assertEquals("servlet", runtimeDTO.servletContextDTOs[0].servletDTOs[0].name);
+
+        secondContext.unregister();
+
+        runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+        assertEquals(firstContextId.longValue(), runtimeDTO.servletContextDTOs[0].serviceId);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("/first", runtimeDTO.servletContextDTOs[0].contextPath);
+        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+
+        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
+        assertEquals("servlet", runtimeDTO.servletContextDTOs[0].servletDTOs[0].name);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.9
+    @Test
+    public void httServiceIdIsSet()
+    {
+        ServiceReference<?> httpServiceRef = m_context.getServiceReference(HttpService.class.getName());
+        ServiceReference<?> httpServiceRuntimeRef = m_context.getServiceReference(HttpServiceRuntime.class.getName());
+
+        Long expectedId = (Long) httpServiceRef.getProperty(SERVICE_ID);
+        Long actualId = (Long) httpServiceRuntimeRef.getProperty(HTTP_SERVICE_ID_ATTRIBUTE);
+
+        assertEquals(expectedId, actualId);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.9
+    @Test
+    @Ignore // This is still broken
+    public void serviceRegisteredWithHttpServiceHasNegativeServiceId() throws Exception
+    {
+        CountDownLatch initLatch = new CountDownLatch(1);
+        register("/test", new TestServlet(initLatch, null));
+        awaitServiceRegistration(initLatch);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(1, runtimeDTO.servletContextDTOs.length);
+        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
+        assertTrue(0 > runtimeDTO.servletContextDTOs[0].servletDTOs[0].serviceId);
+    }
+
+    // As specified in OSGi Compendium Release 6, Chapter 140.9
+    @Test
+    public void serviceWithoutRequiredPropertiesIsIgnored() throws InterruptedException
+    {
+        // Neither pattern nor error page specified
+        Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_SERVLET_NAME, "servlet");
+
+        m_context.registerService(Servlet.class.getName(), new TestServlet(), properties);
+        awaitServiceRegistration();
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
+        ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
+        assertEquals(0, defaultContext.servletDTOs.length);
+    }
+
+    @Test
+    public void dtosAreIndependentCopies() throws Exception
+    {
+        //register first servlet
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_SERVLET_PATTERN, "/test",
+                HTTP_WHITEBOARD_SERVLET_NAME, "servlet 1",
+                HTTP_WHITEBOARD_SERVLET_INIT_PARAM_PREFIX + "test", "testValue");
+
+        CountDownLatch initLatch = new CountDownLatch(1);
+        m_context.registerService(Servlet.class.getName(), new TestServlet(initLatch, null), properties);
+        awaitServiceRegistration(initLatch);
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTOWithFirstSerlvet = serviceRuntime.getRuntimeDTO();
+
+        //register second servlet
+        registerServlet("testServlet 2", "/servlet_2");
+
+        RuntimeDTO runtimeDTOWithTwoSerlvets = serviceRuntime.getRuntimeDTO();
+
+        assertNotSame(runtimeDTOWithFirstSerlvet, runtimeDTOWithTwoSerlvets);
+
+        ServletContextDTO defaultContextFirstServlet = assertDefaultContext(runtimeDTOWithFirstSerlvet);
+        ServletContextDTO defaultContextTwoServlets = assertDefaultContext(runtimeDTOWithTwoSerlvets);
+        assertNotSame(defaultContextFirstServlet.servletDTOs[0].patterns,
+                defaultContextTwoServlets.servletDTOs[0].patterns);
+
+        boolean mapsModifiable = true;
+        try
+        {
+            runtimeDTOWithTwoSerlvets.servletContextDTOs[0].servletDTOs[0].initParams.clear();
+        } catch (UnsupportedOperationException e)
+        {
+            mapsModifiable = false;
+        }
+
+        if (mapsModifiable)
+        {
+            assertNotSame(defaultContextFirstServlet.servletDTOs[0].initParams,
+                    defaultContextTwoServlets.servletDTOs[0].initParams);
+        }
+    }
+
+    private ServletContextDTO assertDefaultContext(RuntimeDTO runtimeDTO)
+    {
+        assertTrue(0 < runtimeDTO.servletContextDTOs.length);
+        assertEquals("default", runtimeDTO.servletContextDTOs[0].name);
+        return runtimeDTO.servletContextDTOs[0];
+    }
+
+    private void awaitServiceRegistration() throws InterruptedException
+    {
+        // Wait some time until the whiteboard (hopefully) picked up the service
+        Thread.sleep(DEFAULT_SLEEP);
+    }
+
+    private void awaitServiceRegistration(CountDownLatch initLatch) throws InterruptedException
+    {
+        if (!initLatch.await(5, TimeUnit.SECONDS))
+        {
+            fail("Service was not initialized in time!");
+        };
+        awaitServiceRegistration();
+    }
+
+    public static class TestResource
+    {
+        // Tagging class
+    }
+}

Propchange: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message