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: 2007-11-03 22:16:17 -0700 (Sat, 03 Nov 2007) $'
8
 * '$Revision: 3567 $'
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
    
89
    /* Initialize Options*/
90
    static
91
    {
92
  	  try
93
  	  {
94
  		  Options.initialize(new File("build/tests/metacat.properties"));
95
  		  MetaCatUtil.pathsForIndexing 
96
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
97
  	  }
98
  	  catch(Exception e)
99
  	  {
100
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
101
  	  }
102
    }
103
    
104
    /**
105
     * Constructor to build the test
106
     * 
107
     * @param name the name of the test method
108
     */
109
    public QuerySpecificationTest(String name)
110
    {
111
        super(name);
112
    }
113

    
114
    /*
115
     * @see TestCase#setUp()
116
     */
117
    protected void setUp() throws Exception
118
    {
119
        super.setUp();
120
        try {
121
            File propertyFile = new File("./lib/metacat.properties");
122
            Options options = Options.initialize(propertyFile);
123
            util = new MetaCatUtil();
124
            String xmlfile = "./test/query.xml";
125
            xml = new FileReader(new File(xmlfile));
126
        } catch (FileNotFoundException e) {
127
            fail(e.getMessage());
128
        } catch (IOException e) {
129
            fail(e.getMessage());
130
        }
131
    }
132

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

    
144
    /**
145
     * Print the sql generated from this specification
146
     */
147
    public void testPrintSQL()
148
    {
149
        try {
150
            System.out.println("---- SQL ------------------");
151
            QuerySpecification qspec = new QuerySpecification(xml, 
152
                    MetaCatUtil.getOption("saxparser"), 
153
                    MetaCatUtil.getOption("accNumberSeparator"));
154
            String query = qspec.printSQL(false);
155
           assertTrue(selectionQuery.equals(query.trim()));
156
        } catch (IOException e) {
157
            fail(e.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("---- EXT SQL ------------------");
168
            QuerySpecification qspec = new QuerySpecification(xml, 
169
                    MetaCatUtil.getOption("saxparser"), 
170
                    MetaCatUtil.getOption("accNumberSeparator"));
171
            String query = 
172
                    qspec.printExtendedSQL(
173
                            "'obfs.45337', 'obfs.45338', 'obfs.45346'", false);
174
            assertTrue(extendedQuery.equals(query));
175
        } catch (IOException e) {
176
            fail(e.getMessage());
177
        }
178
    }
179

    
180
 
181
}
(12-12/17)