Project

General

Profile

1 2068 jones
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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.FileReader;
29
import java.io.IOException;
30 6602 leinfelder
import java.sql.PreparedStatement;
31
import java.util.ArrayList;
32
import java.util.List;
33 2068 jones
34
import junit.framework.Test;
35
import junit.framework.TestSuite;
36
37 4150 daigle
import edu.ucsb.nceas.MCTestCase;
38 6602 leinfelder
import edu.ucsb.nceas.metacat.DBQuery;
39 2068 jones
import edu.ucsb.nceas.metacat.QuerySpecification;
40 6602 leinfelder
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
41 5035 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
42 4700 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
43 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
44 2068 jones
45
/**
46
 * @author jones
47
 *
48
 * Test the output of the QuerySpecification class
49
 */
50 4150 daigle
public class QuerySpecificationTest extends MCTestCase
51 2068 jones
{
52
    /** A test query document in xml format */
53
    private FileReader xml;
54
55
    /** The utilities object for accessing property values */
56
57 6602 leinfelder
    private static String selectionQuery =
58
    	"SELECT docid,docname,doctype,date_created, date_updated, rev " +
59
    	"FROM xml_documents WHERE docid IN (((((SELECT DISTINCT docid FROM xml_nodes " +
60
    	"WHERE UPPER(nodedata) LIKE %JONES% ) )))) ";
61 3879 tao
    /*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 " +
62 3567 tao
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
63
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, 'originator/individualName/surName' as " +
64
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
65
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'surName' AND parentnodeid IN " +
66
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'individualName' AND parentnodeid IN " +
67
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'originator' ) ) )  AND xml_nodes.docid in " +
68
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = " +
69
    		"xml_documents.rootnodeid UNION select xml_nodes.docid, 'keyword' as path, xml_nodes.nodedata, " +
70
    		"xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM " +
71
    		"xml_nodes WHERE nodename LIKE 'keyword' )  AND xml_nodes.docid in " +
72
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
73
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, '/eml/@packageId' as " +
74
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
75
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE '@packageId' AND parentnodeid IN " +
76
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) )  " +
77
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
78
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, " +
79
    		"'/eml/dataset/access/@authSystem' as path, xml_nodes.nodedata, xml_nodes.parentnodeid from " +
80
    		"xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
81
    		"LIKE '@authSystem' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' " +
82
    		"AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND " +
83
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND " +
84
    		"parentnodeid = rootnodeid ) ) ) )  AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND " +
85
    		"xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = xml_documents.rootnodeid UNION " +
86
    		"select xml_nodes.docid, '/eml/dataset/access/@order' as path, xml_nodes.nodedata, xml_nodes.parentnodeid " +
87
    		"from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
88
    		"LIKE '@order' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' AND " +
89
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND parentnodeid " +
90
    		"IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) ) ) )  " +
91
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' " +
92 3879 tao
    		"AND xml_nodes.rootnodeid = xml_documents.rootnodeid";*/
93 6602 leinfelder
    private static String extendedQuery =
94
    		"select xml_nodes.docid, xml_index.path, xml_nodes.nodedata,  xml_nodes.parentnodeid, xml_nodes.nodetype " +
95
    		"FROM xml_index, xml_nodes " +
96
    		"WHERE ( (xml_index.nodeid=xml_nodes.parentnodeid AND xml_index.path IN ( dataset/title, originator/individualName/surName , keyword ) " +
97
    		"AND xml_nodes.nodetype = 'TEXT') "+
98
            "OR  (xml_index.nodeid=xml_nodes.nodeid AND ( xml_index.path IN ( /eml/@packageId, /eml/dataset/access/@authSystem , /eml/dataset/access/@order ) " +
99
            "AND xml_nodes.nodetype = 'ATTRIBUTE'))) "+
100
            "AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346')";
101 3567 tao
102 4127 daigle
    /* Initialize properties*/
103 3567 tao
    static
104
    {
105
  	  try
106
  	  {
107 4719 daigle
		  PropertyService.getInstance();
108 4816 daigle
 	  }
109 3567 tao
  	  catch(Exception e)
110
  	  {
111 6602 leinfelder
  		  System.err.println("Exception in initialize option in MetacatServletNetTest " + e.getMessage());
112 3567 tao
  	  }
113
    }
114
115 2068 jones
    /**
116
     * Constructor to build the test
117
     *
118
     * @param name the name of the test method
119
     */
120
    public QuerySpecificationTest(String name)
121
    {
122
        super(name);
123
    }
124
125
    /*
126
     * @see TestCase#setUp()
127
     */
128
    protected void setUp() throws Exception
129
    {
130
        super.setUp();
131
        try {
132 4719 daigle
//  		  	PropertyService.getInstance("build/tests");
133
  		  	PropertyService.getInstance();
134 2068 jones
            String xmlfile = "./test/query.xml";
135
            xml = new FileReader(new File(xmlfile));
136
        } catch (FileNotFoundException e) {
137
            fail(e.getMessage());
138 4127 daigle
        }
139 2068 jones
    }
140
141
    /**
142
     * Create a suite of tests to be run together
143
     */
144
    public static Test suite()
145
    {
146
        TestSuite suite = new TestSuite();
147
        suite.addTest(new QuerySpecificationTest("testPrintSQL"));
148
        suite.addTest(new QuerySpecificationTest("testPrintExtendedSQL"));
149
        return suite;
150
    }
151
152
    /**
153
     * Print the sql generated from this specification
154
     */
155
    public void testPrintSQL()
156
    {
157
        try {
158 2074 jones
            System.out.println("---- SQL ------------------");
159 2068 jones
            QuerySpecification qspec = new QuerySpecification(xml,
160 4213 daigle
                    PropertyService.getProperty("xml.saxparser"),
161 4212 daigle
                    PropertyService.getProperty("document.accNumSeparator"));
162 6602 leinfelder
            // keep track of parameter values
163
            List<Object> parameterValues = new ArrayList<Object>();
164
            String query = qspec.printSQL(false, parameterValues);
165
    		// fill in the values to really check the query string matches original/expected
166
    		PreparedStatement pstmt = DBConnectionPool.getDBConnection("queryGroupTest").prepareStatement(query);
167
    		pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
168
    		String preparedQueryString = pstmt.toString();
169
170
    		System.out.println("Prepared query: " + preparedQueryString);
171
            System.out.println("Original query: " + selectionQuery);
172
173
            assertEquals(selectionQuery, preparedQueryString);
174
175
        } catch (Exception e) {
176
    		e.printStackTrace();
177
			fail(e.getMessage());
178
		}
179 2068 jones
    }
180
181
    /**
182
     * Print the extended SQL for a result set.
183
     */
184
    public void testPrintExtendedSQL()
185
    {
186
        try {
187 6602 leinfelder
            System.out.println("---- orginal EXT SQL  ------------------");
188
            System.out.println(extendedQuery);
189 2068 jones
            QuerySpecification qspec = new QuerySpecification(xml,
190 4213 daigle
                    PropertyService.getProperty("xml.saxparser"),
191 4212 daigle
                    PropertyService.getProperty("document.accNumSeparator"));
192 6602 leinfelder
            // keep track of parameter values
193
            List<Object> parameterValues = new ArrayList<Object>();
194 6739 leinfelder
            List<Object> docListValues = new ArrayList<Object>();
195
            docListValues.add("obfs.45337");
196
            docListValues.add("obfs.45338");
197
            docListValues.add("obfs.45346");
198
199 3567 tao
            String query =
200 2074 jones
                    qspec.printExtendedSQL(
201 6739 leinfelder
                            "?, ?, ?", true, parameterValues, docListValues);
202 6602 leinfelder
203
            System.out.println("---- built EXT SQL ------------------");
204
            System.out.println(query);
205
206
            // fill in the values to really check the query string matches original/expected
207
    		PreparedStatement pstmt = DBConnectionPool.getDBConnection("queryGroupTest").prepareStatement(query);
208
    		pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
209
    		String preparedQueryString = pstmt.toString();
210
211
    		System.out.println("Prepared query: " + preparedQueryString);
212
            System.out.println("Original query: " + extendedQuery);
213
214
            assertEquals(extendedQuery, preparedQueryString);
215
216
        } catch (Exception e) {
217
        	e.printStackTrace();
218 2068 jones
            fail(e.getMessage());
219
        }
220
    }
221
222 3359 tao
223 2068 jones
}