Project

General

Profile

1 2294 jones
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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 4384 daigle
import edu.ucsb.nceas.MCTestCase;
38 2294 jones
import edu.ucsb.nceas.metacat.DocumentImpl;
39
import edu.ucsb.nceas.metacat.McdbException;
40
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
41
import edu.ucsb.nceas.metacat.client.Metacat;
42
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
43
import edu.ucsb.nceas.metacat.client.MetacatException;
44
import edu.ucsb.nceas.metacat.client.MetacatFactory;
45
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
46 5016 daigle
import edu.ucsb.nceas.metacat.database.DatabaseService;
47 4080 daigle
import edu.ucsb.nceas.metacat.service.PropertyService;
48 4127 daigle
import edu.ucsb.nceas.metacat.service.ServiceException;
49 4700 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
50 2294 jones
import edu.ucsb.nceas.utilities.IOUtil;
51 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
52 2294 jones
import junit.framework.Test;
53
import junit.framework.TestSuite;
54
55
/**
56
 * A JUnit test for testing the indexing routines for XML Paths
57
 */
58 4384 daigle
public class BuildIndexTest extends MCTestCase {
59
	private static String metacatUrl;
60
	private static String username;
61 4080 daigle
	private static String password;
62
	static {
63
		try {
64 4816 daigle
			PropertyService.getInstance();
65 4384 daigle
			metacatUrl = PropertyService.getProperty("test.metacatUrl");
66 4231 daigle
			username = PropertyService.getProperty("test.mcUser");
67
			password = PropertyService.getProperty("test.mcPassword");
68 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
69 4384 daigle
			System.err.println("Could not get property in static block: "
70 4080 daigle
					+ pnfe.getMessage());
71 4816 daigle
		} catch (ServiceException se) {
72
			System.err.println("Service problem in static block: "
73
					+ se.getMessage());
74 4080 daigle
		}
75
	}
76
77
	private String prefix = "test";
78
	private String newdocid = null;
79
	private String testfile = "test/eml-sample.xml";
80
	private String testdocument = "";
81
	private Metacat m;
82 2294 jones
83 4384 daigle
	/**
84
	 * Constructor to build the test
85
	 *
86
	 * @param name
87
	 *            the name of the test method
88
	 */
89
	public BuildIndexTest(String name) {
90
		super(name);
91
		newdocid = generateDocid();
92
	}
93 2294 jones
94 4384 daigle
	/**
95
	 * Establish a testing framework by initializing appropriate objects
96
	 */
97
	public void setUp() {
98
		try {
99
			DatabaseService.getInstance();
100 4719 daigle
//			PropertyService.getInstance("build/tests");
101
			PropertyService.getInstance();
102 4384 daigle
			metacatUrl = PropertyService.getProperty("test.metacatUrl");
103
		} catch (ServiceException se) {
104
			fail(se.getMessage());
105
		} catch (PropertyNotFoundException pnfe) {
106
			fail(pnfe.getMessage());
107
		}
108 2294 jones
109 4384 daigle
		try {
110
			FileReader fr = new FileReader(testfile);
111
			testdocument = IOUtil.getAsString(fr, true);
112
		} catch (IOException ioe) {
113
			fail("Can't read test data to run the test: " + testfile);
114
		}
115
	}
116 2294 jones
117 4384 daigle
	/**
118
	 * Release any objects after tests are complete
119
	 */
120
	public void tearDown() {
121
	}
122 2294 jones
123 4384 daigle
	/**
124
	 * Create a suite of tests to be run together
125
	 */
126
	public static Test suite() {
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 2294 jones
134 4384 daigle
	/**
135
	 * Run an initial test that always passes to check that the test harness is
136
	 * working.
137
	 */
138
	public void initialize() {
139
		assertTrue(1 == 1);
140
	}
141 2294 jones
142 4384 daigle
	/**
143
	 * Test the read() function with a known document
144
	 */
145
	public void read() {
146
		try {
147
			System.err.println("Test Metacat: " + metacatUrl);
148
			m = MetacatFactory.createMetacatConnection(metacatUrl);
149
			String identifier = newdocid + ".1";
150
			m.login(username, password);
151
			String response = m.insert(identifier, new StringReader(testdocument), null);
152
			System.err.println(response);
153
			Reader r = m.read(newdocid + ".1");
154
			String doc = IOUtil.getAsString(r, true);
155
			// doc = doc +"\n";
156
			System.err.println(doc);
157
			// assertTrue(doc.equals(testdocument));
158
		} catch (MetacatInaccessibleException mie) {
159
			System.err.println("Metacat is: " + metacatUrl);
160
			fail("Metacat connection failed." + mie.getMessage());
161
		} catch (MetacatAuthException mae) {
162
			fail("Authorization failed:\n" + mae.getMessage());
163
		} catch (InsufficientKarmaException ike) {
164
			assertTrue(1 == 1);
165
			fail("Insufficient karma:\n" + ike.getMessage());
166
		} catch (MetacatException me) {
167
			fail("Metacat Error:\n" + me.getMessage());
168
		} catch (Exception e) {
169
			fail("General exception:\n" + e.getMessage());
170
		}
171
	}
172 2294 jones
173 4384 daigle
	/**
174
	 * Test the buildIndex() function with a known document
175
	 */
176
	public void buildIndex() {
177
		DocumentImpl d = null;
178
		try {
179
			d = new DocumentImpl(newdocid + ".1", false);
180
			d.buildIndex();
181
		} catch (McdbException me) {
182
			System.err.println("Caught McdbException (1): " + me.getMessage());
183
		}
184
	}
185 2294 jones
186 4384 daigle
	/**
187
	 * Create a hopefully unique docid for testing insert and update. Does not
188
	 * include the 'revision' part of the id.
189
	 *
190
	 * @return a String docid based on the current date and time
191
	 */
192
	private String generateDocid() {
193
		StringBuffer docid = new StringBuffer(prefix);
194
		docid.append(".");
195 2294 jones
196 4384 daigle
		// Create a calendar to get the date formatted properly
197
		String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
198
		SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
199
		pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
200
		pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
201
		Calendar calendar = new GregorianCalendar(pdt);
202
		Date trialTime = new Date();
203
		calendar.setTime(trialTime);
204
		docid.append(calendar.get(Calendar.YEAR));
205
		docid.append(calendar.get(Calendar.DAY_OF_YEAR));
206
		docid.append(calendar.get(Calendar.HOUR_OF_DAY));
207
		docid.append(calendar.get(Calendar.MINUTE));
208
		docid.append(calendar.get(Calendar.SECOND));
209 2294 jones
210 4384 daigle
		return docid.toString();
211
	}
212 2294 jones
}