View Javadoc

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