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

    
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.util.Calendar;
34
import java.util.Date;
35
import java.util.GregorianCalendar;
36
import java.util.SimpleTimeZone;
37
import java.util.TimeZone;
38

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

    
54
/**
55
 * A JUnit test for testing the indexing routines for XML Paths
56
 */
57
public class BuildIndexTest extends TestCase
58
{
59
    private String metacatUrl = "@systemidserver@@servlet-path@";
60
    private String propertyFileName = 
61
        "@install-dir@/WEB-INF/metacat.properties";
62
    private String username = "@mcuser@";
63
    private String password = "@mcpassword@";
64
    private String prefix = "test";
65
    private String newdocid = null;
66
    private String testfile = "test/eml-sample.xml";
67
    private String testdocument = "";
68
    private Metacat m;
69
    private MetaCatUtil util;
70
    /* Initialize Options*/
71
    static
72
    {
73
  	  try
74
  	  {
75
  		  Options.initialize(new File("build/tests/metacat.properties"));
76
  		  MetaCatUtil.pathsForIndexing 
77
  		         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
78
  	  }
79
  	  catch(Exception e)
80
  	  {
81
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
82
  	  }
83
    }
84

    
85
    /**
86
     * Constructor to build the test
87
     *
88
     * @param name the name of the test method
89
     */
90
    public BuildIndexTest(String name)
91
    {
92
        super(name);
93
        newdocid = generateDocid();
94
    }
95

    
96
    /**
97
     * Establish a testing framework by initializing appropriate objects
98
     */
99
    public void setUp()
100
    {
101
        try {
102
            File propertyFile = new File(propertyFileName);
103
            Options options = Options.initialize(propertyFile);
104
            util = new MetaCatUtil();
105
            metacatUrl = MetaCatUtil.getOption("junittesturl");
106
        } catch (FileNotFoundException e) {
107
            fail(e.getMessage());
108
        } catch (IOException e) {
109
            fail(e.getMessage());
110
        }
111

    
112
        try {
113
            FileReader fr = new FileReader(testfile);
114
            testdocument = IOUtil.getAsString(fr, true);
115
        } catch (IOException ioe) {
116
            fail("Can't read test data to run the test: " + testfile);
117
        }
118
    }
119

    
120
    /**
121
     * Release any objects after tests are complete
122
     */
123
    public void tearDown()
124
    {
125
    }
126

    
127
    /**
128
     * Create a suite of tests to be run together
129
     */
130
    public static Test suite()
131
    {
132
      TestSuite suite = new TestSuite();
133
        suite.addTest(new BuildIndexTest("initialize"));
134
        suite.addTest(new BuildIndexTest("read"));
135
        suite.addTest(new BuildIndexTest("buildIndex"));
136
        return suite;
137
    }
138

    
139
    /**
140
     * Run an initial test that always passes to check that the test
141
     * harness is working.
142
     */
143
    public void initialize()
144
    {
145
        assertTrue(1 == 1);
146
    }
147

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

    
181
    /**
182
     * Test the buildIndex() function with a known document
183
     */
184
    public void buildIndex()
185
    {
186
        DocumentImpl d = null;
187
        try {
188
            d = new DocumentImpl(newdocid+".1", false);
189
            d.buildIndex();
190
        } catch (McdbException me) {
191
            System.err.println("Caught McdbException (1): " + me.getMessage());
192
        }
193
    }
194

    
195
    /**
196
     * Create a hopefully unique docid for testing insert and update. Does
197
     * not include the 'revision' part of the id.
198
     *
199
     * @return a String docid based on the current date and time
200
     */
201
    private String generateDocid()
202
    {
203
        StringBuffer docid = new StringBuffer(prefix);
204
        docid.append(".");
205

    
206
        // Create a calendar to get the date formatted properly
207
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
208
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
209
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
210
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
211
        Calendar calendar = new GregorianCalendar(pdt);
212
        Date trialTime = new Date();
213
        calendar.setTime(trialTime);
214
        docid.append(calendar.get(Calendar.YEAR));
215
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
216
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
217
        docid.append(calendar.get(Calendar.MINUTE));
218
        docid.append(calendar.get(Calendar.SECOND));
219

    
220
        return docid.toString();
221
    }
222
}
(2-2/17)