Project

General

Profile

« Previous | Next » 

Revision 3567

Added by Jing Tao over 16 years ago

Add code to initialize Options and some code clean up

View differences:

test/edu/ucsb/nceas/metacattest/QuerySpecificationTest.java
50 50
    /** The utilities object for accessing property values */
51 51
    private MetaCatUtil util;
52 52
    
53
    private static String selectionQuery = "SELECT docid,docname,doctype,date_created, date_updated, rev " +
54
    		"FROM xml_documents WHERE docid IN (((((SELECT DISTINCT docid FROM xml_nodes " +
55
    		"WHERE UPPER(nodedata) LIKE '%JONES%' ) ))))";
56
    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 " +
57
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
58
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, 'originator/individualName/surName' as " +
59
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
60
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'surName' AND parentnodeid IN " +
61
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'individualName' AND parentnodeid IN " +
62
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'originator' ) ) )  AND xml_nodes.docid in " +
63
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = " +
64
    		"xml_documents.rootnodeid UNION select xml_nodes.docid, 'keyword' as path, xml_nodes.nodedata, " +
65
    		"xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM " +
66
    		"xml_nodes WHERE nodename LIKE 'keyword' )  AND xml_nodes.docid in " +
67
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
68
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, '/eml/@packageId' as " +
69
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
70
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE '@packageId' AND parentnodeid IN " +
71
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) )  " +
72
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
73
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, " +
74
    		"'/eml/dataset/access/@authSystem' as path, xml_nodes.nodedata, xml_nodes.parentnodeid from " +
75
    		"xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
76
    		"LIKE '@authSystem' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' " +
77
    		"AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND " +
78
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND " +
79
    		"parentnodeid = rootnodeid ) ) ) )  AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND " +
80
    		"xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = xml_documents.rootnodeid UNION " +
81
    		"select xml_nodes.docid, '/eml/dataset/access/@order' as path, xml_nodes.nodedata, xml_nodes.parentnodeid " +
82
    		"from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
83
    		"LIKE '@order' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' AND " +
84
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND parentnodeid " +
85
    		"IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) ) ) )  " +
86
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' " +
87
    		"AND xml_nodes.rootnodeid = xml_documents.rootnodeid";
88
    
89
    /* Initialize Options*/
90
    static
91
    {
92
  	  try
93
  	  {
94
  		  Options.initialize(new File("build/tests/metacat.properties"));
95
  		  MetaCatUtil.pathsForIndexing 
96
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
97
  	  }
98
  	  catch(Exception e)
99
  	  {
100
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
101
  	  }
102
    }
103
    
53 104
    /**
54 105
     * Constructor to build the test
55 106
     * 
......
87 138
        TestSuite suite = new TestSuite();
88 139
        suite.addTest(new QuerySpecificationTest("testPrintSQL"));
89 140
        suite.addTest(new QuerySpecificationTest("testPrintExtendedSQL"));
90
        suite.addTest(new QuerySpecificationTest("testAttributeSQL"));
91 141
        return suite;
92 142
    }
93 143

  
......
101 151
            QuerySpecification qspec = new QuerySpecification(xml, 
102 152
                    MetaCatUtil.getOption("saxparser"), 
103 153
                    MetaCatUtil.getOption("accNumberSeparator"));
104
            System.out.println(qspec.printSQL(false));
154
            String query = qspec.printSQL(false);
155
           assertTrue(selectionQuery.equals(query.trim()));
105 156
        } catch (IOException e) {
106 157
            fail(e.getMessage());
107 158
        }
......
117 168
            QuerySpecification qspec = new QuerySpecification(xml, 
118 169
                    MetaCatUtil.getOption("saxparser"), 
119 170
                    MetaCatUtil.getOption("accNumberSeparator"));
120
            System.out.println(
171
            String query = 
121 172
                    qspec.printExtendedSQL(
122
                            "'obfs.45337', 'obfs.45338', 'obfs.45346'", false));
173
                            "'obfs.45337', 'obfs.45338', 'obfs.45346'", false);
174
            assertTrue(extendedQuery.equals(query));
123 175
        } catch (IOException e) {
124 176
            fail(e.getMessage());
125 177
        }
test/edu/ucsb/nceas/metacattest/SubTreeTest.java
27 27
package edu.ucsb.nceas.metacattest;
28 28

  
29 29
import edu.ucsb.nceas.metacat.*;
30
import edu.ucsb.nceas.utilities.Options;
30 31
//import edu.ucsb.nceas.morpho.framework.*;
31 32
import junit.framework.Test;
32 33
import junit.framework.TestCase;
......
47 48
{
48 49
  private SubTree tree = null;
49 50
  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
50

  
51
  /* Initialize Options*/
