felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1681787 - in /felix/trunk/http/itest: ./ src/test/java/org/apache/felix/http/itest/
Date Tue, 26 May 2015 16:18:47 GMT
Author: cziegeler
Date: Tue May 26 16:18:47 2015
New Revision: 1681787

URL: http://svn.apache.org/r1681787
Log:
Add integration test for session handling

Added:
    felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java
  (with props)
Modified:
    felix/trunk/http/itest/pom.xml
    felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/BaseIntegrationTest.java
    felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpServiceRuntimeTest.java

Modified: felix/trunk/http/itest/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/pom.xml?rev=1681787&r1=1681786&r2=1681787&view=diff
==============================================================================
--- felix/trunk/http/itest/pom.xml (original)
+++ felix/trunk/http/itest/pom.xml Tue May 26 16:18:47 2015
@@ -45,6 +45,24 @@
 			<version>6.0.0</version>
 			<scope>provided</scope>
 		</dependency>
+        <dependency>
+            <groupId>com.googlecode.json-simple</groupId>
+            <artifactId>json-simple</artifactId>
+            <version>1.1.1</version>
+			<scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpcore-osgi</artifactId>
+            <version>4.3.2</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient-osgi</artifactId>
+            <version>4.3.4</version>
+            <scope>provided</scope>
+        </dependency>
 		<dependency>
 			<groupId>org.osgi</groupId>
 			<artifactId>org.osgi.compendium</artifactId>

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=1681787&r1=1681786&r2=1681787&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
Tue May 26 16:18:47 2015
@@ -336,6 +336,9 @@ public abstract class BaseIntegrationTes
 
             mavenBundle("org.mockito", "mockito-core").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
             mavenBundle("org.objenesis", "objenesis").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
+            mavenBundle("com.googlecode.json-simple", "json-simple").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
+            mavenBundle("org.apache.httpcomponents", "httpcore-osgi").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
+            mavenBundle("org.apache.httpcomponents", "httpclient-osgi").versionAsInProject().startLevel(START_LEVEL_SYSTEM_BUNDLES),
 
             junitBundles(), frameworkStartLevel(START_LEVEL_TEST_BUNDLE), felix());
     }

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=1681787&r1=1681786&r2=1681787&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
Tue May 26 16:18:47 2015
@@ -51,8 +51,10 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Dictionary;
+import java.util.HashSet;
 import java.util.List;
 import java.util.NoSuchElementException;
+import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.concurrent.CountDownLatch;
@@ -94,6 +96,8 @@ import org.osgi.service.http.runtime.dto
 @ExamReactorStrategy( EagerSingleStagedReactorFactory.class )
 public class HttpServiceRuntimeTest extends BaseIntegrationTest
 {
+    private static final String HTTP_CONTEXT_NAME = "org.osgi.service.http";
+
     Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
 
     private static final long DEFAULT_SLEEP = 100;
@@ -316,8 +320,11 @@ public class HttpServiceRuntimeTest exte
 
         contextDTO = assertDefaultContext(runtimeDTOWithBothSerlvets);
         assertEquals(2, contextDTO.servletDTOs.length);
-        assertEquals("testServlet 1", contextDTO.servletDTOs[0].name);
-        assertEquals("testServlet 2", contextDTO.servletDTOs[1].name);
+        final Set<String> names = new HashSet<String>();
+        names.add(contextDTO.servletDTOs[0].name);
+        names.add(contextDTO.servletDTOs[1].name);
+        assertTrue(names.contains("testServlet 1"));
+        assertTrue(names.contains("testServlet 2"));
     }
 
     @Test
@@ -535,7 +542,7 @@ public class HttpServiceRuntimeTest exte
         assertEquals(3, runtimeDTOWithAdditionalContext.servletContextDTOs.length);
 
         // default context is last, as it has the lowest service ranking
