Project

General

Profile

1 1783 jones
/**
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$'
8
 *     '$Date$'
9
 * '$Revision$'
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 2242 sgarg
import java.io.File;
29 1784 jones
import java.io.FileReader;
30
import java.io.IOException;
31
import java.io.Reader;
32 1789 jones
import java.io.StringReader;
33 1791 jones
import java.util.Calendar;
34 2242 sgarg
import java.util.Date;
35 1791 jones
import java.util.GregorianCalendar;
36
import java.util.SimpleTimeZone;
37
import java.util.TimeZone;
38 1784 jones
39 2242 sgarg
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 1783 jones
import junit.framework.Test;
47
import junit.framework.TestCase;
48
import junit.framework.TestSuite;
49 2265 sgarg
import java.io.FileInputStream;
50 1783 jones
51
/**
52
 * A JUnit test for testing Step class processing
53
 */
54
public class MetacatClientTest extends TestCase
55
{
56 1828 jones
    private String metacatUrl = "@systemidserver@@servlet-path@";
57 1800 tao
    private String wrongMetacatUrl=
58
                    "http://somepalce.somewhere.com/some/servlet/metacat";
59 1783 jones
    private String username = "@mcuser@";
60
    private String password = "@mcpassword@";
61 1800 tao
    private String anotheruser = "@mcanotheruser@";
62
    private String anotherpassword = "@mcanotherpassword@";
63 1783 jones
    private String failpass = "uidfnkj43987yfdn";
64 1791 jones
    private String prefix = "test";
65
    private String newdocid = null;
66 1784 jones
    private String testfile = "test/jones.204.22.xml";
67 2242 sgarg
    private String onlinetestdatafile = "test/onlineDataFile1";
68 1787 tao
    private String queryFile = "test/query.xml";
69 1784 jones
    private String testdocument = "";
70 1783 jones
    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 1791 jones
        newdocid = generateDocid();
81 1783 jones
    }
82
83
    /**
84
     * Establish a testing framework by initializing appropriate objects
85
     */
86
    public void setUp()
87
    {
88
        try {
89 1784 jones
            FileReader fr = new FileReader(testfile);
90 1788 jones
            testdocument = IOUtil.getAsString(fr, true);
91 1784 jones
        } catch (IOException ioe) {
92
            fail("Can't read test data to run the test: " + testfile);
93
        }
94
95
        try {
96 1828 jones
            System.err.println("Test Metacat: " + metacatUrl);
97 1783 jones
            m = MetacatFactory.createMetacatConnection(metacatUrl);
98
        } catch (MetacatInaccessibleException mie) {
99 1822 jones
            System.err.println("Metacat is: " + metacatUrl);
100 1783 jones
            fail("Metacat connection failed." + mie.getMessage());
101
        }
102
    }
103 2242 sgarg
104 1783 jones
    /**
105
     * Release any objects after tests are complete
106
     */
107
    public void tearDown()
108
    {
109
    }
110 2242 sgarg
111 1783 jones
    /**
112
     * Create a suite of tests to be run together
113
     */
114
    public static Test suite()
115
    {
116 2242 sgarg
      TestSuite suite = new TestSuite();
117 2265 sgarg
      suite.addTest(new MetacatClientTest("initialize"));
118 2987 sgarg
      suite.addTest(new MetacatClientTest("invalidLogin"));
119
      suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
120 2265 sgarg
      suite.addTest(new MetacatClientTest("login"));
121
      suite.addTest(new MetacatClientTest("insert"));
122 2987 sgarg
      suite.addTest(new MetacatClientTest("getNewestDocRevision"));
123 2981 jones
      suite.addTest(new MetacatClientTest("getLastDocid"));
124 2987 sgarg
      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 2265 sgarg
      return suite;
137
  }
138 2242 sgarg
139 1783 jones
    /**
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 2242 sgarg
148 1783 jones
    /**
149
     * Test the login() function with valid credentials
150
     */
151
    public void login()
