[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Thu Apr 12 00:53:31 UTC 2007


gordonp
Wed Apr 11 20:53:31 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test
In directory dev.open-bio.org:/tmp/cvs-serv30416/src/main/org/biomoby/service/test

Modified Files:
	ServletTester.java 
Log Message:
Added ability to load sample data as resource, specify alternative web.xml location, and stricter on testing URL to avoid localhost references
moby-live/Java/src/main/org/biomoby/service/test ServletTester.java,1.2,1.3
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/ServletTester.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/ServletTester.java	2007/03/12 16:57:30	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/ServletTester.java	2007/04/12 00:53:31	1.3
@@ -21,6 +21,23 @@
 import java.io.*;
 
 public class ServletTester{
+    private String alternateWebXML;
+
+    /**
+     * Runs a test on the service described in WEB-INF/web.xml
+     */
+    public ServletTester(){
+	this(null);
+    }
+
+    /**
+     * Mainly for JUnit tests, specify a web.xml file to be used for configuring the test run,
+     * rather than using the default WEB-INF/web.xml in the class path.
+     */
+    public ServletTester(String altWebXML){
+	alternateWebXML = altWebXML;
+    }
+
     public static void main(String[] args) throws Exception{
 
         if(args.length != 2 && args.length != 3 || 
@@ -29,19 +46,23 @@
 			       "<fully-qualified servlet url> <exampleMobyData.xml (register|register_permanent) | unregister>");
 	    throw new Exception("Incorrect command arguments");
         }
-	if(args[0].indexOf("http://localhost") == 0){
+	if(args[0].indexOf("http://localhost") == 0 || args[0].indexOf("http://127.0.0.1/") == 0){
 	    System.err.println("The servlet URL must be fully qualified " + 
-			       "(\"localhost\" is not acceptable)");
+			       "(\"localhost\" or \"127.0.0.1\" are not acceptable)");
 	    throw new Exception("Servlet URL not fully qualified");
 	}
 
 	ServletTester tester = new ServletTester();
+	tester.runService(args);
+    }
 
+    public void runService(String[] args) throws Exception{
         // Open the web.xml file to determine the service name and inputs/outputs.
-	java.net.URL webXmlURL = tester.getClass().getClassLoader().getResource("WEB-INF/web.xml");
+	String webXMLPath = alternateWebXML != null ? alternateWebXML : "WEB-INF/web.xml";
+	java.net.URL webXmlURL = getClass().getClassLoader().getResource(webXMLPath);
 	if(webXmlURL == null){
-	    System.err.println("Could not find \"WEB-INF/web.xml\", aborting!");
-	    throw new Exception("No WEB-INF/web.xml in the code archive");
+	    System.err.println("Could not find \""+webXMLPath+"\", aborting!");
+	    throw new Exception("No \""+ webXMLPath+"\" in the resource path");
 	}
 	
         String centralURL = null;
@@ -108,12 +129,29 @@
 		try{
 		    dataIn = (new URL(args[1])).openStream();
 		} catch(Exception e){
-		    e.printStackTrace();
+		    //e.printStackTrace();
 		}
 	    }
+	    // Last ditch, try as a resource
 	    if(dataIn == null){
-		System.err.println("Could not read example data file \"" + args[1] + "\", aborting!");
-		throw new Exception("Example data file was unreadable: " + args[1]);
+		ClassLoader cl = getClass().getClassLoader();
+		if(cl == null){
+		    cl = ClassLoader.getSystemClassLoader();
+		}
+		System.err.println("Trying to load as resource with " + cl);
+		URL resourceURL = null;
+		if(cl != null){
+		    resourceURL = cl.getResource(args[1]);
+		}
+		if(resourceURL == null){
+		    System.err.println("Could not find the example data named \"" + args[1] + "\", aborting!");
+		    throw new Exception("Example data was not found: " + args[1]);
+		}
+		try{
+		    dataIn = resourceURL.openStream();
+		} catch(Exception e){
+		    e.printStackTrace();
+		}
 	    }
 	    
 	    // Create the data and service instances.




More information about the MOBY-guts mailing list