Project

General

Profile

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: 2007-02-07 15:50:29 -0800 (Wed, 07 Feb 2007) $'
9
 * '$Revision: 3172 $'
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.DocumentImpl;
40
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
41
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
42
import edu.ucsb.nceas.metacat.client.Metacat;
43
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
44
import edu.ucsb.nceas.metacat.client.MetacatException;
45
import edu.ucsb.nceas.metacat.client.MetacatFactory;
46
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
47
import edu.ucsb.nceas.utilities.IOUtil;
48
import junit.framework.Test;
49
import junit.framework.TestCase;
50
import junit.framework.TestSuite;
51
import java.io.FileInputStream;
52

    
53
import org.apache.log4j.Logger;
54

    
55
/**
56
 * A JUnit test for testing Step class processing
57
 */
58
public class MetacatClientTest extends TestCase
59
{
60
    private String metacatUrl = "@systemidserver@@servlet-path@";
61
    private String wrongMetacatUrl=
62
                    "http://somepalce.somewhere.com/some/servlet/metacat";
63
    private String username = "@mcuser@";
64
    private String password = "@mcpassword@";
65
    private String anotheruser = "@mcanotheruser@";
66
    private String anotherpassword = "@mcanotherpassword@";
67
    private String failpass = "uidfnkj43987yfdn";
68
    private String prefix = "test";
69
    private String newdocid = null;
70
    private String testfile = "test/jones.204.22.xml";
71
    private String onlinetestdatafile = "test/onlineDataFile1";
72
    private String queryFile = "test/query.xml";
73
    private String testdocument = "";
74
    private Metacat m;
75
    private String spatialTestFile = "test/spatialEml.xml";
76
    private static final int TIME = 30;
77
    
78
    /**
79
     * Constructor to build the test
80
     *
81
     * @param name the name of the test method
82
     */
83
    public MetacatClientTest(String name)
84
    {
85
        super(name);
86
        newdocid = generateDocid();
87
    }
88

    
89
    /**
90
     * Establish a testing framework by initializing appropriate objects
91
     */
92
    public void setUp()
93
    {
94
        try {
95
            FileReader fr = new FileReader(testfile);
96
            testdocument = IOUtil.getAsString(fr, true);
97
        } catch (IOException ioe) {
98
            fail("Can't read test data to run the test: " + testfile);
99
        }
100

    
101
        try {
102
            System.err.println("Test Metacat: " + metacatUrl);
103
            m = MetacatFactory.createMetacatConnection(metacatUrl);
104
        } catch (MetacatInaccessibleException mie) {
105
            System.err.println("Metacat is: " + metacatUrl);
106
            fail("Metacat connection failed." + mie.getMessage());
107
        }
108
    }
109

    
110
    /**
111
     * Release any objects after tests are complete
112
     */
113
    public void tearDown()
114
    {
115
    }
116

    
117
    /**
118
     * Create a suite of tests to be run together
119
     */
120
    public static Test suite()
121
    {
122
      TestSuite suite = new TestSuite();
123
      suite.addTest(new MetacatClientTest("initialize"));
124
      suite.addTest(new MetacatClientTest("invalidLogin"));
125
      suite.addTest(new MetacatClientTest("logoutAndInvalidInsert"));
126
      suite.addTest(new MetacatClientTest("login"));
127
      suite.addTest(new MetacatClientTest("insert"));
128
      suite.addTest(new MetacatClientTest("getNewestDocRevision"));
129
      suite.addTest(new MetacatClientTest("getLastDocid"));
130
      suite.addTest(new MetacatClientTest("upload"));
131
      suite.addTest(new MetacatClientTest("upload_stream"));
132
      suite.addTest(new MetacatClientTest("invalidRead"));
133
      suite.addTest(new MetacatClientTest("read"));
134
      suite.addTest(new MetacatClientTest("query"));
135
      suite.addTest(new MetacatClientTest("invalidUpdate"));
136
      suite.addTest(new MetacatClientTest("update"));
137
      suite.addTest(new MetacatClientTest("invalidDelete"));
138
      suite.addTest(new MetacatClientTest("delete"));
139
      suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
140
      suite.addTest(new MetacatClientTest("reuseSession"));
141
      suite.addTest(new MetacatClientTest("reuseInvalidSession"));
142
      //suite.addTest(new MetacatClientTest("insertSpatialDocs"));
143
      return suite;
144
  }
145

    
146
    /**
147
     * Run an initial test that always passes to check that the test
148
     * harness is working.
149
     */
150
    public void initialize()
151
    {
152
        assertTrue(1 == 1);
153
    }
154

    
155
    /**
156
     * Test the login() function with valid credentials
157
     */
158
    public void login()
159
    {
160
        // Try a valid login
161
        try {
162
            String response = m.login(username, password);
163
            //System.err.println("Login response: " + response);
164
            assertTrue(response != null);
165
            assertTrue(response.indexOf("<login>") != -1);
166
            String sessionId = m.getSessionId();
167
            //System.err.println("Session ID: " + m.getSessionId());
168
            assertTrue(sessionId != null);
169
            assertTrue(response.indexOf(m.getSessionId()) != -1);
170
        } catch (MetacatAuthException mae) {
171
            fail("Authorization failed:\n" + mae.getMessage());
172
        } catch (MetacatInaccessibleException mie) {
173
            fail("Metacat Inaccessible:\n" + mie.getMessage());
174
        }
175
    }
176

    
177
    /**
178
     * Test the login() function with INVALID credentials
179
     */
180
    public void invalidLogin()
181
    {
182
        // Try an invalid login
183
        try {
184
            m.login(username, failpass);
185
            fail("Authorization should have failed.");
186
        } catch (MetacatAuthException mae) {
187
            assertTrue(1 == 1);
188
        } catch (MetacatInaccessibleException mie) {
189
            fail("Metacat Inaccessible:\n" + mie.getMessage());
190
        }
191
    }
192

    
193
    /**
194
     * Test the logout() function. When logout, user will be public, it couldn't
195
     * insert a document.
196
     */
197
    public void logoutAndInvalidInsert()
198
    {
199
       try {
200
            String identifier = newdocid + ".1";
201
            m.login(username, password);
202
            m.logout();
203
            String response = m.insert(identifier,
204
                    new StringReader(testdocument), null);
205
            //System.err.println("Response in logout: "+response);
206
            assertTrue(response.indexOf("<success>") == -1);
207
        } catch (MetacatAuthException mae) {
208
            fail("Authorization failed:\n" + mae.getMessage());
209
        } catch (MetacatInaccessibleException mie) {
210
            fail("Metacat Inaccessible:\n" + mie.getMessage());
211
        } catch (InsufficientKarmaException ike) {
212
            fail("Insufficient karma:\n" + ike.getMessage());
213
        } catch (MetacatException me) {
214
            if(me.getMessage().
215
              indexOf("Permission denied for user public inser") == -1){
216
               fail("Metacat Error:\n" + me.getMessage());
217
           }
218
        } catch (Exception e) {
219
            fail("General exception:\n" + e.getMessage());
220
        }
221
    }
222

    
223
    /**
224
     * Test the read() function with a known document
225
     */
226
    public void read()
227
    {
228
        try {
229
            m.login(username, password);
230
            Reader r = m.read(newdocid+".1");
231
            String doc = IOUtil.getAsString(r, true);
232
            doc = doc +"\n";
233
            //System.err.println(doc);
234
            assertTrue(doc.equals(testdocument));
235
        } catch (MetacatAuthException mae) {
236
            fail("Authorization failed:\n" + mae.getMessage());
237
        } catch (MetacatInaccessibleException mie) {
238
            fail("Metacat Inaccessible:\n" + mie.getMessage());
239
        } catch (Exception e) {
240
            fail("General exception:\n" + e.getMessage());
241
        }
242
    }
243

    
244
    /**
245
     * A user try to read a document which it doesn't have read permission
246
     */
247
    public void invalidRead()
248
    {
249
        try {
250
            m.login(anotheruser, anotherpassword);
251
            Reader r = m.read(newdocid+".1");
252
            String doc = IOUtil.getAsString(r, true);
253
            assertTrue(doc.indexOf("<error>") != -1);
254
            //System.err.println(doc);
255
        } catch (MetacatAuthException mae) {
256
            fail("Authorization failed:\n" + mae.getMessage());
257
        } catch (MetacatInaccessibleException mie) {
258
            fail("Metacat Inaccessible:\n" + mie.getMessage());
259
        } catch (InsufficientKarmaException ike) {
260
            assertTrue(1 == 1);
261
        } catch (MetacatException e) {
262
            fail("Metacat exception:\n" + e.getMessage());
263
        } catch (IOException ioe) {
264
            fail("IO exception:\n" + ioe.getMessage());
265
        } catch (DocumentNotFoundException ne) {
266
            fail("DocumentNotFound exception:\n" + ne.getMessage());
267

    
268
        }
269
    }
270

    
271
    /**
272
     * Test the query() function with a known document
273
     */
274
    public void query()
275
    {
276
        try {
277
            m.login(username,password);
278
            FileReader fr = new FileReader(queryFile);
279
            Reader r = m.query(fr);
280
            //System.err.println("Starting query...");
281
            String result = IOUtil.getAsString(r, true);
282
            //System.err.println("Query result:\n" + result);
283
            assertTrue(result.indexOf(newdocid+".1")!=-1);
284
        } catch (MetacatAuthException mae) {
285
            fail("Authorization failed:\n" + mae.getMessage());
286
        } catch (MetacatInaccessibleException mie) {
287
            fail("Metacat Inaccessible:\n" + mie.getMessage());
288
        } catch (Exception e) {
289
            fail("General exception:\n" + e.getMessage());
290
        }
291
    }
292

    
293
    /**
294
     * Test the insert() function with a known document
295
     */
296
    public void insert()
297
    {
298
        try {
299
            String identifier = newdocid + ".1";
300
            m.login(username, password);
301
            String response = m.insert(identifier,
302
                    new StringReader(testdocument), null);
303
            assertTrue(response.indexOf("<success>") != -1);
304
            assertTrue(response.indexOf(identifier) != -1);
305
            //System.err.println(response);
306

    
307
        } catch (MetacatAuthException mae) {
308
            fail("Authorization failed:\n" + mae.getMessage());
309
        } catch (MetacatInaccessibleException mie) {
310
            fail("Metacat Inaccessible:\n" + mie.getMessage());
311
        } catch (InsufficientKarmaException ike) {
312
            assertTrue(1 == 1);
313
            fail("Insufficient karma:\n" + ike.getMessage());
314
        } catch (MetacatException me) {
315
            fail("Metacat Error:\n" + me.getMessage());
316
        } catch (Exception e) {
317
            fail("General exception:\n" + e.getMessage());
318
        }
319
    }
320

    
321
    /**
322
     * Test the upload() function with a known document
323
     */
324
    public void upload()
325
    {
326
        try {
327
            newdocid = generateDocid();
328
            String identifier = newdocid + ".1";
329
            m.login(username, password);
330
            String response = m.upload(identifier,
331
                                     new File(onlinetestdatafile));
332
            assertTrue(response.indexOf("<success>") != -1);
333
            assertTrue(response.indexOf(identifier) != -1);
334
            //System.err.println(response);
335

    
336
        } catch (MetacatAuthException mae) {
337
            fail("Authorization failed:\n" + mae.getMessage());
338
        } catch (MetacatInaccessibleException mie) {
339
          mie.printStackTrace();
340
            fail("Metacat Inaccessible:\n" + mie.getMessage());
341
        } catch (InsufficientKarmaException ike) {
342
            assertTrue(1 == 1);
343
            fail("Insufficient karma:\n" + ike.getMessage());
344
        } catch (MetacatException me) {
345
            fail("Metacat Error:\n" + me.getMessage());
346
        } catch (Exception e) {
347
            fail("General exception:\n" + e.getMessage());
348
        }
349
    }
350

    
351
    /**
352
     * Test the upload() function by passing an InputStream
353
     */
354
    public void upload_stream()
355
    {
356
        try {
357
            newdocid = generateDocid();
358
            String identifier = newdocid + ".1";
359
            m.login(username, password);
360
            File testFile = new File(onlinetestdatafile);
361
            String response = m.upload(identifier, "onlineDataFile1",
362
                                       new FileInputStream(testFile),
363
                                       (int) testFile.length());
364

    
365
            assertTrue(response.indexOf("<success>") != -1);
366
            assertTrue(response.indexOf(identifier) != -1);
367
            //System.err.println(response);
368

    
369
        } catch (MetacatAuthException mae) {
370
            fail("Authorization failed:\n" + mae.getMessage());
371
        } catch (MetacatInaccessibleException mie) {
372
          mie.printStackTrace();
373
            fail("Metacat Inaccessible:\n" + mie.getMessage());
374
        } catch (InsufficientKarmaException ike) {
375
            assertTrue(1 == 1);
376
            fail("Insufficient karma:\n" + ike.getMessage());
377
        } catch (MetacatException me) {
378
            fail("Metacat Error:\n" + me.getMessage());
379
        } catch (Exception e) {
380
            fail("General exception:\n" + e.getMessage());
381
        }
382
    }
383

    
384
    /**
385
     * Test the invalidUpdate() function. A user try to update a document
386
     * which it doesn't have permission
387
     */
388
    public void invalidUpdate()
389
    {
390
        try {
391
            String identifier = newdocid + ".2";
392
            m.login(anotheruser, anotherpassword);
393
            String response = m.update(identifier,
394
                    new StringReader(testdocument), null);
395
            assertTrue(response.indexOf("<success>") == -1);
396

    
397
        } catch (MetacatAuthException mae) {
398
            fail("Authorization failed:\n" + mae.getMessage());
399
        } catch (MetacatInaccessibleException mie) {
400
            fail("Metacat Inaccessible:\n" + mie.getMessage());
401
        } catch (InsufficientKarmaException ike) {
402
            assertTrue(1 == 1);
403
        } catch (MetacatException me) {
404
            fail("Metacat Error:\n" + me.getMessage());
405
        } catch (Exception e) {
406
            fail("General exception:\n" + e.getMessage());
407
        }
408
    }
409

    
410
    /**
411
     * Test the update() function with a known document
412
     */
413
    public void update()
414
    {
415
        try {
416
            String identifier = newdocid + ".2";
417
            m.login(username, password);
418
            String response = m.update(identifier,
419
                    new StringReader(testdocument), null);
420
            assertTrue(response.indexOf("<success>") != -1);
421
            assertTrue(response.indexOf(identifier) != -1);
422
            //System.err.println(response);
423

    
424
        } catch (MetacatAuthException mae) {
425
            fail("Authorization failed:\n" + mae.getMessage());
426
        } catch (MetacatInaccessibleException mie) {
427
            fail("Metacat Inaccessible:\n" + mie.getMessage());
428
        } catch (InsufficientKarmaException ike) {
429
            fail("Insufficient karma:\n" + ike.getMessage());
430
        } catch (MetacatException me) {
431
            fail("Metacat Error:\n" + me.getMessage());
432
        } catch (Exception e) {
433
            fail("General exception:\n" + e.getMessage());
434
        }
435
    }
436

    
437
    /**
438
     * A user try delete a document which it doesn't have permission
439
     */
440
    public void invalidDelete()
441
    {
442
        try {
443
            String identifier = newdocid + ".2";
444
            m.login(anotheruser, anotherpassword);
445
            String response = m.delete(identifier);
446
            assertTrue(response.indexOf("<success>") == -1);
447
            //System.err.println(response);
448

    
449
        } catch (MetacatAuthException mae) {
450
            fail("Authorization failed:\n" + mae.getMessage());
451
        } catch (MetacatInaccessibleException mie) {
452
            fail("Metacat Inaccessible:\n" + mie.getMessage());
453
        } catch (InsufficientKarmaException ike) {
454
            assertTrue(1 == 1);
455
        } catch (MetacatException me) {
456
            assertTrue(1 == 1);
457
        } catch (Exception e) {
458
            fail("General exception:\n" + e.getMessage());
459
        }
460
    }
461

    
462
    /**
463
     * Test the delete() function with a known document
464
     */
465
    public void delete()
466
    {
467
        try {
468
            String identifier = newdocid + ".2";
469
            m.login(username, password);
470
            String response = m.delete(identifier);
471
            assertTrue(response.indexOf("<success>") != -1);
472
            //System.err.println(response);
473

    
474
        } catch (MetacatAuthException mae) {
475
            fail("Authorization failed:\n" + mae.getMessage());
476
        } catch (MetacatInaccessibleException mie) {
477
            fail("Metacat Inaccessible:\n" + mie.getMessage());
478
        } catch (InsufficientKarmaException ike) {
479
            fail("Insufficient karma:\n" + ike.getMessage());
480
        } catch (MetacatException me) {
481
            fail("Metacat Error:\n" + me.getMessage());
482
        } catch (Exception e) {
483
            fail("General exception:\n" + e.getMessage());
484
        }
485
    }
486

    
487
    /**
488
     * Test the to connect a wrong metacat url. Create a new metacat with a
489
     * inaccessible url. Then try to login it. If get a MetacatInaccessible
490
     * exception, this is right.
491
     */
492
    public void inaccessibleMetacat()
493
    {
494
        Metacat mWrong = null;
495
        try {
496
            mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl);
497
        } catch (MetacatInaccessibleException mie) {
498
            fail("Metacat Inaccessible:\n" + mie.getMessage());
499
        }
500

    
501
        try {
502
            mWrong.login(username, password);
503
        } catch (MetacatInaccessibleException mie) {
504
            assertTrue(1 == 1);
505
        } catch (MetacatAuthException mae) {
506
            fail("Authorization failed:\n" + mae.getMessage());
507
        }
508
    }
