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