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: leinfelder $'
|
7
|
* '$Date: 2011-11-04 12:32:37 -0700 (Fri, 04 Nov 2011) $'
|
8
|
* '$Revision: 6602 $'
|
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.sql.PreparedStatement;
|
27
|
import java.util.ArrayList;
|
28
|
import java.util.List;
|
29
|
|
30
|
import junit.framework.Test;
|
31
|
import junit.framework.TestSuite;
|
32
|
|
33
|
import edu.ucsb.nceas.MCTestCase;
|
34
|
import edu.ucsb.nceas.metacat.DBQuery;
|
35
|
import edu.ucsb.nceas.metacat.QueryGroup;
|
36
|
import edu.ucsb.nceas.metacat.QueryTerm;
|
37
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
38
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
39
|
import edu.ucsb.nceas.metacat.util.MetacatUtil;
|
40
|
|
41
|
/**
|
42
|
* @author jones
|
43
|
*
|
44
|
* Test the output of the QuerySpecification class
|
45
|
*/
|
46
|
public class QueryGroupTest extends MCTestCase
|
47
|
{
|
48
|
private QueryGroup group = null;
|
49
|
/* Initialize properties*/
|
50
|
static
|
51
|
{
|
52
|
try
|
53
|
{
|
54
|
PropertyService.getInstance();
|
55
|
}
|
56
|
catch(Exception e)
|
57
|
{
|
58
|
System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
|
59
|
}
|
60
|
}
|
61
|
/**
|
62
|
* NOTE: there are no quotes around the values because we are comparing it to a PreparedStatement.toString()
|
63
|
* String after binding the parameter values. Please trust that the PreparedStatement is correct.
|
64
|
*/
|
65
|
private String query =
|
66
|
"(SELECT DISTINCT docid FROM xml_path_index WHERE (UPPER(nodedata) "+
|
67
|
"LIKE %LAND% AND path IN ( dataset/title, geographicCoverage/boundingCoordinates/southBoundingCoordinate )) " +
|
68
|
"OR ((UPPER(nodedata) LIKE %JONES% AND path LIKE organizationName) ) OR ((UPPER(nodedata) LIKE %LAND % AND path LIKE keyword) ) " +
|
69
|
"OR ((UPPER(nodedata) LIKE %DATOS% AND path LIKE entityName) ) UNION ((SELECT DISTINCT docid FROM xml_nodes WHERE UPPER(nodedata) " +
|
70
|
"LIKE %VALUE1% AND parentnodeid IN (SELECT nodeid FROM xml_index WHERE path LIKE path1) ) UNION " +
|
71
|
"(SELECT DISTINCT docid FROM xml_nodes WHERE UPPER(nodedata) LIKE %VALUE2% AND parentnodeid IN " +
|
72
|
"(SELECT nodeid FROM xml_index WHERE path LIKE path2) ) ))";
|
73
|
|
74
|
/**
|
75
|
* Constructor to build the test
|
76
|
*
|
77
|
* @param name the name of the test method
|
78
|
*/
|
79
|
public QueryGroupTest(String name)
|
80
|
{
|
81
|
super(name);
|
82
|
}
|
83
|
/**
|
84
|
* Establishes a testing framework by initializing appropriate objects.
|
85
|
*/
|
86
|
protected void setUp() throws Exception
|
87
|
{
|
88
|
super.setUp();
|
89
|
|
90
|
}
|
91
|
|
92
|
|
93
|
/**
|
94
|
* Releases any objects after tests are complete.
|
95
|
*/
|
96
|
protected void tearDown() throws Exception
|
97
|
{
|
98
|
|
99
|
super.tearDown();
|
100
|
}
|
101
|
|
102
|
|
103
|
|
104
|
/**
|
105
|
* Tests initial
|
106
|
*/
|
107
|
public void initial()
|
108
|
{
|
109
|
assertTrue(1 == 1);
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Tests print out sql command of QueryGroup with UNION
|
114
|
*/
|
115
|
public void printUnion()
|
116
|
{
|
117
|
group = new QueryGroup("UNION");
|
118
|
QueryTerm term1 = new QueryTerm ( false, "contains", "land", "dataset/title");
|
119
|
QueryTerm term2 = new QueryTerm ( false, "contains", "jones", "organizationName");
|
120
|
QueryTerm term3 = new QueryTerm ( false, "contains", "land ", "keyword");
|
121
|
QueryTerm term4 = new QueryTerm ( false, "contains", "land", "geographicCoverage/boundingCoordinates/southBoundingCoordinate");
|
122
|
QueryTerm term5 = new QueryTerm ( false, "contains", "datos", "entityName");
|
123
|
QueryGroup child = new QueryGroup("UNION");
|
124
|
QueryTerm term6 = new QueryTerm ( false, "contains", "value1", "path1");
|
125
|
QueryTerm term7 = new QueryTerm ( false, "contains", "value2", "path2");
|
126
|
child.addChild(term6);
|
127
|
child.addChild(term7);
|
128
|
group.addChild(term1);
|
129
|
group.addChild(term2);
|
130
|
group.addChild(term3);
|
131
|
group.addChild(term4);
|
132
|
group.addChild(term5);
|
133
|
group.addChild(child);
|
134
|
// keep track of parameter values
|
135
|
List<Object> parameterValues = new ArrayList<Object>();
|
136
|
String queryString = group.printSQL(true, parameterValues);
|
137
|
try {
|
138
|
// fill in the values to really check the query string matches original/expected
|
139
|
PreparedStatement pstmt = DBConnectionPool.getDBConnection("queryGroupTest").prepareStatement(queryString);
|
140
|
pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
|
141
|
String preparedQueryString = pstmt.toString();
|
142
|
System.out.println("Prepared query: " + preparedQueryString);
|
143
|
System.out.println("Original query: " + query);
|
144
|
|
145
|
assertEquals(query, preparedQueryString);
|
146
|
} catch (Exception e) {
|
147
|
e.printStackTrace();
|
148
|
fail(e.getMessage());
|
149
|
}
|
150
|
}
|
151
|
|
152
|
|
153
|
/**
|
154
|
* Create a suite of tests to be run together
|
155
|
*/
|
156
|
public static Test suite()
|
157
|
{
|
158
|
TestSuite suite = new TestSuite();
|
159
|
suite.addTest(new QueryGroupTest("initial"));
|
160
|
suite.addTest(new QueryGroupTest("printUnion"));
|
161
|
return suite;
|
162
|
}
|
163
|
|
164
|
|
165
|
}
|