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: jones $'
7
 *     '$Date: 2004-09-16 13:32:11 -0700 (Thu, 16 Sep 2004) $'
8
 * '$Revision: 2294 $'
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

    
25
package edu.ucsb.nceas.metacattest;
26

    
27
import java.io.File;
28
import java.io.FileNotFoundException;
29
import java.io.FileReader;
30
import java.io.IOException;
31
import java.io.Reader;
32
import java.io.StringReader;
33
import java.sql.SQLException;
34
import java.util.Calendar;
35
import java.util.Date;
36
import java.util.GregorianCalendar;
37
import java.util.SimpleTimeZone;
38
import java.util.TimeZone;
39

    
40
import edu.ucsb.nceas.metacat.DBConnection;
41
import edu.ucsb.nceas.metacat.DBConnectionPool;
42
import edu.ucsb.nceas.metacat.DocumentImpl;
43
import edu.ucsb.nceas.metacat.MetaCatUtil;
44
import edu.ucsb.nceas.metacat.McdbException;
45
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
46
import edu.ucsb.nceas.metacat.client.Metacat;
47
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
48
import edu.ucsb.nceas.metacat.client.MetacatException;
49
import edu.ucsb.nceas.metacat.client.MetacatFactory;
50
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
51
import edu.ucsb.nceas.utilities.IOUtil;
52
import edu.ucsb.nceas.utilities.Options;
53
import junit.framework.Test;
54
import junit.framework.TestCase;
55
import junit.framework.TestSuite;
56

    
57
/**
58
 * A JUnit test for testing the indexing routines for XML Paths
59
 */
