felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1672256 [3/3] - in /felix/trunk/http: base/ base/src/main/java/org/apache/felix/http/base/internal/ base/src/main/java/org/apache/felix/http/base/internal/dispatch/ base/src/main/java/org/apache/felix/http/base/internal/handler/ base/src/m...
Date Thu, 09 Apr 2015 08:09:50 GMT
Added: felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/SimpleServletHandlerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/SimpleServletHandlerTest.java?rev=1672256&view=auto
==============================================================================
--- felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/SimpleServletHandlerTest.java (added)
+++ felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/SimpleServletHandlerTest.java Thu Apr  9 08:09:49 2015
@@ -0,0 +1,230 @@
+/*
+ * 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.base.internal.handler;
+
+import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
+import static javax.servlet.http.HttpServletResponse.SC_OK;
+import static javax.servlet.http.HttpServletResponse.SC_PAYMENT_REQUIRED;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.Map;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.http.base.internal.runtime.ServletInfo;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SimpleServletHandlerTest extends AbstractHandlerTest
+{
+    private Servlet servlet;
+
+    @Override
+    @Before
+    public void setUp()
+    {
+        super.setUp();
+        this.servlet = mock(Servlet.class);
+    }
+
+    @Test
+    public void testDestroy()
+    {
+        ServletHandler h1 = createHandler("/a");
+        h1.destroy();
+        verify(this.servlet).destroy();
+    }
+
+    @Test
+    public void testHandleFound() throws Exception
+    {
+        ServletHandler h1 = createHandler("/a");
+        HttpServletRequest req = mock(HttpServletRequest.class);
+        HttpServletResponse res = mock(HttpServletResponse.class);
+        when(this.context.handleSecurity(req, res)).thenReturn(true);
+
+        when(req.getPathInfo()).thenReturn("/a/b");
+        boolean result = h1.handle(req, res);
+
+        assertTrue(result);
+        verify(this.servlet).service(any(HttpServletRequest.class), any(HttpServletResponse.class));
+    }
+
+    @Test
+    public void testHandleFoundContextRoot() throws Exception
+    {
+        ServletHandler h1 = createHandler("/");
+        HttpServletRequest req = mock(HttpServletRequest.class);
+        HttpServletResponse res = mock(HttpServletResponse.class);
+        when(this.context.handleSecurity(req, res)).thenReturn(true);
+
+        when(req.getPathInfo()).thenReturn(null);
+        boolean result = h1.handle(req, res);
+
+        assertTrue(result);
+        verify(this.servlet).service(any(HttpServletRequest.class), any(HttpServletResponse.class));
+    }
+
+    /**
+     * FELIX-3988: only send an error for uncomitted responses with default status codes.
+     */
+    @Test
+    public void testHandleFoundForbidden() throws Exception
+    {
+        ServletHandler h1 = createHandler("/a");
+        HttpServletRequest req = mock(HttpServletRequest.class);
+        HttpServletResponse res = mock(HttpServletResponse.class);
+
+        when(req.getPathInfo()).thenReturn("/a");
+        // Default behaviour: uncomitted response and default status code...
+        when(res.isCommitted()).thenReturn(false);
+        when(res.getStatus()).thenReturn(SC_OK);
+
+        when(this.context.handleSecurity(req, res)).thenReturn(false);
+
+        when(req.getPathInfo()).thenReturn("/a/b");
+        boolean result = h1.handle(req, res);
+
+        assertFalse(result);
+        verify(this.servlet, never()).service(req, res);
+        verify(res).sendError(SC_FORBIDDEN);
+    }
+
+    /**
+     * FELIX-3988: do not try to write to an already committed response.
+     */
+    @Test
+    public void testHandleFoundForbiddenCommittedOwnResponse() throws Exception
+    {
+        ServletHandler h1 = createHandler("/a");
+        HttpServletRequest req = mock(HttpServletRequest.class);
+        HttpServletResponse res = mock(HttpServletResponse.class);
+
+        when(req.getPathInfo()).thenReturn("/a");
+        // Comitted response with default status code...
+        when(res.isCommitted()).thenReturn(true);
+        when(res.getStatus()).thenReturn(SC_OK);
+
+        when(this.context.handleSecurity(req, res)).thenReturn(false);
+
+        when(req.getPathInfo()).thenReturn("/a/b");
+        boolean result = h1.handle(req, res);
+
+        assertFalse(result);
+        verify(this.servlet, never()).service(req, res);
+        verify(res, never()).sendError(SC_FORBIDDEN);
+    }
+
+    /**
+     * FELIX-3988: do not overwrite custom set status code.
+     */
+    @Test
+    public void testHandleFoundForbiddenCustomStatusCode() throws Exception
+    {
+        ServletHandler h1 = createHandler("/a");
+        HttpServletRequest req = mock(HttpServletRequest.class);
+        HttpServletResponse res = mock(HttpServletResponse.class);
+
+        when(req.getPathInfo()).thenReturn("/a");
+        // Unomitted response with default status code...
+        when(res.isCommitted()).thenReturn(false);
+        when(res.getStatus()).thenReturn(SC_PAYMENT_REQUIRED);
+
+        when(this.context.handleSecurity(req, res)).thenReturn(false);
+
+        when(req.getPathInfo()).thenReturn("/a/b");
+        boolean result = h1.handle(req, res);
+
+        assertFalse(result);
+        verify(this.servlet, never()).service(req, res);
+        verify(res, never()).sendError(SC_FORBIDDEN);
+    }
+
+    @Test
+    public void testHandleNotFound() throws Exception
+    {
+        ServletHandler h1 = createHandler("/a");
+        HttpServletRequest req = mock(HttpServletRequest.class);
+        HttpServletResponse res = mock(HttpServletResponse.class);
+
+        when(req.getPathInfo()).thenReturn("/");
+        boolean result = h1.handle(req, res);
+
+        assertFalse(result);
+        verify(this.servlet, never()).service(req, res);
+    }
+
+    @Test
+    public void testHandleNotFoundContextRoot() throws Exception
+    {
+        ServletHandler h1 = createHandler("/a");
+        HttpServletRequest req = mock(HttpServletRequest.class);
+        HttpServletResponse res = mock(HttpServletResponse.class);
+        when(this.context.handleSecurity(req, res)).thenReturn(true);
+
+        when(req.getRequestURI()).thenReturn(null);
+        boolean result = h1.handle(req, res);
+
+        assertTrue(result);
+        verify(this.servlet).service(req, res);
+    }
+
+    @Test
+    public void testInit() throws Exception
+    {
+        ServletHandler h1 = createHandler("/a");
+        h1.init();
+        verify(this.servlet).init(any(ServletConfig.class));
+    }
+
+    @Override
+    protected AbstractHandler createHandler()
+    {
+        return createHandler("/dummy", null);
+    }
+
+    @Override
+    protected AbstractHandler createHandler(Map<String, String> map)
+    {
+        return createHandler("/dummy", map);
+    }
+
+    private ServletHandler createHandler(String alias)
+    {
+        return createHandler(alias, null);
+    }
+
+    private ServletHandler createHandler(String alias, Map<String, String> map)
+    {
+        if ( map == null )
+        {
+            map = Collections.emptyMap();
+        }
+        final ServletInfo info = new ServletInfo(null, alias, 0, map);
+        return new SimpleServletHandler(this.context, info, this.servlet);
+    }
+}