509

    
510
    /**
511
     * Try to perform an action that requires a session to be valid without
512
     * having logged in first by setting the sessionId from a previous
513
     * session.  Then insert a document.
514
     */
515
    public void reuseSession()
516
    {
517
        String oldSessionId = "";
518
        try {
519
            Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
520
            String response = mtemp.login(username, password);
521
            oldSessionId = mtemp.getSessionId();
522
            //System.err.println("SessionId (mtemp): " + oldSessionId);
523
        } catch (MetacatAuthException mae) {
524
            fail("Authorization failed:\n" + mae.getMessage());
525
        } catch (MetacatInaccessibleException mie) {
526
            System.err.println("Metacat is: " + metacatUrl);
527
            fail("Metacat connection failed." + mie.getMessage());
528
        }
529

    
530
        try {
531
            String identifier = generateDocid() + ".1";
532
            Metacat m2 = null;
533
            try {
534
                m2 = MetacatFactory.createMetacatConnection(metacatUrl);
535
            } catch (MetacatInaccessibleException mie) {
536
                System.err.println("Metacat is: " + metacatUrl);
537
                fail("Metacat connection failed." + mie.getMessage());
538
            }
539
            System.err.println("SessionId (m2): " + m2.getSessionId());
540
            m2.setSessionId(oldSessionId);
541
            System.err.println("SessionId (m2 after set): " + m2.getSessionId());
542
            String response = m2.insert(identifier,
543
                    new StringReader(testdocument), null);
544
            System.err.println("Reuse Insert response: " + response);
545
            assertTrue(response.indexOf("<success>") != -1);
546
        } catch (MetacatInaccessibleException mie) {
547
            fail("Metacat Inaccessible:\n" + mie.getMessage());
548
        } catch (InsufficientKarmaException ike) {
549
            fail("Insufficient karma:\n" + ike.getMessage());
550
        } catch (MetacatException me) {
551
            fail("Metacat Error:\n" + me.getMessage());
552
        } catch (Exception e) {
553
            fail("General exception:\n" + e.getMessage());
554
        }
555
    }
