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.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 |
|
|
private String metacatUrl = "@systemidserver@@servlet-path@";
|
60 |
2315
|
jones
|
private String propertyFileName =
|
61 |
|
|
"@install-dir@/WEB-INF/metacat.properties";
|
62 |
2294
|
jones
|
private String username = "@mcuser@";
|
63 |
|
|
private String password = "@mcpassword@";
|
64 |
|
|
private String prefix = "test";
|
65 |
|
|
private String newdocid = null;
|
66 |
2315
|
jones
|
private String testfile = "test/eml-sample.xml";
|
67 |
2294
|
jones
|
private String testdocument = "";
|
68 |
|
|
private Metacat m;
|
69 |
|
|
private MetaCatUtil util;
|
70 |
|
|
|
71 |
|
|
/**
|
72 |
|
|
* Constructor to build the test
|
73 |
|
|
*
|
74 |
|
|
* @param name the name of the test method
|
75 |
|
|
*/
|
76 |
|
|
public BuildIndexTest(String name)
|
77 |
|
|
{
|
78 |
|
|
super(name);
|
79 |
|
|
newdocid = generateDocid();
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Establish a testing framework by initializing appropriate objects
|
84 |
|
|
*/
|
85 |
|
|
public void setUp()
|
86 |
|
|
{
|
87 |
|
|
try {
|
88 |
2315
|
jones
|
File propertyFile = new File(propertyFileName);
|
89 |
2294
|
jones
|
Options options = Options.initialize(propertyFile);
|
90 |
|
|
util = new MetaCatUtil();
|
91 |
2306
|
jones
|
metacatUrl = MetaCatUtil.getOption("junittesturl");
|
92 |
2294
|
jones
|
} catch (FileNotFoundException e) {
|
93 |
|
|
fail(e.getMessage());
|
94 |
|
|
} catch (IOException e) {
|
95 |
|
|
fail(e.getMessage());
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
try {
|
99 |
|
|
FileReader fr = new FileReader(testfile);
|
100 |
|
|
testdocument = IOUtil.getAsString(fr, true);
|
101 |
|
|
} catch (IOException ioe) {
|
102 |
|
|
fail("Can't read test data to run the test: " + testfile);
|
103 |
|
|
}
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Release any objects after tests are complete
|
108 |
|
|
*/
|
109 |
|
|
public void tearDown()
|
110 |
|
|
{
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
|
|
* Create a suite of tests to be run together
|
115 |
|
|
*/
|
116 |
|
|
public static Test suite()
|
117 |
|
|
{
|
118 |
|
|
TestSuite suite = new TestSuite();
|
119 |
|
|
suite.addTest(new BuildIndexTest("initialize"));
|
120 |
2306
|
jones
|
suite.addTest(new BuildIndexTest("read"));
|
121 |
2315
|
jones
|
suite.addTest(new BuildIndexTest("buildIndex"));
|
122 |
2294
|
jones
|
return suite;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* Run an initial test that always passes to check that the test
|
127 |
|
|
* harness is working.
|
128 |
|
|
*/
|
129 |
|
|
public void initialize()
|
130 |
|
|
{
|
131 |
|
|
assertTrue(1 == 1);
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Test the read() function with a known document
|
136 |
|
|
*/
|
137 |
|
|
public void read()
|
138 |
|
|
{
|
139 |
|
|
try {
|
140 |
|
|
System.err.println("Test Metacat: " + metacatUrl);
|
141 |
|
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
142 |
|
|
String identifier = newdocid + ".1";
|
143 |
|
|
m.login(username, password);
|
144 |
|
|
String response = m.insert(identifier,
|
145 |
|
|
new StringReader(testdocument), null);
|
146 |
|
|
System.err.println(response);
|
147 |
|
|
Reader r = m.read(newdocid+".1");
|
148 |
|
|
String doc = IOUtil.getAsString(r, true);
|
149 |
2315
|
jones
|
//doc = doc +"\n";
|
150 |
2294
|
jones
|
System.err.println(doc);
|
151 |
2315
|
jones
|
//assertTrue(doc.equals(testdocument));
|
152 |
2294
|
jones
|
} catch (MetacatInaccessibleException mie) {
|
153 |
|
|
System.err.println("Metacat is: " + metacatUrl);
|
154 |
|
|
fail("Metacat connection failed." + mie.getMessage());
|
155 |
|
|
} catch (MetacatAuthException mae) {
|
156 |
|
|
fail("Authorization failed:\n" + mae.getMessage());
|
157 |
|
|
} catch (InsufficientKarmaException ike) {
|
158 |
|
|
assertTrue(1 == 1);
|
159 |
|
|
fail("Insufficient karma:\n" + ike.getMessage());
|
160 |
|
|
} catch (MetacatException me) {
|
161 |
|
|
fail("Metacat Error:\n" + me.getMessage());
|
162 |
|
|
} catch (Exception e) {
|
163 |
|
|
fail("General exception:\n" + e.getMessage());
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
/**
|
168 |
|
|
* Test the buildIndex() function with a known document
|
169 |
|
|
*/
|
170 |
|
|
public void buildIndex()
|
171 |
|
|
{
|
172 |
|
|
DocumentImpl d = null;
|
173 |
|
|
try {
|
174 |
2315
|
jones
|
d = new DocumentImpl(newdocid+".1", false);
|
175 |
2294
|
jones
|
d.buildIndex();
|
176 |
|
|
} catch (McdbException me) {
|
177 |
|
|
System.err.println("Caught McdbException (1): " + me.getMessage());
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
/**
|
182 |
|
|
* Create a hopefully unique docid for testing insert and update. Does
|
183 |
|
|
* not include the 'revision' part of the id.
|
184 |
|
|
*
|
185 |
|
|
* @return a String docid based on the current date and time
|
186 |
|
|
*/
|
187 |
|
|
private String generateDocid()
|
188 |
|
|
{
|
189 |
|
|
StringBuffer docid = new StringBuffer(prefix);
|
190 |
|
|
docid.append(".");
|
191 |
|
|
|
192 |
|
|
// Create a calendar to get the date formatted properly
|
193 |
|
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
194 |
|
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
195 |
|
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
196 |
|
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
197 |
|
|
Calendar calendar = new GregorianCalendar(pdt);
|
198 |
|
|
Date trialTime = new Date();
|
199 |
|
|
calendar.setTime(trialTime);
|
200 |
|
|
docid.append(calendar.get(Calendar.YEAR));
|
201 |
|
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
202 |
|
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
203 |
|
|
docid.append(calendar.get(Calendar.MINUTE));
|
204 |
|
|
docid.append(calendar.get(Calendar.SECOND));
|
205 |
|
|
|
206 |
|
|
return docid.toString();
|
207 |
|
|
}
|
208 |
|
|
}
|