152
    {
153
        // Try a valid login
154
        try {
155 1822 jones
            String response = m.login(username, password);
156 2981 jones
            //System.err.println("Login response: " + response);
157 1822 jones
            assertTrue(response != null);
158
            assertTrue(response.indexOf("<login>") != -1);
159
            String sessionId = m.getSessionId();
160 2981 jones
            //System.err.println("Session ID: " + m.getSessionId());
161 1825 jones
            assertTrue(sessionId != null);
162
            assertTrue(response.indexOf(m.getSessionId()) != -1);
163 1783 jones
        } 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 2242 sgarg
186 1800 tao
    /**
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 2242 sgarg
            String response = m.insert(identifier,
197 1800 tao
                    new StringReader(testdocument), null);
198 2981 jones
            //System.err.println("Response in logout: "+response);
199 1800 tao
            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 2265 sgarg
            if(me.getMessage().
208
              indexOf("Permission denied for user public inser") == -1){
209
               fail("Metacat Error:\n" + me.getMessage());
210
           }
211 1800 tao
        } catch (Exception e) {
212
            fail("General exception:\n" + e.getMessage());
213
        }
214
    }
215 1784 jones
216
    /**
217
     * Test the read() function with a known document
218
     */
219
    public void read()
220
    {
221
        try {
222
            m.login(username, password);
223 1791 jones
            Reader r = m.read(newdocid+".1");
224 1788 jones
            String doc = IOUtil.getAsString(r, true);
225 1800 tao
            doc = doc +"\n";
226 2981 jones
            //System.err.println(doc);
227 1784 jones
            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 2242 sgarg
237 1787 tao
    /**
238 1800 tao
     * 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 2981 jones
            //System.err.println(doc);
248 1800 tao
        } 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 2242 sgarg
261 1800 tao
    /**
262 1787 tao
     * Test the query() function with a known document
263
     */
264
    public void query()
265
    {
266
        try {
267 1828 jones
            m.login(username,password);
268 1787 tao
            FileReader fr = new FileReader(queryFile);
269
            Reader r = m.query(fr);
270 2981 jones
            //System.err.println("Starting query...");
271 1788 jones
            String result = IOUtil.getAsString(r, true);
272 2981 jones
            //System.err.println("Query result:\n" + result);
273 1792 jones
            assertTrue(result.indexOf(newdocid+".1")!=-1);
274 1828 jones
        } catch (MetacatAuthException mae) {
275
            fail("Authorization failed:\n" + mae.getMessage());
276 1787 tao
        } catch (MetacatInaccessibleException mie) {
277
            fail("Metacat Inaccessible:\n" + mie.getMessage());
278
        } catch (Exception e) {
279
            fail("General exception:\n" + e.getMessage());
280
        }
281
    }
282 1789 jones
283
    /**
284
     * Test the insert() function with a known document
285
     */
286
    public void insert()
287
    {
288
        try {
289 1791 jones
            String identifier = newdocid + ".1";
290 1789 jones
            m.login(username, password);
291 2242 sgarg
            String response = m.insert(identifier,
292 1789 jones
                    new StringReader(testdocument), null);
293
            assertTrue(response.indexOf("<success>") != -1);
294 1791 jones
            assertTrue(response.indexOf(identifier) != -1);
295 2981 jones
            //System.err.println(response);
296 1789 jones
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 1800 tao
            assertTrue(1 == 1);
303 1789 jones
            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 1791 jones
311
    /**
312 2242 sgarg
     * 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 2981 jones
            //System.err.println(response);
325 2242 sgarg
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 2265 sgarg
     * 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 2981 jones
            //System.err.println(response);
358 2265 sgarg
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 1800 tao
     * 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 2242 sgarg
            String response = m.update(identifier,
384 1800 tao
                    new StringReader(testdocument), null);
385
            assertTrue(response.indexOf("<success>") == -1);
386 2242 sgarg
387 1800 tao
        } 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 2242 sgarg
400 1800 tao
    /**
401 1795 jones
     * 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 2242 sgarg
            String response = m.update(identifier,
409 1795 jones
                    new StringReader(testdocument), null);
410
            assertTrue(response.indexOf("<success>") != -1);
411
            assertTrue(response.indexOf(identifier) != -1);
412 2981 jones
            //System.err.println(response);
413 1795 jones
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 2242 sgarg
427 1800 tao
    /**
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 2981 jones
            //System.err.println(response);
438 1795 jones
439 1800 tao
        } 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 2242 sgarg
452 1795 jones
    /**
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 2981 jones
            //System.err.println(response);
463 1795 jones
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 2242 sgarg
477 1800 tao
    /**
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 1826 jones
    public void inaccessibleMetacat()
483 1800 tao
    {
484
        Metacat mWrong = null;
485
        try {
486
            mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
487
        } catch (MetacatInaccessibleException mie) {
488
            fail("Metacat Inaccessible:\n" + mie.getMessage());
489
        }
490 2242 sgarg
491 1800 tao
        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 1795 jones
500
    /**
501 1826 jones
     * 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 2981 jones
            //System.err.println("SessionId (mtemp): " + oldSessionId);
513 1826 jones
        } 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 1828 jones
            System.err.println("SessionId (m2): " + m2.getSessionId());
530 1826 jones
            m2.setSessionId(oldSessionId);
531 1828 jones
            System.err.println("SessionId (m2 after set): " + m2.getSessionId());
532 2242 sgarg
            String response = m2.insert(identifier,
533 1826 jones
                    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 1828 jones
     * Try to perform an action that requires a session to be valid without
549 2242 sgarg
     * having logged in first, but use an invalid session identifier.
550 1829 jones
     * Then insert a document, which should fail.
551 1828 jones
     */
552
    public void reuseInvalidSession()
553
    {
554 1829 jones
        System.err.println("Starting resuseInvalidSession test...");
555 1828 jones
        String oldSessionId = "foobar";
556
        try {
557
            String identifier = generateDocid() + ".1";
558 1829 jones
            Metacat m3 = null;
559 1828 jones
            try {
560 1829 jones
                m3 = MetacatFactory.createMetacatConnection(metacatUrl);
561 1828 jones
            } catch (MetacatInaccessibleException mie) {
562
                System.err.println("Metacat is: " + metacatUrl);
563
                fail("Metacat connection failed." + mie.getMessage());
564
            }
565 1829 jones
            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 2242 sgarg
            String response = m3.insert(identifier,
571 1828 jones
                    new StringReader(testdocument), null);
572 1829 jones
            System.err.println("ReuseInvalid Insert response: " + response);
573 1828 jones
            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 2265 sgarg
            if(me.getMessage().
580
               indexOf("Permission denied for user public inser") == -1){
581
                fail("Metacat Error:\n" + me.getMessage());
582
            }
583 1828 jones
        } catch (Exception e) {
584
            fail("General exception:\n" + e.getMessage());
585
        }
586
    }
587 2338 tao
588 2981 jones
   /**
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 2992 berkley
           System.err.println("Last Id: " + lastId);
605 2981 jones
           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 2338 tao
    * 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 2981 jones
       //System.err.println("Starting getNewestDocRevision test...");
621 2338 tao
       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 2981 jones
           //System.err.println("Newest revision number is: " + revision);
633 2338 tao
           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 1791 jones
     * 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 2988 sgarg
	StringBuffer docid = new StringBuffer(prefix);
653
	docid.append(".");
654
655 1791 jones
        // 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 2988 sgarg
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 1791 jones
    }
706 1783 jones
}