Index: test/edu/ucsb/nceas/metacattest/QuerySpecificationTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/QuerySpecificationTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/QuerySpecificationTest.java	(revision 3567)
@@ -50,6 +50,57 @@
     /** The utilities object for accessing property values */
     private MetaCatUtil util;
     
+    private static String selectionQuery = "SELECT docid,docname,doctype,date_created, date_updated, rev " +
+    		"FROM xml_documents WHERE docid IN (((((SELECT DISTINCT docid FROM xml_nodes " +
+    		"WHERE UPPER(nodedata) LIKE '%JONES%' ) ))))";
+    private static String extendedQuery = "select xml_nodes.docid, 'dataset/title' as path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'title' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' ) )  AND xml_nodes.docid in " +
+    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
+    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, 'originator/individualName/surName' as " +
+    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
+    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'surName' AND parentnodeid IN " +
+    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'individualName' AND parentnodeid IN " +
+    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'originator' ) ) )  AND xml_nodes.docid in " +
+    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = " +
+    		"xml_documents.rootnodeid UNION select xml_nodes.docid, 'keyword' as path, xml_nodes.nodedata, " +
+    		"xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM " +
+    		"xml_nodes WHERE nodename LIKE 'keyword' )  AND xml_nodes.docid in " +
+    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
+    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, '/eml/@packageId' as " +
+    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
+    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE '@packageId' AND parentnodeid IN " +
+    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) )  " +
+    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
+    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, " +
+    		"'/eml/dataset/access/@authSystem' as path, xml_nodes.nodedata, xml_nodes.parentnodeid from " +
+    		"xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
+    		"LIKE '@authSystem' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' " +
+    		"AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND " +
+    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND " +
+    		"parentnodeid = rootnodeid ) ) ) )  AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND " +
+    		"xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = xml_documents.rootnodeid UNION " +
+    		"select xml_nodes.docid, '/eml/dataset/access/@order' as path, xml_nodes.nodedata, xml_nodes.parentnodeid " +
+    		"from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
+    		"LIKE '@order' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' AND " +
+    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND parentnodeid " +
+    		"IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) ) ) )  " +
+    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' " +
+    		"AND xml_nodes.rootnodeid = xml_documents.rootnodeid";
+    
+    /* Initialize Options*/
+    static
+    {
+  	  try
+  	  {
+  		  Options.initialize(new File("build/tests/metacat.properties"));
+  		  MetaCatUtil.pathsForIndexing 
+  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
+  	  }
+  	  catch(Exception e)
+  	  {
+  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
+  	  }
+    }
+    
     /**
      * Constructor to build the test
      * 
@@ -87,7 +138,6 @@
         TestSuite suite = new TestSuite();
         suite.addTest(new QuerySpecificationTest("testPrintSQL"));
         suite.addTest(new QuerySpecificationTest("testPrintExtendedSQL"));
-        suite.addTest(new QuerySpecificationTest("testAttributeSQL"));
         return suite;
     }
 
@@ -101,7 +151,8 @@
             QuerySpecification qspec = new QuerySpecification(xml, 
                     MetaCatUtil.getOption("saxparser"), 
                     MetaCatUtil.getOption("accNumberSeparator"));
-            System.out.println(qspec.printSQL(false));
+            String query = qspec.printSQL(false);
+           assertTrue(selectionQuery.equals(query.trim()));
         } catch (IOException e) {
             fail(e.getMessage());
         }
@@ -117,9 +168,10 @@
             QuerySpecification qspec = new QuerySpecification(xml, 
                     MetaCatUtil.getOption("saxparser"), 
                     MetaCatUtil.getOption("accNumberSeparator"));
-            System.out.println(
+            String query = 
                     qspec.printExtendedSQL(
-                            "'obfs.45337', 'obfs.45338', 'obfs.45346'", false));
+                            "'obfs.45337', 'obfs.45338', 'obfs.45346'", false);
+            assertTrue(extendedQuery.equals(query));
         } catch (IOException e) {
             fail(e.getMessage());
         }
Index: test/edu/ucsb/nceas/metacattest/SubTreeTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/SubTreeTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/SubTreeTest.java	(revision 3567)
@@ -27,6 +27,7 @@
 package edu.ucsb.nceas.metacattest;
 
 import edu.ucsb.nceas.metacat.*;
+import edu.ucsb.nceas.utilities.Options;
 //import edu.ucsb.nceas.morpho.framework.*;
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -47,7 +48,20 @@
 {
   private SubTree tree = null;
   private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
-
+  /* Initialize Options*/
+  static
+  {
+	  try
+	  {
+		  Options.initialize(new File("build/tests/metacat.properties"));
+		  MetaCatUtil.pathsForIndexing 
+		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
+	  }
+	  catch(Exception e)
+	  {
+		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
+	  }
+  }
   /**
    * Constructor to build the test
    *
@@ -155,7 +169,7 @@
         log.debug("Error in SubTreeTest.suite: "+ e.getMessage());
     }//catch
 
-    while (!nodeStack.empty())
+    while (nodeStack != null && !nodeStack.empty())
     {
       NodeRecord node =(NodeRecord)nodeStack.pop();
       String nodeType = node.getNodeType();
@@ -173,6 +187,6 @@
           log.debug("text: " + node.getNodeData());
       }
     }
-    assertTrue(1 == 1);
+   
   }
 }
Index: test/edu/ucsb/nceas/metacattest/QueryResultBuilderTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/QueryResultBuilderTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/QueryResultBuilderTest.java	(revision 3567)
@@ -56,7 +56,7 @@
 	/** Return field value in xml_returnfield table*/
 	private String[] returnFieldString = {"dataset/title", "entityName", "individualName/surName", "keyword"};
 	/** Return field id value for above return field string in xml_returnfield*/
