1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
291 assertNull(
292 adapter.getToken()
293 ) ;
294
295
296 adapter.login(
297 account,
298 password
299 ) ;
300
301
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
333 adapter.login(
334 account,
335 password
336 ) ;
337
338
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
355 adapter.login(
356 account,
357 password
358 ) ;
359
360
361 AladinAdapterContainer root = adapter.getRoot() ;
362
363
364 Iterator iter = root.getChildNodes().iterator() ;
365 while (iter.hasNext())
366 {
367 AladinAdapterNode next = (AladinAdapterNode) iter.next() ;
368
369
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
387 adapter.login(
388 account,
389 password
390 ) ;
391
392
393 AladinAdapterContainer root = adapter.getRoot() ;
394
395
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
412 adapter.login(
413 account,
414 password
415 ) ;
416
417
418 AladinAdapterContainer root = adapter.getRoot() ;
419
420
421 root.addContainer(
422 this.getContainerName()
423 ) ;
424
425
426 AladinAdapterNode found = null ;
427 Iterator iter = root.getChildNodes().iterator() ;
428 while (iter.hasNext())
429 {
430 AladinAdapterNode next = (AladinAdapterNode) iter.next() ;
431
432
433 if (this.getContainerName().equals(next.getName()))
434 {
435 return ;
436 }
437 }
438 fail("Expected to find aladin container") ;
439 }
440
441
442
443
444
445 public void testDuplicateContainer()
446 throws Exception
447 {
448
449
450 adapter.login(
451 account,
452 password
453 ) ;
454
455
456 AladinAdapterContainer root = adapter.getRoot() ;
457
458
459 root.addContainer(
460 this.getContainerName()
461 ) ;
462
463
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
485 adapter.login(
486 account,
487 password
488 ) ;
489
490
491 AladinAdapterContainer root = adapter.getRoot() ;
492
493
494 AladinAdapterContainer node = root.addContainer(
495 this.getContainerName()
496 ) ;
497
498
499 assertNotNull(
500 node.addFile(
501 "data.txt"
502 )
503 ) ;
504 }
505
506
507
508
509
510 public void testDuplicateFile()
511 throws Exception
512 {
513
514
515 adapter.login(
516 account,
517 password
518 ) ;
519
520
521 AladinAdapterContainer root = adapter.getRoot() ;
522
523
524 AladinAdapterContainer node = root.addContainer(
525 this.getContainerName()
526 ) ;
527
528
529 node.addFile(
530 "results.txt"
531 ) ;
532
533
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
555 adapter.login(
556 account,
557 password
558 ) ;
559
560
561 AladinAdapterContainer root = adapter.getRoot() ;
562
563
564 AladinAdapterContainer node = root.addContainer(
565 this.getContainerName()
566 ) ;
567
568
569 node.addFile(
570 "results.txt"
571 ) ;
572
573
574 AladinAdapterNode found = null ;
575 Iterator iter = node.getChildNodes().iterator() ;
576 while (iter.hasNext())
577 {
578 AladinAdapterNode next = (AladinAdapterNode) iter.next() ;
579
580
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
598 adapter.login(
599 account,
600 password
601 ) ;
602
603
604 AladinAdapterContainer root = adapter.getRoot() ;
605
606
607 AladinAdapterContainer node = root.addContainer(
608 this.getContainerName()
609 ) ;
610
611
612 AladinAdapterFile file = node.addFile(
613 "results.txt"
614 ) ;
615
616
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
631 adapter.login(
632 account,
633 password
634 ) ;
635
636
637 AladinAdapterContainer root = adapter.getRoot() ;
638
639
640 AladinAdapterContainer node = root.addContainer(
641 this.getContainerName()
642 ) ;
643
644
645 AladinAdapterFile file = node.addFile(
646 "results.txt"
647 ) ;
648
649
650 OutputStream stream = file.getOutputStream() ;
651
652
653 stream.write(
654 TEST_BYTES
655 ) ;
656
657
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
670 adapter.login(
671 account,
672 password
673 ) ;
674
675
676 AladinAdapterContainer root = adapter.getRoot() ;
677
678
679 AladinAdapterContainer node = root.addContainer(
680 this.getContainerName()
681 ) ;
682
683
684 AladinAdapterFile file = node.addFile(
685 "results.txt"
686 ) ;
687
688
689 assertNotNull(
690 file.getInputStream()
691 ) ;
692 }
693
694 public void testGetURL() throws Exception {
695 adapter.login(account,password);
696
697 AladinAdapterContainer root = adapter.getRoot() ;
698
699
700 AladinAdapterContainer node = root.addContainer(
701 this.getContainerName()
702 ) ;
703
704
705 AladinAdapterFile file = node.addFile(
706 "results.txt"
707 ) ;
708
709
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
724 adapter.login(
725 account,
726 password
727 ) ;
728
729
730 AladinAdapterContainer root = adapter.getRoot() ;
731
732
733 AladinAdapterContainer node = root.addContainer(
734 this.getContainerName()
735 ) ;
736
737
738 AladinAdapterFile file = node.addFile(
739 "results.txt"
740 ) ;
741
742
743 OutputStream output = file.getOutputStream() ;
744
745
746 output.write(
747 TEST_BYTES
748 ) ;
749
750
751 output.close() ;
752
753
754 InputStream input = file.getInputStream() ;
755
756
757 byte[] data = new byte[TEST_BYTES.length] ;
758 input.read(data) ;
759
760
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
779 adapter.login(
780 account,
781 password
782 ) ;
783
784
785 AladinAdapterContainer root = adapter.getRoot() ;
786
787
788 AladinAdapterContainer node = root.addContainer(
789 this.getContainerName()
790 ) ;
791
792
793 AladinAdapterFile file = node.addFile(
794 "results.unknown"
795 ) ;
796
797
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
812 adapter.login(
813 account,
814 password
815 ) ;
816
817
818 AladinAdapterContainer root = adapter.getRoot() ;
819
820
821 AladinAdapterContainer node = root.addContainer(
822 this.getContainerName()
823 ) ;
824
825
826 AladinAdapterFile file = node.addFile(
827 "results.xml"
828 ) ;
829
830
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
846 adapter.login(
847 account,
848 password
849 ) ;
850
851
852 AladinAdapterContainer root = adapter.getRoot() ;
853
854
855 AladinAdapterContainer node = root.addContainer(
856 this.getContainerName()
857 ) ;
858
859
860 AladinAdapterFile file = node.addFile(
861 "results.vot"
862 ) ;
863
864
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
880 adapter.login(
881 account,
882 password
883 ) ;
884
885
886 AladinAdapterContainer root = adapter.getRoot() ;
887
888
889 AladinAdapterContainer node = root.addContainer(
890 this.getContainerName()
891 ) ;
892
893
894 AladinAdapterFile file = node.addFile(
895 "results.votable"
896 ) ;
897
898
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
914 try {
915 adapter.getRoot() ;
916 }
917 catch (AladinAdapterSecurityException ouch)
918 {
919 return ;
920 }
921 fail("Expected AladinAdapterSecurityException") ;
922
923
924 adapter.login(
925 account,
926 password
927 ) ;
928
929
930 assertNotNull(
931 adapter.getRoot()
932 ) ;
933
934
935 adapter.logout() ;
936
937
938 try {
939 adapter.getRoot() ;
940 }
941 catch (AladinAdapterSecurityException ouch)
942 {
943 return ;
944 }
945 fail("Expected AladinAdapterSecurityException") ;
946 }
947
948
949 }