Project

General

Profile

« Previous | Next » 

Revision 1788

Added by Matt Jones over 20 years ago

Refactored to use IOUtil routines. Reformatted for within-file consistency.

View differences:

MetacatClientTest.java
74 74
    {
75 75
        try {
76 76
            FileReader fr = new FileReader(testfile);
77
            StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
78
            testdocument = buf.toString();
77
            testdocument = IOUtil.getAsString(fr, true);
78
            //StringBuffer buf = IOUtil.getAsStringBuffer(fr, true);
79
            //testdocument = buf.toString();
79 80
        } catch (IOException ioe) {
80 81
            fail("Can't read test data to run the test: " + testfile);
81 82
        }
......
156 157
        try {
157 158
            m.login(username, password);
158 159
            Reader r = m.read(docid);
159
            String doc = readerToString(r);
160
            String doc = IOUtil.getAsString(r, true);
160 161
            System.err.println(doc);
161 162
            assertTrue(doc.equals(testdocument));
162 163

  
......
177 178
        try {
178 179
            FileReader fr = new FileReader(queryFile);
179 180
            Reader r = m.query(fr);
180
            String result = readerToString(r);
181
            String result = IOUtil.getAsString(r, true);
181 182
            assertTrue(result.indexOf("obfs.100.3")!=-1);
182
        } catch (MetacatAuthException mae) {
183
            fail("Authorization failed:\n" + mae.getMessage());
184 183
        } catch (MetacatInaccessibleException mie) {
185 184
            fail("Metacat Inaccessible:\n" + mie.getMessage());
186 185
        } catch (Exception e) {
187 186
            fail("General exception:\n" + e.getMessage());
188 187
        }
189 188
    }
190
    
191
/*
192
 * PRIVATE METHODS
193
 */
194

  
195
    /**
196
     * Utility method to convert a reader to a String.
197
     *
198
     * @param reader the Reader to be converted
199
     * @return a String representation fo the data read
200
     */
201
    private String readerToString(Reader reader) throws Exception
202
    {
203
        String response = null;
204

  
205
        try {
206
            StringWriter sw = new StringWriter();
207
            int len;
208
            char[] characters = new char[512];
209
            while ((len = reader.read(characters, 0, 512)) != -1) {
210
                sw.write(characters, 0, len);
211
            }
212
            response = sw.toString();
213
            sw.close();
214
            reader.close();
215
        } catch (Exception e) {
216
            throw e;
217
        }
218
        return response;
219
    }
220 189
}

Also available in: Unified diff