You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by tw...@apache.org on 2007/12/18 18:18:09 UTC

svn commit: r605261 - in /incubator/uima/sandbox/trunk/SimpleServer/src: main/java/org/apache/uima/simpleserver/servlet/ test/java/org/apache/uima/simpleserver/test/

Author: twgoetz
Date: Tue Dec 18 09:18:07 2007
New Revision: 605261

URL: http://svn.apache.org/viewvc?rev=605261&view=rev
Log:
Jira UIMA-652: work on test cases, some servlet error handling (return proper http codes)

https://issues.apache.org/jira/browse/UIMA-652

Added:
    incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerFailureTest.java
    incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/Utils.java
Modified:
    incubator/uima/sandbox/trunk/SimpleServer/src/main/java/org/apache/uima/simpleserver/servlet/SimpleServerServlet.java
    incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerTest.java

Modified: incubator/uima/sandbox/trunk/SimpleServer/src/main/java/org/apache/uima/simpleserver/servlet/SimpleServerServlet.java
URL: http://svn.apache.org/viewvc/incubator/uima/sandbox/trunk/SimpleServer/src/main/java/org/apache/uima/simpleserver/servlet/SimpleServerServlet.java?rev=605261&r1=605260&r2=605261&view=diff
==============================================================================
--- incubator/uima/sandbox/trunk/SimpleServer/src/main/java/org/apache/uima/simpleserver/servlet/SimpleServerServlet.java (original)
+++ incubator/uima/sandbox/trunk/SimpleServer/src/main/java/org/apache/uima/simpleserver/servlet/SimpleServerServlet.java Tue Dec 18 09:18:07 2007
@@ -68,7 +68,7 @@
   protected Map<String, Map<String, String>> servletGETParamOptions;
 
   protected Map<String, Map<String, String>> servletPOSTParamOptions;
-  
+
   private final boolean localInit;
 
   public SimpleServerServlet(boolean localInit) {
@@ -79,7 +79,7 @@
     this.servletGETParamOptions = new HashMap<String, Map<String, String>>();
     this.servletPOSTParamOptions = new HashMap<String, Map<String, String>>();
   }
-  
+
   public SimpleServerServlet() {
     this(false);
   }
@@ -152,10 +152,6 @@
         writer.print(this.getHtmlForm(request.getRequestURL().toString()));
         writer.close();
       } else if ("xmldesc".equals(mode)) {
-        if (this.server == null) {
-          throw new RuntimeException("Server object is null");
-        }
-
         writer.print(this.server.getServiceDescription());
         writer.close();
       } else {
@@ -164,8 +160,8 @@
       }
     } catch (IOException e) {
       getLogger().log(Level.SEVERE, "An error occured processing this request", e);
-      // TODO: finish
-      // response.sendError(arg0, arg1)
+      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+          "An internal error occured, this service has not been properly initialized.");
     }
   }
 
@@ -220,7 +216,7 @@
     this.initializationSuccessful = initServer();
     declareServletParameters();
   }
-  
+
   public void init(File descriptorFile, File serviceSpecFile) throws ServletException {
     super.init();
     this.initializationSuccessful = false;

Added: incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerFailureTest.java
URL: http://svn.apache.org/viewvc/incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerFailureTest.java?rev=605261&view=auto
==============================================================================
--- incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerFailureTest.java (added)
+++ incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerFailureTest.java Tue Dec 18 09:18:07 2007
@@ -0,0 +1,61 @@
+/*
+ * 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.uima.simpleserver.test;
+
+import static org.junit.Assert.assertTrue;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.http.HttpResponse;
+import org.apache.uima.simpleserver.servlet.SimpleServerServlet;
+import org.junit.Test;
+import org.mortbay.jetty.Server;
+
+/**
+ * Test server errors.
+ */
+public class ServerFailureTest {
+
+  @Test
+  public void uimaServiceNotInitialized() {
+    Server server = Utils.createServer();
+    String pathSpec = "/notinitialized";
+    // Create a simple service but don't initialize it (should throw http error 500 when called)
+    Utils.addServletWithMapping(server, new SimpleServerServlet(true), pathSpec);
+    try {
+      server.start();
+    } catch (Exception e) {
+      e.printStackTrace();
+      assertTrue(false);
+    }
+    HttpResponse response = Utils.callGet(Utils.getHost(server), Utils.getPort(server), pathSpec);
+    assertTrue("Expected http return code 500 (internal server error)", (response.getStatusLine()
+        .getStatusCode() == HttpServletResponse.SC_INTERNAL_SERVER_ERROR));
+    System.out.println(Utils.getResponseContent(response));
+    try {
+      server.stop();
+      server.join();
+    } catch (Exception e) {
+      e.printStackTrace();
+      assertTrue(false);
+    }
+  }
+
+}

Modified: incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerTest.java
URL: http://svn.apache.org/viewvc/incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerTest.java?rev=605261&r1=605260&r2=605261&view=diff
==============================================================================
--- incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerTest.java (original)
+++ incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/ServerTest.java Tue Dec 18 09:18:07 2007
@@ -19,40 +19,23 @@
 
 package org.apache.uima.simpleserver.test;
 
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-import java.net.MalformedURLException;
-import java.net.ServerSocket;
-import java.net.URISyntaxException;
-import java.net.URL;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.http.HttpException;
 import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.uima.simpleserver.servlet.SimpleServerServlet;
 import org.apache.uima.test.junit_extension.JUnitExtension;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
-import org.mortbay.jetty.Connector;
 import org.mortbay.jetty.Server;
-import org.mortbay.jetty.nio.SelectChannelConnector;
-import org.mortbay.jetty.servlet.ServletHandler;
-import org.mortbay.jetty.servlet.ServletHolder;
 
 public class ServerTest {
 
@@ -66,34 +49,16 @@
     }
   }
 
