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: 2012-10-18 15:14:22 -0700 (Thu, 18 Oct 2012) $'
8
 * '$Revision: 7407 $'
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 xml_documents.docid, docname, doctype, date_created, date_updated, xml_documents.rev " +
59
    	"FROM xml_documents, identifier " +
60
    	"WHERE xml_documents.docid = identifier.docid AND xml_documents.rev = identifier.rev " +
61
    	"AND xml_documents.docid IN (((((SELECT DISTINCT docid " +
62
    	"FROM xml_nodes " +
63
    	"WHERE UPPER(nodedata) LIKE %JONES% ) )))) ";
64
    /*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 " +
65
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
66
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, 'originator/individualName/surName' as " +
67
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
68
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'surName' AND parentnodeid IN " +
69
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'individualName' AND parentnodeid IN " +
70
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'originator' ) ) )  AND xml_nodes.docid in " +
71
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = " +
72
    		"xml_documents.rootnodeid UNION select xml_nodes.docid, 'keyword' as path, xml_nodes.nodedata, " +
73
    		"xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM " +
74
    		"xml_nodes WHERE nodename LIKE 'keyword' )  AND xml_nodes.docid in " +
75
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
76
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, '/eml/@packageId' as " +
77
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
78
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE '@packageId' AND parentnodeid IN " +
79
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) )  " +
80
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
81
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, " +
82
    		"'/eml/dataset/access/@authSystem' as path, xml_nodes.nodedata, xml_nodes.parentnodeid from " +
83
    		"xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
84
    		"LIKE '@authSystem' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' " +
85
    		"AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND " +
86
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND " +
87
    		"parentnodeid = rootnodeid ) ) ) )  AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND " +
88
    		"xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = xml_documents.rootnodeid UNION " +
89
    		"select xml_nodes.docid, '/eml/dataset/access/@order' as path, xml_nodes.nodedata, xml_nodes.parentnodeid " +
90
    		"from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
91
    		"LIKE '@order' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' AND " +
92
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND parentnodeid " +
93
    		"IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) ) ) )  " +
94
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' " +
95
    		"AND xml_nodes.rootnodeid = xml_documents.rootnodeid";*/
96
    private static String extendedQuery =
97
    		"select xml_nodes.docid, xml_index.path, xml_nodes.nodedata,  xml_nodes.parentnodeid, xml_nodes.nodetype " +
98
    		"FROM xml_index, xml_nodes " +
99
    		"WHERE ( (xml_index.nodeid=xml_nodes.parentnodeid AND xml_index.path IN ( dataset/title, originator/individualName/surName , keyword ) " +
100
    		"AND xml_nodes.nodetype = 'TEXT') "+
101
            "OR  (xml_index.nodeid=xml_nodes.nodeid AND ( xml_index.path IN ( /eml/@packageId, /eml/dataset/access/@authSystem , /eml/dataset/access/@order ) " +
102
            "AND xml_nodes.nodetype = 'ATTRIBUTE'))) "+
103
            "AND xml_nodes.docid in (obfs.45337, obfs.45338, obfs.45346)";
104
    
105
    /* Initialize properties*/
106
    static
107
    {
108
  	  try
109
  	  {
110
		  PropertyService.getInstance();
111
 	  }
112
  	  catch(Exception e)
113
  	  {
114
  		  System.err.println("Exception in initialize option in MetacatServletNetTest " + e.getMessage());
115
  	  }
116
    }
117
    
118
    /**
119
     * Constructor to build the test
120
     * 
121
     * @param name the name of the test method
122
     */
123
    public QuerySpecificationTest(String name)
124
    {
125
        super(name);
126
    }
127

    
128
    /*
129
     * @see TestCase#setUp()
130
     */
131
    protected void setUp() throws Exception
132
    {
133
        super.setUp();
134
        try {
135
//  		  	PropertyService.getInstance("build/tests");
136
  		  	PropertyService.getInstance();
137
            String xmlfile = "./test/query.xml";
138
            xml = new FileReader(new File(xmlfile));
139
        } catch (FileNotFoundException e) {
140
            fail(e.getMessage());
141
        } 
142
    }
143

    
144
    /**
145
     * Create a suite of tests to be run together
146
     */
147
    public static Test suite()
148
    {
149
        TestSuite suite = new TestSuite();
150
        suite.addTest(new QuerySpecificationTest("testPrintSQL"));
151
        suite.addTest(new QuerySpecificationTest("testPrintExtendedSQL"));
152
        return suite;
153
    }
154

    
155
    /**
156
     * Print the sql generated from this specification
157
     */
158
    public void testPrintSQL()
159
    {
160
        try {
161
            System.out.println("---- SQL ------------------");
162
            QuerySpecification qspec = new QuerySpecification(xml, 
163
                    PropertyService.getProperty("xml.saxparser"), 
164
                    PropertyService.getProperty("document.accNumSeparator"));
165
            // keep track of parameter values
166
            List<Object> parameterValues = new ArrayList<Object>();
167
            String query = qspec.printSQL(false, parameterValues);
168
    		// fill in the values to really check the query string matches original/expected
169
    		PreparedStatement pstmt = DBConnectionPool.getDBConnection("queryGroupTest").prepareStatement(query);
170
    		pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
171
    		String preparedQueryString = pstmt.toString();
172

    
173
    		System.out.println("Prepared query: " + preparedQueryString);
174
            System.out.println("Original query: " + selectionQuery);
175
            
176
            assertEquals(selectionQuery, preparedQueryString);
177
        	 
178
        } catch (Exception e) {
179
    		e.printStackTrace();
180
			fail(e.getMessage());
181
		}
182
    }
183

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

    
202
            String query = 
203
                    qspec.printExtendedSQL(
204
                            "?, ?, ?", true, parameterValues, docListValues);
205
            
206
            System.out.println("---- built EXT SQL ------------------");
207
            System.out.println(query);
208
            
209
            // fill in the values to really check the query string matches original/expected
210
    		PreparedStatement pstmt = DBConnectionPool.getDBConnection("queryGroupTest").prepareStatement(query);
211
    		pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
212
    		String preparedQueryString = pstmt.toString();
213

    
214
    		System.out.println("Prepared query: " + preparedQueryString);
215
            System.out.println("Original query: " + extendedQuery);
216
            
217
            assertEquals(extendedQuery, preparedQueryString);
218
            
219
        } catch (Exception e) {
220
        	e.printStackTrace();
221
            fail(e.getMessage());
222
        }
223
    }
224

    
225
 
226
}
(17-17/24)