Project

General

Profile

« Previous | Next » 

Revision 3212

Added by Jing Tao over 17 years ago

Currently every MetacatClientTest will generate new doicd and the the docid will be reused. So it will generate problem. I add new method to persistent the doicd in a file and this problem was fixed.

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
27 27

  
28 28
import java.io.File;
29 29
import java.io.FileReader;
30
import java.io.FileWriter;
30 31
import java.io.IOException;
31 32
import java.io.Reader;
32 33
import java.io.StringReader;
34
import java.io.StringWriter;
33 35
import java.util.Calendar;
34 36
import java.util.Date;
35 37
import java.util.GregorianCalendar;
......
74 76
    private Metacat m;
75 77
    private String spatialTestFile = "test/spatialEml.xml";
76 78
    private static final int TIME = 30;
79
    private static final String DOCID = "docid";
80
    private static final String DATAID = "dataid";
77 81
    
78 82
    /**
79 83
     * Constructor to build the test
......
227 231
    {
228 232
        try {
229 233
            m.login(username, password);
230
            Reader r = m.read(newdocid+".1");
234
            String docid = readIdFromFile(DOCID);
235
            Reader r = m.read(docid+".1");
231 236
            String doc = IOUtil.getAsString(r, true);
232 237
            doc = doc +"\n";
233 238
            //System.err.println(doc);
......
248 253
    {
249 254
        try {
250 255
            m.login(anotheruser, anotherpassword);
251
            Reader r = m.read(newdocid+".1");
256
            String docid = readIdFromFile(DOCID);
257
            Reader r = m.read(docid+".1");
252 258
            String doc = IOUtil.getAsString(r, true);
253 259
            assertTrue(doc.indexOf("<error>") != -1);
254 260
            //System.err.println(doc);
......
265 271
        } catch (DocumentNotFoundException ne) {
266 272
            fail("DocumentNotFound exception:\n" + ne.getMessage());
267 273

  
274
        } catch(Exception e) {
275
            fail("Exception:\n" + e.getMessage());
268 276
        }
269 277
    }
270 278

  
......
280 288
            //System.err.println("Starting query...");
281 289
            String result = IOUtil.getAsString(r, true);
282 290
            //System.err.println("Query result:\n" + result);
283
            assertTrue(result.indexOf(newdocid+".1")!=-1);
291
            String docid = readIdFromFile(DOCID);
292
            assertTrue(result.indexOf(docid+".1")!=-1);
284 293
        } catch (MetacatAuthException mae) {
285 294
            fail("Authorization failed:\n" + mae.getMessage());
286 295
        } catch (MetacatInaccessibleException mie) {
......
297 306
    {
298 307
        try {
299 308
            String identifier = newdocid + ".1";
309
            //write newdocid into file for persistance
310
            writeIdToFile(DOCID, newdocid);
300 311
            m.login(username, password);
301 312
            String response = m.insert(identifier,
302 313
                    new StringReader(testdocument), null);
......
326 337
        try {
327 338
            newdocid = generateDocid();
328 339
            String identifier = newdocid + ".1";
340
            writeIdToFile(DATAID, newdocid);
329 341
            m.login(username, password);
330 342
            String response = m.upload(identifier,
331 343
                                     new File(onlinetestdatafile));
......
388 400
    public void invalidUpdate()
389 401
    {
390 402
        try {
391
            String identifier = newdocid + ".2";
403
        	String docid = readIdFromFile(DOCID);
404
            String identifier = docid + ".2";
392 405
            m.login(anotheruser, anotherpassword);
393 406
            String response = m.update(identifier,
394 407
                    new StringReader(testdocument), null);
......
413 426
    public void update()
414 427
    {
415 428
        try {
416
            String identifier = newdocid + ".2";
429
        	String docid = readIdFromFile(DOCID);
430
            String identifier = docid + ".2";
417 431
            m.login(username, password);
418 432
            String response = m.update(identifier,
419 433
                    new StringReader(testdocument), null);
......
465 479
    public void delete()
466 480
    {
467 481
        try {
468
            String identifier = newdocid + ".2";
482
        	Thread.sleep(10000);
483
        	String docid = readIdFromFile(DOCID);
484
            String identifier = docid + ".2";
469 485
            m.login(username, password);
470 486
            String response = m.delete(identifier);
471 487
            assertTrue(response.indexOf("<success>") != -1);
488
            // delete the docid persistence file.
489
            deleteFile(DOCID);
490
            deleteFile(DATAID);
472 491
            //System.err.println(response);
473 492

  
474 493
        } catch (MetacatAuthException mae) {
......
612 631
           }
613 632
           String lastId = m3.getLastDocid(prefix);
614 633
           System.err.println("Last Id: " + lastId);
615
           assertTrue(lastId.equals(newdocid + ".1"));
634
           //get docid from file
635
           String docid = readIdFromFile(DOCID);
636
           assertTrue(lastId.equals(docid + ".1"));
616 637
       } catch (MetacatException me) {
617 638
           fail("Metacat Error:\n" + me.getMessage());
618 639
       } catch (Exception e) {
......
637 658
               System.err.println("Metacat is: " + metacatUrl);
638 659
               fail("Metacat connection failed." + mie.getMessage());
639 660
           }
640
          
641
           int revision = m3.getNewestDocRevision(newdocid);
661
           // get docid from file
662
           String docid = readIdFromFile(DOCID);
663
           int revision = m3.getNewestDocRevision(docid);
642 664
           //System.err.println("Newest revision number is: " + revision);
643 665
           assertTrue(revision == 1);
644 666
       } catch (MetacatException me) {
......
762 784
	
763 785
	return docid.toString();
764 786
    }
787
    
788
    /*
789
     * Write id to a file for persistance
790
     */
791
    private void writeIdToFile(String fileName, String id) throws Exception
792
    {
793
    	File file = new File(fileName);
794
    	StringReader reader = new StringReader(id);
795
    	FileWriter writer = new FileWriter(file);
796
    	char [] buffer = new char[1024];
797
    	int c = reader.read(buffer);
798
    	while (c != -1)
799
    	{
800
    		writer.write(buffer, 0, c);
801
    		c = reader.read(buffer);
802
    	}
803
    	writer.close();
804
    	reader.close();
805
    }
806
    
807
    /*
808
     * Read id from a given file
809
     */
810
    private String readIdFromFile(String fileName) throws Exception
811
    {
812
    	File file = new File(fileName);
813
    	FileReader reader = new FileReader(file);
814
    	StringWriter writer = new StringWriter();
815
    	char [] buffer = new char[1024];
816
    	int c = reader.read(buffer);
817
    	while (c != -1)
818
    	{
819
    		writer.write(buffer, 0, c);
820
    		c = reader.read(buffer);
821
    	}
822
    	reader.close();
823
    	return writer.toString();
824
    }
825
    
826
    /*
827
     * Delete the given file
828
     */
829
    private void deleteFile(String fileName) throws Exception
830
    {
831
    	File file = new File(fileName);
832
    	file.delete();
833
    }
765 834
}

Also available in: Unified diff