-        assertEquals("Http Service context", runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
+        assertEquals(HTTP_CONTEXT_NAME, runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
         assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[0].contextPath);
         assertEquals("contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[1].name);
         assertEquals("/contextA", runtimeDTOWithAdditionalContext.servletContextDTOs[1].contextPath);
@@ -552,7 +559,7 @@ public class HttpServiceRuntimeTest exte
         assertEquals(4, runtimeDTOWithAllContexts.servletContextDTOs.length);
 
         // default context is last, as it has the lowest service ranking
-        assertEquals("Http Service context", runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
+        assertEquals(HTTP_CONTEXT_NAME, runtimeDTOWithAdditionalContext.servletContextDTOs[0].name);
         assertEquals("", runtimeDTOWithAdditionalContext.servletContextDTOs[0].contextPath);
         assertEquals("contextA", runtimeDTOWithAllContexts.servletContextDTOs[1].name);
         assertEquals("/contextA", runtimeDTOWithAllContexts.servletContextDTOs[1].contextPath);
@@ -596,7 +603,7 @@ public class HttpServiceRuntimeTest exte
         assertEquals(0, runtimeDTO.failedServletDTOs.length);
 
         assertEquals(3, runtimeDTO.servletContextDTOs.length);
-        assertEquals("Http Service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
         assertEquals("test-context", runtimeDTO.servletContextDTOs[1].name);
         assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
 
@@ -802,7 +809,7 @@ public class HttpServiceRuntimeTest exte
         assertEquals(FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE, runtimeDTO.failedServletContextDTOs[0].failureReason);
 
         assertEquals(3, runtimeDTO.servletContextDTOs.length);
-        assertEquals("Http Service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
         assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
 
         assertEquals("contextA", runtimeDTO.servletContextDTOs[1].name);
@@ -815,7 +822,7 @@ public class HttpServiceRuntimeTest exte
         assertEquals(0, runtimeDTO.failedServletContextDTOs.length);
 
         assertEquals(3, runtimeDTO.servletContextDTOs.length);
-        assertEquals("Http Service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
         assertEquals("default", runtimeDTO.servletContextDTOs[2].name);
 
         assertEquals("contextA", runtimeDTO.servletContextDTOs[1].name);
@@ -1107,7 +1114,7 @@ public class HttpServiceRuntimeTest exte
 
         defaultContext = assertDefaultContext(runtimeWithShadowedServlet);
         ServletContextDTO httpServiceContext = runtimeWithShadowedServlet.servletContextDTOs[0];
-        assertEquals("Http Service context", httpServiceContext.name);
+        assertEquals(HTTP_CONTEXT_NAME, httpServiceContext.name);
         assertEquals(1, httpServiceContext.servletDTOs.length);
         assertArrayEquals(new String[] {"/pathcollision"}, httpServiceContext.servletDTOs[0].patterns);
 
@@ -1169,7 +1176,7 @@ public class HttpServiceRuntimeTest exte
         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(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
 
         assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
         assertEquals("servlet", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
@@ -1196,7 +1203,7 @@ public class HttpServiceRuntimeTest exte
         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(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
 
         assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
         assertEquals("servlet", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
@@ -1211,7 +1218,7 @@ public class HttpServiceRuntimeTest exte
         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(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
 
         assertEquals(1, runtimeDTO.servletContextDTOs[1].servletDTOs.length);
         assertEquals("servlet", runtimeDTO.servletContextDTOs[1].servletDTOs[0].name);
@@ -1351,7 +1358,7 @@ public class HttpServiceRuntimeTest exte
     private ServletContextDTO assertDefaultContext(RuntimeDTO runtimeDTO)
     {
         assertTrue(1 < runtimeDTO.servletContextDTOs.length);
-        assertEquals("Http Service context", runtimeDTO.servletContextDTOs[0].name);
+        assertEquals(HTTP_CONTEXT_NAME, runtimeDTO.servletContextDTOs[0].name);
         assertEquals("default", runtimeDTO.servletContextDTOs[1].name);
         return runtimeDTO.servletContextDTOs[1];
     }

Added: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java?rev=1681787&view=auto
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java
(added)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java
Tue May 26 16:18:47 2015
@@ -0,0 +1,219 @@
+/*
+ * 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 org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+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_SERVLET_NAME;
+import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.Servlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.client.config.CookieSpecs;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.BasicCookieStore;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.json.simple.parser.ParseException;
+import org.junit.After;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.ExamReactorStrategy;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.ops4j.pax.exam.spi.reactors.EagerSingleStagedReactorFactory;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.context.ServletContextHelper;
+
+@RunWith(JUnit4TestRunner.class)
+@ExamReactorStrategy( EagerSingleStagedReactorFactory.class )
+public class SessionHandlingTest extends BaseIntegrationTest
+{
+    private List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
+
+    private CountDownLatch initLatch;
+    private CountDownLatch destroyLatch;
+
+    private void setupLatches(int count)
+    {
+        initLatch = new CountDownLatch(count);
+        destroyLatch = new CountDownLatch(count);
+    }
+
+    private void setupServlet(final String name, String[] path, int rank, final String context)
throws Exception
+    {
+        Dictionary<String, Object> servletProps = new Hashtable<String, Object>();
+        servletProps.put(HTTP_WHITEBOARD_SERVLET_NAME, name);
+        servletProps.put(HTTP_WHITEBOARD_SERVLET_PATTERN, path);
+        servletProps.put(SERVICE_RANKING, rank);
+        if (context != null)
+        {
+            servletProps.put(HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HTTP_WHITEBOARD_CONTEXT_NAME
+ "=" + context + ")");
+        }
+
+        Servlet sessionServlet = new TestServlet(initLatch, destroyLatch)
+        {
+            private static final long serialVersionUID = 1L;
+
+            @Override
+            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws IOException
+            {
+                final boolean create = req.getParameter("create") != null;
+                if ( create )
+                {
+                    req.getSession();
+                }
+                final HttpSession s = req.getSession(false);
+                if ( s != null )
+                {
+                    s.setAttribute("value", context);
+                }
+
+                final PrintWriter pw = resp.getWriter();
+                pw.println("{");
+                if ( s == null )
+                {
+                    pw.println(" \"session\" : false");
+                }
+                else
+                {
+                    pw.println(" \"session\" : true,");
+                    pw.println(" \"isNew\" : " + s.isNew() + ",");
+                    pw.println(" \"sessionId\" : \"" + s.getId() + "\",");
+                    pw.println(" \"value\" : \"" + s.getAttribute("value") + "\"");
+                }
+                pw.println("}");
+            }
+        };
+
+        registrations.add(m_context.registerService(Servlet.class.getName(), sessionServlet,
servletProps));
+    }
+
+    private void setupContext(String name, String path) throws InterruptedException
+    {
+        Dictionary<String, ?> properties = createDictionary(
+                HTTP_WHITEBOARD_CONTEXT_NAME, name,
+                HTTP_WHITEBOARD_CONTEXT_PATH, path);
+
+        ServletContextHelper servletContextHelper = new ServletContextHelper(m_context.getBundle()){
+            // test helper
+        };
+        registrations.add(m_context.registerService(ServletContextHelper.class.getName(),
servletContextHelper, properties));
+
+        Thread.sleep(500);
+    }
+
+    @After
+    public void unregisterServices() throws InterruptedException
+    {
+        for (ServiceRegistration<?> serviceRegistration : registrations)
+        {
+            serviceRegistration.unregister();
+        }
+
+        assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
+
+        Thread.sleep(500);
+    }
+
+    private JSONObject getJSONResponse(final CloseableHttpClient client, final String path)
throws IOException, ParseException
+    {
+        final HttpGet httpGet = new HttpGet(createURL(path).toExternalForm().toString());
+        CloseableHttpResponse response1 = client.execute(httpGet);
+
+        try {
+            HttpEntity entity1 = response1.getEntity();
+            final String content = EntityUtils.toString(entity1);
+
+            return (JSONObject) JSONValue.parseWithException(content);
+        } finally {
+            response1.close();
+        }
+
+    }
+    @Test
+    public void testSessionAttributes() throws Exception
+    {
+        setupContext("test1", "/");
+        setupContext("test2", "/");
+
+        setupLatches(2);
+
+        setupServlet("lowRankServlet", new String[] { "/foo" }, 1, "test1");
+        setupServlet("highRankServlet", new String[] { "/bar" }, 2, "test2" );
+
+        assertTrue(initLatch.await(5, TimeUnit.SECONDS));
+
+        RequestConfig globalConfig = RequestConfig.custom()
+                .setCookieSpec(CookieSpecs.BEST_MATCH)
+                .build();
+        final CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
+                .setDefaultCookieStore(new BasicCookieStore())
+                .build();
+
+        // session should not be available
+        final JSONObject obj1 = getJSONResponse(httpclient, "/foo");
+        assertFalse(((Boolean)obj1.get("session")).booleanValue());
+
+        final JSONObject obj2 = getJSONResponse(httpclient, "/bar");
+        assertFalse(((Boolean)obj2.get("session")).booleanValue());
+
+        // create session for one context
+        final JSONObject obj3 = getJSONResponse(httpclient, "/foo?create=true");
+        assertTrue(((Boolean)obj3.get("session")).booleanValue());
+        assertEquals("test1", obj3.get("value"));
+        final JSONObject obj4 = getJSONResponse(httpclient, "/bar");
+        assertFalse(((Boolean)obj4.get("session")).booleanValue());
+        final JSONObject obj5 = getJSONResponse(httpclient, "/foo");
+        assertTrue(((Boolean)obj5.get("session")).booleanValue());
+        assertEquals("test1", obj5.get("value"));
+
+        // create session for second context
+        final JSONObject obj6 = getJSONResponse(httpclient, "/bar?create=true");
+        assertTrue(((Boolean)obj6.get("session")).booleanValue());
+        assertEquals("test2", obj6.get("value"));
+        final JSONObject obj7 = getJSONResponse(httpclient, "/foo");
+        assertTrue(((Boolean)obj7.get("session")).booleanValue());
+        assertEquals("test1", obj7.get("value"));
+    }
+}

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

Propchange: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/SessionHandlingTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url



Mime
View raw message