Project

General

Profile

« Previous | Next » 

Revision 1791

Added by Matt Jones over 21 years ago

New test code for metacat client. Auto-generate id. Query and read from
the inserted document. Still having some problems with the tests.

View differences:

test/query.xml
12 12
    <querygroup operator="UNION">
13 13
      <querygroup operator="UNION">
14 14
        <queryterm searchmode="contains" casesensitive="false">
15
          <value>Coweeta</value>
15
          <value>Jones</value>
16 16
        </queryterm>
17 17
      </querygroup>
18 18
    </querygroup>
test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
34 34
import java.io.Reader;
35 35
import java.io.StringReader;
36 36
import java.io.StringWriter;
37
import java.text.DateFormat;
38
import java.util.Calendar;
39
import java.util.GregorianCalendar;
40
import java.util.Date;
41
import java.util.SimpleTimeZone;
42
import java.util.TimeZone;
37 43

  
38 44
import junit.framework.Test;
39 45
import junit.framework.TestCase;
......
47 53
{
48 54
    private String metacatUrl = 
49 55
        "http://knb.ecoinformatics.org/knb/servlet/metacat";
56
        //"http://dev.nceas.ucsb.edu/tao/servlet/metacat";
50 57
    private String username = "@mcuser@";
51 58
    private String password = "@mcpassword@";
52 59
    private String failpass = "uidfnkj43987yfdn";
53 60
    private String docid = "jones.204.22";
54
    private String newdocid = "@newdocid@";
61
    private String prefix = "test";
62
    private String newdocid = null;
55 63
    private String testfile = "test/jones.204.22.xml";
56 64
    private String queryFile = "test/query.xml";
57 65
    //private String docid = "Gramling.61.26";
......
67 75
    public MetacatClientTest(String name)
68 76
    {
69 77
        super(name);
78
        newdocid = generateDocid();
70 79
    }
71 80

  
72 81
    /**
......
77 86
        try {
78 87
            FileReader fr = new FileReader(testfile);
79 88
            testdocument = IOUtil.getAsString(fr, true);
80
            //StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
81
            //testdocument = buf.toString();
82 89
        } catch (IOException ioe) {
83 90
            fail("Can't read test data to run the test: " + testfile);
84 91
        }
......
159 166
    {
160 167
        try {
161 168
            m.login(username, password);
162
            Reader r = m.read(docid);
169
            Reader r = m.read(newdocid+".1");
163 170
            String doc = IOUtil.getAsString(r, true);
164 171
            System.err.println(doc);
165 172
            assertTrue(doc.equals(testdocument));
166

  
167 173
        } catch (MetacatAuthException mae) {
168 174
            fail("Authorization failed:\n" + mae.getMessage());
169 175
        } catch (MetacatInaccessibleException mie) {
......
182 188
            FileReader fr = new FileReader(queryFile);
183 189
            Reader r = m.query(fr);
184 190
            String result = IOUtil.getAsString(r, true);
185
            assertTrue(result.indexOf("obfs.100.3")!=-1);
191
            assertTrue(result.indexOf("jones.204.22")!=-1);
186 192
        } catch (MetacatInaccessibleException mie) {
187 193
            fail("Metacat Inaccessible:\n" + mie.getMessage());
188 194
        } catch (Exception e) {
......
196 202
    public void insert()
197 203
    {
198 204
        try {
205
            String identifier = newdocid + ".1";
199 206
            m.login(username, password);
200
            String response = m.insert(newdocid, 
207
            String response = m.insert(identifier, 
201 208
                    new StringReader(testdocument), null);
209
            System.err.println(
210
                DateFormat.getDateTimeInstance().format(new Date()));
211
            //Thread.sleep(20000);
212
            System.err.println(
213
                DateFormat.getDateTimeInstance().format(new Date()));
202 214
            assertTrue(response.indexOf("<success>") != -1);
203
            assertTrue(response.indexOf(newdocid) != -1);
215
            assertTrue(response.indexOf(identifier) != -1);
216
            System.err.println(response);
204 217

  
205 218
        } catch (MetacatAuthException mae) {
206 219
            fail("Authorization failed:\n" + mae.getMessage());
......
214 227
            fail("General exception:\n" + e.getMessage());
215 228
        }
216 229
    }
230

  
231
    /**
232
     * Create a hopefully unique docid for testing insert and update. Does
233
     * not include the 'revision' part of the id.
234
     *
235
     * @return a String docid based on the current date and time
236
     */
237
    private String generateDocid()
238
    {
239
        StringBuffer docid = new StringBuffer(prefix);
240
        docid.append(".");
241
        
242
        // Create a calendar to get the date formatted properly
243
        String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
244
        SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
245
        pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
246
        pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
247
        Calendar calendar = new GregorianCalendar(pdt);
248
        Date trialTime = new Date();
249
        calendar.setTime(trialTime);
250
        docid.append(calendar.get(Calendar.YEAR));
251
        docid.append(calendar.get(Calendar.DAY_OF_YEAR));
252
        docid.append(calendar.get(Calendar.HOUR_OF_DAY));
253
        docid.append(calendar.get(Calendar.MINUTE));
254
        docid.append(calendar.get(Calendar.SECOND));
255

  
256
        return docid.toString();
257
    }
217 258
}
build.xml
195 195
      <property name="srcdir" value="./src" />
196 196
      <property name="testdir" value="./test" />
197 197
      <property name="testtorun" value="MetacatClientTest" />
198
      <property name="newdocid" value="foombj.102.1"/>
199
      <filter token="newdocid" value="${newdocid}"/>
200 198
      <property name="junittestsdir" value="./test/edu/ucsb/nceas/metacattest" />
201 199
      <property name="junitnettestsdir" value="./test/edu/ucsb/nceas/metacatnettest" />
202 200
      <property name="build.dir" value="./build"/>
......
488 486
      <javac srcdir="${build.tests}"
489 487
             destdir="${build.tests}"
490 488
             classpath="${cpath}:${build.dir}/${name}.jar"
489
             debug="on"
491 490
       includes="**/*.java" />
492 491

  
493 492
      <jar jarfile="${build.dir}/${name}-junittests.jar"

Also available in: Unified diff