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 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
290 assertNull(
291 adapter.getToken()
292 ) ;
293
294
295 adapter.login(
296 account,
297 password
298 ) ;
299
300
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
332 adapter.login(
333 account,
334 password
335 ) ;
336
337
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
354 adapter.login(
355 account,
356 password
357 ) ;
358
359
360 Container root = adapter.getRoot() ;
361
362
363 Iterator iter = root.getChildNodes().iterator() ;
364 while (iter.hasNext())
365 {
366 Node next = (Node) iter.next() ;
367
368
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
386 adapter.login(
387 account,
388 password
389 ) ;
390
391
392 Container root = adapter.getRoot() ;
393
394
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
411 adapter.login(
412 account,
413 password
414 ) ;
415
416
417 Container root = adapter.getRoot() ;
418
419
420 root.addContainer(
421 this.getContainerName()
422 ) ;
423
424
425 Node found = null ;
426 Iterator iter = root.getChildNodes().iterator() ;
427 while (iter.hasNext())
428 {
429 Node next = (Node) iter.next() ;
430
431
432 if (this.getContainerName().equals(next.getName()))
433 {
434 return ;
435 }
436 }
437 fail("Expected to find aladin container") ;
438 }
439
440
441
442
443
444 public void testDuplicateContainer()
445 throws Exception
446 {
447
448
449 adapter.login(
450 account,
451 password
452 ) ;
453
454
455 Container root = adapter.getRoot() ;
456
457
458 root.addContainer(
459 this.getContainerName()
460 ) ;
461
462
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
484 adapter.login(
485 account,
486 password
487 ) ;
488
489
490 Container root = adapter.getRoot() ;
491
492
493 Container node = root.addContainer(
494 this.getContainerName()
495 ) ;
496
497
498 assertNotNull(
499 node.addFile(
500 "data.txt"
501 )
502 ) ;
503 }
504
505
506
507
508
509 public void testDuplicateFile()
510 throws Exception
511 {
512
513
514 adapter.login(
515 account,
516 password
517 ) ;
518
519
520 Container root = adapter.getRoot() ;
521
522
523 Container node = root.addContainer(
524 this.getContainerName()
525 ) ;
526
527
528 node.addFile(
529 "results.txt"
530 ) ;
531
532
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
554 adapter.login(
555 account,
556 password
557 ) ;
558
559
560 Container root = adapter.getRoot() ;
561
562
563 Container node = root.addContainer(
564 this.getContainerName()
565 ) ;
566
567
568 node.addFile(
569 "results.txt"
570 ) ;
571
572
573 Node found = null ;
574 Iterator iter = node.getChildNodes().iterator() ;
575 while (iter.hasNext())
576 {
577 Node next = (Node) iter.next() ;
578
579
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
597 adapter.login(
598 account,
599 password
600 ) ;
601
602
603 Container root = adapter.getRoot() ;
604
605
606 Container node = root.addContainer(
607 this.getContainerName()
608 ) ;
609
610
611 File file = node.addFile(
612 "results.txt"
613 ) ;
614
615
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
630 adapter.login(
631 account,
632 password
633 ) ;
634
635
636 Container root = adapter.getRoot() ;
637
638
639 Container node = root.addContainer(
640 this.getContainerName()
641 ) ;
642
643
644 File file = node.addFile(
645 "results.txt"
646 ) ;
647
648
649 OutputStream stream = file.getOutputStream() ;
650
651
652 stream.write(
653 TEST_BYTES
654 ) ;
655
656
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
669 adapter.login(
670 account,
671 password
672 ) ;
673
674
675 Container root = adapter.getRoot() ;
676
677
678 Container node = root.addContainer(
679 this.getContainerName()
680 ) ;
681
682
683 File file = node.addFile(
684 "results.txt"
685 ) ;
686
687
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
702 adapter.login(
703 account,
704 password
705 ) ;
706
707
708 Container root = adapter.getRoot() ;
709
710
711 Container node = root.addContainer(
712 this.getContainerName()
713 ) ;
714
715
716 File file = node.addFile(
717 "results.txt"
718 ) ;
719
720
721 OutputStream output = file.getOutputStream() ;
722
723
724 output.write(
725 TEST_BYTES
726 ) ;
727
728
729 output.close() ;
730
731
732 InputStream input = file.getInputStream() ;
733
734
735 byte[] data = new byte[TEST_BYTES.length] ;
736 input.read(data) ;
737
738
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
757 adapter.login(
758 account,
759 password
760 ) ;
761
762
763 Container root = adapter.getRoot() ;
764
765
766 Container node = root.addContainer(
767 this.getContainerName()
768 ) ;
769
770
771 File file = node.addFile(
772 "results.unknown"
773 ) ;
774
775
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
790 adapter.login(
791 account,
792 password
793 ) ;
794
795
796 Container root = adapter.getRoot() ;
797
798
799 Container node = root.addContainer(
800 this.getContainerName()
801 ) ;
802
803
804 File file = node.addFile(
805 "results.xml"
806 ) ;
807
808
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
824 adapter.login(
825 account,
826 password
827 ) ;
828
829
830 Container root = adapter.getRoot() ;
831
832
833 Container node = root.addContainer(
834 this.getContainerName()
835 ) ;
836
837
838 File file = node.addFile(
839 "results.vot"
840 ) ;
841
842
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
858 adapter.login(
859 account,
860 password
861 ) ;
862
863
864 Container root = adapter.getRoot() ;
865
866
867 Container node = root.addContainer(
868 this.getContainerName()
869 ) ;
870
871
872 File file = node.addFile(
873 "results.votable"
874 ) ;
875
876
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
892 try {
893 adapter.getRoot() ;
894 }
895 catch (TreeClientSecurityException ouch)
896 {
897 return ;
898 }
899 fail("Expected AladinAdapterSecurityException") ;
900
901
902 adapter.login(
903 account,
904 password
905 ) ;
906
907
908 assertNotNull(
909 adapter.getRoot()
910 ) ;
911
912
913 adapter.logout() ;
914
915
916 try {
917 adapter.getRoot() ;
918 }
919 catch (TreeClientSecurityException ouch)
920 {
921 return ;
922 }
923 fail("Expected AladinAdapterSecurityException") ;
924 }
925
926
927 }