Project

General

Profile

« Previous | Next » 

Revision 4301

Added by daigle over 15 years ago

Convert class to extend MCTestCase. Add debug statements.

View differences:

test/edu/ucsb/nceas/metacattest/MetaCatServletTest.java
26 26

  
27 27
package edu.ucsb.nceas.metacattest;
28 28

  
29
import edu.ucsb.nceas.MCTestCase;
29 30
import edu.ucsb.nceas.metacat.service.PropertyService;
30 31
import edu.ucsb.nceas.utilities.HttpMessage;
31 32
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
32 33
import junit.framework.Test;
33
import junit.framework.TestCase;
34
import junit.framework.TestResult;
35 34
import junit.framework.TestSuite;
36
import org.apache.commons.logging.Log;
37
import org.apache.commons.logging.LogFactory;
38 35

  
39 36
import java.io.*;
40 37
import java.net.*;
......
43 40
/**
44 41
 * A JUnit test for testing Step class processing
45 42
 */
46
public class MetaCatServletTest extends TestCase {
43
public class MetaCatServletTest extends MCTestCase {
47 44
	private static String metacatURL;
48 45
	private String serialNumber;
49
	private static final Log log = LogFactory
50
			.getLog("edu.ucsb.nceas.metacattest.MetaCatServletTest");
51
	/* Initialize properties*/
46

  
47
	/* Initialize properties */
52 48
	static {
53 49
		try {
54
			PropertyService.getInstance("build/tests");
55 50
			metacatURL = PropertyService.getProperty("test.metacatUrl");
56
		} catch (Exception e) {
57
			System.err.println("Exception in initialize option in MetacatServletNetTest "
58
					+ e.getMessage());
51
		} catch (PropertyNotFoundException pnfe) {
52
			System.err.println("Could not get property in static block: "
53
					+ pnfe.getMessage());
59 54
		}
60 55
	}
61 56

  
62 57
	/**
63 58
	 * Constructor to build the test
64
	 *
65
	 * @param name the name of the test method
59
	 * 
60
	 * @param name
61
	 *            the name of the test method
66 62
	 */
67 63
	public MetaCatServletTest(String name) {
68 64
		super(name);
......
70 66

  
71 67
	/**
72 68
	 * Constructor to build the test
73
	 *
74
	 * @param name the name of the test method
69
	 * 
70
	 * @param name
71
	 *            the name of the test method
75 72
	 */
76 73
	public MetaCatServletTest(String name, String serial) {
77 74
		super(name);
......
105 102
		suite.addTest(new MetaCatServletTest("testPiscoReferralLogin"));
106 103
		suite.addTest(new MetaCatServletTest("testPiscoReferralLoginFail"));
107 104
		suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
108
		//Should put a login successfully at the end of login test
109
		//So insert or update can have cookie.
105
		// Should put a login successfully at the end of login test
106
		// So insert or update can have cookie.
110 107
		suite.addTest(new MetaCatServletTest("testNCEASLogin"));
111 108

  
112
		//create random number for docid, so it can void repeat
109
		// create random number for docid, so it can void repeat
113 110
		number = Math.random() * 100000;
114 111
		serial = Integer.toString(((new Double(number)).intValue()));
115
		log.debug("serial: " + serial);
112
		debug("serial: " + serial);
116 113
		suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
117 114
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", serial));
118 115
		suite.addTest(new MetaCatServletTest("testUpdateXMLDocument", serial));
......
122 119

  
123 120
		suite.addTest(new MetaCatServletTest("testDeleteXMLDocument", serial));
124 121

  
125
		//insert invalid xml document
122
		// insert invalid xml document
126 123
		number = Math.random() * 100000;
127 124
		serial = Integer.toString(((new Double(number)).intValue()));
128 125
		suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument", serial));
129
		//insert non well formed document
126
		// insert non well formed document
130 127
		number = Math.random() * 100000;
131 128
		serial = Integer.toString(((new Double(number)).intValue()));
132 129
		suite
......
139 136
	}
140 137

  
141 138
	/**
142
	 * Run an initial test that always passes to check that the test
143
	 * harness is working.
139
	 * Run an initial test that always passes to check that the test harness is
140
	 * working.
144 141
	 */
145 142
	public void initialize() {
146 143
		assertTrue(1 == 1);
......
150 147
	 * Test the login to neceas succesfully
151 148
	 */
152 149
	public void testNCEASLogin() {
150
		debug("\nRunning: testNCEASLogin test");
153 151
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
154 152
		String passwd = "123456";
155 153
		assertTrue(logIn(user, passwd));
156
		//assertTrue( withProtocol.getProtocol().equals("http"));
154
		// assertTrue( withProtocol.getProtocol().equals("http"));
157 155
	}
158 156

  
159 157
	/**
160 158
	 * Test the login to neceas failed
161 159
	 */
162 160
	public void testNCEASLoginFail() {
161
		debug("\nRunning: testNCEASLoginFail test");
163 162
		String user = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
164 163
		String passwd = "12345678";
165 164
		assertTrue(!logIn(user, passwd));
......
170 169
	 * Test the login to lter succesfully
171 170
	 */
172 171
	public void testLterReferralLogin() {
172
		debug("\nRunning: testLterReferralLogin test");
173 173
		String user = null;
174 174
		String passwd = null;
175 175
		try {
......
178 178
		} catch (PropertyNotFoundException pnfe) {
179 179
			fail("Could not find property: " + pnfe.getMessage());
180 180
		}
181

  
182
		debug("Logging into lter: " + user + " : " + passwd);
181 183
		assertTrue(logIn(user, passwd));
182 184

  
183 185
	}
......
186 188
	 * Test the login to lter failed
187 189
	 */
188 190
	public void testLterReferralLoginFail() {
191
		debug("\nRunning: testLterReferralLoginFail test");
189 192
		String user = "uid=jtao,o=LTER,dc=ecoinformatics,dc=org";
190 193
		String passwd = "qVyGpveb";
191 194
		assertTrue(!logIn(user, passwd));
192
		//assertTrue( withProtocol.getProtocol().equals("http"));
195
		// assertTrue( withProtocol.getProtocol().equals("http"));
193 196
	}
194 197

  
195 198
	/**
196 199
	 * Test the login to pisco succesfully
197 200
	 */
198 201
	public void testPiscoReferralLogin() {
202
		debug("\nRunning: testPiscoReferralLogin test");
199 203
		String user = null;
200 204
		String passwd = null;
201 205
		try {
......
204 208
		} catch (PropertyNotFoundException pnfe) {
205 209
			fail("Could not find property: " + pnfe.getMessage());
206 210
		}
211
		debug("logging in pisco user: " + user + ":" + passwd);
207 212
		assertTrue(logIn(user, passwd));
208 213
		// assertTrue( withProtocol.getProtocol().equals("http"));
209 214
	}
......
212 217
	 * Test the login to pisco failed
213 218
	 */
214 219
	public void testPiscoReferralLoginFail() {
220
		debug("\nRunning: testPiscoReferralLoginFail test");
215 221
		String user = "uid=tao,o=PISCO,dc=ecoinformatics,dc=org";
216 222
		String passwd = "hello";
217 223
		assertTrue(!logIn(user, passwd));
218
		//assertTrue( withProtocol.getProtocol().equals("http"));
224
		// assertTrue( withProtocol.getProtocol().equals("http"));
219 225
	}
220 226

  
221 227
	/**
222 228
	 * Test insert a xml document successfully
223 229
	 */
224 230
	public void testInsertXMLDocument() {
231
		debug("\nRunning: testInsertXMLDocument test");
225 232
		String name = null;
226 233
		try {
227
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
234
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
235
					+ serialNumber
228 236
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
229 237
		} catch (PropertyNotFoundException pnfe) {
230 238
			fail("Could not find property: " + pnfe.getMessage());
231 239
		}
232
		log.debug("insert docid: " + name);
240
		debug("insert docid: " + name);
233 241
		String content = "<?xml version=\"1.0\"?>"
234 242
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
235 243
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
......
240 248
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
241 249
				+ "<principal>public</principal>" + "<permission>read</permission>"
242 250
				+ "</allow>" + "</acl>";
243
		log.debug("xml document: " + content);
251
		debug("xml document: " + content);
244 252
		assertTrue(handleXMLDocument(content, name, "insert"));
245 253

  
246 254
	}
247 255

  
248 256
	/**
249
	 * Test insert a invalidate xml document successfully
250
	 * In the String, there is no <!Doctype ... Public/System/>
257
	 * Test insert a invalidate xml document successfully In the String, there
258
	 * is no <!Doctype ... Public/System/>
251 259
	 */
252 260
	public void testInsertInvalidateXMLDocument() {
261
		debug("\nRunning: testInsertInvalidateXMLDocument test");
253 262
		String name = null;
254 263
		try {
255
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
256
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
264
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
265
					+ serialNumber
266
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
257 267
		} catch (PropertyNotFoundException pnfe) {
258 268
			fail("Could not find property: " + pnfe.getMessage());
259 269
		}
260
		log.debug("insert docid: " + name);
270
		debug("insert docid: " + name);
261 271
		String content = "<?xml version=\"1.0\"?>"
262 272
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
263 273
				+ "</identifier>" + "<allow>"
......
265 275
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
266 276
				+ "<principal>public</principal>" + "<permission>read</permission>"
267 277
				+ "</allow>" + "</acl>";
268
		log.debug("xml document: " + content);
278
		debug("xml document: " + content);
269 279
		assertTrue(handleXMLDocument(content, name, "insert"));
270 280
	}
271 281

  
272 282
	/**
273
	 * Test insert a non well-formed xml document successfully
274
	 * There is no </acl> in this string
283
	 * Test insert a non well-formed xml document successfully There is no
284
	 * </acl> in this string
275 285
	 */
276 286
	public void testInsertNonWellFormedXMLDocument() {
287
		debug("\nRunning: testInsertNonWellFormedXMLDocument test");
277 288
		String name = null;
278 289
		try {
279
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
280
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
290
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
291
					+ serialNumber
292
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
281 293
		} catch (PropertyNotFoundException pnfe) {
282 294
			fail("Could not find property: " + pnfe.getMessage());
283 295
		}
284
		log.debug("insert non well-formed docid: " + name);
296
		debug("insert non well-formed docid: " + name);
285 297
		String content = "<?xml version=\"1.0\"?>"
286 298
				+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
287 299
				+ "</identifier>" + "<allow>"
......
290 302
				+ "<principal>public</principal>" + "<permission>read</permission>"
291 303
				+ "</allow>";
292 304

  
293
		log.debug("xml document: " + content);
305
		debug("xml document: " + content);
294 306
		assertTrue(!handleXMLDocument(content, name, "insert"));
295 307
	}
296 308

  
297 309
	/**
298
	 * Test read a xml document  in xml format successfully
310
	 * Test read a xml document in xml format successfully
299 311
	 */
300 312
	public void testReadXMLDocumentXMLFormat() {
313
		debug("\nRunning: testReadXMLDocumentXMLFormat test");
301 314
		String name = null;
302 315
		try {
303
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
304
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
316
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
317
					+ serialNumber
318
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
305 319
		} catch (PropertyNotFoundException pnfe) {
306 320
			fail("Could not find property: " + pnfe.getMessage());
307 321
		}
......
310 324
	}
311 325

  
312 326
	/**
313
	 * Test read a xml document  in html format successfully
327
	 * Test read a xml document in html format successfully
314 328
	 */
315 329
	public void testReadXMLDocumentHTMLFormat() {
330
		debug("\nRunning: testReadXMLDocumentHTMLFormat test");
316 331
		String name = null;
317 332
		try {
318
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
319
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
333
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
334
					+ serialNumber
335
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
320 336
		} catch (PropertyNotFoundException pnfe) {
321 337
			fail("Could not find property: " + pnfe.getMessage());
322
		}	
338
		}
323 339
		assertTrue(handleReadAction(name, "html"));
324 340

  
325 341
	}
326 342

  
327 343
	/**
328
	 * Test read a xml document  in zip format successfully
344
	 * Test read a xml document in zip format successfully
329 345
	 */
330 346
	public void testReadXMLDocumentZipFormat() {
347
		debug("\nRunning: testReadXMLDocumentZipFormat test");
331 348
		String name = null;
332 349
		try {
333
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
334
				+ PropertyService.getProperty("document.accNumSeparator") + "1";
350
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
351
					+ serialNumber
352
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
335 353
		} catch (PropertyNotFoundException pnfe) {
336 354
			fail("Could not find property: " + pnfe.getMessage());
337
		}	
355
		}
338 356
		assertTrue(handleReadAction(name, "zip"));
339 357

  
340 358
	}
......
343 361
	 * Test insert a xml document successfully
344 362
	 */
345 363
	public void testUpdateXMLDocument() {
364
		debug("\nRunning: testUpdateXMLDocument test");
346 365
		String name = null;
347 366
		try {
348
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
349
				+ PropertyService.getProperty("document.accNumSeparator") + "2";
367
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
368
					+ serialNumber
369
					+ PropertyService.getProperty("document.accNumSeparator") + "2";
350 370
		} catch (PropertyNotFoundException pnfe) {
351 371
			fail("Could not find property: " + pnfe.getMessage());
352
		}		
353
		log.debug("update docid: " + name);
372
		}
373
		debug("update docid: " + name);
354 374
		String content = "<?xml version=\"1.0\"?>"
355 375
				+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
356 376
				+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
......
361 381
				+ "<permission>all</permission>" + "</allow>" + "<allow>"
362 382
				+ "<principal>public</principal>" + "<permission>read</permission>"
363 383
				+ "</allow>" + "</acl>";
364
		log.debug("xml document: " + content);
384
		debug("xml document: " + content);
365 385
		assertTrue(handleXMLDocument(content, name, "update"));
366 386

  
367 387
	}
......
370 390
	 * Test delete a xml document successfully
371 391
	 */
372 392
	public void testDeleteXMLDocument() {
393
		debug("\nRunning: testDeleteXMLDocument test");
373 394
		String name = null;
374 395
		try {
375
			name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
376
				+ PropertyService.getProperty("document.accNumSeparator") + "2";
396
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
397
					+ serialNumber
398
					+ PropertyService.getProperty("document.accNumSeparator") + "2";
377 399
		} catch (PropertyNotFoundException pnfe) {
378 400
			fail("Could not find property: " + pnfe.getMessage());
379 401
		}
380
		log.debug("delete docid: " + name);
402
		debug("delete docid: " + name);
381 403
		assertTrue(handleDeleteFile(name));
382 404

  
383 405
	}
......
386 408
	 * Test logout action
387 409
	 */
388 410
	public void testLogOut() {
389

  
411
		debug("\nRunning: testLogOut test");
390 412
		assertTrue(handleLogOut());
391 413

  
392 414
	}
393 415

  
394 416
	/**
395 417
	 * Method to hanld login action
396
	 *
397
	 * @param usrerName, the DN name of the test method
398
	 * @param passWord, the passwd of the user
418
	 * 
419
	 * @param usrerName,
420
	 *            the DN name of the test method
421
	 * @param passWord,
422
	 *            the passwd of the user
399 423
	 */
400 424

  
401 425
	public boolean logIn(String userName, String passWord) {
......
407 431

  
408 432
		// Now contact metacat
409 433
		String response = getMetacatString(prop);
410
		log.debug("Login Message: " + response);
434
		debug("Login Message: " + response);
411 435
		boolean connected = false;
412 436
		if (response.indexOf("<login>") != -1) {
413 437
			connected = true;
......
421 445

  
422 446
	/**
423 447
	 * Method to hanld logout action
424
	 *
425
	 * @param usrerName, the DN name of the test method
426
	 * @param passWord, the passwd of the user
448
	 * 
449
	 * @param usrerName,
450
	 *            the DN name of the test method
451
	 * @param passWord,
452
	 *            the passwd of the user
427 453
	 */
428 454

  
429 455
	public boolean handleLogOut() {
......
433 459
		prop.put("qformat", "xml");
434 460

  
435 461
		String response = getMetacatString(prop);
436
		log.debug("Logout Message: " + response);
462
		debug("Logout Message: " + response);
437 463
		HttpMessage.setCookie(null);
438 464

  
439 465
		if (response.indexOf("<logout>") != -1) {
......
447 473

  
448 474
	/**
449 475
	 * Method to hanld read both xml and data file
450
	 *
451
	 * @param docid, the docid of the document want to read
452
	 * @param qformat, the format of document user want to get
476
	 * 
477
	 * @param docid,
478
	 *            the docid of the document want to read
479
	 * @param qformat,
480
	 *            the format of document user want to get
453 481
	 */
454 482
	public boolean handleReadAction(String docid, String qformat) {
455 483
		Properties prop = new Properties();
......
460 488

  
461 489
		message = getMetacatString(prop);
462 490
		message = message.trim();
463
		if (message == null || message.equals("") || message.indexOf("<error>") != -1) {//there was an error
491
		if (message == null || message.equals("") || message.indexOf("<error>") != -1) {// there
492
																						// was
493
																						// an
494
																						// error
464 495

  
465 496
			return false;
466
		} else {//successfully
497
		} else {// successfully
467 498
			return true;
468 499
		}
469 500

  
......
471 502

  
472 503
	/**
473 504
	 * Method to hanld inset or update xml document
474
	 *
475
	 * @param xmlDocument, the content of xml qformat
476
	 * @param docid, the docid of the document
477
	 * @param action, insert or update
505
	 * 
506
	 * @param xmlDocument,
507
	 *            the content of xml qformat
508
	 * @param docid,
509
	 *            the docid of the document
510
	 * @param action,
511
	 *            insert or update
478 512
	 */
479 513
	public boolean handleXMLDocument(String xmlDocument, String docid, String action)
480 514

  
481
	{ //-attempt to write file to metacat
515
	{ // -attempt to write file to metacat
482 516
		String access = "no";
483 517
		StringBuffer fileText = new StringBuffer();
484 518
		StringBuffer messageBuf = new StringBuffer();
485 519
		String accessFileId = null;
486 520
		Properties prop = new Properties();
487 521
		prop.put("action", action);
488
		prop.put("public", access); //This is the old way of controlling access
522
		prop.put("public", access); // This is the old way of controlling access
489 523
		prop.put("doctext", xmlDocument);
490 524
		prop.put("docid", docid);
491 525

  
492 526
		String message = getMetacatString(prop);
493
		log.debug("Insert or Update Message: " + message);
494
		if (message.indexOf("<error>") != -1) {//there was an error
527
		debug("Insert or Update Message: " + message);
528
		if (message.indexOf("<error>") != -1) {// there was an error
495 529

  
496 530
			return false;
497
		} else if (message.indexOf("<success>") != -1) {//the operation worked
498
			//write the file to the cache and return the file object
531
		} else if (message.indexOf("<success>") != -1) {// the operation worked
532
			// write the file to the cache and return the file object
499 533
			return true;
500 534

  
501
		} else {//something weird happened.
535
		} else {// something weird happened.
502 536
			return false;
503 537
		}
504 538

  
......
511 545
		prop.put("docid", name);
512 546

  
513 547
		String message = getMetacatString(prop);
514
		log.debug("Delete Message: " + message);
515
		if (message.indexOf("<error>") != -1) {//there was an error
548
		debug("Delete Message: " + message);
549
		if (message.indexOf("<error>") != -1) {// there was an error
516 550

  
517 551
			return false;
518
		} else if (message.indexOf("<success>") != -1) {//the operation worked
519
			//write the file to the cache and return the file object
552
		} else if (message.indexOf("<success>") != -1) {// the operation worked
553
			// write the file to the cache and return the file object
520 554
			return true;
521 555

  
522
		} else {//something weird happened.
556
		} else {// something weird happened.
523 557
			return false;
524 558
		}
525 559
	}
......
549 583

  
550 584
	/**
551 585
	 * Send a request to Metacat
552
	 *
553
	 * @param prop the properties to be sent to Metacat
586
	 * 
587
	 * @param prop
588
	 *            the properties to be sent to Metacat
554 589
	 * @return InputStream as returned by Metacat
555 590
	 */
556 591
	public InputStream getMetacatInputStream(Properties prop) {

Also available in: Unified diff