556

    
557
    /**
558
     * Try to perform an action that requires a session to be valid without
559
     * having logged in first, but use an invalid session identifier.
560
     * Then insert a document, which should fail.
561
     */
562
    public void reuseInvalidSession()
563
    {
564
        System.err.println("Starting resuseInvalidSession test...");
565
        String oldSessionId = "foobar";
566
        try {
567
            String identifier = generateDocid() + ".1";
568
            Metacat m3 = null;
569
            try {
570
                m3 = MetacatFactory.createMetacatConnection(metacatUrl);
571
            } catch (MetacatInaccessibleException mie) {
572
                System.err.println("Metacat is: " + metacatUrl);
573
                fail("Metacat connection failed." + mie.getMessage());
574
            }
575
            System.err.println("SessionId (m3): " + m3.getSessionId());
576
            m3.setSessionId(oldSessionId);
577
            System.err.println("SessionId (m3 after set): " + m3.getSessionId());
578
            System.err.println("Performing resuseInvalidSession insert: " +
579
                    identifier);
580
            String response = m3.insert(identifier,
581
                    new StringReader(testdocument), null);
582
            System.err.println("ReuseInvalid Insert response: " + response);
583
            assertTrue(response.indexOf("<success>") == -1);
584
        } catch (MetacatInaccessibleException mie) {
585
            fail("Metacat Inaccessible:\n" + mie.getMessage());
586
        } catch (InsufficientKarmaException ike) {
587
            fail("Insufficient karma:\n" + ike.getMessage());
588
        } catch (MetacatException me) {
589
            if(me.getMessage().
590
               indexOf("Permission denied for user public inser") == -1){
591
                fail("Metacat Error:\n" + me.getMessage());
592
            }
593
        } catch (Exception e) {
594
            fail("General exception:\n" + e.getMessage());
595
        }
596
    }