Propchange: felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/SimpleServletHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/SimpleServletHandlerTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/handler/SimpleServletHandlerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/WhiteboardServiceHelper.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/WhiteboardServiceHelper.java?rev=1672256&r1=1672255&r2=1672256&view=diff
==============================================================================
--- felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/WhiteboardServiceHelper.java (original)
+++ felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/runtime/WhiteboardServiceHelper.java Thu Apr  9 08:09:49 2015
@@ -33,7 +33,7 @@ import javax.servlet.Servlet;
 
 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.handler.SimpleServletHandler;
 import org.apache.felix.http.base.internal.runtime.dto.ErrorPageRuntime;
 import org.apache.felix.http.base.internal.runtime.dto.FilterRuntime;
 import org.apache.felix.http.base.internal.runtime.dto.ServletRuntime;
@@ -98,24 +98,26 @@ public final class WhiteboardServiceHelp
     }
 
     public static ServletRuntime createTestServletWithServiceId(String identifier,
-            ExtServletContext context)
+            ExtServletContext context,
+            long contextServiceId)
     {
-        return createTestServlet(identifier, context, ID_COUNTER.incrementAndGet());
+        return createTestServlet(identifier, context, ID_COUNTER.incrementAndGet(), contextServiceId);
     }
 
-    public static ServletRuntime createTestServlet(String identifier, ExtServletContext context)
+    public static ServletRuntime createTestServlet(String identifier, ExtServletContext context, long contextServiceId)
     {
-        return createTestServlet(identifier, context, -ID_COUNTER.incrementAndGet());
+        return createTestServlet(identifier, context, -ID_COUNTER.incrementAndGet(), contextServiceId);
     }
 
     private static ServletRuntime createTestServlet(String identifier,
             ExtServletContext context,
-            Long serviceId)
+            Long serviceId,
+            Long contextServiceId)
     {
         ServletInfo servletInfo = createServletInfo(identifier, serviceId);
         Servlet servlet = mock(Servlet.class);
         when(servlet.getServletInfo()).thenReturn("info_" + identifier);
-        return new ServletHandler(null, context, servletInfo, servlet);
+        return new SimpleServletHandler(contextServiceId, context, servletInfo, servlet);
     }
 
     private static ServletInfo createServletInfo(String identifier, Long serviceId)
@@ -156,21 +158,22 @@ public final class WhiteboardServiceHelp
                 };
     }
 
-    public static ErrorPageRuntime createErrorPageWithServiceId(String identifier, ExtServletContext context)
+    public static ErrorPageRuntime createErrorPageWithServiceId(String identifier, ExtServletContext context, long contextServiceId)
     {
-        return createErrorPage(identifier, context, ID_COUNTER.incrementAndGet());
+        return createErrorPage(identifier, context, ID_COUNTER.incrementAndGet(), contextServiceId);
     }
 
