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: tao $'
7
 *     '$Date: 2008-05-16 11:22:58 -0700 (Fri, 16 May 2008) $'
8
 * '$Revision: 3879 $'
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.util.Hashtable;
31

    
32
import junit.framework.Test;
33
import junit.framework.TestCase;
34
import junit.framework.TestSuite;
35

    
36
import edu.ucsb.nceas.metacat.MetaCatUtil;
37
import edu.ucsb.nceas.metacat.QuerySpecification;
38
import edu.ucsb.nceas.utilities.Options;
39

    
40
/**
41
 * @author jones
42
 * 
43
 * Test the output of the QuerySpecification class
44
 */
45
public class QuerySpecificationTest extends TestCase
46
{
47
    /** A test query document in xml format */
48
    private FileReader xml;
49
    
50
    /** The utilities object for accessing property values */
51
    private MetaCatUtil util;
52
    
53
    private static String selectionQuery = "SELECT docid,docname,doctype,date_created, date_updated, rev " +
54
    		"FROM xml_documents WHERE docid IN (((((SELECT DISTINCT docid FROM xml_nodes " +
55
    		"WHERE UPPER(nodedata) LIKE '%JONES%' ) ))))";
56
    /*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 " +
57
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
58
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, 'originator/individualName/surName' as " +
59
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
60
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'surName' AND parentnodeid IN " +
61
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'individualName' AND parentnodeid IN " +
62
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'originator' ) ) )  AND xml_nodes.docid in " +
63
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = " +
64
    		"xml_documents.rootnodeid UNION select xml_nodes.docid, 'keyword' as path, xml_nodes.nodedata, " +
65
    		"xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM " +
66
    		"xml_nodes WHERE nodename LIKE 'keyword' )  AND xml_nodes.docid in " +
67
    		"('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
68
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, '/eml/@packageId' as " +
69
    		"path, xml_nodes.nodedata, xml_nodes.parentnodeid from xml_nodes, xml_documents where parentnodeid IN " +
70
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE '@packageId' AND parentnodeid IN " +
71
    		"(SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) )  " +
72
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' AND " +
73
    		"xml_nodes.rootnodeid = xml_documents.rootnodeid UNION select xml_nodes.docid, " +
74
    		"'/eml/dataset/access/@authSystem' as path, xml_nodes.nodedata, xml_nodes.parentnodeid from " +
75
    		"xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
76
    		"LIKE '@authSystem' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' " +
77
    		"AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND " +
78
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND " +
79
    		"parentnodeid = rootnodeid ) ) ) )  AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND " +
80
    		"xml_nodes.nodetype = 'TEXT' AND xml_nodes.rootnodeid = xml_documents.rootnodeid UNION " +
81
    		"select xml_nodes.docid, '/eml/dataset/access/@order' as path, xml_nodes.nodedata, xml_nodes.parentnodeid " +
82
    		"from xml_nodes, xml_documents where parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename " +
83
    		"LIKE '@order' AND parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'access' AND " +
84
    		"parentnodeid IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'dataset' AND parentnodeid " +
85
    		"IN (SELECT nodeid FROM xml_nodes WHERE nodename LIKE 'eml' AND parentnodeid = rootnodeid ) ) ) )  " +
86
    		"AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346') AND xml_nodes.nodetype = 'TEXT' " +
87
    		"AND xml_nodes.rootnodeid = xml_documents.rootnodeid";*/
88
    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"+
89
                                    " ( (xml_index.nodeid=xml_nodes.parentnodeid AND xml_index.path IN ('dataset/title', 'originator/individualName/surName' , 'keyword' ) AND xml_nodes.nodetype = 'TEXT') "+
90
                                    "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'))) "+
91
                                    "AND xml_nodes.docid in ('obfs.45337', 'obfs.45338', 'obfs.45346')";
92
    
93
    /* Initialize Options*/
94
    static
95
    {
96
  	  try
97
  	  {
98
  		  Options.initialize(new File("build/tests/metacat.properties"));
99
  		  MetaCatUtil.pathsForIndexing 
100
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("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
            File propertyFile = new File("./lib/metacat.properties");
126
            Options options = Options.initialize(propertyFile);
127
            util = new MetaCatUtil();
128
            String xmlfile = "./test/query.xml";
129
            xml = new FileReader(new File(xmlfile));
130
        } catch (FileNotFoundException e) {
131
            fail(e.getMessage());
132
        } catch (IOException e) {
133
            fail(e.getMessage());
134
        }
135
    }
136

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

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

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

    
185
 
186
}
(13-13/18)