597
    
598
   /**
599
    * Get the most recent document id for a given scope and be sure
600
    * that it matches the one we last inserted. Assumes this test is run
601
    * immediately following a successful insert() test.
602
    */
603
   public void getLastDocid()
604
   {
605
       try {
606
           Metacat m3 = null;
607
           try {
608
               m3 = MetacatFactory.createMetacatConnection(metacatUrl);
609
           } catch (MetacatInaccessibleException mie) {
610
               System.err.println("Metacat is: " + metacatUrl);
611
               fail("Metacat connection failed." + mie.getMessage());
612
           }
613
           String lastId = m3.getLastDocid(prefix);
614
           System.err.println("Last Id: " + lastId);
615
           assertTrue(lastId.equals(newdocid + ".1"));
616
       } catch (MetacatException me) {
617
           fail("Metacat Error:\n" + me.getMessage());
618
       } catch (Exception e) {
619
           fail("General exception:\n" + e.getMessage());
620
       }
621
   }
622

    
623
   /**
624
    * Try to perform an action that requires a session to be valid without
625
    * having logged in first, but use an invalid session identifier.
626
    * Then insert a document, which should fail.
627
    */
628
   public void getNewestDocRevision()
629
   {
630
       //System.err.println("Starting getNewestDocRevision test...");
631
       try {
632
           
633
           Metacat m3 = null;
634
           try {
635
               m3 = MetacatFactory.createMetacatConnection(metacatUrl);
636
           } catch (MetacatInaccessibleException mie) {
637
               System.err.println("Metacat is: " + metacatUrl);
638
               fail("Metacat connection failed." + mie.getMessage());
639
           }
640
          
641
           int revision = m3.getNewestDocRevision(newdocid);
642
           //System.err.println("Newest revision number is: " + revision);
643
           assertTrue(revision == 1);
644
       } catch (MetacatException me) {
645
           if(me.getMessage().
646
              indexOf("Permission denied for user public inser") == -1){
647
               fail("Metacat Error:\n" + me.getMessage());
648
           }
649
       } catch (Exception e) {
650
           fail("General exception:\n" + e.getMessage());
651
       }
652
   }
