Index: junittests/edu/ucsb/nceas/metacatjunittests/MetaCatServletTest.java
===================================================================
--- junittests/edu/ucsb/nceas/metacatjunittests/MetaCatServletTest.java	(revision 1081)
+++ junittests/edu/ucsb/nceas/metacatjunittests/MetaCatServletTest.java	(revision 1082)
@@ -44,8 +44,8 @@
  */
 public class MetaCatServletTest extends TestCase
 {
-  private String metacatURL=
-                    "http://dev.nceas.ucsb.edu/tao/servlet/metacat";
+  private String metacatURL=MetaCatUtil.getOption("junittesturl");
+  private String serialNumber;
   /**
    * Constructor to build the test
    *
@@ -54,15 +54,26 @@
   public MetaCatServletTest(String name)
   {
     super(name);
+    System.out.println("metacatURL: "+metacatURL);
   }
 
+ /**
+   * Constructor to build the test
+   *
+   * @param name the name of the test method
+   */
+  public MetaCatServletTest(String name, String serial)
+  {
+    super(name);
+    serialNumber=serial;
+  }
   /**
    * Establish a testing framework by initializing appropriate objects
    */
   public void setUp()
-  {
+ {
     
-  }
+ }
 
   /**
    * Release any objects after tests are complete
@@ -76,14 +87,39 @@
    */
   public static Test suite()
   {
+  
+    
+    
     TestSuite suite = new TestSuite();
     suite.addTest(new MetaCatServletTest("initialize"));
-    suite.addTest(new MetaCatServletTest("testNCEASLogin"));
-    suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
     suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
     suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
     suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
     suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
+    suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
+    //Should put a login successfully at the end of login test
+    //So insert or update can have cookie.
+    suite.addTest(new MetaCatServletTest("testNCEASLogin"));
+    
+    //create random number for docid, so it can void repeat
+    double number = Math.random()*10000;
+    String serial = Integer.toString(((new Double(number)).intValue()));
+    MetaCatUtil.debugMessage("serial: "+serial, 1);
+    suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
+    suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", 
+                                                        serial));
+        
+    suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", 
+                                                        serial));
+    suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", 
+                                                        serial));
+    suite.addTest(new MetaCatServletTest("testUpdateXMLDocument",serial));
+    suite.addTest(new MetaCatServletTest("testDeleteXMLDocument",serial));
+    
+    suite.addTest(new MetaCatServletTest("testLogOut"));
+                                                        
+                         
+                              
     return suite;
   }
   
@@ -99,7 +135,7 @@
   }
 
   /**
-   * Test if the getProtocol() function works given a url
+   * Test the login to neceas succesfully
    */
   public void testNCEASLogin()
   {
@@ -109,21 +145,31 @@
     //assert( withProtocol.getProtocol().equals("http"));
   }
   
+  /**
+   * Test the login to neceas failed
+   */
   public void testNCEASLoginFail()
   {
     String user="uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
     String passwd="12345678";
     assert(!logIn(user,passwd));
-    //assert( withProtocol.getProtocol().equals("http"));
+    
   }
-   public void testLterReferralLogin()
+  
+  /**
+   * Test the login to lter succesfully
+   */
+  public void testLterReferralLogin()
   {
     String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
     String passwd="qVyGpVeb";
     assert(logIn(user,passwd));
-    //assert( withProtocol.getProtocol().equals("http"));
+    
   }
   
+  /**
+   * Test the login to lter failed
+   */
   public void testLterReferralLoginFail()
   {
     String user="uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
@@ -132,6 +178,9 @@
     //assert( withProtocol.getProtocol().equals("http"));
   }
   
+ /**
+   * Test the login to pisco succesfully
+   */
    public void testPiscoReferralLogin()
   {
     String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
@@ -140,6 +189,9 @@
     //assert( withProtocol.getProtocol().equals("http"));
   }
   
+ /**
+   * Test the login to pisco failed
+   */
   public void testPiscoReferralLoginFail()
   {
     String user="uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
@@ -148,6 +200,126 @@
     //assert( withProtocol.getProtocol().equals("http"));
   }
   
