Project

General

Profile

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-12-08 13:15:18 -0800 (Thu, 08 Dec 2011) $'
8
 * '$Revision: 6753 $'
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
import java.sql.PreparedStatement;
31
import java.util.ArrayList;
32
import java.util.List;
33

    
34
import junit.framework.Test;
35
import junit.framework.TestSuite;
36

    
37
import edu.ucsb.nceas.MCTestCase;
38
import edu.ucsb.nceas.metacat.DBQuery;
39
import edu.ucsb.nceas.metacat.QuerySpecification;
40
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
41
import edu.ucsb.nceas.metacat.properties.PropertyService;
42
import edu.ucsb.nceas.metacat.util.MetacatUtil;
43
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
44

    
45
/**
46
 * @author jones
47
 * 
48
 * Test the output of the QuerySpecification class
49
 */
50
public class QuerySpecificationTest extends MCTestCase
51
{
52
    /** A test query document in xml format */
53
    private FileReader xml;
54
    
55
    /** The utilities object for accessing property values */
56
    
57
    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
    /*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
    		"('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
    		"AND xml_nodes.rootnodeid = xml_documents.rootnodeid";*/
93
    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
    
102
    /* Initialize properties*/
103
    static
104
    {
105
  	  try
106
  	  {
107
		  PropertyService.getInstance();
108
 	  }
109
  	  catch(Exception e)
110
  	  {
111
  		  System.err.println("Exception in initialize option in MetacatServletNetTest " + e.getMessage());
112
  	  }
113
    }
114
    
115
    /**
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
//  		  	PropertyService.getInstance("build/tests");
133
  		  	PropertyService.getInstance();
134
            String xmlfile = "./test/query.xml";
135
            xml = new FileReader(new File(xmlfile));
136
        } catch (FileNotFoundException e) {
137
            fail(e.getMessage());
138
        } 
139
    }
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
            System.out.println("---- SQL ------------------");
159
            QuerySpecification qspec = new QuerySpecification(xml, 
160
                    PropertyService.getProperty("xml.saxparser"), 
161
                    PropertyService.getProperty("document.accNumSeparator"));
162
            // 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
    }
180

    
181
    /**
182
     * Print the extended SQL for a result set.
183
     */
184
    public void testPrintExtendedSQL()
185
    {
186
        try {
187
            System.out.println("---- orginal EXT SQL  ------------------");
188
            System.out.println(extendedQuery);
189
            QuerySpecification qspec = new QuerySpecification(xml, 
190
                    PropertyService.getProperty("xml.saxparser"), 
191
                    PropertyService.getProperty("document.accNumSeparator"));
192
            // keep track of parameter values
193
            List<Object> parameterValues = new ArrayList<Object>();
194
            List<Object> docListValues = new ArrayList<Object>();
195
            docListValues.add("obfs.45337");
196
            docListValues.add("obfs.45338");
197
            docListValues.add("obfs.45346");
198

    
199
            String query = 
200
                    qspec.printExtendedSQL(
201
                            "?, ?, ?", true, parameterValues, docListValues);
202
            
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
            fail(e.getMessage());
219
        }
220
    }
221

    
222
 
223
}
(17-17/24)