Project

General

Profile

« Previous | Next » 

Revision 5889

use more unique scope (document id prefix) so that getlastdocid runs successfully. use superclass generateDocumentId() method

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
33 33
import java.io.Reader;
34 34
import java.io.StringReader;
35 35
import java.io.StringWriter;
36
import java.util.Calendar;
37
import java.util.Date;
38
import java.util.GregorianCalendar;
39
import java.util.SimpleTimeZone;
40
import java.util.TimeZone;
41 36

  
42 37
import edu.ucsb.nceas.MCTestCase;
43 38
import edu.ucsb.nceas.metacat.client.DocumentNotFoundException;
......
48 43
import edu.ucsb.nceas.metacat.client.MetacatFactory;
49 44
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
50 45
import edu.ucsb.nceas.metacat.properties.PropertyService;
51
import edu.ucsb.nceas.utilities.FileUtil;
52 46
import edu.ucsb.nceas.utilities.IOUtil;
53 47
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
54 48
import junit.framework.Test;
55 49
import junit.framework.TestSuite;
56 50
import java.io.FileInputStream;
57 51

  
58
import org.apache.log4j.Logger;
59

  
60 52
/**
61 53
 * A JUnit test for testing Step class processing
62 54
 */
......
84 76
		}
85 77
	}
86 78
    private String failpass = "uidfnkj43987yfdn";
87
    private String prefix = "test";
88 79
    private String newdocid = null;
89 80
    private String testfile = "test/jones.204.22.xml";
90 81
    private String onlinetestdatafile = "test/onlineDataFile1";
......
104 95
    public MetacatClientTest(String name)
105 96
    {
106 97
        super(name);
107
        newdocid = generateDocid();
98
        prefix = "metacatClientTest";
99
        newdocid = generateDocumentId();
108 100
    }
109 101

  
110 102
    /**
......
391 383
    {
392 384
        debug("\nStarting upload test...");
393 385
        try {
394
            newdocid = generateDocid();
386
            newdocid = generateDocumentId();
395 387
            String identifier = newdocid + ".1";
396 388
            writeIdToFile(DATAID, newdocid);
397 389
            m.login(username, password);
......
441 433
    {
442 434
        debug("\nStarting upload_stream test...");
443 435
        try {
444
            newdocid = generateDocid();
436
            newdocid = generateDocumentId();
445 437
            String identifier = newdocid + ".1";
446 438
            m.login(username, password);
447 439
            File testFile = new File(onlinetestdatafile);
......
637 629
        }
638 630

  
639 631
        try {
640
            String identifier = generateDocid() + ".1";
632
            String identifier = generateDocumentId() + ".1";
641 633
            Metacat m2 = null;
642 634
            try {
643 635
            	debug("Creating second connection and logging out of it.");
......
676 668
        debug("\nStarting resuseInvalidSession test...");
677 669
        String oldSessionId = "foobar";
678 670
        try {
679
            String identifier = generateDocid() + ".1";
671
            String identifier = generateDocumentId() + ".1";
680 672
            Metacat m3 = null;
681 673
            try {
682 674
                m3 = MetacatFactory.createMetacatConnection(metacatUrl);
......
784 776
    	{
785 777
	    	try {
786 778
	    		Thread.sleep(20000);
787
	    		newdocid = generateDocid();
779
	    		newdocid = generateDocumentId();
788 780
	            String identifier = newdocid + ".1";
789 781
	            debug("insertSpatialDocs(): the docid is "+identifier);
790 782
	            m.login(username, password);
......
817 809
    	}
818 810
    }
819 811
    
820
    /**
821
     * Create a hopefully unique docid for testing insert and update. Does
822
     * not include the 'revision' part of the id.
823
     *
824
     * @return a String docid based on the current date and time
825
     */
826
    private String generateDocid()
827
    {
828
	StringBuffer docid = new StringBuffer(prefix);
829
	docid.append(".");
830
		     
831
        // Create a calendar to get the date formatted properly
832
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
833
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
834
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
835
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
836
        Calendar calendar = new GregorianCalendar(pdt);
837
        Date trialTime = new Date();
838
        calendar.setTime(trialTime);
839

  
840
	int time = 0; 
841
	
842
	docid.append(calendar.get(Calendar.YEAR));
843
	
844
	time = calendar.get(Calendar.DAY_OF_YEAR);
845
	if(time < 10){
846
		docid.append("0");
847
		docid.append("0");
848
		docid.append(time);
849
	} else if(time < 100) {
850
		docid.append("0");
851
		docid.append(time);
852
	} else {
853
		docid.append(time);
854
	}
855
	
856
	time = calendar.get(Calendar.HOUR_OF_DAY);
857
	if(time < 10){
858
		docid.append("0");
859
		docid.append(time);
860
	} else {
861
		docid.append(time);
862
	}
863
	
864
	time = calendar.get(Calendar.MINUTE);
865
	if(time < 10){
866
		docid.append("0");
867
		docid.append(time);
868
	} else {
869
		docid.append(time);
870
	}
871
	
872
	time = calendar.get(Calendar.SECOND);
873
	if(time < 10){
874
		docid.append("0");
875
		docid.append(time);
876
	} else {
877
		docid.append(time);
878
	}
879
    
880
	 //sometimes this number is not unique, so we append a random number
881
	int random = (new Double(Math.random()*100)).intValue();
882
	docid.append(random);
883
	
884
	return docid.toString();
885
    }
886
    
887 812
    /*
888 813
     * Write id to a file for persistance
889 814
     */

Also available in: Unified diff