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: 2009-08-24 14:42:25 -0700 (Mon, 24 Aug 2009) $'
8
 * '$Revision: 5035 $'
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.properties.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();
98
 	  }
99
  	  catch(Exception e)
100
  	  {
101
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
102
  	  }
103
    }
104
    
105
    /**
106
     * Constructor to build the test
107
     * 
108
     * @param name the name of the test method
109
     */
110
    public QuerySpecificationTest(String name)
111
    {
112
        super(name);
113
    }
114

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

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

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

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

    
183
 
184
}
(17-17/24)