1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2003 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Purpose: To test the MetaCatURL class by JUnit
|
6
|
*
|
7
|
* '$Author: jones $'
|
8
|
* '$Date: 2003-08-12 10:55:58 -0700 (Tue, 12 Aug 2003) $'
|
9
|
* '$Revision: 1792 $'
|
10
|
*
|
11
|
* This program is free software; you can redistribute it and/or modify
|
12
|
* it under the terms of the GNU General Public License as published by
|
13
|
* the Free Software Foundation; either version 2 of the License, or
|
14
|
* (at your option) any later version.
|
15
|
*
|
16
|
* This program is distributed in the hope that it will be useful,
|
17
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
18
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
19
|
* GNU General Public License for more details.
|
20
|
*
|
21
|
* You should have received a copy of the GNU General Public License
|
22
|
* along with this program; if not, write to the Free Software
|
23
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
24
|
*/
|
25
|
|
26
|
package edu.ucsb.nceas.metacattest.client;
|
27
|
|
28
|
import edu.ucsb.nceas.metacat.client.*;
|
29
|
import edu.ucsb.nceas.utilities.IOUtil;
|
30
|
|
31
|
import java.io.FileReader;
|
32
|
import java.io.InputStreamReader;
|
33
|
import java.io.IOException;
|
34
|
import java.io.Reader;
|
35
|
import java.io.StringReader;
|
36
|
import java.io.StringWriter;
|
37
|
import java.text.DateFormat;
|
38
|
import java.util.Calendar;
|
39
|
import java.util.GregorianCalendar;
|
40
|
import java.util.Date;
|
41
|
import java.util.SimpleTimeZone;
|
42
|
import java.util.TimeZone;
|
43
|
|
44
|
import junit.framework.Test;
|
45
|
import junit.framework.TestCase;
|
46
|
import junit.framework.TestResult;
|
47
|
import junit.framework.TestSuite;
|
48
|
|
49
|
/**
|
50
|
* A JUnit test for testing Step class processing
|
51
|
*/
|
52
|
public class MetacatClientTest extends TestCase
|
53
|
{
|
54
|
private String metacatUrl =
|
55
|
"http://dev.nceas.ucsb.edu/tao/servlet/metacat";
|
56
|
//"http://dev.nceas.ucsb.edu/tao/servlet/metacat";
|
57
|
private String username = "@mcuser@";
|
58
|
private String password = "@mcpassword@";
|
59
|
private String failpass = "uidfnkj43987yfdn";
|
60
|
private String docid = "jones.204.22";
|
61
|
private String prefix = "test";
|
62
|
private String newdocid = null;
|
63
|
private String testfile = "test/jones.204.22.xml";
|
64
|
private String queryFile = "test/query.xml";
|
65
|
//private String docid = "Gramling.61.26";
|
66
|
private String testdocument = "";
|
67
|
|
68
|
private Metacat m;
|
69
|
|
70
|
/**
|
71
|
* Constructor to build the test
|
72
|
*
|
73
|
* @param name the name of the test method
|
74
|
*/
|
75
|
public MetacatClientTest(String name)
|
76
|
{
|
77
|
super(name);
|
78
|
newdocid = generateDocid();
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Establish a testing framework by initializing appropriate objects
|
83
|
*/
|
84
|
public void setUp()
|
85
|
{
|
86
|
try {
|
87
|
FileReader fr = new FileReader(testfile);
|
88
|
testdocument = IOUtil.getAsString(fr, true);
|
89
|
} catch (IOException ioe) {
|
90
|
fail("Can't read test data to run the test: " + testfile);
|
91
|
}
|
92
|
|
93
|
try {
|
94
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
95
|
} catch (MetacatInaccessibleException mie) {
|
96
|
fail("Metacat connection failed." + mie.getMessage());
|
97
|
}
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Release any objects after tests are complete
|
102
|
*/
|
103
|
public void tearDown()
|
104
|
{
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Create a suite of tests to be run together
|
109
|
*/
|
110
|
public static Test suite()
|
111
|
{
|
112
|
TestSuite suite = new TestSuite();
|
113
|
suite.addTest(new MetacatClientTest("initialize"));
|
114
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
115
|
suite.addTest(new MetacatClientTest("login"));
|
116
|
suite.addTest(new MetacatClientTest("insert"));
|
117
|
suite.addTest(new MetacatClientTest("read"));
|
118
|
suite.addTest(new MetacatClientTest("query"));
|
119
|
return suite;
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Run an initial test that always passes to check that the test
|
124
|
* harness is working.
|
125
|
*/
|
126
|
public void initialize()
|
127
|
{
|
128
|
assertTrue(1 == 1);
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Test the login() function with valid credentials
|
133
|
*/
|
134
|
public void login()
|
135
|
{
|
136
|
// Try a valid login
|
137
|
try {
|
138
|
m.login(username, password);
|
139
|
} catch (MetacatAuthException mae) {
|
140
|
fail("Authorization failed:\n" + mae.getMessage());
|
141
|
} catch (MetacatInaccessibleException mie) {
|
142
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
143
|
}
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* Test the login() function with INVALID credentials
|
148
|
*/
|
149
|
public void invalidLogin()
|
150
|
{
|
151
|
// Try an invalid login
|
152
|
try {
|
153
|
m.login(username, failpass);
|
154
|
fail("Authorization should have failed.");
|
155
|
} catch (MetacatAuthException mae) {
|
156
|
assertTrue(1 == 1);
|
157
|
} catch (MetacatInaccessibleException mie) {
|
158
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
159
|
}
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* Test the read() function with a known document
|
164
|
*/
|
165
|
public void read()
|
166
|
{
|
167
|
try {
|
168
|
m.login(username, password);
|
169
|
Reader r = m.read(newdocid+".1");
|
170
|
String doc = IOUtil.getAsString(r, true);
|
171
|
System.err.println(doc);
|
172
|
assertTrue(doc.equals(testdocument));
|
173
|
} catch (MetacatAuthException mae) {
|
174
|
fail("Authorization failed:\n" + mae.getMessage());
|
175
|
} catch (MetacatInaccessibleException mie) {
|
176
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
177
|
} catch (Exception e) {
|
178
|
fail("General exception:\n" + e.getMessage());
|
179
|
}
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Test the query() function with a known document
|
184
|
*/
|
185
|
public void query()
|
186
|
{
|
187
|
try {
|
188
|
FileReader fr = new FileReader(queryFile);
|
189
|
Reader r = m.query(fr);
|
190
|
String result = IOUtil.getAsString(r, true);
|
191
|
assertTrue(result.indexOf(newdocid+".1")!=-1);
|
192
|
} catch (MetacatInaccessibleException mie) {
|
193
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
194
|
} catch (Exception e) {
|
195
|
fail("General exception:\n" + e.getMessage());
|
196
|
}
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* Test the insert() function with a known document
|
201
|
*/
|
202
|
public void insert()
|
203
|
{
|
204
|
try {
|
205
|
String identifier = newdocid + ".1";
|
206
|
m.login(username, password);
|
207
|
String response = m.insert(identifier,
|
208
|
new StringReader(testdocument), null);
|
209
|
System.err.println(
|
210
|
DateFormat.getDateTimeInstance().format(new Date()));
|
211
|
//Thread.sleep(20000);
|
212
|
System.err.println(
|
213
|
DateFormat.getDateTimeInstance().format(new Date()));
|
214
|
assertTrue(response.indexOf("<success>") != -1);
|
215
|
assertTrue(response.indexOf(identifier) != -1);
|
216
|
System.err.println(response);
|
217
|
|
218
|
} catch (MetacatAuthException mae) {
|
219
|
fail("Authorization failed:\n" + mae.getMessage());
|
220
|
} catch (MetacatInaccessibleException mie) {
|
221
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
222
|
} catch (InsufficientKarmaException ike) {
|
223
|
fail("Insufficient karma:\n" + ike.getMessage());
|
224
|
} catch (MetacatException me) {
|
225
|
fail("Metacat Error:\n" + me.getMessage());
|
226
|
} catch (Exception e) {
|
227
|
fail("General exception:\n" + e.getMessage());
|
228
|
}
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Create a hopefully unique docid for testing insert and update. Does
|
233
|
* not include the 'revision' part of the id.
|
234
|
*
|
235
|
* @return a String docid based on the current date and time
|
236
|
*/
|
237
|
private String generateDocid()
|
238
|
{
|
239
|
StringBuffer docid = new StringBuffer(prefix);
|
240
|
docid.append(".");
|
241
|
|
242
|
// Create a calendar to get the date formatted properly
|
243
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
244
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
245
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
246
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
247
|
Calendar calendar = new GregorianCalendar(pdt);
|
248
|
Date trialTime = new Date();
|
249
|
calendar.setTime(trialTime);
|
250
|
docid.append(calendar.get(Calendar.YEAR));
|
251
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
252
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
253
|
docid.append(calendar.get(Calendar.MINUTE));
|
254
|
docid.append(calendar.get(Calendar.SECOND));
|
255
|
|
256
|
return docid.toString();
|
257
|
}
|
258
|
}
|