52
  static
53
  {
54
	  try
55
	  {
56
		  Options.initialize(new File("build/tests/metacat.properties"));
57
		  MetaCatUtil.pathsForIndexing 
58
		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
59
	  }
60
	  catch(Exception e)
61
	  {
62
		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
63
	  }
64
  }
51 65
  /**
52 66
   * Constructor to build the test
53 67
   *
......
155 169
        log.debug("Error in SubTreeTest.suite: "+ e.getMessage());
156 170
    }//catch
157 171

  
158
    while (!nodeStack.empty())
172
    while (nodeStack != null && !nodeStack.empty())
159 173
    {
160 174
      NodeRecord node =(NodeRecord)nodeStack.pop();
161 175
      String nodeType = node.getNodeType();
......
173 187
          log.debug("text: " + node.getNodeData());
174 188
      }
175 189
    }
176
    assertTrue(1 == 1);
190
   
177 191
  }
178 192
}
test/edu/ucsb/nceas/metacattest/QueryResultBuilderTest.java
56 56
	/** Return field value in xml_returnfield table*/
57 57
	private String[] returnFieldString = {"dataset/title", "entityName", "individualName/surName", "keyword"};
58 58
	/** Return field id value for above return field string in xml_returnfield*/
59
	private int returnFieldId = 4;
59
	private int returnFieldId =11;
60 60
	
61 61
	static
