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: tao $'
|
8
|
* '$Date: 2004-12-16 16:35:23 -0800 (Thu, 16 Dec 2004) $'
|
9
|
* '$Revision: 2338 $'
|
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("upload"));
|
124
|
suite.addTest(new MetacatClientTest("upload_stream"));
|
125
|
suite.addTest(new MetacatClientTest("invalidRead"));
|
126
|
suite.addTest(new MetacatClientTest("read"));
|
127
|
suite.addTest(new MetacatClientTest("query"));
|
128
|
suite.addTest(new MetacatClientTest("invalidUpdate"));
|
129
|
suite.addTest(new MetacatClientTest("update"));
|
130
|
suite.addTest(new MetacatClientTest("invalidDelete"));
|
131
|
suite.addTest(new MetacatClientTest("delete"));
|
132
|
suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
|
133
|
suite.addTest(new MetacatClientTest("reuseSession"));
|
134
|
suite.addTest(new MetacatClientTest("reuseInvalidSession"));
|
135
|
return suite;
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Run an initial test that always passes to check that the test
|
140
|
* harness is working.
|
141
|
*/
|
142
|
public void initialize()
|
143
|
{
|
144
|
assertTrue(1 == 1);
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* Test the login() function with valid credentials
|
149
|
*/
|
150
|
public void login()
|
151
|
{
|
152
|
// Try a valid login
|
153
|
try {
|
154
|
String response = m.login(username, password);
|
155
|
System.err.println("Login response: " + response);
|
156
|
assertTrue(response != null);
|
157
|
assertTrue(response.indexOf("<login>") != -1);
|
158
|
String sessionId = m.getSessionId();
|
159
|
System.err.println("Session ID: " + m.getSessionId());
|
160
|
assertTrue(sessionId != null);
|
161
|
assertTrue(response.indexOf(m.getSessionId()) != -1);
|
162
|
} catch (MetacatAuthException mae) {
|
163
|
fail("Authorization failed:\n" + mae.getMessage());
|
164
|
} catch (MetacatInaccessibleException mie) {
|
165
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
166
|
}
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Test the login() function with INVALID credentials
|
171
|
*/
|
172
|
public void invalidLogin()
|
173
|
{
|
174
|
// Try an invalid login
|
175
|
try {
|
176
|
m.login(username, failpass);
|
177
|
fail("Authorization should have failed.");
|
178
|
} catch (MetacatAuthException mae) {
|
179
|
assertTrue(1 == 1);
|
180
|
} catch (MetacatInaccessibleException mie) {
|
181
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
182
|
}
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Test the logout() function. When logout, user will be public, it couldn't
|
187
|
* insert a document.
|
188
|
*/
|
189
|
public void logoutAndInvalidInsert()
|
190
|
{
|
191
|
try {
|
192
|
String identifier = newdocid + ".1";
|
193
|
m.login(username, password);
|
194
|
m.logout();
|
195
|
String response = m.insert(identifier,
|
196
|
new StringReader(testdocument), null);
|
197
|
System.err.println("Response in logout: "+response);
|
198
|
assertTrue(response.indexOf("<success>") == -1);
|
199
|
} catch (MetacatAuthException mae) {
|
200
|
fail("Authorization failed:\n" + mae.getMessage());
|
201
|
} catch (MetacatInaccessibleException mie) {
|
202
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
203
|
} catch (InsufficientKarmaException ike) {
|
204
|
fail("Insufficient karma:\n" + ike.getMessage());
|
205
|
} catch (MetacatException me) {
|
206
|
if(me.getMessage().
|
207
|
indexOf("Permission denied for user public inser") == -1){
|
208
|
fail("Metacat Error:\n" + me.getMessage());
|
209
|
}
|
210
|
} catch (Exception e) {
|
211
|
fail("General exception:\n" + e.getMessage());
|
212
|
}
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Test the read() function with a known document
|
217
|
*/
|
218
|
public void read()
|
219
|
{
|
220
|
try {
|
221
|
m.login(username, password);
|
222
|
Reader r = m.read(newdocid+".1");
|
223
|
String doc = IOUtil.getAsString(r, true);
|
224
|
doc = doc +"\n";
|
225
|
System.err.println(doc);
|
226
|
assertTrue(doc.equals(testdocument));
|
227
|
} catch (MetacatAuthException mae) {
|
228
|
fail("Authorization failed:\n" + mae.getMessage());
|
229
|
} catch (MetacatInaccessibleException mie) {
|
230
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
231
|
} catch (Exception e) {
|
232
|
fail("General exception:\n" + e.getMessage());
|
233
|
}
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* A user try to read a document which it doesn't have read permission
|
238
|
*/
|
239
|
public void invalidRead()
|
240
|
{
|
241
|
try {
|
242
|
m.login(anotheruser, anotherpassword);
|
243
|
Reader r = m.read(newdocid+".1");
|
244
|
String doc = IOUtil.getAsString(r, true);
|
245
|
assertTrue(doc.indexOf("<error>") != -1);
|
246
|
System.err.println(doc);
|
247
|
} catch (MetacatAuthException mae) {
|
248
|
fail("Authorization failed:\n" + mae.getMessage());
|
249
|
} catch (MetacatInaccessibleException mie) {
|
250
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
251
|
} catch (InsufficientKarmaException ike) {
|
252
|
assertTrue(1 == 1);
|
253
|
} catch (MetacatException e) {
|
254
|
fail("Metacat exception:\n" + e.getMessage());
|
255
|
} catch (IOException ioe) {
|
256
|
fail("IO exception:\n" + ioe.getMessage());
|
257
|
}
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* Test the query() function with a known document
|
262
|
*/
|
263
|
public void query()
|
264
|
{
|
265
|
try {
|
266
|
m.login(username,password);
|
267
|
FileReader fr = new FileReader(queryFile);
|
268
|
Reader r = m.query(fr);
|
269
|
System.err.println("Starting query...");
|
270
|
String result = IOUtil.getAsString(r, true);
|
271
|
System.err.println("Query result:\n" + result);
|
272
|
assertTrue(result.indexOf(newdocid+".1")!=-1);
|
273
|
} catch (MetacatAuthException mae) {
|
274
|
fail("Authorization failed:\n" + mae.getMessage());
|
275
|
} catch (MetacatInaccessibleException mie) {
|
276
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
277
|
} catch (Exception e) {
|
278
|
fail("General exception:\n" + e.getMessage());
|
279
|
}
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* Test the insert() function with a known document
|
284
|
*/
|
285
|
public void insert()
|
286
|
{
|
287
|
try {
|
288
|
String identifier = newdocid + ".1";
|
289
|
m.login(username, password);
|
290
|
String response = m.insert(identifier,
|
291
|
new StringReader(testdocument), null);
|
292
|
assertTrue(response.indexOf("<success>") != -1);
|
293
|
assertTrue(response.indexOf(identifier) != -1);
|
294
|
System.err.println(response);
|
295
|
|
296
|
} catch (MetacatAuthException mae) {
|
297
|
fail("Authorization failed:\n" + mae.getMessage());
|
298
|
} catch (MetacatInaccessibleException mie) {
|
299
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
300
|
} catch (InsufficientKarmaException ike) {
|
301
|
assertTrue(1 == 1);
|
302
|
fail("Insufficient karma:\n" + ike.getMessage());
|
303
|
} catch (MetacatException me) {
|
304
|
fail("Metacat Error:\n" + me.getMessage());
|
305
|
} catch (Exception e) {
|
306
|
fail("General exception:\n" + e.getMessage());
|
307
|
}
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* Test the upload() function with a known document
|
312
|
*/
|
313
|
public void upload()
|
314
|
{
|
315
|
try {
|
316
|
newdocid = generateDocid();
|
317
|
String identifier = newdocid + ".1";
|
318
|
m.login(username, password);
|
319
|
String response = m.upload(identifier,
|
320
|
new File(onlinetestdatafile));
|
321
|
assertTrue(response.indexOf("<success>") != -1);
|
322
|
assertTrue(response.indexOf(identifier) != -1);
|
323
|
System.err.println(response);
|
324
|
|
325
|
} catch (MetacatAuthException mae) {
|
326
|
fail("Authorization failed:\n" + mae.getMessage());
|
327
|
} catch (MetacatInaccessibleException mie) {
|
328
|
mie.printStackTrace();
|
329
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
330
|
} catch (InsufficientKarmaException ike) {
|
331
|
assertTrue(1 == 1);
|
332
|
fail("Insufficient karma:\n" + ike.getMessage());
|
333
|
} catch (MetacatException me) {
|
334
|
fail("Metacat Error:\n" + me.getMessage());
|
335
|
} catch (Exception e) {
|
336
|
fail("General exception:\n" + e.getMessage());
|
337
|
}
|
338
|
}
|
339
|
|
340
|
/**
|
341
|
* Test the upload() function by passing an InputStream
|
342
|
*/
|
343
|
public void upload_stream()
|
344
|
{
|
345
|
try {
|
346
|
newdocid = generateDocid();
|
347
|
String identifier = newdocid + ".1";
|
348
|
m.login(username, password);
|
349
|
File testFile = new File(onlinetestdatafile);
|
350
|
String response = m.upload(identifier, "onlineDataFile1",
|
351
|
new FileInputStream(testFile),
|
352
|
(int) testFile.length());
|
353
|
|
354
|
assertTrue(response.indexOf("<success>") != -1);
|
355
|
assertTrue(response.indexOf(identifier) != -1);
|
356
|
System.err.println(response);
|
357
|
|
358
|
} catch (MetacatAuthException mae) {
|
359
|
fail("Authorization failed:\n" + mae.getMessage());
|
360
|
} catch (MetacatInaccessibleException mie) {
|
361
|
mie.printStackTrace();
|
362
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
363
|
} catch (InsufficientKarmaException ike) {
|
364
|
assertTrue(1 == 1);
|
365
|
fail("Insufficient karma:\n" + ike.getMessage());
|
366
|
} catch (MetacatException me) {
|
367
|
fail("Metacat Error:\n" + me.getMessage());
|
368
|
} catch (Exception e) {
|
369
|
fail("General exception:\n" + e.getMessage());
|
370
|
}
|
371
|
}
|
372
|
|
373
|
/**
|
374
|
* Test the invalidUpdate() function. A user try to update a document
|
375
|
* which it doesn't have permission
|
376
|
*/
|
377
|
public void invalidUpdate()
|
378
|
{
|
379
|
try {
|
380
|
String identifier = newdocid + ".2";
|
381
|
m.login(anotheruser, anotherpassword);
|
382
|
String response = m.update(identifier,
|
383
|
new StringReader(testdocument), null);
|
384
|
assertTrue(response.indexOf("<success>") == -1);
|
385
|
|
386
|
} catch (MetacatAuthException mae) {
|
387
|
fail("Authorization failed:\n" + mae.getMessage());
|
388
|
} catch (MetacatInaccessibleException mie) {
|
389
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
390
|
} catch (InsufficientKarmaException ike) {
|
391
|
assertTrue(1 == 1);
|
392
|
} catch (MetacatException me) {
|
393
|
fail("Metacat Error:\n" + me.getMessage());
|
394
|
} catch (Exception e) {
|
395
|
fail("General exception:\n" + e.getMessage());
|
396
|
}
|
397
|
}
|
398
|
|
399
|
/**
|
400
|
* Test the update() function with a known document
|
401
|
*/
|
402
|
public void update()
|
403
|
{
|
404
|
try {
|
405
|
String identifier = newdocid + ".2";
|
406
|
m.login(username, password);
|
407
|
String response = m.update(identifier,
|
408
|
new StringReader(testdocument), null);
|
409
|
assertTrue(response.indexOf("<success>") != -1);
|
410
|
assertTrue(response.indexOf(identifier) != -1);
|
411
|
System.err.println(response);
|
412
|
|
413
|
} catch (MetacatAuthException mae) {
|
414
|
fail("Authorization failed:\n" + mae.getMessage());
|
415
|
} catch (MetacatInaccessibleException mie) {
|
416
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
417
|
} catch (InsufficientKarmaException ike) {
|
418
|
fail("Insufficient karma:\n" + ike.getMessage());
|
419
|
} catch (MetacatException me) {
|
420
|
fail("Metacat Error:\n" + me.getMessage());
|
421
|
} catch (Exception e) {
|
422
|
fail("General exception:\n" + e.getMessage());
|
423
|
}
|
424
|
}
|
425
|
|
426
|
/**
|
427
|
* A user try delete a document which it doesn't have permission
|
428
|
*/
|
429
|
public void invalidDelete()
|
430
|
{
|
431
|
try {
|
432
|
String identifier = newdocid + ".2";
|
433
|
m.login(anotheruser, anotherpassword);
|
434
|
String response = m.delete(identifier);
|
435
|
assertTrue(response.indexOf("<success>") == -1);
|
436
|
System.err.println(response);
|
437
|
|
438
|
} catch (MetacatAuthException mae) {
|
439
|
fail("Authorization failed:\n" + mae.getMessage());
|
440
|
} catch (MetacatInaccessibleException mie) {
|
441
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
442
|
} catch (InsufficientKarmaException ike) {
|
443
|
assertTrue(1 == 1);
|
444
|
} catch (MetacatException me) {
|
445
|
assertTrue(1 == 1);
|
446
|
} catch (Exception e) {
|
447
|
fail("General exception:\n" + e.getMessage());
|
448
|
}
|
449
|
}
|
450
|
|
451
|
/**
|
452
|
* Test the delete() function with a known document
|
453
|
*/
|
454
|
public void delete()
|
455
|
{
|
456
|
try {
|
457
|
String identifier = newdocid + ".2";
|
458
|
m.login(username, password);
|
459
|
String response = m.delete(identifier);
|
460
|
assertTrue(response.indexOf("<success>") != -1);
|
461
|
System.err.println(response);
|
462
|
|
463
|
} catch (MetacatAuthException mae) {
|
464
|
fail("Authorization failed:\n" + mae.getMessage());
|
465
|
} catch (MetacatInaccessibleException mie) {
|
466
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
467
|
} catch (InsufficientKarmaException ike) {
|
468
|
fail("Insufficient karma:\n" + ike.getMessage());
|
469
|
} catch (MetacatException me) {
|
470
|
fail("Metacat Error:\n" + me.getMessage());
|
471
|
} catch (Exception e) {
|
472
|
fail("General exception:\n" + e.getMessage());
|
473
|
}
|
474
|
}
|
475
|
|
476
|
/**
|
477
|
* Test the to connect a wrong metacat url. Create a new metacat with a
|
478
|
* inaccessible url. Then try to login it. If get a MetacatInaccessible
|
479
|
* exception, this is right.
|
480
|
*/
|
481
|
public void inaccessibleMetacat()
|
482
|
{
|
483
|
Metacat mWrong = null;
|
484
|
try {
|
485
|
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
|
486
|
} catch (MetacatInaccessibleException mie) {
|
487
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
488
|
}
|
489
|
|
490
|
try {
|
491
|
mWrong.login(username, password);
|
492
|
} catch (MetacatInaccessibleException mie) {
|
493
|
assertTrue(1 == 1);
|
494
|
} catch (MetacatAuthException mae) {
|
495
|
fail("Authorization failed:\n" + mae.getMessage());
|
496
|
}
|
497
|
}
|
498
|
|
499
|
/**
|
500
|
* Try to perform an action that requires a session to be valid without
|
501
|
* having logged in first by setting the sessionId from a previous
|
502
|
* session. Then insert a document.
|
503
|
*/
|
504
|
public void reuseSession()
|
505
|
{
|
506
|
String oldSessionId = "";
|
507
|
try {
|
508
|
Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
|
509
|
String response = mtemp.login(username, password);
|
510
|
oldSessionId = mtemp.getSessionId();
|
511
|
System.err.println("SessionId (mtemp): " + oldSessionId);
|
512
|
} catch (MetacatAuthException mae) {
|
513
|
fail("Authorization failed:\n" + mae.getMessage());
|
514
|
} catch (MetacatInaccessibleException mie) {
|
515
|
System.err.println("Metacat is: " + metacatUrl);
|
516
|
fail("Metacat connection failed." + mie.getMessage());
|
517
|
}
|
518
|
|
519
|
try {
|
520
|
String identifier = generateDocid() + ".1";
|
521
|
Metacat m2 = null;
|
522
|
try {
|
523
|
m2 = MetacatFactory.createMetacatConnection(metacatUrl);
|
524
|
} catch (MetacatInaccessibleException mie) {
|
525
|
System.err.println("Metacat is: " + metacatUrl);
|
526
|
fail("Metacat connection failed." + mie.getMessage());
|
527
|
}
|
528
|
System.err.println("SessionId (m2): " + m2.getSessionId());
|
529
|
m2.setSessionId(oldSessionId);
|
530
|
System.err.println("SessionId (m2 after set): " + m2.getSessionId());
|
531
|
String response = m2.insert(identifier,
|
532
|
new StringReader(testdocument), null);
|
533
|
System.err.println("Reuse Insert response: " + response);
|
534
|
assertTrue(response.indexOf("<success>") != -1);
|
535
|
} catch (MetacatInaccessibleException mie) {
|
536
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
537
|
} catch (InsufficientKarmaException ike) {
|
538
|
fail("Insufficient karma:\n" + ike.getMessage());
|
539
|
} catch (MetacatException me) {
|
540
|
fail("Metacat Error:\n" + me.getMessage());
|
541
|
} catch (Exception e) {
|
542
|
fail("General exception:\n" + e.getMessage());
|
543
|
}
|
544
|
}
|
545
|
|
546
|
/**
|
547
|
* Try to perform an action that requires a session to be valid without
|
548
|
* having logged in first, but use an invalid session identifier.
|
549
|
* Then insert a document, which should fail.
|
550
|
*/
|
551
|
public void reuseInvalidSession()
|
552
|
{
|
553
|
System.err.println("Starting resuseInvalidSession test...");
|
554
|
String oldSessionId = "foobar";
|
555
|
try {
|
556
|
String identifier = generateDocid() + ".1";
|
557
|
Metacat m3 = null;
|
558
|
try {
|
559
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
560
|
} catch (MetacatInaccessibleException mie) {
|
561
|
System.err.println("Metacat is: " + metacatUrl);
|
562
|
fail("Metacat connection failed." + mie.getMessage());
|
563
|
}
|
564
|
System.err.println("SessionId (m3): " + m3.getSessionId());
|
565
|
m3.setSessionId(oldSessionId);
|
566
|
System.err.println("SessionId (m3 after set): " + m3.getSessionId());
|
567
|
System.err.println("Performing resuseInvalidSession insert: " +
|
568
|
identifier);
|
569
|
String response = m3.insert(identifier,
|
570
|
new StringReader(testdocument), null);
|
571
|
System.err.println("ReuseInvalid Insert response: " + response);
|
572
|
assertTrue(response.indexOf("<success>") == -1);
|
573
|
} catch (MetacatInaccessibleException mie) {
|
574
|
fail("Metacat Inaccessible:\n" + mie.getMessage());
|
575
|
} catch (InsufficientKarmaException ike) {
|
576
|
fail("Insufficient karma:\n" + ike.getMessage());
|
577
|
} catch (MetacatException me) {
|
578
|
if(me.getMessage().
|
579
|
indexOf("Permission denied for user public inser") == -1){
|
580
|
fail("Metacat Error:\n" + me.getMessage());
|
581
|
}
|
582
|
} catch (Exception e) {
|
583
|
fail("General exception:\n" + e.getMessage());
|
584
|
}
|
585
|
}
|
586
|
|
587
|
/**
|
588
|
* Try to perform an action that requires a session to be valid without
|
589
|
* having logged in first, but use an invalid session identifier.
|
590
|
* Then insert a document, which should fail.
|
591
|
*/
|
592
|
public void getNewestDocRevision()
|
593
|
{
|
594
|
System.err.println("Starting getNewestDocRevision test...");
|
595
|
try {
|
596
|
|
597
|
Metacat m3 = null;
|
598
|
try {
|
599
|
m3 = MetacatFactory.createMetacatConnection(metacatUrl);
|
600
|
} catch (MetacatInaccessibleException mie) {
|
601
|
System.err.println("Metacat is: " + metacatUrl);
|
602
|
fail("Metacat connection failed." + mie.getMessage());
|
603
|
}
|
604
|
|
605
|
int revision = m3.getNewestDocRevision(newdocid);
|
606
|
System.err.println("Newest revision number is: " + revision);
|
607
|
assertTrue(revision == 1);
|
608
|
} catch (MetacatException me) {
|
609
|
if(me.getMessage().
|
610
|
indexOf("Permission denied for user public inser") == -1){
|
611
|
fail("Metacat Error:\n" + me.getMessage());
|
612
|
}
|
613
|
} catch (Exception e) {
|
614
|
fail("General exception:\n" + e.getMessage());
|
615
|
}
|
616
|
}
|
617
|
|
618
|
|
619
|
/**
|
620
|
* Create a hopefully unique docid for testing insert and update. Does
|
621
|
* not include the 'revision' part of the id.
|
622
|
*
|
623
|
* @return a String docid based on the current date and time
|
624
|
*/
|
625
|
private String generateDocid()
|
626
|
{
|
627
|
StringBuffer docid = new StringBuffer(prefix);
|
628
|
docid.append(".");
|
629
|
|
630
|
// Create a calendar to get the date formatted properly
|
631
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
632
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
633
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
634
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
635
|
Calendar calendar = new GregorianCalendar(pdt);
|
636
|
Date trialTime = new Date();
|
637
|
calendar.setTime(trialTime);
|
638
|
docid.append(calendar.get(Calendar.YEAR));
|
639
|
docid.append(calendar.get(Calendar.DAY_OF_YEAR));
|
640
|
docid.append(calendar.get(Calendar.HOUR_OF_DAY));
|
641
|
docid.append(calendar.get(Calendar.MINUTE));
|
642
|
docid.append(calendar.get(Calendar.SECOND));
|
643
|
|
644
|
return docid.toString();
|
645
|
}
|
646
|
}
|