653
   
654
   /**
655
    * Try to insert a bunch of eml documents which contain spatial information
656
    * This test is used to try to find a bug in spatial part of metacat
657
    */
658
    public void insertSpatialDocs() throws IOException
659
    {
660
    	FileReader fr = new FileReader(spatialTestFile);
661
        String spatialtestdocument = IOUtil.getAsString(fr, true);
662
        System.out.println("the eml is "+spatialtestdocument);
663
    	for (int i=0; i<TIME; i++)
664
    	{
665
	    	try {
666
	    		newdocid = generateDocid();
667
	            String identifier = newdocid + ".1";
668
	            System.out.println("the docid is "+identifier);
669
	            m.login(username, password);
670
	            String response = m.insert(identifier,
671
	                    new StringReader(spatialtestdocument), null);
672
	            assertTrue(response.indexOf("<success>") != -1);
673
	            assertTrue(response.indexOf(identifier) != -1);
674
	            Thread.sleep(8000);
675
	            identifier = newdocid +".2";
676
	            response = m.update(identifier,
677
	                    new StringReader(spatialtestdocument), null);
678
	            assertTrue(response.indexOf("<success>") != -1);
679
	            //System.err.println(response);
680
	
681
	        } catch (MetacatAuthException mae) {
682
	            fail("Authorization failed:\n" + mae.getMessage());
683
	        } catch (MetacatInaccessibleException mie) {
684
	            fail("Metacat Inaccessible:\n" + mie.getMessage());
685
	        } catch (InsufficientKarmaException ike) {
686
	            assertTrue(1 == 1);
687
	            fail("Insufficient karma:\n" + ike.getMessage());
688
	        } catch (MetacatException me) {
689
	            fail("Metacat Error:\n" + me.getMessage());
690
	        } catch (Exception e) {
691
	            fail("General exception:\n" + e.getMessage());
692
	        }
693
    	}
694
    }