62 62
	  {
......
77 77
    {
78 78
        TestSuite suite = new TestSuite();
79 79
        suite.addTest(new QueryResultBuilderTest("initialize"));
80
        //suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
80
        suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
81 81
        return suite;
82 82
    }
83 83
   
......
116 116
    
117 117

  
118 118
    /**
119
     * Test how long it will take to build records in xml_queryresult table
119
     * Tests how long it will take to build records in xml_queryresult table
120 120
     */
121 121
    public void testBuildRecords() throws Exception
122 122
    {
123 123
        double start = System.currentTimeMillis()/1000;
124 124
        DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
125 125
        int serialNumber = dbconn.getCheckOutSerialNumber();
126
        String sql = "SELECT docid, nodedata from xml_path_index where path like ?";
126
        String sql = "SELECT docid, nodedata, path FROM xml_path_index WHERE path IN (";
127 127
        PreparedStatement pstmt = null;
128 128
        ResultSet result = null;
129
        //Gets the document information such as doctype, docname and et al
130
        Hashtable docsInformation = getDocumentsInfo();
129 131
        //The key of the hash is docid, the element is the return field value (in xml_format)
130 132
        Hashtable table = new Hashtable();
131 133
        int size = returnFieldString.length;
132
        // Loop the return filed value in xml_path_index table and put the docid and value into hashtable
134
        // Constructs the sql base on the return fields
135
        boolean first = true;
133 136
        for (int i=0; i< size; i++)
134 137
        {
138
        	if (!first)
139
        	{
140
        		sql=sql+",";
141
        	}
142
        	sql=sql+"'";
135 143
        	String path = returnFieldString[i];
136
        	pstmt = dbconn.prepareStatement(sql);
137
            pstmt.setString(1,path);
138
            pstmt.execute();
139
            result = pstmt.getResultSet();
140
            boolean hasNext = result.next();
141
            // Get path value for one docid
142
            while (hasNext)
143
            {
144
            	String docid = result.getString(1);
145
            	String value = result.getString(2);
146
            	StringBuffer buffer  = new StringBuffer();
147
            	buffer.append("<param name=\"");
148
                buffer.append(path);
149
                buffer.append("\">");
150
                buffer.append(value);
151
                buffer.append("</param>");
152
                String xmlValue = buffer.toString();
153
                //If the hashtable already has key for this docid,
154
                //we should append obove value to the old value
155
            	if (table.containsKey(docid))
156
            	{
157
            		String oldValue = (String)table.get(docid);
158
            		String newValue = oldValue+xmlValue;
159
            		table.put(docid, newValue);
160
            	}
161
            	else
162
            	{
163
            		table.put(docid, xmlValue);
164
            	}
165
            	hasNext = result.next();
166
            }
167
            result.close();
168
            pstmt.close();
144
        	 sql = sql+path;
145
        	 sql=sql+"'";
146
        	 first = false;
169 147
        }
148
        sql = sql+")";
149
        System.out.println("The final sql is "+sql);
150
    	pstmt = dbconn.prepareStatement(sql);
151
        pstmt.execute();
152
        result = pstmt.getResultSet();
153
        boolean hasNext = result.next();
154
        // Gets returning value for docids and puts them into hashtable
155
        while (hasNext)
156
        {
157
        	String docid = result.getString(1);
158
        	String value = result.getString(2);
159
        	String path  = result.getString(3);
160
        	// Gets document information from document information hash table.
161
        	String docInfo = (String)docsInformation.get(docid);
162
        	StringBuffer buffer  = new StringBuffer();
163
        	if (docInfo != null)
164
        	{
165
        		buffer.append(docInfo);
166
        	}
167
        	buffer.append("<param name=\"");
168
            buffer.append(path);
169
            buffer.append("\">");
170
            buffer.append(value);
171
            buffer.append("</param>");
172
            String xmlValue = buffer.toString();
173
            //If the hashtable already has key for this docid,
174
            //we should append obove value to the old value
175
        	if (table.containsKey(docid))
176
        	{
177
        		String oldValue = (String)table.get(docid);
178
        		String newValue = oldValue+xmlValue;
179
        		table.put(docid, newValue);
180
        	}
181
        	else
182
        	{
183
        		table.put(docid, xmlValue);
184
        	}
185
        	hasNext = result.next();
186
        }
187
        result.close();
188
        pstmt.close();
170 189
        // Insert the hashtable value into xml_queryresult table
171 190
        Enumeration docids = table.keys();
172 191
        while (docids.hasMoreElements())
......
190 209
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
191 210
        assertTrue(1 == 1);
192 211
    }
212
    
213
    /*
214
     * Gets a Hashtable which contains documents' information. 
215
     * The key of this Hashtable is docid. The element value is the information, like
216
     * docid, docname,doctype and et al. This method will be used in testBuildRecords 
217
     * method to combine a completed return information for query.
218
     */
219
    private Hashtable getDocumentsInfo() throws Exception
220
    {
221
    	DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
222
        int serialNumber = dbconn.getCheckOutSerialNumber();
223
        String sql = "SELECT docid, rev, docname, doctype, date_created,date_updated from xml_documents";
224
        PreparedStatement pstmt = null;
225
        ResultSet result = null;
226
        //The key of the hash is docid, the element is the document information (in xml_format)
227
        Hashtable table = new Hashtable();
228
        pstmt = dbconn.prepareStatement(sql);
229
        pstmt.execute();
230
        result = pstmt.getResultSet();
231
        boolean hasNext = result.next();
232
        // Get  values from the xml_document table
233
        while (hasNext)
234
        {
235
        	String             docid = result.getString(1);
236
        	int                      rev = result.getInt(2);
237
        	String        docname = result.getString(3);
238
        	String         doctype = result.getString(4);
239
        	String   createDate = result.getString(5);
240
        	String updateDate = result.getString(6);
241
        	String completeDocid = docid
242
                             + MetaCatUtil.getOption("accNumSeparator");
243
            completeDocid += rev;
244
            // Put everything into a string buffer
245
            StringBuffer document = new StringBuffer();
246
            document.append("<docid>").append(completeDocid).append("</docid>");
247
            if (docname != null)
248
            {
249
                document.append("<docname>" + docname + "</docname>");
250
            }
251
            if (doctype != null)
252
            {
253
               document.append("<doctype>" + doctype + "</doctype>");
254
            }
255
            if (createDate != null)
256
            {
257
                document.append("<createdate>" + createDate + "</createdate>");
258
            }
259
            if (updateDate != null)
260
            {
261
              document.append("<updatedate>" + updateDate + "</updatedate>");
262
            }
263
            String information = document.toString();
264
            // Put the docid and info into Hashtable
265
            table.put(docid, information);
266
            hasNext = result.next();
267
        }
268
        result.close();
269
        pstmt.close();
270
        return table;
271
    }
193 272

  
194 273
}
test/edu/ucsb/nceas/metacattest/QueryGroupTest.java
46 46
public class QueryGroupTest extends TestCase
47 47
{
48 48
	private QueryGroup group = null;
49
	 /* Initialize Options*/
50
    static
51
    {
52
  	  try
53
  	  {
54
  		  Options.initialize(new File("build/tests/metacat.properties"));
55
  		  MetaCatUtil.pathsForIndexing 
56
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
57
  	  }
58
  	  catch(Exception e)
59
  	  {
60
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
61
  	  }
62
    }
63
    private String query = "(SELECT DISTINCT docid FROM xml_path_index WHERE  (UPPER(nodedata) "+
64
                                            "LIKE '%LAND%' AND path IN ('dataset/title','geographicCoverage/boundingCoordinates/southBoundingCoordinate')) " +
65
                                            "OR ((UPPER(nodedata) LIKE '%JONES%' AND path LIKE 'organizationName') ) OR ((UPPER(nodedata) LIKE '%LAND %' AND path LIKE 'keyword') ) " +
66
                                            "OR ((UPPER(nodedata) LIKE '%DATOS%' AND path LIKE 'entityName') ) UNION ((SELECT DISTINCT docid FROM xml_nodes WHERE UPPER(nodedata) " +
67
                                            "LIKE '%VALUE1%' AND parentnodeid IN (SELECT nodeid FROM xml_index WHERE path LIKE 'path1') )  UNION " +
68
                                            "(SELECT DISTINCT docid FROM xml_nodes WHERE UPPER(nodedata) LIKE '%VALUE2%' AND parentnodeid IN " +
69
                                            "(SELECT nodeid FROM xml_index WHERE path LIKE 'path2') ) ))";
49 70
    
50 71
    /**
51 72
     * Constructor to build the test
......
107 128
    	group.addChild(term4);
108 129
    	group.addChild(term5);
109 130
    	group.addChild(child);
110
    	System.out.println("the query is "+group.printSQL(true));
111
    	assertTrue(1 == 1); 
131
    	String queryString = group.printSQL(true);
132
    	assertTrue(queryString.equals(query)); 
112 133
    	
113 134
    }
114 135
    
test/edu/ucsb/nceas/metacattest/MetaCatServletTest.java
28 28

  
29 29
import edu.ucsb.nceas.metacat.*;
30 30
import edu.ucsb.nceas.utilities.HttpMessage;
31
import edu.ucsb.nceas.utilities.Options;
31 32
//import edu.ucsb.nceas.morpho.framework.*;
32 33
import junit.framework.Test;
33 34
import junit.framework.TestCase;
......
49 50
  private String metacatURL=MetaCatUtil.getOption("junittesturl");
50 51
  private String serialNumber;
51 52
  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
53
  /* Initialize Options*/
54
  static
55
  {
56
	  try
57
	  {
58
		  Options.initialize(new File("build/tests/metacat.properties"));
59
	  }
60
	  catch(Exception e)
61
	  {
62
		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
63
	  }
64
  }
52 65
  
53 66
  /**
54 67
   * Constructor to build the test
test/edu/ucsb/nceas/metacattest/BuildIndexTest.java
67 67
    private String testdocument = "";
68 68
    private Metacat m;
69 69
    private MetaCatUtil util;
70
    /* Initialize Options*/
71
    static
72
    {
73
  	  try
74
  	  {
75
  		  Options.initialize(new File("build/tests/metacat.properties"));
76
  		  MetaCatUtil.pathsForIndexing 
77
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
78
  	  }
79
  	  catch(Exception e)
80
  	  {
81
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
82
  	  }
83
    }
70 84

  
71 85
    /**
72 86
     * Constructor to build the test
test/edu/ucsb/nceas/metacattest/InlineDataAccessTest.java
1319 1319
            fail("Metacat Inaccessible:\n" + mie.getMessage());
1320 1320
        }
1321 1321
        catch (InsufficientKarmaException ike) {
1322
            if (!expextedKarmaFailure) {
1323
                fail("Insufficient karma:\n" + ike.getMessage());
1322
        	if (result) {
1323
                fail("Metacat Error:\n" + ike.getMessage());
1324 1324
            }
1325
            else {
1326
                System.out.println("Metacat Error:\n" + ike.getMessage());
1327
            }
1325 1328
        }
1326 1329
        catch (MetacatException me) {
1327 1330
            if (result) {
test/edu/ucsb/nceas/metacattest/ReplicationServerListTest.java
27 27
package edu.ucsb.nceas.metacattest;
28 28

  
29 29
import edu.ucsb.nceas.metacat.*;
30
import edu.ucsb.nceas.utilities.Options;
30 31
//import edu.ucsb.nceas.morpho.framework.*;
31 32
import junit.framework.Test;
32 33
import junit.framework.TestCase;
......
45 46
 */
46 47
public class ReplicationServerListTest extends TestCase
47 48
{
48
  private static String metacatReplicationURL=
49
	 /* Initialize Options*/
50
	  static
51
	  {
52
		  try
53
		  {
54
			  Options.initialize(new File("build/tests/metacat.properties"));
55
			  MetaCatUtil.pathsForIndexing 
56
			         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
57
		  }
58
		  catch(Exception e)
59
		  {
60
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
61
		  }
62
	  }
63
     private static String metacatReplicationURL=
49 64
                                   MetaCatUtil.getOption("junitreplicationurl");
50
  private ReplicationServerList serverList = null;
51
  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
52
  
65
     private ReplicationServerList serverList = null;
66
     private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.ReplicationServerListTest");
67
 
53 68
  /**
54 69
   * Constructor to build the test
55 70
   *

Also available in: Unified diff