-  private static final String localhost = "127.0.0.1";
-
-  private static final String httpprotocol = "http";
-
   private static Server server = null;
 
-  private static int port = -1;
-
   @BeforeClass
   public static void setUp() {
     // Set up the server
-    server = new Server();
-    Connector connector = new SelectChannelConnector();
-    port = findFreePort();
-    assertTrue("Could not find a free port to run Jetty", (port >= 0));
-    System.out.println("Using port: " + port);
-    connector.setPort(port);
-    connector.setHost(localhost);
-    server.addConnector(connector);
-    server.setStopAtShutdown(true);
-
-    // Set up the servlet handler
-    ServletHandler servletHandler = new ServletHandler();
-    server.setHandler(servletHandler);
+    server = Utils.createServer();
 
     // Add servlets
-    servletHandler.addServletWithMapping(HelloServlet.class, "/hello");
-    
+    Utils.addServletWithMapping(server, HelloServlet.class, "/hello");
+
     // Set up UIMA servlet
     SimpleServerServlet uimaServlet = new SimpleServerServlet(true);
     File descriptorFile = JUnitExtension.getFile("desc/simpleServerTestDescriptor.xml");
@@ -106,8 +71,7 @@
       e1.printStackTrace();
       assertTrue(false);
     }
-    ServletHolder uimaServletHolder = new ServletHolder(uimaServlet);
-    servletHandler.addServletWithMapping(uimaServletHolder, "/uima");
+    Utils.addServletWithMapping(server, uimaServlet, "/uima");
 
     // Start the server
     try {
@@ -124,119 +88,17 @@
    */
   @Test
   public void test() {
-    HttpClient httpClient = new DefaultHttpClient();
-    HttpGet method = null;
-    URL url = null;
-    try {
-      url = new URL(httpprotocol, localhost, port, "/hello");
-    } catch (MalformedURLException e1) {
-      // TODO Auto-generated catch block
-      e1.printStackTrace();
-    }
-    try {
-      System.out.println("URL: " + url.toString());
-      method = new HttpGet(url.toString());
-    } catch (URISyntaxException e) {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
-    }
-    HttpResponse response = null;
-    try {
-      response = httpClient.execute(method);
-    } catch (HttpException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (IOException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (InterruptedException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    }
-    assertNotNull(response);
+    HttpResponse response = Utils.callGet(Utils.getHost(server), Utils.getPort(server), "/hello");
     assertTrue(response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK);
-
-    String out = null;
-    try {
-      Reader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
-          SimpleServerServlet.DEFAULT_CODE_PAGE));
-      char[] chars = new char[1024];
-      int len = 0;
-      StringBuffer buf = new StringBuffer();
-      while ((len = reader.read(chars)) >= 0) {
-        buf.append(chars, 0, len);
-      }
-      out = buf.toString();
-    } catch (UnsupportedEncodingException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (IllegalStateException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (IOException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    }
-    System.out.println(out);
+    System.out.println(Utils.getResponseContent(response));
   }
 
   @Test
   public void test1() {
-    HttpClient httpClient = new DefaultHttpClient();
-    HttpGet method = null;
-    URL url = null;
-    try {
-      url = new URL(httpprotocol, localhost, port, "/uima?text=foo%20bar");
-      
-    } catch (MalformedURLException e1) {
-      // TODO Auto-generated catch block
-      e1.printStackTrace();
-    }
-    try {
-      System.out.println("URL: " + url.toString());
-      method = new HttpGet(url.toString());
-    } catch (URISyntaxException e) {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
-    }
-    HttpResponse response = null;
-    try {
-      response = httpClient.execute(method);
-    } catch (HttpException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (IOException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (InterruptedException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    }
-    assertNotNull(response);
+    HttpResponse response = Utils.callGet(Utils.getHost(server), Utils.getPort(server),
+        "/uima?text=foo%20bar");
     assertTrue(response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK);
-
-    String out = null;
-    try {
-      Reader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
-          SimpleServerServlet.DEFAULT_CODE_PAGE));
-      char[] chars = new char[1024];
-      int len = 0;
-      StringBuffer buf = new StringBuffer();
-      while ((len = reader.read(chars)) >= 0) {
-        buf.append(chars, 0, len);
-      }
-      out = buf.toString();
-    } catch (UnsupportedEncodingException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (IllegalStateException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    } catch (IOException e) {
-      e.printStackTrace();
-      assertTrue(false);
-    }
-    System.out.println(out);
+    System.out.println(Utils.getResponseContent(response));
   }
 
   @AfterClass
@@ -248,19 +110,6 @@
       e.printStackTrace();
       assertTrue("Exception shutting down Jetty", false);
     }
-  }
-
-  private static final int findFreePort() {
-    int p = -1;
-    try {
-      // Create a new server socket on an unused port.
-      ServerSocket serverSocket = new ServerSocket(0);
-      p = serverSocket.getLocalPort();
-      serverSocket.close();
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-    return p;
   }
 
 }

Added: incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/Utils.java
URL: http://svn.apache.org/viewvc/incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/Utils.java?rev=605261&view=auto
==============================================================================
--- incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/Utils.java (added)
+++ incubator/uima/sandbox/trunk/SimpleServer/src/test/java/org/apache/uima/simpleserver/test/Utils.java Tue Dec 18 09:18:07 2007
@@ -0,0 +1,157 @@
+/*
+ * 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.uima.simpleserver.test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.ServerSocket;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import javax.servlet.Servlet;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.uima.simpleserver.servlet.SimpleServerServlet;
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.nio.SelectChannelConnector;
+import org.mortbay.jetty.servlet.ServletHandler;
+import org.mortbay.jetty.servlet.ServletHolder;
+
+public class Utils {
+
+  public static Server createServer() {
+    Server server = new Server();
+    Connector connector = new SelectChannelConnector();
+    final int port = findFreePort();
+    assertTrue("Could not find a free port to run Jetty", (port >= 0));
+    System.out.println("Using port: " + port);
+    connector.setPort(port);
+    connector.setHost("127.0.0.1");
+    server.addConnector(connector);
+    server.setStopAtShutdown(true);
+
+    // Set up the servlet handler
+    server.setHandler(new ServletHandler());
+
+    return server;
+  }
+
+  public static void addServletWithMapping(Server server, Class<?> servlet, String pathSpec) {
+    ((ServletHandler) server.getHandler()).addServletWithMapping(servlet, pathSpec);
+  }
+
+  public static void addServletWithMapping(Server server, Servlet servlet, String pathSpec) {
+    ((ServletHandler) server.getHandler()).addServletWithMapping(new ServletHolder(servlet),
+        pathSpec);
+  }
+  
+  public static String getHost(Server server) {
+    return server.getConnectors()[0].getHost();
+  }
+  
+  public static int getPort(Server server) {
+    return server.getConnectors()[0].getPort();
+  }
+
+  private static final int findFreePort() {
+    int p = -1;
+    try {
+      // Create a new server socket on an unused port.
+      ServerSocket serverSocket = new ServerSocket(0);
+      p = serverSocket.getLocalPort();
+      serverSocket.close();
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+    return p;
+  }
+
+  public static HttpResponse callGet(String host, int port, String file) {
+    HttpClient httpClient = new DefaultHttpClient();
+    HttpGet method = null;
+    URL url = null;
+    try {
+      url = new URL("http", host, port, file);
+    } catch (MalformedURLException e1) {
+      e1.printStackTrace();
+      assertTrue(false);
+    }
+    try {
+      System.out.println("URL: " + url.toString());
+      method = new HttpGet(url.toString());
+    } catch (URISyntaxException e) {
+      e.printStackTrace();
+      assertTrue(false);
+    }
+    HttpResponse response = null;
+    try {
+      response = httpClient.execute(method);
+    } catch (HttpException e) {
+      e.printStackTrace();
+      assertTrue(false);
+    } catch (IOException e) {
+      e.printStackTrace();
+      assertTrue(false);
+    } catch (InterruptedException e) {
+      e.printStackTrace();
+      assertTrue(false);
+    }
+    assertNotNull(response);
+    return response;
+  }
+  
+  public static String getResponseContent(HttpResponse response) {
+    try {
+      Reader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),
+          SimpleServerServlet.DEFAULT_CODE_PAGE));
+      char[] chars = new char[1024];
+      int len = 0;
+      StringBuffer buf = new StringBuffer();
+      while ((len = reader.read(chars)) >= 0) {
+        buf.append(chars, 0, len);
+      }
+      return buf.toString();
+    } catch (UnsupportedEncodingException e) {
+      e.printStackTrace();
+      assertTrue(false);
+    } catch (IllegalStateException e) {
+      e.printStackTrace();
+      assertTrue(false);
+    } catch (IOException e) {
+      e.printStackTrace();
+      assertTrue(false);
+    }
+    // Unreachable
+    return null;
+  }
+  
+}