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-12-26 16:33:20 -0800 (Fri, 26 Dec 2008) $'
8
 * '$Revision: 4719 $'
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
		  PropertyService.getInstance();
99
  		  MetacatUtil.pathsForIndexing 
100
  		         = MetacatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
101
  	  }
102
  	  catch(Exception e)
103
  	  {
104
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
105
  	  }
106
    }
107
    
108
    /**
109
     * Constructor to build the test
110
     * 
111
     * @param name the name of the test method
112
     */
113
    public QuerySpecificationTest(String name)
114
    {
115
        super(name);
116
    }
117

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

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

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

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

    
186
 
187
}
(14-14/20)