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: 2009-11-06 14:46:33 -0800 (Fri, 06 Nov 2009) $'
8
 * '$Revision: 5110 $'
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.InputStreamReader;
30
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
import edu.ucsb.nceas.MCTestCase;
39
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
import edu.ucsb.nceas.metacat.database.DatabaseService;
48
import edu.ucsb.nceas.metacat.properties.PropertyService;
49
import edu.ucsb.nceas.metacat.shared.ServiceException;
50
import edu.ucsb.nceas.metacat.util.MetacatUtil;
51
import edu.ucsb.nceas.utilities.IOUtil;
52
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
53
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
public class BuildIndexTest extends MCTestCase {
60
	private static String metacatUrl;
61
	private static String username;
62
	private static String password;
63
	static {
64
		try {
65
			PropertyService.getInstance();
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("Service problem 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

    
84
	/**
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

    
95
	/**
96
	 * Establish a testing framework by initializing appropriate objects
97
	 */
98
	public void setUp() {
99
		try {
100
			DatabaseService.getInstance();
101
//			PropertyService.getInstance("build/tests");
102
			PropertyService.getInstance();
103
			metacatUrl = PropertyService.getProperty("test.metacatUrl");
104
		} catch (ServiceException se) {
105
			fail(se.getMessage());
106
		} catch (PropertyNotFoundException pnfe) {
107
			fail(pnfe.getMessage());
108
		}
109

    
110
		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

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

    
124
	/**
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

    
135
	/**
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

    
143
	/**
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
			Reader r = new InputStreamReader(m.read(newdocid + ".1"));
155
			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

    
174
	/**
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

    
187
	/**
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

    
197
		// 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

    
211
		return docid.toString();
212
	}
213
}
(4-4/22)