-    public static ErrorPageRuntime createErrorPage(String identifier, ExtServletContext context)
+    public static ErrorPageRuntime createErrorPage(String identifier, ExtServletContext context, long contextServiceId)
     {
-        return createErrorPage(identifier, context, -ID_COUNTER.incrementAndGet());
+        return createErrorPage(identifier, context, -ID_COUNTER.incrementAndGet(), contextServiceId);
     }
 
     private static ErrorPageRuntime createErrorPage(String identifier,
             ExtServletContext context,
-            Long serviceId)
+            Long serviceId,
+            long contextServiceId)
     {
-        ServletRuntime servletHandler = createTestServlet(identifier, context, serviceId);
+        ServletRuntime servletHandler = createTestServlet(identifier, context, serviceId, contextServiceId);
         Collection<Integer> errorCodes = Arrays.asList(400, 500);
         Collection<String> exceptions = Arrays.asList("Bad request", "Error");
 

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=1672256&r1=1672255&r2=1672256&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 Thu Apr  9 08:09:49 2015
@@ -57,7 +57,7 @@ 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.handler.SimpleServletHandler;
 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.ServletContextHelperInfo;
@@ -208,10 +208,15 @@ public class RuntimeDTOBuilderTest
 
     public void setupRegistry(List<ServletContextHelperRuntime> contexts,
             List<ContextRuntime> contextRuntimes,
+            ServletRegistryRuntime servletRegistryRuntime,
             Map<Long, Collection<ServiceReference<?>>> listenerRuntimes,
             FailureRuntime failures)
     {
-        registry = new RegistryRuntime(contexts, contextRuntimes, listenerRuntimes, failures);
+        registry = new RegistryRuntime(contexts,
+            contextRuntimes,
+            servletRegistryRuntime,
+            listenerRuntimes,
+            failures);
     }
 
     @Test
@@ -221,32 +226,36 @@ public class RuntimeDTOBuilderTest
         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<ServletRuntime> servlets = new ArrayList<ServletRuntime>();
+        List<ServletRuntime> resources = new ArrayList<ServletRuntime>();
+
+        servlets.add(createTestServlet("1", context_0, ID_0));
+        resources.add(createTestServlet("1", context_0, ID_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<ErrorPageRuntime> errorPages_0 = asList(createErrorPage("E_1", context_0, ID_0));
+        ContextRuntime contextRuntime_0 = new ContextRuntime(filters_0, errorPages_0, ID_0);
 
-        List<ServletRuntime> servlets_A = asList(createTestServlet("A_1", context_A));
+        servlets.add(createTestServlet("A_1", context_A, ID_A));
+        resources.add(createTestServlet("A_1", context_A, ID_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<ErrorPageRuntime> errorPages_A = asList(createErrorPage("E_A_1", context_A, ID_A));
+        ContextRuntime contextRuntime_A = new ContextRuntime(filters_A, errorPages_A, ID_A);
 
-        List<ServletRuntime> servlets_B = asList(createTestServletWithServiceId("B_1", context_B),
-                createTestServletWithServiceId("B_2", context_B));
+        servlets.addAll(asList(createTestServletWithServiceId("B_1", context_B, ID_B),
+                createTestServletWithServiceId("B_2", context_B, ID_B)));
+        resources.addAll(asList(createTestServletWithServiceId("B_1", context_B, ID_B),
+            createTestServletWithServiceId("B_2", context_B, ID_B)));
         List<FilterRuntime> filters_B = asList(createTestFilterWithServiceId("B_1", context_B),
                 createTestFilterWithServiceId("B_2", context_B));
-        List<ServletRuntime> resources_B = asList(createTestServletWithServiceId("B_1", context_B),
-                createTestServletWithServiceId("B_2", context_B));
-        List<ErrorPageRuntime> errorPages_B = asList(createErrorPageWithServiceId("E_B_1", context_B),
-                createErrorPageWithServiceId("E_B_2", context_B));
-        ContextRuntime contextRuntime_B = new ContextRuntime(servlets_B, filters_B, resources_B, errorPages_B, ID_B);
+        List<ErrorPageRuntime> errorPages_B = asList(createErrorPageWithServiceId("E_B_1", context_B, ID_B),
+                createErrorPageWithServiceId("E_B_2", context_B, ID_B));
+        ContextRuntime contextRuntime_B = new ContextRuntime(filters_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),
+                new ServletRegistryRuntime(servlets, resources),
                 listenerRuntimes,
                 FailureRuntime.empty());
 
@@ -565,7 +574,7 @@ public class RuntimeDTOBuilderTest
                 true,
                 Collections.<String, String>emptyMap());
         Servlet servlet = mock(Servlet.class);
-        ServletRuntime servletHandler = new ServletHandler(null, context_0, servletInfo, servlet);
+        ServletRuntime servletHandler = new SimpleServletHandler(context_0, servletInfo, servlet);
         when(servlet.getServletInfo()).thenReturn("info_0");
 
         FilterInfo filterInfo = createFilterInfo(0,
@@ -587,14 +596,13 @@ public class RuntimeDTOBuilderTest
                 true,
                 Collections.<String, String>emptyMap());
         Servlet resource = mock(Servlet.class);
-        ServletRuntime resourceHandler = new ServletHandler(null, context_0, resourceInfo, resource);
+        ServletRuntime resourceHandler = new SimpleServletHandler(ID_0, context_0, resourceInfo, resource);
 
-        ContextRuntime contextRuntime = new ContextRuntime(asList(servletHandler),
-                asList(filterHandler),
-                asList(resourceHandler),
+        ContextRuntime contextRuntime = new ContextRuntime(asList(filterHandler),
                 Collections.<ErrorPageRuntime>emptyList(),
                 ID_0);
         setupRegistry(asList(contextHandler), asList(contextRuntime),
+                new ServletRegistryRuntime(asList(resourceHandler), asList(servletHandler)),
                 Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
                 FailureRuntime.empty());
 
@@ -618,6 +626,7 @@ public class RuntimeDTOBuilderTest
 
         setupRegistry(asList(contextHandler_0, contextHandler_A),
                 asList(ContextRuntime.empty(ID_0), ContextRuntime.empty(ID_A)),
+                ServletRegistryRuntime.empty(),
                 Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
                 FailureRuntime.empty());
 
@@ -646,15 +655,14 @@ public class RuntimeDTOBuilderTest
                 true,
                 Collections.<String, String>emptyMap());
         Servlet servlet = mock(Servlet.class);
-        ServletRuntime servletHandler = new ServletHandler(null, context_0, servletInfo, servlet);
+        ServletRuntime servletHandler = new SimpleServletHandler(ID_0, context_0, servletInfo, servlet);
         when(servlet.getServletInfo()).thenReturn("info_0");
 
-        ContextRuntime contextRuntime = new ContextRuntime(asList(servletHandler),
-                Collections.<FilterRuntime>emptyList(),
-                Collections.<ServletRuntime>emptyList(),
+        ContextRuntime contextRuntime = new ContextRuntime(Collections.<FilterRuntime>emptyList(),
                 Collections.<ErrorPageRuntime>emptyList(),
                 ID_0);
         setupRegistry(asList(contextHandler), asList(contextRuntime),
+                new ServletRegistryRuntime(asList(servletHandler), Collections.<ServletRuntime>emptyList()),
                 Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
                 FailureRuntime.empty());
 
@@ -732,6 +740,7 @@ public class RuntimeDTOBuilderTest
     {
         setupRegistry(Collections.<ServletContextHelperRuntime>emptyList(),
                 Collections.<ContextRuntime>emptyList(),
+                ServletRegistryRuntime.empty(),
                 Collections.<Long, Collection<ServiceReference<?>>>emptyMap(),
                 setupFailures());
 

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=1672256&r1=1672255&r2=1672256&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 Thu Apr  9 08:09:49 2015
@@ -313,7 +313,7 @@ public abstract class BaseIntegrationTes
             bootDelegationPackage("sun.*"),
             cleanCaches(),
             CoreOptions.systemProperty("logback.configurationFile").value("file:src/test/resources/logback.xml"), //
-            //            CoreOptions.vmOption("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8787"),
+//                        CoreOptions.vmOption("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8787"),
 
             mavenBundle("org.slf4j", "slf4j-api").version("1.6.5").startLevel(START_LEVEL_SYSTEM_BUNDLES),
             mavenBundle("ch.qos.logback", "logback-core").version("1.0.6").startLevel(START_LEVEL_SYSTEM_BUNDLES),

Modified: 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=1672256&r1=1672255&r2=1672256&view=diff
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java (original)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java Thu Apr  9 08:09:49 2015
@@ -68,7 +68,6 @@ import javax.servlet.http.HttpSessionAtt
 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;
@@ -78,6 +77,7 @@ import org.osgi.framework.ServiceRegistr
 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.FailedErrorPageDTO;
 import org.osgi.service.http.runtime.dto.FailedServletDTO;
 import org.osgi.service.http.runtime.dto.RuntimeDTO;
 import org.osgi.service.http.runtime.dto.ServletContextDTO;
@@ -237,20 +237,20 @@ public class HttpServiceRuntimeTest exte
         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());
+        ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
 
+        assertEquals(0, defaultContext.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());
+        // TODO Should be "/" ?
+        assertEquals("", defaultContext.contextPath);
+        assertEquals(0, defaultContext.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);
+        assertEquals(0, defaultContext.filterDTOs.length);
+        assertEquals(0, defaultContext.servletDTOs.length);
+        assertEquals(0, defaultContext.resourceDTOs.length);
+        assertEquals(0, defaultContext.errorPageDTOs.length);
+        assertEquals(0, defaultContext.listenerDTOs.length);
     }
 
     @Test
@@ -360,7 +360,7 @@ public class HttpServiceRuntimeTest exte
         assertEquals(0, runtimeDTOWithFirstErrorPage.failedServletDTOs.length);
         assertEquals(0, runtimeDTOWithFirstErrorPage.failedErrorPageDTOs.length);
 
-        ServletContextDTO contextDTO = runtimeDTOWithFirstErrorPage.servletContextDTOs[0];
+        ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstErrorPage);
         assertEquals(1, contextDTO.errorPageDTOs.length);
         assertEquals("error page 1", contextDTO.errorPageDTOs[0].name);
         assertArrayEquals(new String[] { NoSuchElementException.class.getName() }, contextDTO.errorPageDTOs[0].exceptions);
@@ -395,9 +395,8 @@ public class HttpServiceRuntimeTest exte
         RuntimeDTO runtimeDTOWithFirstListener = serviceRuntime.getRuntimeDTO();
 
         assertEquals(0, runtimeDTOWithFirstListener.failedListenerDTOs.length);
-        assertEquals(1, runtimeDTOWithFirstListener.servletContextDTOs.length);
 
-        ServletContextDTO contextDTO = runtimeDTOWithFirstListener.servletContextDTOs[0];
+        ServletContextDTO contextDTO = assertDefaultContext(runtimeDTOWithFirstListener);
         // TODO fix : servlet context listener is only added when registerd before context activation
         assertEquals(0, contextDTO.listenerDTOs.length);
         // TODO
@@ -419,9 +418,8 @@ public class HttpServiceRuntimeTest exte
         RuntimeDTO runtimeDTOWithAllListeners = serviceRuntime.getRuntimeDTO();
 
         assertEquals(0, runtimeDTOWithAllListeners.failedListenerDTOs.length);
-        assertEquals(1, runtimeDTOWithAllListeners.servletContextDTOs.length);
 
-        contextDTO = runtimeDTOWithAllListeners.servletContextDTOs[0];
+        contextDTO = assertDefaultContext(runtimeDTOWithAllListeners);
         // TODO
         assertEquals(5, contextDTO.listenerDTOs.length);
 //        assertEquals(ServletContextListener.class.getName(), contextDTO.listenerDTOs[0].types[0]);
@@ -444,14 +442,16 @@ public class HttpServiceRuntimeTest exte
         RuntimeDTO runtimeDTOWithAdditionalContext = serviceRuntime.getRuntimeDTO();
 
         assertEquals(0, runtimeDTOWithAdditionalContext.failedServletContextDTOs.length);
-        assertEquals(2, runtimeDTOWithAdditionalContext.servletContextDTOs.length);
+        assertEquals(3, 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);
+        assertEquals("Http service context", runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
+        assertEquals("/", runtimeDTOWithAdditionalContext.servletContextDTOs[0].contextPath);
+        assertEquals("contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[1].name);
+        assertEquals("/contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[1].contextPath);
+        assertEquals("default", runtimeDTOWithAdditionalContext.servletContextDTOs[2].name);
         // TODO should this be "/" ?
-        assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[1].contextPath);
+        assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[2].contextPath);
 
         // register second additional context
         registerContext("contextB", "/contextB");
@@ -459,15 +459,17 @@ public class HttpServiceRuntimeTest exte
         RuntimeDTO runtimeDTOWithAllContexts = serviceRuntime.getRuntimeDTO();
 
         assertEquals(0, runtimeDTOWithAllContexts.failedServletContextDTOs.length);
-        assertEquals(3, runtimeDTOWithAllContexts.servletContextDTOs.length);
+        assertEquals(4, 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);
+        assertEquals("Http service context", runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
+        assertEquals("/", runtimeDTOWithAdditionalContext.servletContextDTOs[0].contextPath);
+        assertEquals("contextA", runtimeDTOWithAllContexts.servletContextDTOs[1].name);
+        assertEquals("/contextA", runtimeDTOWithAllContexts.servletContextDTOs[1].contextPath);
+        assertEquals("contextB", runtimeDTOWithAllContexts.servletContextDTOs[2].name);
+        assertEquals("/contextB", runtimeDTOWithAllContexts.servletContextDTOs[2].contextPath);
+        assertEquals("default", runtimeDTOWithAllContexts.servletContextDTOs[3].name);
+        assertEquals("", runtimeDTOWithAllContexts.servletContextDTOs[3].contextPath);
     }
 
     @Test
@@ -503,11 +505,12 @@ public class HttpServiceRuntimeTest exte
         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);
+        assertEquals(3, runtimeDTO.servletContextDTOs.length);
+        assertEquals("Http service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
 
-        ServletContextDTO defaultContextDTO = runtimeDTO.servletContextDTOs[1];
+        ServletContextDTO defaultContextDTO = runtimeDTO.servletContextDTOs[2];
         long contextServiceId = defaultContextDTO.serviceId;
 
         assertEquals(1, defaultContextDTO.servletDTOs.length);
@@ -523,7 +526,7 @@ public class HttpServiceRuntimeTest exte
         assertEquals(ServletRequestListener.class.getName(), defaultContextDTO.listenerDTOs[0].types[0]);
         assertEquals(contextServiceId, defaultContextDTO.listenerDTOs[0].servletContextId);
 
-        ServletContextDTO testContextDTO = runtimeDTO.servletContextDTOs[0];
+        ServletContextDTO testContextDTO = runtimeDTO.servletContextDTOs[1];
         contextServiceId = testContextDTO.serviceId;
 
         assertEquals(1, testContextDTO.servletDTOs.length);
@@ -633,11 +636,12 @@ public class HttpServiceRuntimeTest exte
         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(3, runtimeDTO.servletContextDTOs.length);
+        assertEquals("Http service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
 
-        assertEquals("contextA", runtimeDTO.servletContextDTOs[0].name);
-        assertEquals("/first", runtimeDTO.servletContextDTOs[0].contextPath);
+        assertEquals("contextA", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals("/first", runtimeDTO.servletContextDTOs[1].contextPath);
 
         firstContextReg.unregister();
 
@@ -645,11 +649,12 @@ public class HttpServiceRuntimeTest exte
 
         assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
 
-        assertEquals(2, runtimeDTO.servletContextDTOs.length);
-        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals(3, runtimeDTO.servletContextDTOs.length);
+        assertEquals("Http service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
 
-        assertEquals("contextA", runtimeDTO.servletContextDTOs[0].name);
-        assertEquals("/second", runtimeDTO.servletContextDTOs[0].contextPath);
+        assertEquals("contextA", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals("/second", runtimeDTO.servletContextDTOs[1].contextPath);
     }
 
     // As specified in OSGi Compendium Release 6, Chapter 140.1
@@ -723,10 +728,10 @@ public class HttpServiceRuntimeTest exte
         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);
+        assertEquals(3, runtimeDTO.servletContextDTOs.length);
+        assertEquals("contextA", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
+        assertEquals("servlet 1", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
     }
 
     // As specified in OSGi Compendium Release 6, Chapter 140.3
@@ -848,18 +853,105 @@ public class HttpServiceRuntimeTest exte
 
     // As specified in OSGi Compendium Release 6, Chapter 140.4.1
     @Test
-    @Ignore
-    public void multipleErrorPagesForSameExceptionsChoosenByServiceRankingRules()
+    public void multipleErrorPagesForSameExceptionsChoosenByServiceRankingRules() throws InterruptedException
     {
-        // TODO
+        registerErrorPage("error page 1", asList(NullPointerException.class.getName(), "500"));
+
+        HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
+        assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
+
+        RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        ServletContextDTO defaultContext = assertDefaultContext(runtimeDTO);
+
+        assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
+        assertEquals(1, defaultContext.errorPageDTOs.length);
+
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_SERVLET_ERROR_PAGE, asList("500", IllegalArgumentException.class.getName()),
+                HTTP_WHITEBOARD_SERVLET_NAME, "error page 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);
+        awaitServiceRegistration(initLatch);
+
+        RuntimeDTO runtimeWithShadowedErrorPage = serviceRuntime.getRuntimeDTO();
+
+        defaultContext = assertDefaultContext(runtimeWithShadowedErrorPage);
+
+        assertEquals(2, defaultContext.errorPageDTOs.length);
+        assertEquals("error page 2", defaultContext.errorPageDTOs[0].name);
+        assertArrayEquals(new long[] { 500 }, defaultContext.errorPageDTOs[0].errorCodes);
+        assertArrayEquals(new String[] { IllegalArgumentException.class.getName() }, defaultContext.errorPageDTOs[0].exceptions);
+        assertEquals("error page 1", defaultContext.errorPageDTOs[1].name);
+        assertArrayEquals(new long[] { 500 }, defaultContext.errorPageDTOs[1].errorCodes);
+        assertArrayEquals(new String[] { NullPointerException.class.getName() }, defaultContext.errorPageDTOs[1].exceptions);
+
+        assertEquals(1, runtimeWithShadowedErrorPage.failedErrorPageDTOs.length);
+        FailedErrorPageDTO failedErrorPageDTO = runtimeWithShadowedErrorPage.failedErrorPageDTOs[0];
+        assertEquals("error page 1", failedErrorPageDTO.name);
+        assertArrayEquals(new long[] { 500 }, failedErrorPageDTO.errorCodes);
+        assertArrayEquals(new String[] { NullPointerException.class.getName() }, failedErrorPageDTO.exceptions);
+        assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, failedErrorPageDTO.failureReason);
+
+        higherRankingServlet.unregister();
+        awaitServiceRegistration(destroyLatch);
+
+        runtimeDTO = serviceRuntime.getRuntimeDTO();
+
+        defaultContext = assertDefaultContext(runtimeDTO);
+
+        assertEquals(0, runtimeDTO.failedErrorPageDTOs.length);
+        assertEquals(1, defaultContext.errorPageDTOs.length);
+        assertEquals("error page 1", defaultContext.errorPageDTOs[0].name);
     }
 
     // As specified in OSGi Compendium Release 6, Chapter 140.4
     @Test
-    @Ignore
-    public void mulitpleServletsWithSamePatternHttpServiceRegistrationWins()
+    public void mulitpleServletsWithSamePatternHttpServiceRegistrationWins() throws Exception
     {
-        // TODO
+        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);
+
+        CountDownLatch initLatch = new CountDownLatch(1);
+        CountDownLatch destroyLatch = new CountDownLatch(1);
+        TestServlet testServlet = new TestServlet(initLatch, destroyLatch);
+        register("/pathcollision", testServlet);
+
+        RuntimeDTO runtimeWithShadowedServlet = serviceRuntime.getRuntimeDTO();
+        awaitServiceRegistration(initLatch);
+
+        defaultContext = assertDefaultContext(runtimeWithShadowedServlet);
+        ServletContextDTO httpServiceContext = runtimeWithShadowedServlet.servletContextDTOs[0];
+        assertEquals("Http service context", httpServiceContext.name);
+        assertEquals(1, httpServiceContext.servletDTOs.length);
+        assertArrayEquals(new String[] {"/pathcollision", "/pathcollision/*"}, httpServiceContext.servletDTOs[0].patterns);
+
+        assertEquals(1, runtimeWithShadowedServlet.failedServletDTOs.length);
+        FailedServletDTO failedServletDTO = runtimeWithShadowedServlet.failedServletDTOs[0];
+        assertEquals("servlet 1", failedServletDTO.name);
+        assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, failedServletDTO.failureReason);
+
+        unregister(testServlet);
+        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.7
@@ -896,14 +988,15 @@ public class HttpServiceRuntimeTest exte
         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(3, runtimeDTO.servletContextDTOs.length);
+        assertEquals(firstContextId.longValue(), runtimeDTO.servletContextDTOs[1].serviceId);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals("/first", runtimeDTO.servletContextDTOs[1].contextPath);
+        assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
+        assertEquals("Http service context", runtimeDTO.servletContextDTOs[0].name);
 
-        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
-        assertEquals("servlet", runtimeDTO.servletContextDTOs[0].servletDTOs[0].name);
+        assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
+        assertEquals("servlet", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
 
         Dictionary<String, ?> properties = createDictionary(
                 HTTP_WHITEBOARD_CONTEXT_NAME, "test-context",
@@ -920,34 +1013,36 @@ public class HttpServiceRuntimeTest exte
         assertEquals("test-context", runtimeDTO.failedServletContextDTOs[0].name);
         assertEquals("/first", runtimeDTO.failedServletContextDTOs[0].contextPath);
 
-        assertEquals(2, runtimeDTO.servletContextDTOs.length);
+        assertEquals(3, 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(secondContextId.longValue(), runtimeDTO.servletContextDTOs[1].serviceId);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals("/second", runtimeDTO.servletContextDTOs[1].contextPath);
+        assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
+        assertEquals("Http service context", runtimeDTO.servletContextDTOs[0].name);
 
-        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
-        assertEquals("servlet", runtimeDTO.servletContextDTOs[0].servletDTOs[0].name);
+        assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
+        assertEquals("servlet", runtimeDTO.servletContextDTOs[1].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(3, runtimeDTO.servletContextDTOs.length);
+        assertEquals(firstContextId.longValue(), runtimeDTO.servletContextDTOs[1].serviceId);
+        assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
+        assertEquals("/first", runtimeDTO.servletContextDTOs[1].contextPath);
+        assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
+        assertEquals("Http service context", runtimeDTO.servletContextDTOs[0].name);
 
-        assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
-        assertEquals("servlet", runtimeDTO.servletContextDTOs[0].servletDTOs[0].name);
+        assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
+        assertEquals("servlet", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
     }
 
     // As specified in OSGi Compendium Release 6, Chapter 140.9
     @Test
-    public void httServiceIdIsSet()
+    public void httpServiceIdIsSet()
     {
         ServiceReference<?> httpServiceRef = m_context.getServiceReference(HttpService.class.getName());
         ServiceReference<?> httpServiceRuntimeRef = m_context.getServiceReference(HttpServiceRuntime.class.getName());
@@ -960,7 +1055,6 @@ public class HttpServiceRuntimeTest exte
 
     // 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);
@@ -972,7 +1066,7 @@ public class HttpServiceRuntimeTest exte
 
         RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
 
-        assertEquals(1, runtimeDTO.servletContextDTOs.length);
+        assertEquals(2, runtimeDTO.servletContextDTOs.length);
         assertEquals(1, runtimeDTO.servletContextDTOs[0].servletDTOs.length);
         assertTrue(0 > runtimeDTO.servletContextDTOs[0].servletDTOs[0].serviceId);
     }
@@ -1030,7 +1124,7 @@ public class HttpServiceRuntimeTest exte
         boolean mapsModifiable = true;
         try
         {
-            runtimeDTOWithTwoSerlvets.servletContextDTOs[0].servletDTOs[0].initParams.clear();
+            defaultContextTwoServlets.servletDTOs[0].initParams.clear();
         } catch (UnsupportedOperationException e)
         {
             mapsModifiable = false;
@@ -1045,9 +1139,10 @@ public class HttpServiceRuntimeTest exte
 
     private ServletContextDTO assertDefaultContext(RuntimeDTO runtimeDTO)
     {
-        assertTrue(0 < runtimeDTO.servletContextDTOs.length);
-        assertEquals("default", runtimeDTO.servletContextDTOs[0].name);
-        return runtimeDTO.servletContextDTOs[0];
+        assertTrue(1 < runtimeDTO.servletContextDTOs.length);
+        assertEquals("Http service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
+        return runtimeDTO.servletContextDTOs[1];
     }
 
     private void awaitServiceRegistration() throws InterruptedException

Modified: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/ServletPatternTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/ServletPatternTest.java?rev=1672256&r1=1672255&r2=1672256&view=diff
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/ServletPatternTest.java (original)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/ServletPatternTest.java Thu Apr  9 08:09:49 2015
@@ -20,6 +20,12 @@
 package org.apache.felix.http.itest;
 
 import static org.junit.Assert.assertTrue;
+import static org.osgi.framework.Constants.SERVICE_RANKING;
+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_INIT_PARAM_PREFIX;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
 
 import java.io.IOException;
 import java.util.Dictionary;
@@ -36,135 +42,203 @@ import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.junit.JUnit4TestRunner;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
-
+import org.osgi.service.http.context.ServletContextHelper;
 
 @RunWith(JUnit4TestRunner.class)
-public class ServletPatternTest extends BaseIntegrationTest 
+public class ServletPatternTest extends BaseIntegrationTest
 {
-	
-	@Test
-	public void testHighRankReplaces() throws Exception
-	{
-		CountDownLatch initLatch = new CountDownLatch(2);
-		CountDownLatch destroyLatch = new CountDownLatch(2);
-		
-		TestServlet lowRankServlet = new TestServlet(initLatch, destroyLatch)
-		{
-			private static final long serialVersionUID = 1L;
-			
-			@Override
-			protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
-			{
-				resp.getWriter().print("lowRankServlet");
-				resp.flushBuffer();
-			}
-		};
-		
-		TestServlet highRankServlet = new TestServlet(initLatch, destroyLatch)
-		{
-			private static final long serialVersionUID = 1L;
-			
-			@Override
-			protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
-			{
-				resp.getWriter().print("highRankServlet");
-				resp.flushBuffer();
-			}
-		};
-		
-		Dictionary<String, Object> lowRankProps = new Hashtable<String, Object>();
-		String lowRankPattern[] = {"/foo", "/bar"};
-		lowRankProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, lowRankPattern);
-		lowRankProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "lowRankServlet");
-		lowRankProps.put(Constants.SERVICE_RANKING, 1);
-		
-		ServiceRegistration<?> lowRankReg = m_context.registerService(Servlet.class.getName(), lowRankServlet, lowRankProps);
-		
-		Dictionary<String, Object> highRankProps = new Hashtable<String, Object>();
-		String highRankPattern[] = {"/foo", "/baz"};
-		highRankProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, highRankPattern);
-		highRankProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "highRankServlet");
-		highRankProps.put(Constants.SERVICE_RANKING, 2);
-
-		ServiceRegistration<?> highRankReg = m_context.registerService(Servlet.class.getName(), highRankServlet, highRankProps);
-		
-		try {
-			assertTrue(initLatch.await(5, TimeUnit.SECONDS));
-			
-			assertContent("highRankServlet", createURL("/foo"));
-			assertContent("lowRankServlet", createURL("/bar"));
-			assertContent("highRankServlet", createURL("/baz"));
-			
-		} finally {
-			lowRankReg.unregister();
-			highRankReg.unregister();
-		}
-		
-		assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
-	}
-	
-	@Test
-	public void testSameRankDoesNotReplace() throws Exception
-	{
-		CountDownLatch initLatch = new CountDownLatch(2);
-		CountDownLatch destroyLatch = new CountDownLatch(2);
-		
-		TestServlet servlet1 = new TestServlet(initLatch, destroyLatch)
-		{
-			private static final long serialVersionUID = 1L;
-			
-			@Override
-			protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
-			{
-				resp.getWriter().print("servlet1");
-				resp.flushBuffer();
-			}
-		};
-		
-		TestServlet servlet2 = new TestServlet(initLatch, destroyLatch)
-		{
-			private static final long serialVersionUID = 1L;
-			
-			@Override
-			protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
-			{
-				resp.getWriter().print("servlet2");
-				resp.flushBuffer();
-			}
-		};
-		
-		Dictionary<String, Object> props1 = new Hashtable<String, Object>();
-		String lowRankPattern[] = {"/foo", "/bar"};
-		props1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, lowRankPattern);
-		props1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "lowRankServlet");
-		props1.put(Constants.SERVICE_RANKING, 2);
-		
-		ServiceRegistration<?> reg1 = m_context.registerService(Servlet.class.getName(), servlet1, props1);
-		
-		Dictionary<String, Object> props2 = new Hashtable<String, Object>();
-		String highRankPattern[] = {"/foo", "/baz"};
-		props2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, highRankPattern);
-		props2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "highRankServlet");
-		props2.put(Constants.SERVICE_RANKING, 2);
-
-		ServiceRegistration<?> reg2 = m_context.registerService(Servlet.class.getName(), servlet2, props2);
-		
-		try {
-			assertTrue(initLatch.await(5, TimeUnit.SECONDS));
-			
-			assertContent("servlet1", createURL("/foo"));
-			assertContent("servlet1", createURL("/bar"));
-			assertContent("servlet2", createURL("/baz"));
-			
-		} finally {
-			reg1.unregister();
-			reg2.unregister();
-		}
-		
-		assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
-	}
-}
-
 
+    @Test
+    public void testHighRankReplaces() throws Exception
+    {
+        CountDownLatch initLatch = new CountDownLatch(2);
+        CountDownLatch destroyLatch = new CountDownLatch(2);
+
+        TestServlet lowRankServlet = new TestServlet(initLatch, destroyLatch)
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws IOException
+            {
+                resp.getWriter().print("lowRankServlet");
+                resp.flushBuffer();
+            }
+        };
+
+        TestServlet highRankServlet = new TestServlet(initLatch, destroyLatch)
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws IOException
+            {
+                resp.getWriter().print("highRankServlet");
+                resp.flushBuffer();
+            }
+        };
+
+        Dictionary<String, Object> lowRankProps = new Hashtable<String, Object>();
+        String lowRankPattern[] = { "/foo", "/bar" };
+        lowRankProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, lowRankPattern);
+        lowRankProps.put(HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "lowRankServlet");
+        lowRankProps.put(SERVICE_RANKING, 1);
+
+        ServiceRegistration<?> lowRankReg = m_context.registerService(Servlet.class.getName(),
+            lowRankServlet, lowRankProps);
+
+        Dictionary<String, Object> highRankProps = new Hashtable<String, Object>();
+        String highRankPattern[] = { "/foo", "/baz" };
+        highRankProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, highRankPattern);
+        highRankProps.put(HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "highRankServlet");
+        highRankProps.put(Constants.SERVICE_RANKING, 2);
+
+        ServiceRegistration<?> highRankReg = m_context.registerService(Servlet.class.getName(),
+            highRankServlet, highRankProps);
+
+        try
+        {
+            assertTrue(initLatch.await(5, TimeUnit.SECONDS));
+
+            assertContent("highRankServlet", createURL("/foo"));
+            assertContent("lowRankServlet", createURL("/bar"));
+            assertContent("highRankServlet", createURL("/baz"));
+
+        }
+        finally
+        {
+            lowRankReg.unregister();
+            highRankReg.unregister();
+        }
+
+        assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
+    }
+
+    @Test
+    public void testHttpServiceReplaces() throws Exception
+    {
+        Dictionary<String, ?> properties = createDictionary(
+            HTTP_WHITEBOARD_CONTEXT_NAME, "test",
+            HTTP_WHITEBOARD_CONTEXT_PATH, "/test");
+
+        ServletContextHelper servletContextHelper = new ServletContextHelper(m_context.getBundle()) {};
+        m_context.registerService(ServletContextHelper.class.getName(), servletContextHelper, properties);
+
+        CountDownLatch initLatch = new CountDownLatch(2);
+        CountDownLatch destroyLatch = new CountDownLatch(2);
+
+        TestServlet whiteboardServlet = new TestServlet(initLatch, destroyLatch)
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+            {
+                resp.getWriter().print("whiteboardServlet");
+                resp.flushBuffer();
+            }
+        };
+
+        TestServlet httpServiceServlet = new TestServlet(initLatch, destroyLatch)
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+            {
+                resp.getWriter().print("httpServiceServlet");
+                resp.flushBuffer();
+            }
+        };
+
+        String whiteboardPattern[] = { "/foo", "/bar" };
+        Dictionary<String, Object> whiteboardProps = new Hashtable<String, Object>();
+        whiteboardProps.put(HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=test)");
+        whiteboardProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, whiteboardPattern);
+        whiteboardProps.put(SERVICE_RANKING, Integer.MAX_VALUE);
+        ServiceRegistration<?> serviceRegistration = m_context.registerService(Servlet.class.getName(), whiteboardServlet, whiteboardProps);
+
+        register("/test/foo", httpServiceServlet);
+
+        try
+        {
+            assertTrue(initLatch.await(5, TimeUnit.SECONDS));
+
+            assertContent("whiteboardServlet", createURL("/test/bar"));
+            assertContent("httpServiceServlet", createURL("/test/foo"));
+        }
+        finally
+        {
+            serviceRegistration.unregister();
+            unregister(httpServiceServlet);
+        }
+    }
+
+    @Test
+    public void testSameRankDoesNotReplace() throws Exception
+    {
+        CountDownLatch initLatch = new CountDownLatch(2);
+        CountDownLatch destroyLatch = new CountDownLatch(2);
+
+        TestServlet servlet1 = new TestServlet(initLatch, destroyLatch)
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+            {
+                resp.getWriter().print("servlet1");
+                resp.flushBuffer();
+            }
+        };
+
+        TestServlet servlet2 = new TestServlet(initLatch, destroyLatch)
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+            {
+                resp.getWriter().print("servlet2");
+                resp.flushBuffer();
+            }
+        };
+
+        Dictionary<String, Object> props1 = new Hashtable<String, Object>();
+        String lowRankPattern[] = { "/foo", "/bar" };
+        props1.put(HTTP_WHITEBOARD_SERVLET_PATTERN, lowRankPattern);
+        props1.put(HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "lowRankServlet");
+        props1.put(SERVICE_RANKING, 2);
+
+        ServiceRegistration<?> reg1 = m_context.registerService(Servlet.class.getName(),
+            servlet1, props1);
+
+        Dictionary<String, Object> props2 = new Hashtable<String, Object>();
+        String highRankPattern[] = { "/foo", "/baz" };
+        props2.put(HTTP_WHITEBOARD_SERVLET_PATTERN, highRankPattern);
+        props2.put(HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "highRankServlet");
+        props2.put(SERVICE_RANKING, 2);
+
+        ServiceRegistration<?> reg2 = m_context.registerService(Servlet.class.getName(),
+            servlet2, props2);
+
+        try
+        {
+            assertTrue(initLatch.await(5, TimeUnit.SECONDS));
+
+            assertContent("servlet1", createURL("/foo"));
+            assertContent("servlet1", createURL("/bar"));
+            assertContent("servlet2", createURL("/baz"));
+
+        }
+        finally
+        {
+            reg1.unregister();
+            reg2.unregister();
+        }
 
+        assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
+    }
+}



Mime
View raw message