Project

General

Profile

« Previous | Next » 

Revision 1784

Added by Matt Jones over 20 years ago

Implemented the 'read' API call. Some more to go...

View differences:

MetacatClientTest.java
26 26
package edu.ucsb.nceas.metacattest.client;
27 27

  
28 28
import edu.ucsb.nceas.metacat.client.*;
29
import edu.ucsb.nceas.utilities.IOUtil;
29 30

  
31
import java.io.FileReader;
32
import java.io.InputStreamReader;
33
import java.io.IOException;
34
import java.io.Reader;
35
import java.io.StringWriter;
36

  
30 37
import junit.framework.Test;
31 38
import junit.framework.TestCase;
32 39
import junit.framework.TestResult;
......
42 49
    private String username = "@mcuser@";
43 50
    private String password = "@mcpassword@";
44 51
    private String failpass = "uidfnkj43987yfdn";
52
    private String docid = "jones.204.22";
53
    private String testfile = "test/jones.204.22.xml";
54
    //private String docid = "Gramling.61.26";
55
    private String testdocument = "";
45 56
    
46 57
    private Metacat m;
47 58

  
......
61 72
    public void setUp()
62 73
    {
63 74
        try {
75
            FileReader fr = new FileReader(testfile);
76
            StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
77
            testdocument = buf.toString();
78
        } catch (IOException ioe) {
79
            fail("Can't read test data to run the test: " + testfile);
80
        }
81

  
82
        try {
64 83
            m = MetacatFactory.createMetacatConnection(metacatUrl);
65 84
        } catch (MetacatInaccessibleException mie) {
66 85
            fail("Metacat connection failed." + mie.getMessage());
......
83 102
        suite.addTest(new MetacatClientTest("initialize"));
84 103
        suite.addTest(new MetacatClientTest("invalidLogin"));
85 104
        suite.addTest(new MetacatClientTest("login"));
105
        suite.addTest(new MetacatClientTest("read"));
86 106
        return suite;
87 107
    }
88 108
  
......
117 137
    {
118 138
        // Try an invalid login
119 139
        try {
120
            System.err.println("Username: " + username);
121 140
            m.login(username, failpass);
122 141
            fail("Authorization should have failed.");
123 142
        } catch (MetacatAuthException mae) {
......
126 145
            fail("Metacat Inaccessible:\n" + mie.getMessage());
127 146
        }
128 147
    }
148

  
149
    /**
150
     * Test the read() function with a known document
151
     */
152
    public void read()
153
    {
154
        try {
155
            m.login(username, password);
156
            Reader r = m.read(docid);
157
            String doc = readerToString(r);
158
            System.err.println(doc);
159
            assertTrue(doc.equals(testdocument));
160

  
161
        } catch (MetacatAuthException mae) {
162
            fail("Authorization failed:\n" + mae.getMessage());
163
        } catch (MetacatInaccessibleException mie) {
164
            fail("Metacat Inaccessible:\n" + mie.getMessage());
165
        } catch (Exception e) {
166
            fail("General exception:\n" + e.getMessage());
167
        }
168
    }
169

  
170
/*
171
 * PRIVATE METHODS
172
 */
173

  
174
    /**
175
     * Utility method to convert a reader to a String.
176
     *
177
     * @param reader the Reader to be converted
178
     * @return a String representation fo the data read
179
     */
180
    private String readerToString(Reader reader) throws Exception
181
    {
182
        String response = null;
183

  
184
        try {
185
            StringWriter sw = new StringWriter();
186
            int len;
187
            char[] characters = new char[512];
188
            while ((len = reader.read(characters, 0, 512)) != -1) {
189
                sw.write(characters, 0, len);
190
            }
191
            response = sw.toString();
192
            sw.close();
193
            reader.close();
194
        } catch (Exception e) {
195
            throw e;
196
        }
197
        return response;
198
    }
129 199
}

Also available in: Unified diff