60
public class BuildIndexTest extends TestCase
61
{
62
    private String metacatUrl = "@systemidserver@@servlet-path@";
63
    private String wrongMetacatUrl=
64
                    "http://somepalce.somewhere.com/some/servlet/metacat";
65
    private String username = "@mcuser@";
66
    private String password = "@mcpassword@";
67
    private String anotheruser = "@mcanotheruser@";
68
    private String anotherpassword = "@mcanotherpassword@";
69
    private String failpass = "uidfnkj43987yfdn";
70
    private String prefix = "test";
71
    private String newdocid = null;
72
    private String testfile = "test/jones.204.22.xml";
73
    private String onlinetestdatafile = "test/onlineDataFile1";
74
    private String queryFile = "test/query.xml";
75
    private String testdocument = "";
76
    private Metacat m;
77
    private MetaCatUtil util;
78

    
79
    /**
80
     * Constructor to build the test
81
     *
82
     * @param name the name of the test method
83
     */
84
    public BuildIndexTest(String name)
85
    {
86
        super(name);
87
        newdocid = generateDocid();
88
    }
89

    
90
    /**
91
     * Establish a testing framework by initializing appropriate objects
92
     */
93
    public void setUp()
94
    {
95
        try {
96
            //File propertyFile = new File("./lib/metacat.properties");
97
            File propertyFile = new File(
98
                "/usr/local/devtools/jakarta-tomcat/webapps/knb/WEB-INF/metacat.properties");
99
            Options options = Options.initialize(propertyFile);
100
            util = new MetaCatUtil();
101
        } catch (FileNotFoundException e) {
102
            fail(e.getMessage());
103
        } catch (IOException e) {
104
            fail(e.getMessage());
105
        }
106

    
107
        try {
108
            FileReader fr = new FileReader(testfile);
109
            testdocument = IOUtil.getAsString(fr, true);
110
        } catch (IOException ioe) {
111
            fail("Can't read test data to run the test: " + testfile);
112
        }
113
    }
114

    
115
    /**
116
     * Release any objects after tests are complete
117
     */
118
    public void tearDown()
119
    {
120
    }
121

    
122
    /**
123
     * Create a suite of tests to be run together
124
     */
125
    public static Test suite()
126
    {
127
      TestSuite suite = new TestSuite();
128
        suite.addTest(new BuildIndexTest("initialize"));
129
        //suite.addTest(new BuildIndexTest("read"));
130
        suite.addTest(new BuildIndexTest("buildIndex"));
131
        return suite;
132
    }
133

    
134
    /**
135
     * Run an initial test that always passes to check that the test
136
     * harness is working.
137
     */
138
    public void initialize()
139
    {
140
        assertTrue(1 == 1);
141
    }
142

    
143
    /**
144
     * Test the read() function with a known document
145
     */
146
    public void read()
147
    {
148
        try {
149
            System.err.println("Test Metacat: " + metacatUrl);
150
            m = MetacatFactory.createMetacatConnection(metacatUrl);
151
            String identifier = newdocid + ".1";
152
            m.login(username, password);
153
            String response = m.insert(identifier,
154
                    new StringReader(testdocument), null);
155
            System.err.println(response);
156
            Reader r = m.read(newdocid+".1");
157
            String doc = IOUtil.getAsString(r, true);
158
            doc = doc +"\n";
159
            System.err.println(doc);
160
            assertTrue(doc.equals(testdocument));
161
        } catch (MetacatInaccessibleException mie) {
162
            System.err.println("Metacat is: " + metacatUrl);
163
            fail("Metacat connection failed." + mie.getMessage());
164
        } catch (MetacatAuthException mae) {
165
            fail("Authorization failed:\n" + mae.getMessage());
166
        } catch (InsufficientKarmaException ike) {
167
            assertTrue(1 == 1);
168
            fail("Insufficient karma:\n" + ike.getMessage());
169
        } catch (MetacatException me) {
170
            fail("Metacat Error:\n" + me.getMessage());
171
        } catch (Exception e) {
172
            fail("General exception:\n" + e.getMessage());
173
        }
174
    }
175

    
176
    /**
177
     * Test the buildIndex() function with a known document
178
     */
179
    public void buildIndex()
180
    {
181
        DocumentImpl d = null;
182
        DBConnection dbconn = null;
183
        int serialNumber = -1;
184
        try {
185
            mark(MetaCatUtil.getOption("dbAdapter"));
186
            dbconn = DBConnectionPool.getDBConnection(
187
                "BuildIndexTest.buildIndex");
188
            serialNumber = dbconn.getCheckOutSerialNumber();
189
        
190
            //d = new DocumentImpl(newdocid+".1", false);
191
            d = new DocumentImpl("test.2004236121212.1", false);
192
            d.buildIndex();
193
            dbconn.close();
194
        } catch (McdbException me) {
195
            System.err.println("Caught McdbException (1): " + me.getMessage());
196
        } catch (SQLException se) {
197
            System.err.println("Caught SQLException: " + se.getMessage());
198
        } finally {
199
            // Return db connection
200
            DBConnectionPool.returnDBConnection(dbconn, serialNumber);
201
        }
202
    }
203

    
204
    private void mark(String marker) {
205
        System.err.println("MARKER: " + marker);
206
    }
207

    
208
    /**
209
     * Create a hopefully unique docid for testing insert and update. Does
210
     * not include the 'revision' part of the id.
211
     *
212
     * @return a String docid based on the current date and time
213
     */
214
    private String generateDocid()
215
    {
216
        StringBuffer docid = new StringBuffer(prefix);
217
        docid.append(".");
218

    
219
        // Create a calendar to get the date formatted properly
220
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
221
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
222
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
223
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
224
        Calendar calendar = new GregorianCalendar(pdt);
225
        Date trialTime = new Date();
226
        calendar.setTime(trialTime);
227
        docid.append(calendar.get(Calendar.YEAR));
228
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
229
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
230
        docid.append(calendar.get(Calendar.MINUTE));
231
        docid.append(calendar.get(Calendar.SECOND));
232

    
233
        return docid.toString();
234
    }
235
}
(2-2/11)