1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2004 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: tao $'
|
7
|
* '$Date: 2007-11-03 22:16:17 -0700 (Sat, 03 Nov 2007) $'
|
8
|
* '$Revision: 3567 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
package edu.ucsb.nceas.metacattest;
|
25
|
|
26
|
import java.io.File;
|
27
|
import java.io.FileNotFoundException;
|
28
|
import java.io.IOException;
|
29
|
import java.sql.PreparedStatement;
|
30
|
import java.sql.ResultSet;
|
31
|
import java.util.Enumeration;
|
32
|
import java.util.Hashtable;
|
33
|
import java.util.Iterator;
|
34
|
|
35
|
import edu.ucsb.nceas.metacat.DBConnection;
|
36
|
import edu.ucsb.nceas.metacat.DBConnectionPool;
|
37
|
import edu.ucsb.nceas.metacat.MetaCatUtil;
|
38
|
import edu.ucsb.nceas.metacat.Version;
|
39
|
import edu.ucsb.nceas.utilities.Options;
|
40
|
|
41
|
import junit.framework.Test;
|
42
|
import junit.framework.TestCase;
|
43
|
import junit.framework.TestSuite;
|
44
|
|
45
|
|
46
|
/**
|
47
|
* @author jones
|
48
|
*
|
49
|
* Test class for getting idea how long it will take - to build
|
50
|
* records xml_queryresult table for a given return fields
|
51
|
*/
|
52
|
public class QueryResultBuilderTest extends TestCase
|
53
|
{
|
54
|
/** DBConnection pool*/
|
55
|
private DBConnectionPool connPool = null;
|
56
|
/** Return field value in xml_returnfield table*/
|
57
|
private String[] returnFieldString = {"dataset/title", "entityName", "individualName/surName", "keyword"};
|
58
|
/** Return field id value for above return field string in xml_returnfield*/
|
59
|
private int returnFieldId =11;
|
60
|
|
61
|
static
|
62
|
{
|
63
|
try
|
64
|
{
|
65
|
Options.initialize(new File("build/tests/metacat.properties"));
|
66
|
}
|
67
|
catch(Exception e)
|
68
|
{
|
69
|
System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
|
70
|
}
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* Create a suite of tests to be run together
|
75
|
*/
|
76
|
public static Test suite()
|
77
|
{
|
78
|
TestSuite suite = new TestSuite();
|
79
|
suite.addTest(new QueryResultBuilderTest("initialize"));
|
80
|
suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
|
81
|
return suite;
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Constructor to build the test
|
86
|
*
|
87
|
* @param name the name of the test method
|
88
|
*/
|
89
|
public QueryResultBuilderTest(String name)
|
90
|
{
|
91
|
super(name);
|
92
|
}
|
93
|
|
94
|
/*
|
95
|
* @see TestCase#setUp()
|
96
|
*/
|
97
|
protected void setUp() throws Exception
|
98
|
{
|
99
|
super.setUp();
|
100
|
try {
|
101
|
connPool = DBConnectionPool.getInstance();
|
102
|
} catch (Exception e) {
|
103
|
fail(e.getMessage());
|
104
|
}
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Run an initial test that always passes to check that the test
|
109
|
* harness is working.
|
110
|
*/
|
111
|
public void initialize()
|
112
|
{
|
113
|
assertTrue(1 == 1);
|
114
|
}
|
115
|
|
116
|
|
117
|
|
118
|
/**
|
119
|
* Tests how long it will take to build records in xml_queryresult table
|
120
|
*/
|
121
|
public void testBuildRecords() throws Exception
|
122
|
{
|
123
|
double start = System.currentTimeMillis()/1000;
|
124
|
DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
|
125
|
int serialNumber = dbconn.getCheckOutSerialNumber();
|
126
|
String sql = "SELECT docid, nodedata, path FROM xml_path_index WHERE path IN (";
|
127
|
PreparedStatement pstmt = null;
|
128
|
ResultSet result = null;
|
129
|
//Gets the document information such as doctype, docname and et al
|
130
|
Hashtable docsInformation = getDocumentsInfo();
|
131
|
//The key of the hash is docid, the element is the return field value (in xml_format)
|
132
|
Hashtable table = new Hashtable();
|
133
|
int size = returnFieldString.length;
|
134
|
// Constructs the sql base on the return fields
|
135
|
boolean first = true;
|
136
|
for (int i=0; i< size; i++)
|
137
|
{
|
138
|
if (!first)
|
139
|
{
|
140
|
sql=sql+",";
|
141
|
}
|
142
|
sql=sql+"'";
|
143
|
String path = returnFieldString[i];
|
144
|
sql = sql+path;
|
145
|
sql=sql+"'";
|
146
|
first = false;
|
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();
|
189
|
// Insert the hashtable value into xml_queryresult table
|
190
|
Enumeration docids = table.keys();
|
191
|
while (docids.hasMoreElements())
|
192
|
{
|
193
|
String docId = (String)docids.nextElement();
|
194
|
String returnString = (String)table.get(docId);
|
195
|
String query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
|
196
|
+ "queryresult_string) VALUES (?, ?, ?)";
|
197
|
|
198
|
pstmt = dbconn.prepareStatement(query);
|
199
|
pstmt.setInt(1,returnFieldId );
|
200
|
pstmt.setString(2,docId);
|
201
|
pstmt.setString(3, returnString);
|
202
|
pstmt.execute();
|
203
|
pstmt.close();
|
204
|
|
205
|
}
|
206
|
|
207
|
double end = System.currentTimeMillis()/1000;
|
208
|
System.out.println("The time to build xml_queryresult is "+(end-start));
|
209
|
DBConnectionPool.returnDBConnection(dbconn, serialNumber);
|
210
|
assertTrue(1 == 1);
|
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
|
}
|
272
|
|
273
|
}
|