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: daigle $'
7
 *     '$Date: 2008-08-05 17:50:14 -0700 (Tue, 05 Aug 2008) $'
8
 * '$Revision: 4213 $'
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

    
31
import junit.framework.Test;
32
import junit.framework.TestSuite;
33

    
34
import edu.ucsb.nceas.MCTestCase;
35
import edu.ucsb.nceas.metacat.QuerySpecification;
36
import edu.ucsb.nceas.metacat.service.PropertyService;
37
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39

    
40
/**
41
 * @author jones
42
 * 
43
 * Test the output of the QuerySpecification class
44
 */
45
public class QuerySpecificationTest extends MCTestCase
46
{
47
    /** A test query document in xml format */
48
    private FileReader xml;
49
    
50
    /** The utilities object for accessing property values */
51
    
52
    private static String selectionQuery = "SELECT docid,docname,doctype,date_created, date_updated, rev " +
53
    		"FROM xml_documents WHERE docid IN (((((SELECT DISTINCT docid FROM xml_nodes " +
54
    		"WHERE UPPER(nodedata) LIKE '%JONES%' ) ))))";
55
    /*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 " +
56
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
57
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, 'originator/individualName/surName' as " +
58
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
59
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'surName' AND parentnodeid IN " +
60
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'individualName' AND parentnodeid IN " +
61
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'originator' ) ) )  AND xml_nodes.docid in " +
62
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = " +
63
    		"xml_documents.rootnodeid UNION select xml_nodes.docid, 'keyword' as path, xml_nodes.nodedata, " +
64
    		"xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM " +
65
    		"xml_nodes WHERE nodename LIKE 'keyword' )  AND xml_nodes.docid in " +
66
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
67
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, '/eml/@packageId' as " +
68
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
69
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE '@packageId' AND parentnodeid IN " +
70
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) )  " +
71
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
72
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, " +
73
    		"'/eml/dataset/access/@authSystem' as path, xml_nodes.nodedata, xml_nodes.parentnodeid from " +
74
    		"xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
75
    		"LIKE '@authSystem' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' " +
76
    		"AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND " +
77
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND " +
78
    		"parentnodeid = rootnodeid ) ) ) )  AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND " +
79
    		"xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = xml_documents.rootnodeid UNION " +
80
    		"select xml_nodes.docid, '/eml/dataset/access/@order' as path, xml_nodes.nodedata, xml_nodes.parentnodeid " +
81
    		"from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
82
    		"LIKE '@order' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' AND " +
83
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND parentnodeid " +
84
    		"IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) ) ) )  " +
85
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' " +
86
    		"AND xml_nodes.rootnodeid = xml_documents.rootnodeid";*/
87
    private static String extendedQuery ="select xml_nodes.docid, xml_index.path, xml_nodes.nodedata,  xml_nodes.parentnodeid, xml_nodes.nodetype FROM xml_index, xml_nodes WHERE"+
88
                                    " ( (xml_index.nodeid=xml_nodes.parentnodeid AND xml_index.path IN ('dataset/title', 'originator/individualName/surName' , 'keyword' ) AND xml_nodes.nodetype = 'TEXT') "+
89
                                    "OR  (xml_index.nodeid=xml_nodes.nodeid AND ( xml_index.path IN ( '/eml/@packageId', '/eml/dataset/access/@authSystem' , '/eml/dataset/access/@order' ) AND xml_nodes.nodetype = 'ATTRIBUTE'))) "+
90
                                    "AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346')";
91
    
92
    /* Initialize properties*/
93
    static
94
    {
95
  	  try
96
  	  {
97
		  PropertyService.getInstance("build/tests");
98
  		  MetaCatUtil.pathsForIndexing 
99
  		         = MetaCatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
100
  	  }
101
  	  catch(Exception e)
102
  	  {
103
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
104
  	  }
105
    }
106
    
107
    /**
108
     * Constructor to build the test
109
     * 
110
     * @param name the name of the test method
111
     */
112
    public QuerySpecificationTest(String name)
113
    {
114
        super(name);
115
    }
116

    
117
    /*
118
     * @see TestCase#setUp()
119
     */
120
    protected void setUp() throws Exception
121
    {
122
        super.setUp();
123
        try {
124
  		  	PropertyService.getInstance("build/tests");
125
            String xmlfile = "./test/query.xml";
126
            xml = new FileReader(new File(xmlfile));
127
        } catch (FileNotFoundException e) {
128
            fail(e.getMessage());
129
        } 
130
    }
131

    
132
    /**
133
     * Create a suite of tests to be run together
134
     */
135
    public static Test suite()
136
    {
137
        TestSuite suite = new TestSuite();
138
        suite.addTest(new QuerySpecificationTest("testPrintSQL"));
139
        suite.addTest(new QuerySpecificationTest("testPrintExtendedSQL"));
140
        return suite;
141
    }
142

    
143
    /**
144
     * Print the sql generated from this specification
145
     */
146
    public void testPrintSQL()
147
    {
148
        try {
149
            System.out.println("---- SQL ------------------");
150
            QuerySpecification qspec = new QuerySpecification(xml, 
151
                    PropertyService.getProperty("xml.saxparser"), 
152
                    PropertyService.getProperty("document.accNumSeparator"));
153
            String query = qspec.printSQL(false);
154
           assertTrue(selectionQuery.equals(query.trim()));
155
        } catch (IOException e) {
156
            fail(e.getMessage());
157
        } catch (PropertyNotFoundException pnfe) {
158
            fail(pnfe.getMessage());
159
        }
160
    }
161

    
162
    /**
163
     * Print the extended SQL for a result set.
164
     */
165
    public void testPrintExtendedSQL()
166
    {
167
        try {
168
            System.out.println("---- orginal EXT SQL  ------------------\n"+extendedQuery);
169
            QuerySpecification qspec = new QuerySpecification(xml, 
170
                    PropertyService.getProperty("xml.saxparser"), 
171
                    PropertyService.getProperty("document.accNumSeparator"));
172
            String query = 
173
                    qspec.printExtendedSQL(
174
                            "'obfs.45337', 'obfs.45338', 'obfs.45346'", true);
175
            System.out.println("---- builded EXT SQL ------------------ \n"+query);
176
            assertTrue(extendedQuery.equals(query));
177
        } catch (IOException e) {
178
            fail(e.getMessage());
179
        } catch (PropertyNotFoundException pnfe) {
180
            fail(pnfe.getMessage());
181
        }
182
    }
183

    
184
 
185
}
(14-14/19)