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: 2008-09-03 09:07:48 -0700 (Wed, 03 Sep 2008) $'
8
 * '$Revision: 4337 $'
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.FileReader;
28
import java.io.IOException;
29
import java.io.Reader;
30
import java.io.StringReader;
31
import java.util.Calendar;
32
import java.util.Date;
33
import java.util.GregorianCalendar;
34
import java.util.SimpleTimeZone;
35
import java.util.TimeZone;
36

    
37
import edu.ucsb.nceas.metacat.DocumentImpl;
38
import edu.ucsb.nceas.metacat.McdbException;
39
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
40
import edu.ucsb.nceas.metacat.client.Metacat;
41
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
42
import edu.ucsb.nceas.metacat.client.MetacatException;
43
import edu.ucsb.nceas.metacat.client.MetacatFactory;
44
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
45
import edu.ucsb.nceas.metacat.service.DatabaseService;
46
import edu.ucsb.nceas.metacat.service.PropertyService;
47
import edu.ucsb.nceas.metacat.service.ServiceException;
48
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
49
import edu.ucsb.nceas.utilities.IOUtil;
50
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
51
import junit.framework.Test;
52
import junit.framework.TestCase;
53
import junit.framework.TestSuite;
54

    
55
/**
56
 * A JUnit test for testing the indexing routines for XML Paths
57
 */
58
public class BuildIndexTest extends TestCase
59
{
60
    private static String metacatUrl;
61
    private static String username;
62
	private static String password;
63
	static {
64
		try {
65
			PropertyService.getInstance("build/tests");
66
		    metacatUrl = PropertyService.getProperty("test.metacatUrl");
67
			username = PropertyService.getProperty("test.mcUser");
68
			password = PropertyService.getProperty("test.mcPassword");
69
		} catch (PropertyNotFoundException pnfe) {
70
			System.err.println("Could not get property in static block: " 
71
					+ pnfe.getMessage());
72
		} catch (ServiceException se) {
73
			System.err.println("Could not get PropertyService instance in static block: " 
74
					+ se.getMessage());
75
		}
76
	}
77

    
78
	private String prefix = "test";
79
	private String newdocid = null;
80
	private String testfile = "test/eml-sample.xml";
81
	private String testdocument = "";
82
	private Metacat m;
83
    /* Initialize properties */
84
    static
85
    {
86
  	  try
87
  	  {
88
  		  PropertyService.getInstance("build/tests");
89
  		  MetaCatUtil.pathsForIndexing 
90
  		         = MetaCatUtil.getOptionList(PropertyService.getProperty("xml.indexPaths"));
91
  	  }
92
  	  catch(Exception e)
93
  	  {
94
  		  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
95
  	  }
96
    }
97

    
98
    /**
99
     * Constructor to build the test
100
     *
101
     * @param name the name of the test method
102
     */
103
    public BuildIndexTest(String name)
104
    {
105
        super(name);
106
        newdocid = generateDocid();
107
    }
108

    
109
    /**
110
     * Establish a testing framework by initializing appropriate objects
111
     */
112
    public void setUp()
113
    {
114
        try {
115
        	DatabaseService.getInstance();
116
        	PropertyService.getInstance("build/tests");
117
            metacatUrl = PropertyService.getProperty("test.metacatUrl");
118
        } catch (ServiceException se) {
119
            fail(se.getMessage());
120
        } catch (PropertyNotFoundException pnfe) {
121
        	fail (pnfe.getMessage());
122
        }
123

    
124
        try {
125
            FileReader fr = new FileReader(testfile);
126
            testdocument = IOUtil.getAsString(fr, true);
127
        } catch (IOException ioe) {
128
            fail("Can't read test data to run the test: " + testfile);
129
        }
130
    }
131

    
132
    /**
133
     * Release any objects after tests are complete
134
     */
135
    public void tearDown()
136
    {
137
    }
138

    
139
    /**
140
     * Create a suite of tests to be run together
141
     */
142
    public static Test suite()
143
    {
144
      TestSuite suite = new TestSuite();
145
        suite.addTest(new BuildIndexTest("initialize"));
146
        suite.addTest(new BuildIndexTest("read"));
147
        suite.addTest(new BuildIndexTest("buildIndex"));
148
        return suite;
149
    }
150

    
151
    /**
152
     * Run an initial test that always passes to check that the test
153
     * harness is working.
154
     */
155
    public void initialize()
156
    {
157
        assertTrue(1 == 1);
158
    }
159

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

    
193
    /**
194
     * Test the buildIndex() function with a known document
195
     */
196
    public void buildIndex()
197
    {
198
        DocumentImpl d = null;
199
        try {
200
            d = new DocumentImpl(newdocid+".1", false);
201
            d.buildIndex();
202
        } catch (McdbException me) {
203
            System.err.println("Caught McdbException (1): " + me.getMessage());
204
        }
205
    }
206

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

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

    
232
        return docid.toString();
233
    }
234
}
(3-3/19)