Revision 1800
Added by Jing Tao over 21 years ago
test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java | ||
---|---|---|
52 | 52 |
public class MetacatClientTest extends TestCase |
53 | 53 |
{ |
54 | 54 |
private String metacatUrl = |
55 |
"http://dev.nceas.ucsb.edu:8091/tao/servlet/metacat"; |
|
55 |
"http://dev.nceas.ucsb.edu:8091/tao/servlet/metacat";
|
|
56 | 56 |
//"http://knb.ecoinformatics.org/knb/servlet/metacat"; |
57 |
private String wrongMetacatUrl= |
|
58 |
"http://somepalce.somewhere.com/some/servlet/metacat"; |
|
57 | 59 |
private String username = "@mcuser@"; |
58 | 60 |
private String password = "@mcpassword@"; |
61 |
private String anotheruser = "@mcanotheruser@"; |
|
62 |
private String anotherpassword = "@mcanotherpassword@"; |
|
59 | 63 |
private String failpass = "uidfnkj43987yfdn"; |
60 | 64 |
private String prefix = "test"; |
61 | 65 |
private String newdocid = null; |
... | ... | |
110 | 114 |
TestSuite suite = new TestSuite(); |
111 | 115 |
suite.addTest(new MetacatClientTest("initialize")); |
112 | 116 |
suite.addTest(new MetacatClientTest("invalidLogin")); |
117 |
suite.addTest(new MetacatClientTest("logoutAndInvalidInsert")); |
|
113 | 118 |
suite.addTest(new MetacatClientTest("login")); |
114 | 119 |
suite.addTest(new MetacatClientTest("insert")); |
120 |
suite.addTest(new MetacatClientTest("invalidRead")); |
|
115 | 121 |
suite.addTest(new MetacatClientTest("read")); |
116 | 122 |
suite.addTest(new MetacatClientTest("query")); |
123 |
suite.addTest(new MetacatClientTest("invalidUpdate")); |
|
117 | 124 |
suite.addTest(new MetacatClientTest("update")); |
125 |
suite.addTest(new MetacatClientTest("invalidDelete")); |
|
118 | 126 |
suite.addTest(new MetacatClientTest("delete")); |
127 |
suite.addTest(new MetacatClientTest("inaccessiblemetacat")); |
|
119 | 128 |
return suite; |
120 | 129 |
} |
121 | 130 |
|
... | ... | |
158 | 167 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
159 | 168 |
} |
160 | 169 |
} |
170 |
|
|
171 |
/** |
|
172 |
* Test the logout() function. When logout, user will be public, it couldn't |
|
173 |
* insert a document. |
|
174 |
*/ |
|
175 |
public void logoutAndInvalidInsert() |
|
176 |
{ |
|
177 |
try { |
|
178 |
String identifier = newdocid + ".1"; |
|
179 |
m.login(username, password); |
|
180 |
m.logout(); |
|
181 |
String response = m.insert(identifier, |
|
182 |
new StringReader(testdocument), null); |
|
183 |
System.out.println("response in logout: "+response); |
|
184 |
assertTrue(response.indexOf("<success>") == -1); |
|
185 |
} catch (MetacatAuthException mae) { |
|
186 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
187 |
} catch (MetacatInaccessibleException mie) { |
|
188 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
189 |
} catch (InsufficientKarmaException ike) { |
|
190 |
fail("Insufficient karma:\n" + ike.getMessage()); |
|
191 |
} catch (MetacatException me) { |
|
192 |
fail("Metacat Error:\n" + me.getMessage()); |
|
193 |
} catch (Exception e) { |
|
194 |
fail("General exception:\n" + e.getMessage()); |
|
195 |
} |
|
196 |
|
|
197 |
} |
|
161 | 198 |
|
162 | 199 |
/** |
163 | 200 |
* Test the read() function with a known document |
... | ... | |
168 | 205 |
m.login(username, password); |
169 | 206 |
Reader r = m.read(newdocid+".1"); |
170 | 207 |
String doc = IOUtil.getAsString(r, true); |
208 |
doc = doc +"\n"; |
|
171 | 209 |
System.err.println(doc); |
172 | 210 |
assertTrue(doc.equals(testdocument)); |
173 | 211 |
} catch (MetacatAuthException mae) { |
... | ... | |
180 | 218 |
} |
181 | 219 |
|
182 | 220 |
/** |
221 |
* A user try to read a document which it doesn't have read permission |
|
222 |
*/ |
|
223 |
public void invalidRead() |
|
224 |
{ |
|
225 |
try { |
|
226 |
m.login(anotheruser, anotherpassword); |
|
227 |
Reader r = m.read(newdocid+".1"); |
|
228 |
String doc = IOUtil.getAsString(r, true); |
|
229 |
assertTrue(doc.indexOf("<error>") != -1); |
|
230 |
System.err.println(doc); |
|
231 |
} catch (MetacatAuthException mae) { |
|
232 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
233 |
} catch (MetacatInaccessibleException mie) { |
|
234 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
235 |
} catch (InsufficientKarmaException ike) { |
|
236 |
assertTrue(1 == 1); |
|
237 |
} catch (MetacatException e) { |
|
238 |
fail("Metacat exception:\n" + e.getMessage()); |
|
239 |
} catch (IOException ioe) { |
|
240 |
fail("IO exception:\n" + ioe.getMessage()); |
|
241 |
} |
|
242 |
} |
|
243 |
|
|
244 |
/** |
|
183 | 245 |
* Test the query() function with a known document |
184 | 246 |
*/ |
185 | 247 |
public void query() |
... | ... | |
215 | 277 |
} catch (MetacatInaccessibleException mie) { |
216 | 278 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
217 | 279 |
} catch (InsufficientKarmaException ike) { |
280 |
assertTrue(1 == 1); |
|
218 | 281 |
fail("Insufficient karma:\n" + ike.getMessage()); |
219 | 282 |
} catch (MetacatException me) { |
220 | 283 |
fail("Metacat Error:\n" + me.getMessage()); |
... | ... | |
224 | 287 |
} |
225 | 288 |
|
226 | 289 |
/** |
290 |
* Test the invalidUpdate() function. A user try to update a document |
|
291 |
* which it doesn't have permission |
|
292 |
*/ |
|
293 |
public void invalidUpdate() |
|
294 |
{ |
|
295 |
try { |
|
296 |
String identifier = newdocid + ".2"; |
|
297 |
m.login(anotheruser, anotherpassword); |
|
298 |
String response = m.update(identifier, |
|
299 |
new StringReader(testdocument), null); |
|
300 |
assertTrue(response.indexOf("<success>") == -1); |
|
301 |
|
|
302 |
} catch (MetacatAuthException mae) { |
|
303 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
304 |
} catch (MetacatInaccessibleException mie) { |
|
305 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
306 |
} catch (InsufficientKarmaException ike) { |
|
307 |
assertTrue(1 == 1); |
|
308 |
} catch (MetacatException me) { |
|
309 |
fail("Metacat Error:\n" + me.getMessage()); |
|
310 |
} catch (Exception e) { |
|
311 |
fail("General exception:\n" + e.getMessage()); |
|
312 |
} |
|
313 |
} |
|
314 |
|
|
315 |
/** |
|
227 | 316 |
* Test the update() function with a known document |
228 | 317 |
*/ |
229 | 318 |
public void update() |
... | ... | |
249 | 338 |
fail("General exception:\n" + e.getMessage()); |
250 | 339 |
} |
251 | 340 |
} |
341 |
|
|
342 |
/** |
|
343 |
* A user try delete a document which it doesn't have permission |
|
344 |
*/ |
|
345 |
public void invalidDelete() |
|
346 |
{ |
|
347 |
try { |
|
348 |
String identifier = newdocid + ".2"; |
|
349 |
m.login(anotheruser, anotherpassword); |
|
350 |
String response = m.delete(identifier); |
|
351 |
assertTrue(response.indexOf("<success>") == -1); |
|
352 |
System.err.println(response); |
|
252 | 353 |
|
354 |
} catch (MetacatAuthException mae) { |
|
355 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
356 |
} catch (MetacatInaccessibleException mie) { |
|
357 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
358 |
} catch (InsufficientKarmaException ike) { |
|
359 |
assertTrue(1 == 1); |
|
360 |
} catch (MetacatException me) { |
|
361 |
assertTrue(1 == 1); |
|
362 |
} catch (Exception e) { |
|
363 |
fail("General exception:\n" + e.getMessage()); |
|
364 |
} |
|
365 |
} |
|
366 |
|
|
253 | 367 |
/** |
254 | 368 |
* Test the delete() function with a known document |
255 | 369 |
*/ |
... | ... | |
274 | 388 |
fail("General exception:\n" + e.getMessage()); |
275 | 389 |
} |
276 | 390 |
} |
391 |
|
|
392 |
/** |
|
393 |
* Test the to connect a wrong metacat url. Create a new metacat with a |
|
394 |
* inaccessible url. Then try to login it. If get a MetacatInaccessible |
|
395 |
* exception, this is right. |
|
396 |
*/ |
|
397 |
public void inaccessiblemetacat() |
|
398 |
{ |
|
399 |
Metacat mWrong = null; |
|
400 |
try { |
|
401 |
mWrong = MetacatFactory.createMetacatConnection(wrongMetacatUrl); |
|
402 |
} catch (MetacatInaccessibleException mie) { |
|
403 |
fail("Metacat Inaccessible:\n" + mie.getMessage()); |
|
404 |
} |
|
405 |
|
|
406 |
try { |
|
407 |
mWrong.login(username, password); |
|
408 |
} catch (MetacatInaccessibleException mie) { |
|
409 |
assertTrue(1 == 1); |
|
410 |
} catch (MetacatAuthException mae) { |
|
411 |
fail("Authorization failed:\n" + mae.getMessage()); |
|
412 |
} |
|
413 |
} |
|
277 | 414 |
|
278 | 415 |
/** |
279 | 416 |
* Create a hopefully unique docid for testing insert and update. Does |
Also available in: Unified diff
Add new test case to testing logout, user doesn't have permision to read, insert, update and delete documents. Add new test case to test inaccessible metacat.