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-12-26 13:10:24 -0800 (Fri, 26 Dec 2008) $'
8
 * '$Revision: 4700 $'
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.MCTestCase;
38
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
import edu.ucsb.nceas.metacat.service.DatabaseService;
47
import edu.ucsb.nceas.metacat.service.PropertyService;
48
import edu.ucsb.nceas.metacat.service.ServiceException;
49
import edu.ucsb.nceas.metacat.util.MetacatUtil;
50
import edu.ucsb.nceas.utilities.IOUtil;
51
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
52
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
public class BuildIndexTest extends MCTestCase {
59
	private static String metacatUrl;
60
	private static String username;
61
	private static String password;
62
	static {
63
		try {
64
			metacatUrl = PropertyService.getProperty("test.metacatUrl");
65
			username = PropertyService.getProperty("test.mcUser");
66
			password = PropertyService.getProperty("test.mcPassword");
67
		} catch (PropertyNotFoundException pnfe) {
68
			System.err.println("Could not get property in static block: "
69
					+ pnfe.getMessage());
70
		}
71
	}
72

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

    
90
	/**
91
	 * Constructor to build the test
92
	 * 
93
	 * @param name
94
	 *            the name of the test method
95
	 */
96
	public BuildIndexTest(String name) {
97
		super(name);
98
		newdocid = generateDocid();
99
	}
100

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

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

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

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

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

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

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

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

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

    
216
		return docid.toString();
217
	}
218
}
(3-3/20)