+ /**
+   * Test insert a xml document successfully
+   */
+  public void testInsertXMLDocument()
+  {
+    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
+                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
+    MetaCatUtil.debugMessage("docid: "+name, 20);
+    String content="<?xml version=\"1.0\"?>"
+                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
+                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
+                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
+                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
+                   +"<identifier>"+name+"</identifier>"
+                   +"<allow>"
+             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
+                   +"<permission>all</permission>"
+                   +"</allow>"
+                   +"<allow>"
+                   +"<principal>public</principal>"
+                   +"<permission>read</permission>"
+                   +"</allow>"
+                   +"</acl>";
+     MetaCatUtil.debugMessage("xml document: "+content, 55);
+     assert(handleXMLDocument(content, name, "insert"));
+   
+  }
+  
+
+
+  /**
+   * Test read a xml document  in xml format successfully
+   */
+  public void testReadXMLDocumentXMLFormat()
+  {
+    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
+                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
+    assert(handleReadAction(name, "xml"));
+   
+  } 
+
+  /**
+   * Test read a xml document  in html format successfully
+   */
+  public void testReadXMLDocumentHTMLFormat()
+  {
+    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
+                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
+    assert(handleReadAction(name, "html"));
+   
+  }
+
+ 
+  /**
+   * Test read a xml document  in zip format successfully
+   */
+  public void testReadXMLDocumentZipFormat()
+  {
+    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
+                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"1";
+    assert(handleReadAction(name, "zip"));
+   
+  }
+  
+  /**
+   * Test insert a xml document successfully
+   */
+  public void testUpdateXMLDocument()
+  {
+    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
+                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
+    MetaCatUtil.debugMessage("docid: "+name, 20);
+    String content="<?xml version=\"1.0\"?>"
+                   +"<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
+                   +"eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
+                   +"edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
+                   +"<acl authSystem=\"knb\" order=\"allowFirst\">"
+                   +"<identifier>"+name+"</identifier>"
+                   +"<allow>"
+             +"<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
+                   +"<permission>all</permission>"
+                   +"</allow>"
+                   +"<allow>"
+                   +"<principal>public</principal>"
+                   +"<permission>read</permission>"
+                   +"</allow>"
+                   +"</acl>";
+     MetaCatUtil.debugMessage("xml document: "+content, 55);
+     assert(handleXMLDocument(content, name, "update"));
+   
+  }
+  
+  /**
+   * Test delete a xml document successfully
+   */
+  public void testDeleteXMLDocument()
+  {
+    String name = "john"+MetaCatUtil.getOption("accNumSeparator")
+                     +serialNumber+MetaCatUtil.getOption("accNumSeparator")+"2";
+    assert(handleDeleteFile(name));
+   
+  }
+       
+  /**
+   * Test logout action
+   */
+  public void testLogOut()
+  {
+    
+    assert(handleLogOut());
+   
+  }
+  
+ /**
+   * Method to hanld login action
+   *
+   * @param usrerName, the DN name of the test method
+   * @param passWord, the passwd of the user
+   */
+  
   public boolean logIn(String userName, String passWord)
   {
     Properties prop = new Properties();
@@ -169,6 +341,132 @@
     return connected;
   }
   
+  /**
+   * Method to hanld logout action
+   *
+   * @param usrerName, the DN name of the test method
+   * @param passWord, the passwd of the user
+   */
+  
+  public boolean handleLogOut()
+  {
+    boolean disConnected =false;
+    Properties prop = new Properties();
+    prop.put("action", "logout");
+    prop.put("qformat", "xml");
+  
+    String response = getMetacatString(prop);
+    HttpMessage.setCookie(null);
+     
+    if (response.indexOf("<logout>") != -1) 
+    {
+      disConnected = true;
+    } 
+    else 
+    {
+      disConnected = false;
+    }
+
+    return disConnected;
+  }
+  
+  /**
+   * Method to hanld read both xml and data file
+   *
+   * @param docid, the docid of the document want to read
+   * @param qformat, the format of document user want to get
+   */
+  public boolean handleReadAction(String docid, String qformat)
+  {
+    Properties prop = new Properties();
+    String message ="";
+    prop.put("action", "read");
+    prop.put("qformat", qformat);
+    prop.put("docid", docid);
+    
+    message = getMetacatString(prop);
+    message = message.trim();
+    if (message==null || message.equals("")||message.indexOf("<error>") != -1 )
+    {//there was an error
+      
+      return false;
+    }
+    else
+    {//successfully
+      return true;
+    } 
+    
+  }
+  
+  
+  /**
+   * Method to hanld inset or update xml document
+   *
+   * @param xmlDocument, the content of xml qformat
+   * @param docid, the docid of the document
+   * @param action, insert or update
+   */
+  public boolean handleXMLDocument(String xmlDocument, String docid, 
+                                                              String action)
+ 
+  { //-attempt to write file to metacat
+    String access = "no";
+    StringBuffer fileText = new StringBuffer();
+    StringBuffer messageBuf = new StringBuffer();
+    String accessFileId = null;
+    Properties prop = new Properties();
+    prop.put("action", action);
+    prop.put("public", access);  //This is the old way of controlling access
+    prop.put("doctext", xmlDocument);
+    prop.put("docid", docid);
+    
+    String message = getMetacatString(prop);
+    
+    if(message.indexOf("<error>") != -1)
+    {//there was an error
+      
+      return false;
+    }
+    else if(message.indexOf("<success>") != -1)
+    {//the operation worked
+     //write the file to the cache and return the file object
+     return true;
+       
+    }
+    else
+    {//something weird happened.
+      return false;
+    } 
+   
+   
+  }
+  
+   public boolean handleDeleteFile(String name)
+  {
+    
+    Properties prop = new Properties();
+    prop.put("action", "delete");
+    prop.put("docid", name);
+ 
+    String message = getMetacatString(prop);
+    System.out.println("Message: "+message);
+    if(message.indexOf("<error>") != -1)
+    {//there was an error
+      
+      return false;
+    }
+    else if(message.indexOf("<success>") != -1)
+    {//the operation worked
+     //write the file to the cache and return the file object
+     return true;
+       
+    }
+    else
+    {//something weird happened.
+      return false;
+    } 
+  }
+  
    public String getMetacatString(Properties prop)
   {
     String response = null;
@@ -191,7 +489,7 @@
     }
     catch(Exception e)
     {
-      
+      return null;
     }
   
     return response;