695
    
696
    /**
697
     * Create a hopefully unique docid for testing insert and update. Does
698
     * not include the 'revision' part of the id.
699
     *
700
     * @return a String docid based on the current date and time
701
     */
702
    private String generateDocid()
703
    {
704
	StringBuffer docid = new StringBuffer(prefix);
705
	docid.append(".");
706
		     
707
        // Create a calendar to get the date formatted properly
708
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
709
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
710
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
711
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
712
        Calendar calendar = new GregorianCalendar(pdt);
713
        Date trialTime = new Date();
714
        calendar.setTime(trialTime);
715

    
716
	int time = 0; 
717
	
718
	docid.append(calendar.get(Calendar.YEAR));
719
	
720
	time = calendar.get(Calendar.DAY_OF_YEAR);
721
	if(time < 10){
722
		docid.append("0");
723
		docid.append("0");
724
		docid.append(time);
725
	} else if(time < 100) {
726
		docid.append("0");
727
		docid.append(time);
728
	} else {
729
		docid.append(time);
730
	}
731
	
732
	time = calendar.get(Calendar.HOUR_OF_DAY);
733
	if(time < 10){
734
		docid.append("0");
735
		docid.append(time);
736
	} else {
737
		docid.append(time);
738
	}
739
	
740
	time = calendar.get(Calendar.MINUTE);
741
	if(time < 10){
742
		docid.append("0");
743
		docid.append(time);
744
	} else {
745
		docid.append(time);
746
	}
747
	
748
	time = calendar.get(Calendar.SECOND);
749
	if(time < 10){
750
		docid.append("0");
751
		docid.append(time);
752
	} else {
753
		docid.append(time);
754
	}
755

    
756
	return docid.toString();
757
    }
758
}
    (1-1/1)