-	private int returnFieldId = 4;
+	private int returnFieldId =11;
 	
 	static
 	  {
@@ -77,7 +77,7 @@
     {
         TestSuite suite = new TestSuite();
         suite.addTest(new QueryResultBuilderTest("initialize"));
-        //suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
+        suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
         return suite;
     }
    
@@ -116,7 +116,7 @@
     
 
     /**
-     * Test how long it will take to build records in xml_queryresult table
+     * Tests how long it will take to build records in xml_queryresult table
      */
     public void testBuildRecords() throws Exception
     {
@@ -123,50 +123,69 @@
         double start = System.currentTimeMillis()/1000;
         DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
         int serialNumber = dbconn.getCheckOutSerialNumber();
-        String sql = "SELECT docid, nodedata from xml_path_index where path like ?";
+        String sql = "SELECT docid, nodedata, path FROM xml_path_index WHERE path IN (";
         PreparedStatement pstmt = null;
         ResultSet result = null;
+        //Gets the document information such as doctype, docname and et al
+        Hashtable docsInformation = getDocumentsInfo();
         //The key of the hash is docid, the element is the return field value (in xml_format)
         Hashtable table = new Hashtable();
         int size = returnFieldString.length;
-        // Loop the return filed value in xml_path_index table and put the docid and value into hashtable
+        // Constructs the sql base on the return fields
+        boolean first = true;
         for (int i=0; i< size; i++)
         {
+        	if (!first)
+        	{
+        		sql=sql+",";
+        	}
+        	sql=sql+"'";
         	String path = returnFieldString[i];
-        	pstmt = dbconn.prepareStatement(sql);
-            pstmt.setString(1,path);
-            pstmt.execute();
-            result = pstmt.getResultSet();
-            boolean hasNext = result.next();
-            // Get path value for one docid
-            while (hasNext)
-            {
-            	String docid = result.getString(1);
-            	String value = result.getString(2);
-            	StringBuffer buffer  = new StringBuffer();
-            	buffer.append("<param name=\"");
-                buffer.append(path);
-                buffer.append("\">");
-                buffer.append(value);
-                buffer.append("</param>");
-                String xmlValue = buffer.toString();
-                //If the hashtable already has key for this docid,
-                //we should append obove value to the old value
-            	if (table.containsKey(docid))
-            	{
-            		String oldValue = (String)table.get(docid);
-            		String newValue = oldValue+xmlValue;
-            		table.put(docid, newValue);
-            	}
-            	else
-            	{
-            		table.put(docid, xmlValue);
-            	}
-            	hasNext = result.next();
-            }
-            result.close();
-            pstmt.close();
+        	 sql = sql+path;
+        	 sql=sql+"'";
+        	 first = false;
         }
+        sql = sql+")";
+        System.out.println("The final sql is "+sql);
+    	pstmt = dbconn.prepareStatement(sql);
+        pstmt.execute();
+        result = pstmt.getResultSet();
+        boolean hasNext = result.next();
+        // Gets returning value for docids and puts them into hashtable
+        while (hasNext)
+        {
+        	String docid = result.getString(1);
+        	String value = result.getString(2);
+        	String path  = result.getString(3);
+        	// Gets document information from document information hash table.
+        	String docInfo = (String)docsInformation.get(docid);
+        	StringBuffer buffer  = new StringBuffer();
+        	if (docInfo != null)
+        	{
+        		buffer.append(docInfo);
+        	}
+        	buffer.append("<param name=\"");
+            buffer.append(path);
+            buffer.append("\">");
+            buffer.append(value);
+            buffer.append("</param>");
+            String xmlValue = buffer.toString();
+            //If the hashtable already has key for this docid,
+            //we should append obove value to the old value
+        	if (table.containsKey(docid))
+        	{
+        		String oldValue = (String)table.get(docid);
+        		String newValue = oldValue+xmlValue;
+        		table.put(docid, newValue);
+        	}
+        	else
+        	{
+        		table.put(docid, xmlValue);
+        	}
+        	hasNext = result.next();
+        }
+        result.close();
+        pstmt.close();
         // Insert the hashtable value into xml_queryresult table
         Enumeration docids = table.keys();
         while (docids.hasMoreElements())
@@ -190,5 +209,65 @@
         DBConnectionPool.returnDBConnection(dbconn, serialNumber);
         assertTrue(1 == 1);
     }
+    
+    /*
+     * Gets a Hashtable which contains documents' information. 
+     * The key of this Hashtable is docid. The element value is the information, like
+     * docid, docname,doctype and et al. This method will be used in testBuildRecords 
+     * method to combine a completed return information for query.
+     */
+    private Hashtable getDocumentsInfo() throws Exception
+    {
+    	DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
+        int serialNumber = dbconn.getCheckOutSerialNumber();
+        String sql = "SELECT docid, rev, docname, doctype, date_created,date_updated from xml_documents";
+        PreparedStatement pstmt = null;
+        ResultSet result = null;
+        //The key of the hash is docid, the element is the document information (in xml_format)
+        Hashtable table = new Hashtable();
+        pstmt = dbconn.prepareStatement(sql);
+        pstmt.execute();
+        result = pstmt.getResultSet();
+        boolean hasNext = result.next();
+        // Get  values from the xml_document table
+        while (hasNext)
+        {
+        	String             docid = result.getString(1);
+        	int                      rev = result.getInt(2);
+        	String        docname = result.getString(3);
+        	String         doctype = result.getString(4);
+        	String   createDate = result.getString(5);
+        	String updateDate = result.getString(6);
+        	String completeDocid = docid
+                             + MetaCatUtil.getOption("accNumSeparator");
+            completeDocid += rev;
+            // Put everything into a string buffer
+            StringBuffer document = new StringBuffer();
+            document.append("<docid>").append(completeDocid).append("</docid>");
+            if (docname != null)
+            {
+                document.append("<docname>" + docname + "</docname>");
+            }
+            if (doctype != null)
+            {
+               document.append("<doctype>" + doctype + "</doctype>");
+            }
+            if (createDate != null)
+            {
+                document.append("<createdate>" + createDate + "</createdate>");
+            }
+            if (updateDate != null)
+            {
+              document.append("<updatedate>" + updateDate + "</updatedate>");
+            }
+            String information = document.toString();
+            // Put the docid and info into Hashtable
+            table.put(docid, information);
+            hasNext = result.next();
+        }
+        result.close();
+        pstmt.close();
+        return table;
+    }
 
 }
