abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r431707 - in /incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client: JettyTest.java JettyUtil.java TestSuite.java app/AppTest.java cache/CacheTest.java
Date Tue, 15 Aug 2006 21:23:58 GMT
Author: jmsnell
Date: Tue Aug 15 14:23:57 2006
New Revision: 431707

URL: http://svn.apache.org/viewvc?rev=431707&view=rev
Log:
A global Jetty instance. This isn't all that great, but it works.  

Added:
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
Modified:
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
    incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java?rev=431707&r1=431706&r2=431707&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyTest.java
Tue Aug 15 14:23:57 2006
@@ -17,45 +17,40 @@
 */
 package org.apache.abdera.test.client;
 
-import org.mortbay.jetty.Connector;
-import org.mortbay.jetty.Server;
-import org.mortbay.jetty.bio.SocketConnector;
 import org.mortbay.jetty.servlet.ServletHandler;
 
 import junit.framework.TestCase;
 
-public class JettyTest extends TestCase {
- 
-  private static final String PORT_PROP = "abdera.test.client.cache.port";
+public abstract class JettyTest extends TestCase {
   
-  private static int PORT = 8080;
-  protected static Server server;
+  protected int numtests = 0;
+  protected int testsrun = 0;
   
-  protected JettyTest(String... servletMappings) {
-    if (System.getProperty(PORT_PROP) != null) {
-      PORT = Integer.parseInt(System.getProperty(PORT_PROP));  
-    }
-    server = new Server();
-    Connector connector = new SocketConnector();
-    connector.setPort(PORT);
-    server.setConnectors(new Connector[]{connector});
+  protected JettyTest(int numtests) {
+    this.numtests = numtests;
+  }
+  
+  protected static ServletHandler getServletHandler(String... servletMappings) {
     ServletHandler handler = new ServletHandler();
-    server.setHandler(handler);
     for (int n = 0; n < servletMappings.length; n = n + 2) {
       String name = servletMappings[n];
       String root = servletMappings[n+1];
       handler.addServletWithMapping(name, root);
     }
     try {
-      server.start();
+      JettyUtil.addHandler(handler);
     } catch (Exception e) {}
+    return handler;
   }
+    
+  protected abstract ServletHandler getServletHandler();
   
   protected String getBase() {
-    return "http://localhost:" + PORT;
+    return "http://localhost:" + JettyUtil.getPort();
   }
   
   public void tearDown() throws Exception {
-    server.stop();
+    if (++testsrun == numtests)
+      JettyUtil.removeHandler(getServletHandler());
   }
 }

Added: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java?rev=431707&view=auto
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
(added)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/JettyUtil.java
Tue Aug 15 14:23:57 2006
@@ -0,0 +1,101 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.test.client;
+
+import javax.servlet.http.HttpServlet;
+
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.Handler;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.bio.SocketConnector;
+import org.mortbay.jetty.handler.HandlerWrapper;
+import org.mortbay.jetty.servlet.ServletHandler;
+
+public class JettyUtil {
+
+  private static final String PORT_PROP = "abdera.test.client.port";
+  
+  private static int PORT = 8080;
+  private static Server server = null;
+  
+  public static int getPort() {
+    if (System.getProperty(PORT_PROP) != null) {
+      PORT = Integer.parseInt(System.getProperty(PORT_PROP));  
+    }
+    return PORT;
+  }
+  
+  private static void initServer() throws Exception {
+    server = new Server();
+    Connector connector = new SocketConnector();
+    connector.setPort(getPort());
+    server.setConnectors(new Connector[]{connector});
+    server.setHandler(new HandlerWrapper());
+    server.start();
+  }
+  
+  public static void addHandler(ServletHandler handler) throws Exception {
+    if (server == null) initServer();
+    if (hasHandler(handler)) return;
+    server.addHandler(handler);
+  }
+  
+  private static boolean hasHandler(ServletHandler handler) throws Exception {
+    if (server == null || server.getHandlers() == null) return false;
+    for (Handler h : server.getHandlers()) {
+      if (h.equals(handler)) return true;
+    }
+    return false;
+  }
+  
+  public static void removeHandler(ServletHandler handler) throws Exception {
+    if (server == null) return;
+    server.removeHandler(handler);
+    if (server.getHandlers().length == 1) {
+      server.stop();
+      server = null;
+    }
+  }
+  
+  public static boolean isRunning() {
+    return (server != null);
+  }
+  
+  public static void main(String[] args) throws Exception {
+    
+    ServletHandler handler1 = new ServletHandler();
+    handler1.addServletWithMapping(TestServlet.class, "/foo");
+    System.out.println(JettyUtil.isRunning());
+    JettyUtil.addHandler(handler1);
+    System.out.println(JettyUtil.isRunning());
+    
+    ServletHandler handler2 = new ServletHandler();
+    handler2.addServletWithMapping(TestServlet.class, "/bar");
+    System.out.println(JettyUtil.isRunning());
+    JettyUtil.addHandler(handler2);
+    System.out.println(JettyUtil.isRunning());
+    
+    JettyUtil.removeHandler(handler1);
+    System.out.println(JettyUtil.isRunning());
+    JettyUtil.removeHandler(handler2);
+    System.out.println(JettyUtil.isRunning());
+  }
+  
+  @SuppressWarnings("serial")
+  private static class TestServlet extends HttpServlet {}
+}

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java?rev=431707&r1=431706&r2=431707&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/TestSuite.java
Tue Aug 15 14:23:57 2006
@@ -17,6 +17,7 @@
 */
 package org.apache.abdera.test.client;
 
+import org.apache.abdera.test.client.app.AppTest;
 import org.apache.abdera.test.client.cache.*;
 
 public class TestSuite extends junit.framework.TestSuite {
@@ -28,5 +29,6 @@
   public TestSuite()
   {
     addTestSuite(CacheTest.class);
+    addTestSuite(AppTest.class);
   }
 }

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java?rev=431707&r1=431706&r2=431707&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/app/AppTest.java
Tue Aug 15 14:23:57 2006
@@ -47,6 +47,7 @@
 import org.apache.abdera.protocol.client.Response;
 import org.apache.abdera.test.client.JettyTest;
 import org.apache.abdera.util.MimeTypeHelper;
+import org.mortbay.jetty.servlet.ServletHandler;
 
 /**
  * Test to make sure that we can operate as a simple APP client
@@ -54,6 +55,12 @@
 @SuppressWarnings("serial")
 public class AppTest extends JettyTest {
 
+  private static ServletHandler handler = 
+    JettyTest.getServletHandler(
+      ServiceDocumentServlet.class.getName(), "/service",
+      CollectionServlet.class.getName(), "/collections/*"
+    );
+  
   private static AppTest INSTANCE = null;
   
   private static Document<Service> init_service_document(String base) {
@@ -152,7 +159,6 @@
             if (m != null) {
               response.setStatus(HttpServletResponse.SC_OK);
               response.setContentType("text/plain");
-              response.setHeader("Cache-Control", "no-cache");
               response.getWriter().write(m);
               break;
             } else {
@@ -308,13 +314,14 @@
   }
   
   public AppTest() {
-    super(
-      ServiceDocumentServlet.class.getName(), "/service",
-      CollectionServlet.class.getName(), "/collections/*"
-    );
+    super(1);
     AppTest.INSTANCE = this;
   }
   
+  protected ServletHandler getServletHandler() {
+    return AppTest.handler;
+  }  
+  
   public void testAppClient() throws Exception {
     
     Client client = new CommonsClient();
@@ -440,7 +447,8 @@
     assertEquals(404, response.getStatus());
 
     // is the media resource gone?
-    response = client.get(media);
+    options.setNoCache(true); // need to force revalidation to check
+    response = client.get(media, options);
     assertEquals(404, response.getStatus());
     
     // YAY! We can handle media link entries 

Modified: incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java?rev=431707&r1=431706&r2=431707&view=diff
==============================================================================
--- incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java
(original)
+++ incubator/abdera/java/trunk/client/src/test/java/org/apache/abdera/test/client/cache/CacheTest.java
Tue Aug 15 14:23:57 2006
@@ -29,6 +29,7 @@
 import org.apache.abdera.protocol.client.Response;
 import org.apache.abdera.test.client.JettyTest;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.mortbay.jetty.servlet.ServletHandler;
 
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -49,18 +50,21 @@
 @SuppressWarnings("serial")
 public class CacheTest extends JettyTest {
 
+  private static ServletHandler handler = 
+    JettyTest.getServletHandler(
+      "org.apache.abdera.test.client.cache.CacheTest$CheckCacheInvalidateServlet","/check_cache_invalidate",
+      "org.apache.abdera.test.client.cache.CacheTest$NoCacheServlet", "/no_cache",
+      "org.apache.abdera.test.client.cache.CacheTest$AuthServlet", "/auth",
+      "org.apache.abdera.test.client.cache.CacheTest$CheckMustRevalidateServlet", "/must_revalidate"
+    );
+  
   private static String CHECK_CACHE_INVALIDATE;
   private static String CHECK_NO_CACHE;
   private static String CHECK_AUTH;
   private static String CHECK_MUST_REVALIDATE;
   
   public CacheTest() {
-    super(
-      "org.apache.abdera.test.client.cache.CacheTest$CheckCacheInvalidateServlet","/check_cache_invalidate",
-      "org.apache.abdera.test.client.cache.CacheTest$NoCacheServlet", "/no_cache",
-      "org.apache.abdera.test.client.cache.CacheTest$AuthServlet", "/auth",
-      "org.apache.abdera.test.client.cache.CacheTest$CheckMustRevalidateServlet", "/must_revalidate"
-    );
+    super(11);
     String base = getBase();
     CHECK_CACHE_INVALIDATE = base + "/check_cache_invalidate";
     CHECK_NO_CACHE = base + "/no_cache";
@@ -68,6 +72,9 @@
     CHECK_MUST_REVALIDATE = base + "/must_revalidate";
   }
   
+  protected ServletHandler getServletHandler() {
+    return CacheTest.handler;
+  }
   
   public static class CheckMustRevalidateServlet extends HttpServlet {
     protected void doGet(HttpServletRequest request,
@@ -321,8 +328,7 @@
     Client client = new CommonsClient();
     RequestOptions options = client.getDefaultRequestOptions();
     options.setHeader("x-reqnum", "1");
-    Response response = client.get(CHECK_CACHE_INVALIDATE, options);
-  
+    Response response = client.get(CHECK_CACHE_INVALIDATE, options);  
     String resp1 = getResponse(response.getInputStream());
     assertEquals(resp1, "1");
     



Mime
View raw message