[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Mon Mar 12 16:57:30 UTC 2007


gordonp
Mon Mar 12 12:57:30 EDT 2007
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test
In directory dev.open-bio.org:/tmp/cvs-serv4195/src/main/org/biomoby/service/test

Modified Files:
	MobyServletTestCase.java ServletTester.java 
	TestServletConfig.java 
Log Message:
Updates to base service for better extensibility
moby-live/Java/src/main/org/biomoby/service/test MobyServletTestCase.java,1.2,1.3 ServletTester.java,1.1,1.2 TestServletConfig.java,1.1,1.2
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/MobyServletTestCase.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/MobyServletTestCase.java	2006/12/07 16:37:36	1.2
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/MobyServletTestCase.java	2007/03/12 16:57:30	1.3
@@ -22,6 +22,7 @@
 public class MobyServletTestCase extends TestCase{
     private final static String TEST_MOBY_XML = "org/biomoby/service/test/mobyAASeq.xml";
     private final static String SERVLET_TEST_URL = "http://moby.ucalgary.ca:8089/MobyServlet";
+    private final static String TEST_MAIN_CLASS_NAME = "org.biomoby.service.test.ConvertAAtoFASTA_AA";
     /**
      * @param name Test case name.
      */
@@ -29,18 +30,6 @@
         super(name);
     }
 
-    /**
-     * Sets up the test fixture.
-     * Called before every test case method.
-     */
-    protected void setUp() throws Exception{
-	super.setUp();
-    }
-
-    protected void tearDown() throws Exception{
-        super.tearDown();
-    }
-
     protected URL getExampleURL(){
 	URL testDataURL = getClass().getClassLoader().getResource(TEST_MOBY_XML);
 	if(testDataURL == null){
@@ -50,16 +39,54 @@
 	return testDataURL;
     }
 
+    /**
+     * Override this method to run the main method of more specific servlets
+     * you want to test (e.g. ACDServlet overrides this and specifies itself
+     * as the class to test).
+     */
+    protected String getExampleClass(){
+	return TEST_MAIN_CLASS_NAME;
+    }
+
+    /**
+     * Override this method to run the main method of more specific servlets
+     * you want to test (e.g. ACDServlet overrides this and specifies itself
+     * as the class to test).
+     */
+    protected String getExampleServlet(){
+	return SERVLET_TEST_URL;
+    }
+
     public void testMain() throws Exception{
 	// Should execute main
 	URL testDataURL = getExampleURL();
+	String className = getExampleClass();
+
+	// Use reflection to load the class whose main method should be called
+	// TODO: Maybe we should check that it inherits from MobyServlet first?
+	Class targetClass = null;
+	try {
+	    targetClass = Class.forName(className);
+	} catch(Exception e){
+	    e.printStackTrace();
+	    fail("Could not find class load " + className + ": " + e);
+	}
+
+	java.lang.reflect.Method mainMethod = null;
+	try{
+	    mainMethod = targetClass.getMethod("main", new Class[] {String[].class});
+	} catch(Exception e){
+	    e.printStackTrace();
+	    fail("Could not find main method in class " + className + ": " + e);
+	}
 
 	MobyServlet.setMainTerminationExit(false);
+	String[] args = new String[]{className, testDataURL.toString()};
 	try{
-	    ConvertAAtoFASTA_AA.main(new String[]{"org.biomoby.service.test.ConvertAAtoFASTA_AA",testDataURL.toString()});
+	    mainMethod.invoke(null, new Object[]{args});
 	} catch(Exception e){
 	    e.printStackTrace();
-	    fail("Main method for org.biomoby.service.test.ConvertAAtoFASTA_AA had an exception");
+	    fail("Main method for " + className + " had an exception: " +e);
 	}
 	
 	// Should execute subclass main
@@ -210,7 +237,7 @@
 
     public void testServletTester(){
 	try{
-	    ServletTester.main(new String[]{SERVLET_TEST_URL, "ignored"});
+	    ServletTester.main(new String[]{getExampleServlet(), getExampleURL().toString()});
 	} catch(Exception e){
 	    e.printStackTrace();
 	    fail("Could not run servlet tester successfully: " + e);

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/ServletTester.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/ServletTester.java	2006/12/06 16:07:10	1.1
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/ServletTester.java	2007/03/12 16:57:30	1.2
@@ -1,7 +1,9 @@
 
 /**	
+ * This class is used by the MobyServlet framework
+ * to test a deployed service, and register it with MOBY Central.
  * This is the simplest Java MOBY client you can have.  A fully documented version
- * is available in the TestRequest class, with all the proper error checking. 
+ * of a client is available in the TestRequest class, with all the proper error checking.
  */
 
 package org.biomoby.service.test;
@@ -14,8 +16,9 @@
 import org.w3c.dom.*;
 
 import javax.xml.parsers.*;
+import java.net.URL;
 import java.util.*;
-import java.io.FileInputStream;
+import java.io.*;
 
 public class ServletTester{
     public static void main(String[] args) throws Exception{
@@ -46,49 +49,17 @@
         String testServiceClassName = null;
 	String inParams = null;
 	String outParams = null;
-	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
-	dbf.setNamespaceAware(false);	
-	DocumentBuilder docBuilder = dbf.newDocumentBuilder();
-
-	Element docRoot = null;
-        try{
-	    docRoot = docBuilder.parse(webXmlURL.openStream()).getDocumentElement();
-	    if(docRoot == null || !docRoot.getNodeName().equals("web-app")){
-		System.err.println("Could not find /web-app tag in " + 
-				   webXmlURL  + ", aborting!");
-		throw new Exception("Invalid web.xml, no web-app tag");
-	    }
-	} catch(Exception e){
-	    System.err.println("There appears to be a problem with the \"WEB-INF/web.xml\" file (XML syntax?)." +
-			       "While parsing it I encountered the error: " + e);
-	    e.printStackTrace();
-	    throw new Exception("Unparseable web.xml");
-	}
-	Element servletNode = getChildElement(docRoot, "servlet");
-	if(servletNode == null){
-	    System.err.println("Could not find /web-app/servlet/ tag in " + 
-			       webXmlURL  + ", aborting!");
-	    throw new Exception("Invalid web.xml, no servlet tag");
-	}
-	Element servletName = getChildElement(servletNode, "servlet-name");
-	if(servletName == null){
-	    System.err.println("Could not find /web-app/servlet/servlet-name tag in " + 
-			       webXmlURL  + ", aborting!");
-	    throw new Exception("Invalid web.xml, no servlet-name tag");
-	}
-	Element servletClassName = getChildElement(servletNode, "servlet-class");
-	if(servletClassName == null){
-	    System.err.println("Could not find /web-app/servlet/servlet-class tag in " + 
-			       webXmlURL  + ", aborting!");
-	    throw new Exception("Invalid web.xml, no servlet-class tag");
-	}
 
+	// Parse the web.xml config to set the servlet environment	
+	TestServletConfig servletConfig = new TestServletConfig(new TestServletContext(), 
+								webXmlURL);
         MobyServlet servlet = null;
         try{
 	    // This line can throw many different exception if you didn't get the class right! 
-	    Class servletClass = MobyServlet.class.getClassLoader().loadClass(servletClassName.getTextContent());
+	    Class servletClass = MobyServlet.class.getClassLoader().loadClass(servletConfig.getServletClassName());
             if(servletClass == null){
-                throw new ClassNotFoundException("The servlet Class to run (" + servletClassName.getTextContent() + 
+                throw new ClassNotFoundException("The servlet Class to run (" + 
+						 servletConfig.getServletClassName() + 
 						 ") was not found, please ensure the web.xml is up-to-date.");
             }
 	    servlet = (MobyServlet) servletClass.newInstance();
@@ -98,16 +69,16 @@
 	    throw new Exception("Invalid web.xml, the servlet-class tag was not useable");
         }
 
-	// TODO: should properly parse the web.xml config
-	servlet.init(new TestServletConfig(servletName.getTextContent(), new TestServletContext()));
+	servlet.init(servletConfig);
 	TestHttpServletRequest request = new TestHttpServletRequest();
         request.setRequestURL(args[0]);
 
         MobyService testService = servlet.createServiceFromConfig(request);
-	// When we GET this URL, the RDF is returned
+	// When we GET this URL, the RDF is returned, populated with all the pertinent data for registration
 	if(args.length != 3 || !("register_permanent".equals(args[2]))){
 	    testService.setSignatureURL(null); //signature not set == MOBY agent, please delete this service if requested!
 	}
+	// Otherwise we keep the signature URL that servlet.createServiceFromConfig() generated
 
 	// Setup communication with MOBY Central
         Central worker = null;
@@ -127,11 +98,18 @@
 	MobyContentInstance testData = null;
 	MobyData[] inputTypes = testService.getPrimaryInputs();
 	if(inputTypes.length > 0){
-	    FileInputStream dataIn = null;
+	    InputStream dataIn = null;
 	    try{
 		dataIn = new FileInputStream(args[1]);
 	    } catch(Exception e){
-		e.printStackTrace();
+		System.err.println("The specified resource " + args[1] + " is not a file, trying as a URL");
+	    }
+	    if(dataIn == null){
+		try{
+		    dataIn = (new URL(args[1])).openStream();
+		} catch(Exception e){
+		    e.printStackTrace();
+		}
 	    }
 	    if(dataIn == null){
 		System.err.println("Could not read example data file \"" + args[1] + "\", aborting!");
@@ -203,24 +181,4 @@
 	// TO DO: check RDF discrepancies?
 	System.err.println("Service Successfully Unregistered!");
     }
-    
-    public static Element getChildElement(Element parent, String elementName){
-	if(elementName == null || parent == null){
-	    return null;
-	}
-
-	NodeList children = parent.getChildNodes();
-	for(int i = 0; children != null && i < children.getLength(); i++){
-	    // Make sure it's an element, not a processing instruction, text, etc.
-	    if(!(children.item(i) instanceof Element)){
-		continue;
-	    }
-	    Element child = (Element) children.item(i);
-	    // Make sure it has the right name, or wildcard
-	    if(elementName.equals(child.getTagName())){
-		return child;
-	    }
-	}
-	return null;	
-    }
 }

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/TestServletConfig.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/TestServletConfig.java	2006/12/06 16:07:10	1.1
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/service/test/TestServletConfig.java	2007/03/12 16:57:30	1.2
@@ -8,6 +8,10 @@
 
 package org.biomoby.service.test;
 
+import org.biomoby.shared.MobyPrefixResolver;
+import org.w3c.dom.*;
+
+import javax.xml.parsers.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 import java.util.*;
@@ -15,6 +19,7 @@
 
 public class TestServletConfig implements ServletConfig{
     private String name = null;
+    private String className = null;
     private Hashtable initParams = new Hashtable();
     private ServletContext context = null;
 
@@ -23,6 +28,66 @@
 	context = cont;
     }
 
+    public TestServletConfig(ServletContext cont, java.net.URL u) throws Exception{
+	context = cont;
+
+	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+	dbf.setNamespaceAware(true);	
+	DocumentBuilder docBuilder = dbf.newDocumentBuilder();
+
+	Element docRoot = null;
+        try{
+	    docRoot = docBuilder.parse(u.openStream()).getDocumentElement();
+	    if(docRoot == null || !docRoot.getNodeName().equals("web-app")){
+		throw new Exception("Invalid web.xml (" + u + "), no root web-app tag");
+	    }
+	} catch(Exception e){
+	    throw new Exception("There appears to be a problem with the \"WEB-INF/web.xml\" file (" + 
+				u + ", XML syntax?).\n" +
+			       "While parsing it I encountered the error: " + e);
+	}
+
+	Element servletNode = MobyPrefixResolver.getChildElement(docRoot, "servlet");
+	if(servletNode == null){
+	    System.err.println("Could not find /web-app/servlet/ tag in " + 
+			       u  + ", aborting!");
+	    throw new Exception("Invalid web.xml (" + u +"), no servlet tag");
+	}
+	Element servletName = MobyPrefixResolver.getChildElement(servletNode, "servlet-name");
+	if(servletName == null){
+	    System.err.println("Could not find /web-app/servlet/servlet-name tag in " + 
+			       u  + ", aborting!");
+	    throw new Exception("Invalid web.xml (" + u +"), no servlet-name tag");
+	}
+	name = servletName.getTextContent();
+	if(name.length() == 0){
+	    throw new Exception("Invalid web.xml (" + u +"), servlet-name tag is blank (cannot have nameless servlet)");
+	}
+
+	Element servletClassName = MobyPrefixResolver.getChildElement(servletNode, "servlet-class");
+	if(servletClassName == null){
+	    System.err.println("Could not find /web-app/servlet/servlet-class tag in " + 
+			       u  + ", aborting!");
+	    throw new Exception("Invalid web.xml (" + u + "), no servlet-class tag");
+	}
+	className = servletClassName.getTextContent();
+	if(className.length() == 0){
+	    throw new Exception("Invalid web.xml (" + u +"), servlet-class tag is blank (cannot have nameless class)");
+	}
+
+	NodeList paramNodes = docRoot.getElementsByTagName("context-param");
+	for(int i = 0; i < paramNodes.getLength(); i++){
+	    Element paramNameTag = MobyPrefixResolver.getChildElement((Element) paramNodes.item(i), "param-name");
+	    Element paramValueTag = MobyPrefixResolver.getChildElement((Element) paramNodes.item(i), "param-value");
+	    if(paramNameTag == null || paramValueTag == null){
+		System.err.println("Skipping context parameter #" + (i+1) + 
+				   " because of undefined param-name or param-value tag");
+		continue;
+	    }
+	    addInitParameter(paramNameTag.getTextContent(), paramValueTag.getTextContent());
+	}
+    }
+
     public String getInitParameter(String name){
 	return (String) initParams.get(name);
     }
@@ -42,4 +107,9 @@
     public String getServletName(){
 	return name;
     }
+
+    public String getServletClassName(){
+	return className;
+    }
+
 }




More information about the MOBY-guts mailing list