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: berkley $'
|
8
|
* '$Date: 2006-04-19 09:17:34 -0700 (Wed, 19 Apr 2006) $'
|
9
|
* '$Revision: 2992 $'
|
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 java.io.File;
|
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.client.InsufficientKarmaException;
|
40
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
41
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
42
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
43
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
44
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
45
|
import edu.ucsb.nceas.utilities.IOUtil;
|
46
|
import junit.framework.Test;
|
47
|
import junit.framework.TestCase;
|
48
|
import junit.framework.TestSuite;
|
49
|
import java.io.FileInputStream;
|
50
|
|
51
|
/**
|
52
|
* A JUnit test for testing Step class processing
|
53
|
*/
|
54
|
public class MetacatClientTest extends TestCase
|
55
|
{
|
56
|
private String metacatUrl = "@systemidserver@@servlet-path@";
|
57
|
private String wrongMetacatUrl=
|
58
|
"http://somepalce.somewhere.com/some/servlet/metacat";
|
59
|
private String username = "@mcuser@";
|
60
|
private String password = "@mcpassword@";
|
61
|
private String anotheruser = "@mcanotheruser@";
|
62
|
private String anotherpassword = "@mcanotherpassword@";
|
63
|
private String failpass = "uidfnkj43987yfdn";
|
64
|
private String prefix = "test";
|
65
|
private String newdocid = null;
|
66
|
private String testfile = "test/jones.204.22.xml";
|
67
|
private String onlinetestdatafile = "test/onlineDataFile1";
|
68
|
private String queryFile = "test/query.xml";
|
69
|
private String testdocument = "";
|
70
|
private Metacat m;
|
71
|
|
72
|
/**
|
73
|
* Constructor to build the test
|
74
|
*
|
75
|
* @param name the name of the test method
|
76
|
*/
|
77
|
public MetacatClientTest(String name)
|
78
|
{
|
79
|
super(name);
|
80
|
newdocid = generateDocid();
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Establish a testing framework by initializing appropriate objects
|
85
|
*/
|
86
|
public void setUp()
|
87
|
{
|
88
|
try {
|
89
|
FileReader fr = new FileReader(testfile);
|
90
|
testdocument = IOUtil.getAsString(fr, true);
|
91
|
} catch (IOException ioe) {
|
92
|
fail("Can't read test data to run the test: " + testfile);
|
93
|
}
|
94
|
|
95
|
try {
|
96
|
System.err.println("Test Metacat: " + metacatUrl);
|
97
|
m = MetacatFactory.createMetacatConnection(metacatUrl);
|
98
|
} catch (MetacatInaccessibleException mie) {
|
99
|
System.err.println("Metacat is: " + metacatUrl);
|
100
|
fail("Metacat connection failed." + mie.getMessage());
|
101
|
}
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Release any objects after tests are complete
|
106
|
*/
|
107
|
public void tearDown()
|
108
|
{
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* Create a suite of tests to be run together
|
113
|
*/
|
114
|
public static Test suite()
|
115
|
{
|
116
|
TestSuite suite = new TestSuite();
|
117
|
suite.addTest(new MetacatClientTest("initialize"));
|
118
|
suite.addTest(new MetacatClientTest("invalidLogin"));
|
119
|
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
|
120
|
suite.addTest(new MetacatClientTest("login"));
|
121
|
suite.addTest(new MetacatClientTest("insert"));
|
122
|
suite.addTest(new MetacatClientTest("getNewestDocRevision"));
|
123
|
suite.addTest(new MetacatClientTest("getLastDocid"));
|
124
|
suite.addTest(new MetacatClientTest("upload"));
|
125
|
suite.addTest(new MetacatClientTest("upload_stream"));
|
126
|
suite.addTest(new MetacatClientTest("invalidRead"));
|
127
|
suite.addTest(new MetacatClientTest("read"));
|
128
|
suite.addTest(new MetacatClientTest("query"));
|
129
|
suite.addTest(new MetacatClientTest("invalidUpdate"));
|
130
|
suite.addTest(new MetacatClientTest("update"));
|
131
|
suite.addTest(new MetacatClientTest("invalidDelete"));
|
132
|
suite.addTest(new MetacatClientTest("delete"));
|
133
|
suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
|
134
|
suite.addTest(new MetacatClientTest("reuseSession"));
|
135
|
suite.addTest(new MetacatClientTest("reuseInvalidSession"));
|
136
|
return suite;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Run an initial test that always passes to check that the test
|
141
|
* harness is working.
|
142
|
*/
|
143
|
public void initialize()
|
144
|
{
|
145
|
assertTrue(1 == 1);
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Test the login() function with valid credentials
|
150
|
*/
|
151
|
public void login()
|
152
|
{
|
153
|
// Try a valid login
|
154
|
try {
|
155
|
String response = m.login(username, password);
|
156
|
//System.err.println("Login response: " + response);
|
157
|
assertTrue(response != null);
|
158
|
assertTrue(response.indexOf("<login>") != -1);
|
159
|
String sessionId = m.getSessionId();
|
160
|
//System.err.println("Session ID: " + m.getSessionId());
|
161
|
assertTrue(sessionId != null);
|
162
|
assertTrue(response.indexOf(m.getSessionId()) != -1);
|
163
|
} catch (MetacatAuthException mae) {
|
164
|
fail("Authorization failed:\n" + mae.getMessage());
|
165
|
} catch (MetacatInaccessibleException mie) {
|
166
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
167
|
}
|
168
|
}
|
169
|
|
170
|
/**
|
171
|
* Test the login() function with INVALID credentials
|
172
|
*/
|
173
|
public void invalidLogin()
|
174
|
{
|
175
|
// Try an invalid login
|
176
|
try {
|
177
|
m.login(username, failpass);
|
178
|
fail("Authorization should have failed.");
|
179
|
} catch (MetacatAuthException mae) {
|
180
|
assertTrue(1 == 1);
|
181
|
} catch (MetacatInaccessibleException mie) {
|
182
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
183
|
}
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* Test the logout() function. When logout, user will be public, it couldn't
|
188
|
* insert a document.
|
189
|
*/
|
190
|
public void logoutAndInvalidInsert()
|
191
|
{
|
192
|
try {
|
193
|
String identifier = newdocid + ".1";
|
194
|
m.login(username, password);
|
195
|
m.logout();
|
196
|
String response = m.insert(identifier,
|
197
|
new StringReader(testdocument), null);
|
198
|
//System.err.println("Response in logout: "+response);
|
199
|
assertTrue(response.indexOf("<success>") == -1);
|
200
|
} catch (MetacatAuthException mae) {
|
201
|
fail("Authorization failed:\n" + mae.getMessage());
|
202
|
} catch (MetacatInaccessibleException mie) {
|
203
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
204
|
} catch (InsufficientKarmaException ike) {
|
205
|
fail("Insufficient karma:\n" + ike.getMessage());
|
206
|
} catch (MetacatException me) {
|
207
|
if(me.getMessage().
|
208
|
indexOf("Permission denied for user public inser") == -1){
|
209
|
fail("Metacat Error:\n" + me.getMessage());
|
210
|
}
|
211
|
} catch (Exception e) {
|
212
|
fail("General exception:\n" + e.getMessage());
|
213
|
}
|
214
|
}
|
215
|
|
216
|
/**
|
217
|
* Test the read() function with a known document
|
218
|
*/
|
219
|
public void read()
|
220
|
{
|
221
|
try {
|
222
|
m.login(username, password);
|
223
|
Reader r = m.read(newdocid+".1");
|
224
|
String doc = IOUtil.getAsString(r, true);
|
225
|
doc = doc +"\n";
|
226
|
//System.err.println(doc);
|
227
|
assertTrue(doc.equals(testdocument));
|
228
|
} catch (MetacatAuthException mae) {
|
229
|
fail("Authorization failed:\n" + mae.getMessage());
|
230
|
} catch (MetacatInaccessibleException mie) {
|
231
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
232
|
} catch (Exception e) {
|
233
|
fail("General exception:\n" + e.getMessage());
|
234
|
}
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* A user try to read a document which it doesn't have read permission
|
239
|
*/
|
240
|
public void invalidRead()
|
241
|
{
|
242
|
try {
|
243
|
m.login(anotheruser, anotherpassword);
|
244
|
Reader r = m.read(newdocid+".1");
|
245
|
String doc = IOUtil.getAsString(r, true);
|
246
|
assertTrue(doc.indexOf("<error>") != -1);
|
247
|
//System.err.println(doc);
|
248
|
} catch (MetacatAuthException mae) {
|
249
|
fail("Authorization failed:\n" + mae.getMessage());
|
250
|
} catch (MetacatInaccessibleException mie) {
|
251
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
252
|
} catch (InsufficientKarmaException ike) {
|
253
|
assertTrue(1 == 1);
|
254
|
} catch (MetacatException e) {
|
255
|
fail("Metacat exception:\n" + e.getMessage());
|
256
|
} catch (IOException ioe) {
|
257
|
fail("IO exception:\n" + ioe.getMessage());
|
258
|
}
|
259
|
}
|
260
|
|
261
|
/**
|
262
|
* Test the query() function with a known document
|
263
|
*/
|
264
|
public void query()
|
265
|
{
|
266
|
try {
|
267
|
m.login(username,password);
|
268
|
FileReader fr = new FileReader(queryFile);
|
269
|
Reader r = m.query(fr);
|
270
|
//System.err.println("Starting query...");
|
271
|
String result = IOUtil.getAsString(r, true);
|
272
|
//System.err.println("Query result:\n" + result);
|
273
|
assertTrue(result.indexOf(newdocid+".1")!=-1);
|
274
|
} catch (MetacatAuthException mae) {
|
275
|
fail("Authorization failed:\n" + mae.getMessage());
|
276
|
} catch (MetacatInaccessibleException mie) {
|
277
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
278
|
} catch (Exception e) {
|
279
|
fail("General exception:\n" + e.getMessage());
|
280
|
}
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Test the insert() function with a known document
|
285
|
*/
|
286
|
public void insert()
|
287
|
{
|
288
|
try {
|
289
|
String identifier = newdocid + ".1";
|
290
|
m.login(username, password);
|
291
|
String response = m.insert(identifier,
|
292
|
new StringReader(testdocument), null);
|
293
|
assertTrue(response.indexOf("<success>") != -1);
|
294
|
assertTrue(response.indexOf(identifier) != -1);
|
295
|
//System.err.println(response);
|
296
|
|
297
|
} catch (MetacatAuthException mae) {
|
298
|
fail("Authorization failed:\n" + mae.getMessage());
|
299
|
} catch (MetacatInaccessibleException mie) {
|
300
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
301
|
} catch (InsufficientKarmaException ike) {
|
302
|
assertTrue(1 == 1);
|
303
|
fail("Insufficient karma:\n" + ike.getMessage());
|
304
|
} catch (MetacatException me) {
|
305
|
fail("Metacat Error:\n" + me.getMessage());
|
306
|
} catch (Exception e) {
|
307
|
fail("General exception:\n" + e.getMessage());
|
308
|
}
|
309
|
}
|
310
|
|
311
|
/**
|
312
|
* Test the upload() function with a known document
|
313
|
*/
|
314
|
public void upload()
|
315
|
{
|
316
|
try {
|
317
|
newdocid = generateDocid();
|
318
|
String identifier = newdocid + ".1";
|
319
|
m.login(username, password);
|
320
|
String response = m.upload(identifier,
|
321
|
new File(onlinetestdatafile));
|
322
|
assertTrue(response.indexOf("<success>") != -1);
|
323
|
assertTrue(response.indexOf(identifier) != -1);
|
324
|
//System.err.println(response);
|
325
|
|
326
|
} catch (MetacatAuthException mae) {
|
327
|
fail("Authorization failed:\n" + mae.getMessage());
|
328
|
} catch (MetacatInaccessibleException mie) {
|
329
|
mie.printStackTrace();
|
330
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
331
|
} catch (InsufficientKarmaException ike) {
|
332
|
assertTrue(1 == 1);
|
333
|
fail("Insufficient karma:\n" + ike.getMessage());
|
334
|
} catch (MetacatException me) {
|
335
|
fail("Metacat Error:\n" + me.getMessage());
|
336
|
} catch (Exception e) {
|
337
|
fail("General exception:\n" + e.getMessage());
|
338
|
}
|
339
|
}
|
340
|
|
341
|
/**
|
342
|
* Test the upload() function by passing an InputStream
|
343
|
*/
|
344
|
public void upload_stream()
|
345
|
{
|
346
|
try {
|
347
|
newdocid = generateDocid();
|
348
|
String identifier = newdocid + ".1";
|
349
|
m.login(username, password);
|
350
|
File testFile = new File(onlinetestdatafile);
|
351
|
String response = m.upload(identifier, "onlineDataFile1",
|
352
|
new FileInputStream(testFile),
|
353
|
(int) testFile.length());
|
354
|
|
355
|
assertTrue(response.indexOf("<success>") != -1);
|
356
|
assertTrue(response.indexOf(identifier) != -1);
|
357
|
//System.err.println(response);
|
358
|
|
359
|
} catch (MetacatAuthException mae) {
|
360
|
fail("Authorization failed:\n" + mae.getMessage());
|
361
|
} catch (MetacatInaccessibleException mie) {
|
362
|
mie.printStackTrace();
|
363
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
364
|
} catch (InsufficientKarmaException ike) {
|
365
|
assertTrue(1 == 1);
|
366
|
fail("Insufficient karma:\n" + ike.getMessage());
|
367
|
} catch (MetacatException me) {
|
368
|
fail("Metacat Error:\n" + me.getMessage());
|
369
|
} catch (Exception e) {
|
370
|
fail("General exception:\n" + e.getMessage());
|
371
|
}
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Test the invalidUpdate() function. A user try to update a document
|
376
|
* which it doesn't have permission
|
377
|
*/
|
378
|
public void invalidUpdate()
|
379
|
{
|
380
|
try {
|
381
|
String identifier = newdocid + ".2";
|
382
|
m.login(anotheruser, anotherpassword);
|
383
|
String response = m.update(identifier,
|
384
|
new StringReader(testdocument), null);
|
385
|
assertTrue(response.indexOf("<success>") == -1);
|
386
|
|
387
|
} catch (MetacatAuthException mae) {
|
388
|
fail("Authorization failed:\n" + mae.getMessage());
|
389
|
} catch (MetacatInaccessibleException mie) {
|
390
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
391
|
} catch (InsufficientKarmaException ike) {
|
392
|
assertTrue(1 == 1);
|
393
|
} catch (MetacatException me) {
|
394
|
fail("Metacat Error:\n" + me.getMessage());
|
395
|
} catch (Exception e) {
|
396
|
fail("General exception:\n" + e.getMessage());
|
397
|
}
|
398
|
}
|
399
|
|
400
|
/**
|
401
|
* Test the update() function with a known document
|
402
|
*/
|
403
|
public void update()
|
404
|
{
|
405
|
try {
|
406
|
String identifier = newdocid + ".2";
|
407
|
m.login(username, password);
|
408
|
String response = m.update(identifier,
|
409
|
new StringReader(testdocument), null);
|
410
|
assertTrue(response.indexOf("<success>") != -1);
|
411
|
assertTrue(response.indexOf(identifier) != -1);
|
412
|
//System.err.println(response);
|
413
|
|
414
|
} catch (MetacatAuthException mae) {
|
415
|
fail("Authorization failed:\n" + mae.getMessage());
|
416
|
} catch (MetacatInaccessibleException mie) {
|
417
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
418
|
} catch (InsufficientKarmaException ike) {
|
419
|
fail("Insufficient karma:\n" + ike.getMessage());
|
420
|
} catch (MetacatException me) {
|
421
|
fail("Metacat Error:\n" + me.getMessage());
|
422
|
} catch (Exception e) {
|
423
|
fail("General exception:\n" + e.getMessage());
|
424
|
}
|
425
|
}
|
426
|
|
427
|
/**
|
428
|
* A user try delete a document which it doesn't have permission
|
429
|
*/
|
430
|
public void invalidDelete()
|
431
|
{
|
432
|
try {
|
433
|
String identifier = newdocid + ".2";
|
434
|
m.login(anotheruser, anotherpassword);
|
435
|
String response = m.delete(identifier);
|
436
|
assertTrue(response.indexOf("<success>") == -1);
|
437
|
//System.err.println(response);
|
438
|
|
439
|
} catch (MetacatAuthException mae) {
|
440
|
fail("Authorization failed:\n" + mae.getMessage());
|
441
|
} catch (MetacatInaccessibleException mie) {
|
442
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
443
|
} catch (InsufficientKarmaException ike) {
|
444
|
assertTrue(1 == 1);
|
445
|
} catch (MetacatException me) {
|
446
|
assertTrue(1 == 1);
|
447
|
} catch (Exception e) {
|
448
|
fail("General exception:\n" + e.getMessage());
|
449
|
}
|
450
|
}
|
451
|
|
452
|
/**
|
453
|
* Test the delete() function with a known document
|
454
|
*/
|
455
|
public void delete()
|
456
|
{
|
457
|
try {
|
458
|
String identifier = newdocid + ".2";
|
459
|
m.login(username, password);
|
460
|
String response = m.delete(identifier);
|
461
|
assertTrue(response.indexOf("<success>") != -1);
|
462
|
//System.err.println(response);
|
463
|
|
464
|
} catch (MetacatAuthException mae) {
|
465
|
fail("Authorization failed:\n" + mae.getMessage());
|
466
|
} catch (MetacatInaccessibleException mie) {
|
467
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
468
|
} catch (InsufficientKarmaException ike) {
|
469
|
fail("Insufficient karma:\n" + ike.getMessage());
|
470
|
} catch (MetacatException me) {
|
471
|
fail("Metacat Error:\n" + me.getMessage());
|
472
|
} catch (Exception e) {
|
473
|
fail("General exception:\n" + e.getMessage());
|
474
|
}
|
475
|
}
|
476
|
|
477
|
/**
|
478
|
* Test the to connect a wrong metacat url. Create a new metacat with a
|
479
|
* inaccessible url. Then try to login it. If get a MetacatInaccessible
|
480
|
* exception, this is right.
|
481
|
*/
|
482
|
public void inaccessibleMetacat()
|
483
|
{
|
484
|
Metacat mWrong = null;
|
485
|
try {
|
486
|
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
|
487
|
} catch (MetacatInaccessibleException mie) {
|
488
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
489
|
}
|
490
|
|
491
|
try {
|
492
|
mWrong.login(username, password);
|
493
|
} catch (MetacatInaccessibleException mie) {
|
494
|
assertTrue(1 == 1);
|
495
|
} catch (MetacatAuthException mae) {
|
496
|
fail("Authorization failed:\n" + mae.getMessage());
|
497
|
}
|
498
|
}
|
499
|
|
500
|
/**
|
501
|
* Try to perform an action that requires a session to be valid without
|
502
|
* having logged in first by setting the sessionId from a previous
|
503
|
* session. Then insert a document.
|
504
|
*/
|
505
|
public void reuseSession()
|
506
|
{
|
507
|
String oldSessionId = "";
|
508
|
try {
|
509
|
Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
|
510
|
String response = mtemp.login(username, password);
|
511
|
oldSessionId = mtemp.getSessionId();
|
512
|
//System.err.println("SessionId (mtemp): " + oldSessionId);
|
513
|
} catch (MetacatAuthException mae) {
|
514
|
fail("Authorization failed:\n" + mae.getMessage());
|
515
|
} catch (MetacatInaccessibleException mie) {
|
516
|
System.err.println("Metacat is: " + metacatUrl);
|
517
|
fail("Metacat connection failed." + mie.getMessage());
|
518
|
}
|
519
|
|
520
|
try {
|
521
|
String identifier = generateDocid() + ".1";
|
522
|
Metacat m2 = null;
|
523
|
try {
|
524
|
m2 = MetacatFactory.createMetacatConnection(metacatUrl);
|
525
|
} catch (MetacatInaccessibleException mie) {
|
526
|
System.err.println("Metacat is: " + metacatUrl);
|
527
|
fail("Metacat connection failed." + mie.getMessage());
|
528
|
}
|
529
|
System.err.println("SessionId (m2): " + m2.getSessionId());
|
530
|
m2.setSessionId(oldSessionId);
|
531
|
System.err.println("SessionId (m2 after set): " + m2.getSessionId());
|
532
|
String response = m2.insert(identifier,
|
533
|
new StringReader(testdocument), null);
|
534
|
System.err.println("Reuse Insert response: " + response);
|
535
|
assertTrue(response.indexOf("<success>") != -1);
|
536
|
} catch (MetacatInaccessibleException mie) {
|
537
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
538
|
} catch (InsufficientKarmaException ike) {
|
539
|
fail("Insufficient karma:\n" + ike.getMessage());
|
540
|
} catch (MetacatException me) {
|
541
|
fail("Metacat Error:\n" + me.getMessage());
|
542
|
} catch (Exception e) {
|
543
|
fail("General exception:\n" + e.getMessage());
|
544
|
}
|
545
|
}
|
546
|
|
547
|
/**
|
548
|
* Try to perform an action that requires a session to be valid without
|
549
|
* having logged in first, but use an invalid session identifier.
|
550
|
* Then insert a document, which should fail.
|
551
|
*/
|
552
|
public void reuseInvalidSession()
|
553
|
{
|
554
|
System.err.println("Starting resuseInvalidSession test...");
|
555
|
String oldSessionId = "foobar";
|
556
|
try {
|
557
|
String identifier = generateDocid() + ".1";
|
558
|
Metacat m3 = null;
|
559
|
try {
|
560
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
561
|
} catch (MetacatInaccessibleException mie) {
|
562
|
System.err.println("Metacat is: " + metacatUrl);
|
563
|
fail("Metacat connection failed." + mie.getMessage());
|
564
|
}
|
565
|
System.err.println("SessionId (m3): " + m3.getSessionId());
|
566
|
m3.setSessionId(oldSessionId);
|
567
|
System.err.println("SessionId (m3 after set): " + m3.getSessionId());
|
568
|
System.err.println("Performing resuseInvalidSession insert: " +
|
569
|
identifier);
|
570
|
String response = m3.insert(identifier,
|
571
|
new StringReader(testdocument), null);
|
572
|
System.err.println("ReuseInvalid Insert response: " + response);
|
573
|
assertTrue(response.indexOf("<success>") == -1);
|
574
|
} catch (MetacatInaccessibleException mie) {
|
575
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
576
|
} catch (InsufficientKarmaException ike) {
|
577
|
fail("Insufficient karma:\n" + ike.getMessage());
|
578
|
} catch (MetacatException me) {
|
579
|
if(me.getMessage().
|
580
|
indexOf("Permission denied for user public inser") == -1){
|
581
|
fail("Metacat Error:\n" + me.getMessage());
|
582
|
}
|
583
|
} catch (Exception e) {
|
584
|
fail("General exception:\n" + e.getMessage());
|
585
|
}
|
586
|
}
|
587
|
|
588
|
/**
|
589
|
* Get the most recent document id for a given scope and be sure
|
590
|
* that it matches the one we last inserted. Assumes this test is run
|
591
|
* immediately following a successful insert() test.
|
592
|
*/
|
593
|
public void getLastDocid()
|
594
|
{
|
595
|
try {
|
596
|
Metacat m3 = null;
|
597
|
try {
|
598
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
599
|
} catch (MetacatInaccessibleException mie) {
|
600
|
System.err.println("Metacat is: " + metacatUrl);
|
601
|
fail("Metacat connection failed." + mie.getMessage());
|
602
|
}
|
603
|
String lastId = m3.getLastDocid(prefix);
|
604
|
System.err.println("Last Id: " + lastId);
|
605
|
assertTrue(lastId.equals(newdocid + ".1"));
|
606
|
} catch (MetacatException me) {
|
607
|
fail("Metacat Error:\n" + me.getMessage());
|
608
|
} catch (Exception e) {
|
609
|
fail("General exception:\n" + e.getMessage());
|
610
|
}
|
611
|
}
|
612
|
|
613
|
/**
|
614
|
* Try to perform an action that requires a session to be valid without
|
615
|
* having logged in first, but use an invalid session identifier.
|
616
|
* Then insert a document, which should fail.
|
617
|
*/
|
618
|
public void getNewestDocRevision()
|
619
|
{
|
620
|
//System.err.println("Starting getNewestDocRevision test...");
|
621
|
try {
|
622
|
|
623
|
Metacat m3 = null;
|
624
|
try {
|
625
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
626
|
} catch (MetacatInaccessibleException mie) {
|
627
|
System.err.println("Metacat is: " + metacatUrl);
|
628
|
fail("Metacat connection failed." + mie.getMessage());
|
629
|
}
|
630
|
|
631
|
int revision = m3.getNewestDocRevision(newdocid);
|
632
|
//System.err.println("Newest revision number is: " + revision);
|
633
|
assertTrue(revision == 1);
|
634
|
} catch (MetacatException me) {
|
635
|
if(me.getMessage().
|
636
|
indexOf("Permission denied for user public inser") == -1){
|
637
|
fail("Metacat Error:\n" + me.getMessage());
|
638
|
}
|
639
|
} catch (Exception e) {
|
640
|
fail("General exception:\n" + e.getMessage());
|
641
|
}
|
642
|
}
|
643
|
|
644
|
/**
|
645
|
* Create a hopefully unique docid for testing insert and update. Does
|
646
|
* not include the 'revision' part of the id.
|
647
|
*
|
648
|
* @return a String docid based on the current date and time
|
649
|
*/
|
650
|
private String generateDocid()
|
651
|
{
|
652
|
StringBuffer docid = new StringBuffer(prefix);
|
653
|
docid.append(".");
|
654
|
|
655
|
// Create a calendar to get the date formatted properly
|
656
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
657
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
658
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
659
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
660
|
Calendar calendar = new GregorianCalendar(pdt);
|
661
|
Date trialTime = new Date();
|
662
|
calendar.setTime(trialTime);
|
663
|
|
664
|
int time = 0;
|
665
|
|
666
|
docid.append(calendar.get(Calendar.YEAR));
|
667
|
|
668
|
time = calendar.get(Calendar.DAY_OF_YEAR);
|
669
|
if(time < 10){
|
670
|
docid.append("0");
|
671
|
docid.append("0");
|
672
|
docid.append(time);
|
673
|
} else if(time < 100) {
|
674
|
docid.append("0");
|
675
|
docid.append(time);
|
676
|
} else {
|
677
|
docid.append(time);
|
678
|
}
|
679
|
|
680
|
time = calendar.get(Calendar.HOUR_OF_DAY);
|
681
|
if(time < 10){
|
682
|
docid.append("0");
|
683
|
docid.append(time);
|
684
|
} else {
|
685
|
docid.append(time);
|
686
|
}
|
687
|
|
688
|
time = calendar.get(Calendar.MINUTE);
|
689
|
if(time < 10){
|
690
|
docid.append("0");
|
691
|
docid.append(time);
|
692
|
} else {
|
693
|
docid.append(time);
|
694
|
}
|
695
|
|
696
|
time = calendar.get(Calendar.SECOND);
|
697
|
if(time < 10){
|
698
|
docid.append("0");
|
699
|
docid.append(time);
|
700
|
} else {
|
701
|
docid.append(time);
|
702
|
}
|
703
|
|
704
|
return docid.toString();
|
705
|
}
|
706
|
}
|