View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/mySpace/client/src/java/org/astrogrid/store/tree/TreeClientTest.java,v $</cvs:source>
3    * <cvs:author>$Author: clq2 $</cvs:author>
4    * <cvs:date>$Date: 2004/11/17 16:22:53 $</cvs:date>
5    * <cvs:version>$Revision: 1.2 $</cvs:version>
6    * <cvs:log>
7    *   $Log: TreeClientTest.java,v $
8    *   Revision 1.2  2004/11/17 16:22:53  clq2
9    *   nww-itn07-704
10   *
11   *   Revision 1.1.2.2  2004/11/16 17:27:58  nw
12   *   tidied imports
13   *
14   *   Revision 1.1.2.1  2004/11/16 16:47:28  nw
15   *   copied aladinAdapter interfaces into a neutrally-named package.
16   *   deprecated original interfaces.
17   *   javadoc
18   *
19   *   Revision 1.3  2004/11/11 17:50:42  clq2
20   *   Noel's aladin stuff
21   *
22   *   Revision 1.2.6.1  2004/11/11 13:12:36  nw
23   *   added some further checking of the root container
24   *
25   *   Revision 1.2  2004/10/05 15:39:29  dave
26   *   Merged changes to AladinAdapter ...
27   *
28   *   Revision 1.1.2.1  2004/10/05 15:30:44  dave
29   *   Moved test base from test to src tree ....
30   *   Added MimeTypeUtil
31   *   Added getMimeType to the adapter API
32   *   Added logout to the adapter API
33   *
34   *   Revision 1.2  2004/09/28 10:24:19  dave
35   *   Added AladinAdapter interfaces and mock implementation.
36   *
37   *   Revision 1.1.2.7  2004/09/27 22:46:53  dave
38   *   Added AdapterFile interface, with input and output stream API.
39   *
40   *   Revision 1.1.2.6  2004/09/24 01:36:18  dave
41   *   Refactored File as Node and Container ...
42   *
43   *   Revision 1.1.2.5  2004/09/24 01:12:09  dave
44   *   Added initial test for child nodes.
45   *
46   *   Revision 1.1.2.4  2004/09/23 16:32:02  dave
47   *   Added better Exception handling ....
48   *   Added initial mock container ....
49   *   Added initial root container tests ...
50   *
51   *   Revision 1.1.2.3  2004/09/23 12:21:31  dave
52   *   Added mock security service and login test ...
53   *
54   *   Revision 1.1.2.2  2004/09/23 10:12:19  dave
55   *   Added config properties for JUnit tests ....
56   *   Added test for null password.
57   *
58   *   Revision 1.1.2.1  2004/09/23 09:18:13  dave
59   *   Renamed AbstractTest to TestBase to exclude it from batch test ....
60   *   Added first test for null account ....
61   *
62   *   Revision 1.1.2.1  2004/09/22 16:47:37  dave
63   *   Added initial classes and tests for AladinAdapter.
64   *
65   * </cvs:log>
66   *
67   */
68  package org.astrogrid.store.tree;
69  
70  import org.astrogrid.store.Ivorn;
71  import org.astrogrid.store.util.MimeTypeUtil;
72  
73  import java.io.InputStream;
74  import java.io.OutputStream;
75  import java.util.Iterator;
76  
77  import junit.framework.TestCase;
78  
79  /***
80   * A JUnit test for the Aladin adapter.
81   *
82   */
83  public class TreeClientTest
84  	extends TestCase
85  	{
86  
87  	/***
88  	 * A test string.
89  	 * "A short test string ...."
90  	 *
91  	 */
92  	public static final String TEST_STRING = "A short test string ...." ;
93  
94  	/***
95  	 * A test byte array.
96  	 * "A short byte array ...."
97  	 *
98  	 */
99  	public static final byte[] TEST_BYTES = {
100 		0x41,
101 		0x20,
102 		0x73,
103 		0x68,
104 		0x6f,
105 		0x72,
106 		0x74,
107 		0x20,
108 		0x62,
109 		0x79,
110 		0x74,
111 		0x65,
112 		0x20,
113 		0x61,
114 		0x72,
115 		0x72,
116 		0x61,
117 		0x79,
118 		0x20,
119 		0x2e,
120 		0x2e,
121 		0x2e,
122 		0x2e
123 		} ;
124 
125 	/***
126 	 * Our target adapter.
127 	 *
128 	 */
129 	protected TreeClient adapter ;
130 
131 	/***
132 	 * Setup our test adapter.
133 	 *
134 	 */
135 	protected void setTestAdapter(TreeClient adapter)
136 		{
137 		this.adapter = adapter ;
138 		}
139 
140 	/***
141 	 * Our test account.
142 	 *
143 	 */
144 	protected Ivorn account ;
145 
146 	/***
147 	 * Setup our test account.
148 	 *
149 	 */
150 	public void setTestAccount(Ivorn ivorn)
151 		{
152 		this.account = ivorn ;
153 		}
154 
155 	/***
156 	 * Our test password.
157 	 *
158 	 */
159 	protected String password ;
160 
161 	/***
162 	 * Setup our test password.
163 	 *
164 	 */
165 	public void setTestPassword(String pass)
166 		{
167 		this.password = pass ;
168 		}
169 
170 	/***
171 	 * Our target container name.
172 	 *
173 	 */
174 	protected String container ;
175 
176 	/***
177 	 * Setup our container name.
178 	 *
179 	 */
180 	public void setContainerName(String name)
181 		{
182 		this.container = name ;
183 		}
184 
185 	/***
186 	 * Get our container name.
187 	 *
188 	 */
189 	public String getContainerName()
190 		{
191 		return this.container ;
192 		}
193 
194 	/***
195 	 * Create our container name.
196 	 * This uses the current timestamp to create a unique container name for each test.
197 	 *
198 	 */
199 	public void initContainerName()
200 		{
201 		this.setContainerName(
202 			"aladin-" + 
203 			String.valueOf(
204 				System.currentTimeMillis()
205 				)
206 			) ;
207 		}
208 
209 	/***
210 	 * Check we have an adapter.
211 	 *
212 	 */
213 	public void testAdapterNotNull()
214 		throws Exception
215 		{
216 		assertNotNull(
217 			adapter
218 			) ;
219 		}
220 
221 	/***
222 	 * Check we get the right Exception for a null account.
223 	 *
224 	 */
225 	public void testLoginNullAccount()
226 		throws Exception
227 		{
228 		try {
229 			adapter.login(
230 				null,
231 				password
232 				) ;
233 			}
234 		catch (IllegalArgumentException ouch)
235 			{
236 			return ;
237 			}
238 		fail("Expected IllegalArgumentException") ;
239 		}
240 
241 	/***
242 	 * Check we get the right Exception for a null password.
243 	 *
244 	 */
245 	public void testLoginNullPassword()
246 		throws Exception
247 		{
248 		try {
249 			adapter.login(
250 				account,
251 				null
252 				) ;
253 			}
254 		catch (IllegalArgumentException ouch)
255 			{
256 			return ;
257 			}
258 		fail("Expected IllegalArgumentException") ;
259 		}
260 
261 	/***
262 	 * Check we get the right Exception for the wrong password
263 	 *
264 	 */
265 	public void testLoginWrongPassword()
266 		throws Exception
267 		{
268 		try {
269 			adapter.login(
270 				account,
271 				(password + "WRONG")
272 				) ;
273 			}
274 		catch (TreeClientLoginException ouch)
275 			{
276 			return ;
277 			}
278 		fail("Expected AladinAdapterLoginException") ;
279 		}
280 
281 	/***
282 	 * Check we can login with the right password.
283 	 *
284 	 */
285 	public void testLoginValidPassword()
286 		throws Exception
287 		{
288 		//
289 		// Check we are not logged in.
290 		assertNull(
291 			adapter.getToken()
292 			) ;
293 		//
294 		// Login using our account and password.
295 		adapter.login(
296 			account,
297 			password
298 			) ;
299 		//
300 		// Check we are logged in.
301 		assertNotNull(
302 			adapter.getToken()
303 			) ;
304 		}
305 
306 	/***
307 	 * Check we get the right exception if we are not logged in.
308 	 *
309 	 */
310 	public void testGetRootFails()
311 		throws Exception
312 		{
313 		try {
314 			adapter.getRoot() ;
315 			}
316 		catch (TreeClientSecurityException ouch)
317 			{
318 			return ;
319 			}
320 		fail("Expected AladinAdapterSecurityException") ;
321 		}
322 
323 	/***
324 	 * Check we get the a root node if we are logged in.
325 	 *
326 	 */
327 	public void testGetRoot()
328 		throws Exception
329 		{
330 		//
331 		// Login using our account and password.
332 		adapter.login(
333 			account,
334 			password
335 			) ;
336 		//
337 		// Check we have a root node.
338 		Container root = adapter.getRoot();
339         assertNotNull(
340 			root
341 			) ;
342         assertTrue(root.isContainer());
343 		}
344 
345 	/***
346 	 * Check the root node has the default 'workflow' node.
347 	 *
348 	 */
349 	public void testWorkflowNode()
350 		throws Exception
351 		{
352 		//
353 		// Login.
354 		adapter.login(
355 			account,
356 			password
357 			) ;
358 		//
359 		// Get the root container.
360 		Container root = adapter.getRoot() ;
361 		//
362 		// Check the child nodes for the expected defaults.
363 		Iterator iter = root.getChildNodes().iterator() ; 
364 		while (iter.hasNext())
365 			{
366 			Node next = (Node) iter.next() ;
367 			//
368 			// Check for a 'workflow' node.
369 			if ("workflow".equals(next.getName()))
370 				{
371 				return ;
372 				}
373 			}
374 		fail("Expected to find workflow container") ;
375 		}
376 
377 	/***
378 	 * Check we can add a container.
379 	 *
380 	 */
381 	public void testAddContainer()
382 		throws Exception
383 		{
384 		//
385 		// Login.
386 		adapter.login(
387 			account,
388 			password
389 			) ;
390 		//
391 		// Get the root node.
392 		Container root = adapter.getRoot() ;
393 		//
394 		// Create our new container.
395 		assertNotNull(
396 			root.addContainer(
397 				this.getContainerName()
398 				)
399 			) ;
400 		}
401 
402 	/***
403 	 * Check we can find our a container.
404 	 *
405 	 */
406 	public void testFindContainer()
407 		throws Exception
408 		{
409 		//
410 		// Login.
411 		adapter.login(
412 			account,
413 			password
414 			) ;
415 		//
416 		// Get the root node.
417 		Container root = adapter.getRoot() ;
418 		//
419 		// Create our new container.
420 		root.addContainer(
421 			this.getContainerName()
422 			) ;
423 		//
424 		// Get an iterator for the child nodes.
425 		Node found = null ;
426 		Iterator iter = root.getChildNodes().iterator() ; 
427 		while (iter.hasNext())
428 			{
429 			Node next = (Node) iter.next() ;
430 			//
431 			// Check for a matching node.
432 			if (this.getContainerName().equals(next.getName()))
433 				{
434 				return ;
435 				}
436 			}
437 		fail("Expected to find aladin container") ;
438 		}
439 
440 	/*
441 	 * Check we get the right exception for a duplicate container.
442 	 *
443 	 */
444 	public void testDuplicateContainer()
445 		throws Exception
446 		{
447 		//
448 		// Login.
449 		adapter.login(
450 			account,
451 			password
452 			) ;
453 		//
454 		// Get the root node.
455 		Container root = adapter.getRoot() ;
456 		//
457 		// Create our new container.
458 		root.addContainer(
459 			this.getContainerName()
460 			) ;
461 		//
462 		// Try creating the same container again.
463 		try {
464 			root.addContainer(
465 				this.getContainerName()
466 				) ;
467 			}
468 		catch (TreeClientDuplicateException ouch)
469 			{
470 			return ;
471 			}
472 		fail("Expected AladinAdapterDuplicateException") ;
473 		}
474 
475 	/***
476 	 * Check we can add a file.
477 	 *
478 	 */
479 	public void testAddFile()
480 		throws Exception
481 		{
482 		//
483 		// Login.
484 		adapter.login(
485 			account,
486 			password
487 			) ;
488 		//
489 		// Get the root node.
490 		Container root = adapter.getRoot() ;
491 		//
492 		// Create our new container.
493 		Container node = root.addContainer(
494 			this.getContainerName()
495 			) ;
496 		//
497 		// Create a new (empty) file.
498 		assertNotNull(
499 			node.addFile(
500 				"data.txt"
501 				)
502 			) ;
503 		}
504 
505 	/*
506 	 * Check we get the right exception for a duplicate file.
507 	 *
508 	 */
509 	public void testDuplicateFile()
510 		throws Exception
511 		{
512 		//
513 		// Login.
514 		adapter.login(
515 			account,
516 			password
517 			) ;
518 		//
519 		// Get the root node.
520 		Container root = adapter.getRoot() ;
521 		//
522 		// Create our new container.
523 		Container node = root.addContainer(
524 			this.getContainerName()
525 			) ;
526 		//
527 		// Create our new file.
528 		node.addFile(
529 			"results.txt"
530 			) ;
531 		//
532 		// Try creating the same file again.
533 		try {
534 			node.addFile(
535 				"results.txt"
536 				) ;
537 			}
538 		catch (TreeClientDuplicateException ouch)
539 			{
540 			return ;
541 			}
542 		fail("Expected AladinAdapterDuplicateException") ;
543 		}
544 
545 	/***
546 	 * Check a new file appears in the list of child nodes.
547 	 *
548 	 */
549 	public void testFindFile()
550 		throws Exception
551 		{
552 		//
553 		// Login.
554 		adapter.login(
555 			account,
556 			password
557 			) ;
558 		//
559 		// Get the root node.
560 		Container root = adapter.getRoot() ;
561 		//
562 		// Create our new container.
563 		Container node = root.addContainer(
564 			this.getContainerName()
565 			) ;
566 		//
567 		// Create our new file.
568 		node.addFile(
569 			"results.txt"
570 			) ;
571 		//
572 		// Get an iterator for the child nodes.
573 		Node found = null ;
574 		Iterator iter = node.getChildNodes().iterator() ; 
575 		while (iter.hasNext())
576 			{
577 			Node next = (Node) iter.next() ;
578 			//
579 			// Check for a matching node.
580 			if ("results.txt".equals(next.getName()))
581 				{
582 				return ;
583 				}
584 			}
585 		fail("Expected to find results.txt") ;
586 		}
587 
588 	/***
589 	 * Check we can get an OutputStream for a file.
590 	 *
591 	 */
592 	public void testGetOutputStream()
593 		throws Exception
594 		{
595 		//
596 		// Login.
597 		adapter.login(
598 			account,
599 			password
600 			) ;
601 		//
602 		// Get the root node.
603 		Container root = adapter.getRoot() ;
604 		//
605 		// Create our new container.
606 		Container node = root.addContainer(
607 			this.getContainerName()
608 			) ;
609 		//
610 		// Create our new file.
611 		File file = node.addFile(
612 			"results.txt"
613 			) ;
614 		//
615 		// Get an OutputStream for the file.
616 		assertNotNull(
617 			file.getOutputStream()
618 			) ;
619 		}
620 
621 	/***
622 	 * Check we can transfer some data to the stream.
623 	 *
624 	 */
625 	public void testImportData()
626 		throws Exception
627 		{
628 		//
629 		// Login.
630 		adapter.login(
631 			account,
632 			password
633 			) ;
634 		//
635 		// Get the root node.
636 		Container root = adapter.getRoot() ;
637 		//
638 		// Create our new container.
639 		Container node = root.addContainer(
640 			this.getContainerName()
641 			) ;
642 		//
643 		// Create our new file.
644 		File file = node.addFile(
645 			"results.txt"
646 			) ;
647 		//
648 		// Get an OutputStream for the file.
649 		OutputStream stream = file.getOutputStream() ;
650 		//
651 		// Transfer some data to the stream.
652 		stream.write(
653 			TEST_BYTES
654 			) ;
655 		//
656 		// Close the stream.
657 		stream.close() ;
658 		}
659 
660 	/***
661 	 * Check we can get an InputStream for a file.
662 	 *
663 	 */
664 	public void testGetInputStream()
665 		throws Exception
666 		{
667 		//
668 		// Login.
669 		adapter.login(
670 			account,
671 			password
672 			) ;
673 		//
674 		// Get the root node.
675 		Container root = adapter.getRoot() ;
676 		//
677 		// Create our new container.
678 		Container node = root.addContainer(
679 			this.getContainerName()
680 			) ;
681 		//
682 		// Create our new file.
683 		File file = node.addFile(
684 			"results.txt"
685 			) ;
686 		//
687 		// Get an InputStream for the file.
688 		assertNotNull(
689 			file.getInputStream()
690 			) ;
691 		}
692 
693 	/***
694 	 * Check we can transfer some data from the stream.
695 	 *
696 	 */
697 	public void testImportExportData()
698 		throws Exception
699 		{
700 		//
701 		// Login.
702 		adapter.login(
703 			account,
704 			password
705 			) ;
706 		//
707 		// Get the root node.
708 		Container root = adapter.getRoot() ;
709 		//
710 		// Create our new container.
711 		Container node = root.addContainer(
712 			this.getContainerName()
713 			) ;
714 		//
715 		// Create our new file.
716 		File file = node.addFile(
717 			"results.txt"
718 			) ;
719 		//
720 		// Get an OutputStream for the file.
721 		OutputStream output = file.getOutputStream() ;
722 		//
723 		// Transfer some data to the stream.
724 		output.write(
725 			TEST_BYTES
726 			) ;
727 		//
728 		// Close the stream.
729 		output.close() ;
730 		//
731 		// Get an InputStream for the file.
732 		InputStream input = file.getInputStream() ;
733 		//
734 		// Read some data from the stream.
735 		byte[] data = new byte[TEST_BYTES.length] ;
736 		input.read(data) ;
737 		//
738 		// Check we get the same data back ...
739 		for (int i = 0 ; i < TEST_BYTES.length ; i++)
740 			{
741 			assertEquals(
742 				TEST_BYTES[i],
743 				data[i]
744 				) ;
745 			}
746 		}
747 
748 	/***
749 	 * Check we get the right mime type for an unknown type.
750 	 *
751 	 */
752 	public void testGetMimeUnknown()
753 		throws Exception
754 		{
755 		//
756 		// Login.
757 		adapter.login(
758 			account,
759 			password
760 			) ;
761 		//
762 		// Get the root node.
763 		Container root = adapter.getRoot() ;
764 		//
765 		// Create our new container.
766 		Container node = root.addContainer(
767 			this.getContainerName()
768 			) ;
769 		//
770 		// Create our new file.
771 		File file = node.addFile(
772 			"results.unknown"
773 			) ;
774 		//
775 		// Check the mime type.
776 		assertNull(
777 			file.getMimeType()
778 			) ;
779 		}
780 
781 	/***
782 	 * Check we get the right mime type for an xml file.
783 	 *
784 	 */
785 	public void testGetMimeXml()
786 		throws Exception
787 		{
788 		//
789 		// Login.
790 		adapter.login(
791 			account,
792 			password
793 			) ;
794 		//
795 		// Get the root node.
796 		Container root = adapter.getRoot() ;
797 		//
798 		// Create our new container.
799 		Container node = root.addContainer(
800 			this.getContainerName()
801 			) ;
802 		//
803 		// Create our new file.
804 		File file = node.addFile(
805 			"results.xml"
806 			) ;
807 		//
808 		// Check the mime type.
809 		assertEquals(
810 			MimeTypeUtil.MIME_TYPE_XML,
811 			file.getMimeType()
812 			) ;
813 		}
814 
815 	/***
816 	 * Check we get the right mime type for an votable file.
817 	 *
818 	 */
819 	public void testGetMimeVot()
820 		throws Exception
821 		{
822 		//
823 		// Login.
824 		adapter.login(
825 			account,
826 			password
827 			) ;
828 		//
829 		// Get the root node.
830 		Container root = adapter.getRoot() ;
831 		//
832 		// Create our new container.
833 		Container node = root.addContainer(
834 			this.getContainerName()
835 			) ;
836 		//
837 		// Create our new file.
838 		File file = node.addFile(
839 			"results.vot"
840 			) ;
841 		//
842 		// Check the mime type.
843 		assertEquals(
844 			MimeTypeUtil.MIME_TYPE_VOTABLE,
845 			file.getMimeType()
846 			) ;
847 		}
848 
849 	/***
850 	 * Check we get the right mime type for an votable file.
851 	 *
852 	 */
853 	public void testGetMimeVotable()
854 		throws Exception
855 		{
856 		//
857 		// Login.
858 		adapter.login(
859 			account,
860 			password
861 			) ;
862 		//
863 		// Get the root node.
864 		Container root = adapter.getRoot() ;
865 		//
866 		// Create our new container.
867 		Container node = root.addContainer(
868 			this.getContainerName()
869 			) ;
870 		//
871 		// Create our new file.
872 		File file = node.addFile(
873 			"results.votable"
874 			) ;
875 		//
876 		// Check the mime type.
877 		assertEquals(
878 			MimeTypeUtil.MIME_TYPE_VOTABLE,
879 			file.getMimeType()
880 			) ;
881 		}
882 
883 	/***
884 	 * Check we get the right exception if we logout.
885 	 *
886 	 */
887 	public void testLogout()
888 		throws Exception
889 		{
890 		//
891 		// Check we get an exception if we are not logged in.
892 		try {
893 			adapter.getRoot() ;
894 			}
895 		catch (TreeClientSecurityException ouch)
896 			{
897 			return ;
898 			}
899 		fail("Expected AladinAdapterSecurityException") ;
900 		//
901 		// Login.
902 		adapter.login(
903 			account,
904 			password
905 			) ;
906 		//
907 		// Check we can get the root node.
908 		assertNotNull(
909 			adapter.getRoot()
910 			) ;
911 		//
912 		// Logout.
913 		adapter.logout() ;
914 		//
915 		// Check we get an exception having logged out.
916 		try {
917 			adapter.getRoot() ;
918 			}
919 		catch (TreeClientSecurityException ouch)
920 			{
921 			return ;
922 			}
923 		fail("Expected AladinAdapterSecurityException") ;
924 		}
925 
926 
927 	}