Project

General

Profile

« Previous | Next » 

Revision 1788

Added by Matt Jones over 21 years ago

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

View differences:

test/edu/ucsb/nceas/metacattest/client/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
}
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
35 35
import java.util.Properties;
36 36

  
37 37
import edu.ucsb.nceas.utilities.HttpMessage;
38
import edu.ucsb.nceas.utilities.IOUtil;
38 39

  
40

  
39 41
/**
40 42
 *  This interface provides methods for initializing and logging in to a 
41 43
 *  Metacat server, and then querying, reading, transforming, inserting, 
......
154 156
    {
155 157
        Reader reader = null;
156 158
        String query = null;
157
        try
158
        {
159
          query = readerToString(xmlQuery);
160
        }
161
        catch (IOException ioE)
162
        {
159
        try {
160
          query = IOUtil.getAsString(xmlQuery, true);
161
        } catch (IOException ioE) {
163 162
          throw ioE;
164 163
        }
165 164

  
......
315 314
        }
316 315
        return response;
317 316
    }
318
    /**
319
     * Utility method to convert a reader to a String.
320
     *
321
     * @param reader the Reader to be converted
322
     * @return a String representation fo the data read
323
     */
324
    private String readerToString(Reader reader) throws IOException
325
    {
326
        String response = null;
327

  
328
        try {
329
            StringWriter sw = new StringWriter();
330
            int len;
331
            char[] characters = new char[512];
332
            while ((len = reader.read(characters, 0, 512)) != -1) {
333
                sw.write(characters, 0, len);
334
            }
335
            response = sw.toString();
336
            sw.close();
337
            reader.close();
338
        } catch (IOException e) {
339
            throw e;
340
        }
341
        return response;
342
    }
343
    
344 317
}

Also available in: Unified diff