Project

General

Profile

« Previous | Next » 

Revision 1828

Updated more tests on session reuse. Made the http calls set the sessionid
explicitly rather than relying on cookies being passed around statically.
Still need to modify HttpMessage to get rid of the static cookie.

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
52 52
 */
53 53
public class MetacatClientTest extends TestCase
54 54
{
55
    private String metacatUrl = 
56
         "http://dev.nceas.ucsb.edu/tao/servlet/metacat";
57
        //"http://knb.ecoinformatics.org/knb/servlet/metacat";
55
    private String metacatUrl = "@systemidserver@@servlet-path@";
58 56
    private String wrongMetacatUrl=
59 57
                    "http://somepalce.somewhere.com/some/servlet/metacat";
60 58
    private String username = "@mcuser@";
......
67 65
    private String testfile = "test/jones.204.22.xml";
68 66
    private String queryFile = "test/query.xml";
69 67
    private String testdocument = "";
70
    
71 68
    private Metacat m;
72 69

  
73 70
    /**
......
94 91
        }
95 92

  
96 93
        try {
94
            System.err.println("Test Metacat: " + metacatUrl);
97 95
            m = MetacatFactory.createMetacatConnection(metacatUrl);
98 96
        } catch (MetacatInaccessibleException mie) {
99 97
            System.err.println("Metacat is: " + metacatUrl);
......
128 126
        suite.addTest(new MetacatClientTest("delete"));
129 127
        suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
130 128
        suite.addTest(new MetacatClientTest("reuseSession"));
129
        suite.addTest(new MetacatClientTest("reuseInvalidSession"));
131 130
        return suite;
132 131
    }
133 132
  
......
256 255
    public void query()
257 256
    {
258 257
        try {
258
            m.login(username,password);
259 259
            FileReader fr = new FileReader(queryFile);
260 260
            Reader r = m.query(fr);
261
            System.err.println("Starting query...");
261 262
            String result = IOUtil.getAsString(r, true);
263
            System.err.println("Query result:\n" + result);
262 264
            assertTrue(result.indexOf(newdocid+".1")!=-1);
265
        } catch (MetacatAuthException mae) {
266
            fail("Authorization failed:\n" + mae.getMessage());
263 267
        } catch (MetacatInaccessibleException mie) {
264 268
            fail("Metacat Inaccessible:\n" + mie.getMessage());
265 269
        } catch (Exception e) {
......
433 437
            Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
434 438
            String response = mtemp.login(username, password);
435 439
            oldSessionId = mtemp.getSessionId();
440
            System.err.println("SessionId (mtemp): " + oldSessionId);
436 441
        } catch (MetacatAuthException mae) {
437 442
            fail("Authorization failed:\n" + mae.getMessage());
438 443
        } catch (MetacatInaccessibleException mie) {
......
449 454
                System.err.println("Metacat is: " + metacatUrl);
450 455
                fail("Metacat connection failed." + mie.getMessage());
451 456
            }
457
            System.err.println("SessionId (m2): " + m2.getSessionId());
452 458
            m2.setSessionId(oldSessionId);
459
            System.err.println("SessionId (m2 after set): " + m2.getSessionId());
453 460
            String response = m2.insert(identifier, 
454 461
                    new StringReader(testdocument), null);
455 462
            System.err.println("Reuse Insert response: " + response);
......
466 473
    }
467 474

  
468 475
    /**
476
     * Try to perform an action that requires a session to be valid without
477
     * having logged in first by setting the sessionId from a previous
478
     * session.  Then insert a document.
479
     */
480
    public void reuseInvalidSession()
481
    {
482
        String oldSessionId = "foobar";
483
        try {
484
            String identifier = generateDocid() + ".1";
485
            Metacat m2 = null;
486
            try {
487
                m2 = MetacatFactory.createMetacatConnection(metacatUrl);
488
            } catch (MetacatInaccessibleException mie) {
489
                System.err.println("Metacat is: " + metacatUrl);
490
                fail("Metacat connection failed." + mie.getMessage());
491
            }
492
            System.err.println("SessionId (m2): " + m2.getSessionId());
493
            m2.setSessionId(oldSessionId);
494
            System.err.println("SessionId (m2 after set): " + m2.getSessionId());
495
            String response = m2.insert(identifier, 
496
                    new StringReader(testdocument), null);
497
            System.err.println("Reuse Insert response: " + response);
498
            assertTrue(response.indexOf("<success>") == -1);
499
        } catch (MetacatInaccessibleException mie) {
500
            fail("Metacat Inaccessible:\n" + mie.getMessage());
501
        } catch (InsufficientKarmaException ike) {
502
            fail("Insufficient karma:\n" + ike.getMessage());
503
        } catch (MetacatException me) {
504
            fail("Metacat Error:\n" + me.getMessage());
505
        } catch (Exception e) {
506
            fail("General exception:\n" + e.getMessage());
507
        }
508
    }
509
    /**
469 510
     * Create a hopefully unique docid for testing insert and update. Does
470 511
     * not include the 'revision' part of the id.
471 512
     *
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
90 90
        }
91 91

  
92 92
        if (response.indexOf("<login>") == -1) {
93
            HttpMessage.setCookie(null);
93
            setSessionId("");
94 94
            throw new MetacatAuthException(response);
95 95
        } else {
96 96
            int start = response.indexOf("<sessionId>") + 11;
97 97
            int end = response.indexOf("</sessionId>");
98 98
            if ((start != -1) && (end != -1)) {
99
                sessionId = response.substring(start,end);
99
                setSessionId(response.substring(start,end));
100 100
            }
101 101
        }
102 102
        return response;
......
126 126
        if (response.indexOf("<logout>") == -1) {
127 127
            throw new MetacatException(response);
128 128
        }
129
        this.sessionId = null;
129
        setSessionId("");
130 130
        return response;
131 131
    }
132 132
    
......
410 410
    public void setSessionId(String sessionId)
411 411
    {
412 412
        this.sessionId = sessionId;
413
        HttpMessage.setCookie("JSESSIONID="+this.sessionId);
414 413
    }
415 414

  
416 415
    /************************************************************************
......
428 427
        InputStream returnStream = null;
429 428
        URL url = new URL(metacatUrl);
430 429
        HttpMessage msg = new HttpMessage(url);
430
        msg.setCookie("JSESSIONID="+this.sessionId);
431 431
        returnStream = msg.sendPostData(prop);
432 432
        return returnStream;
433 433
    }

Also available in: Unified diff