Index: test/edu/ucsb/nceas/metacattest/QueryGroupTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/QueryGroupTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/QueryGroupTest.java	(revision 3567)
@@ -46,6 +46,27 @@
 public class QueryGroupTest extends TestCase
 {
 	private QueryGroup group = null;
+	 /* Initialize Options*/
+    static
+    {
+  	  try
+  	  {
+  		  Options.initialize(new File("build/tests/metacat.properties"));
+  		  MetaCatUtil.pathsForIndexing 
+  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
+  	  }
+  	  catch(Exception e)
+  	  {
+  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
+  	  }
+    }
+    private String query = "(SELECT DISTINCT docid FROM xml_path_index WHERE  (UPPER(nodedata) "+
+                                            "LIKE '%LAND%' AND path IN ('dataset/title','geographicCoverage/boundingCoordinates/southBoundingCoordinate')) " +
+                                            "OR ((UPPER(nodedata) LIKE '%JONES%' AND path LIKE 'organizationName') ) OR ((UPPER(nodedata) LIKE '%LAND %' AND path LIKE 'keyword') ) " +
+                                            "OR ((UPPER(nodedata) LIKE '%DATOS%' AND path LIKE 'entityName') ) UNION ((SELECT DISTINCT docid FROM xml_nodes WHERE UPPER(nodedata) " +
+                                            "LIKE '%VALUE1%' AND parentnodeid IN (SELECT nodeid FROM xml_index WHERE path LIKE 'path1') )  UNION " +
+                                            "(SELECT DISTINCT docid FROM xml_nodes WHERE UPPER(nodedata) LIKE '%VALUE2%' AND parentnodeid IN " +
+                                            "(SELECT nodeid FROM xml_index WHERE path LIKE 'path2') ) ))";
     
     /**
      * Constructor to build the test
@@ -107,8 +128,8 @@
     	group.addChild(term4);
     	group.addChild(term5);
     	group.addChild(child);
-    	System.out.println("the query is "+group.printSQL(true));
-    	assertTrue(1 == 1); 
+    	String queryString = group.printSQL(true);
+    	assertTrue(queryString.equals(query)); 
     	
     }
     
Index: test/edu/ucsb/nceas/metacattest/MetaCatServletTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/MetaCatServletTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/MetaCatServletTest.java	(revision 3567)
@@ -28,6 +28,7 @@
 
 import edu.ucsb.nceas.metacat.*;
 import edu.ucsb.nceas.utilities.HttpMessage;
+import edu.ucsb.nceas.utilities.Options;
 //import edu.ucsb.nceas.morpho.framework.*;
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -49,6 +50,18 @@
   private String metacatURL=MetaCatUtil.getOption("junittesturl");
   private String serialNumber;
   private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
+  /* Initialize Options*/
+  static
+  {
+	  try
+	  {
+		  Options.initialize(new File("build/tests/metacat.properties"));
+	  }
+	  catch(Exception e)
+	  {
+		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
+	  }
+  }
   
   /**
    * Constructor to build the test
Index: test/edu/ucsb/nceas/metacattest/BuildIndexTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/BuildIndexTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/BuildIndexTest.java	(revision 3567)
@@ -67,6 +67,20 @@
     private String testdocument = "";
     private Metacat m;
     private MetaCatUtil util;
+    /* Initialize Options*/
+    static
+    {
+  	  try
+  	  {
+  		  Options.initialize(new File("build/tests/metacat.properties"));
+  		  MetaCatUtil.pathsForIndexing 
+  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
+  	  }
+  	  catch(Exception e)
+  	  {
+  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
+  	  }
+    }
 
     /**
      * Constructor to build the test
Index: test/edu/ucsb/nceas/metacattest/InlineDataAccessTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/InlineDataAccessTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/InlineDataAccessTest.java	(revision 3567)
@@ -1319,9 +1319,12 @@
             fail("Metacat Inaccessible:\n" + mie.getMessage());
         }
         catch (InsufficientKarmaException ike) {
-            if (!expextedKarmaFailure) {
-                fail("Insufficient karma:\n" + ike.getMessage());
+        	if (result) {
+                fail("Metacat Error:\n" + ike.getMessage());
             }
+            else {
+                System.out.println("Metacat Error:\n" + ike.getMessage());
+            }
         }
         catch (MetacatException me) {
             if (result) {
Index: test/edu/ucsb/nceas/metacattest/ReplicationServerListTest.java
===================================================================
--- test/edu/ucsb/nceas/metacattest/ReplicationServerListTest.java	(revision 3566)
+++ test/edu/ucsb/nceas/metacattest/ReplicationServerListTest.java	(revision 3567)
@@ -27,6 +27,7 @@
 package edu.ucsb.nceas.metacattest;
 
 import edu.ucsb.nceas.metacat.*;
+import edu.ucsb.nceas.utilities.Options;
 //import edu.ucsb.nceas.morpho.framework.*;
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -45,11 +46,25 @@
  */
 public class ReplicationServerListTest extends TestCase
 {
-  private static String metacatReplicationURL=
+	 /* Initialize Options*/
+	  static
+	  {
+		  try
+		  {
+			  Options.initialize(new File("build/tests/metacat.properties"));
+			  MetaCatUtil.pathsForIndexing 
+			         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
+		  }
+		  catch(Exception e)
+		  {
+			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
+		  }
+	  }
+     private static String metacatReplicationURL=
                                    MetaCatUtil.getOption("junitreplicationurl");
-  private ReplicationServerList serverList = null;
-  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
-  
+     private ReplicationServerList serverList = null;
+     private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
+ 
   /**
    * Constructor to build the test
    *
