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