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 public class DefaultParametersTest extends TestCase
44 {
45 /*** parameter container */
46 protected DefaultParameters params;
47 protected long anyTimeStamp = 123123132L;
48 protected long anyTimeStamp2 = 232342445L;
49
50 /***
51 * @see TestCase#setUp()
52 */
53 protected void setUp() throws Exception
54 {
55 super.setUp();
56 params = new DefaultParameters();
57 }
58
59 /***
60 * Constructor for ParametersImplTest.
61 *
62 * @param arg0 default arg.
63 */
64 public DefaultParametersTest(String arg0)
65 {
66 super(arg0);
67 }
68
69 /***
70 * Test for void ParametersImpl()
71 */
72 public void testParametersImpl()
73 {
74
75 }
76
77 /***
78 * Test for void ParametersImpl(String)
79 */
80 public void testParametersImplString()
81 {
82 params = new DefaultParameters("foo=bar\n");
83 assertEquals(params.get("foo"), "bar");
84 params = new DefaultParameters("foo=bar,buzz,foo\nbar=foo");
85 assertEquals(params.getStrings("foo").length, 3);
86 assertEquals(params.get("bar"), "foo");
87 params = new DefaultParameters("");
88 assertEquals(params.get("foo", "bar"), "bar");
89 try
90 {
91 params = new DefaultParameters("foo");
92 fail("Should throw IllegalArgumentException");
93 }
94 catch (IllegalArgumentException e)
95 {
96
97 }
98 try
99 {
100 params = new DefaultParameters((String)null);
101 fail("Should throw IllegalArgumentException");
102 }
103 catch (IllegalArgumentException e)
104 {
105
106 }
107
108
109 String source = "foo=bar,foo\nbar=foo\n";
110 params = new DefaultParameters(source);
111 assertEquals(params.get("bar","bar"),"foo");
112 assertEquals(params.getParameterNames().length,2);
113
114 source = "foo=bar,f//\noo\nbar=foo\n";
115 params = new DefaultParameters(source);
116 assertEquals(params.get("bar","bar"),"foo");
117 assertEquals(params.getParameterNames().length,2);
118
119 }
120
121 /***
122 * Test for void ParametersImpl(InputStream, String)
123 *
124 */
125 public void testParametersImplInputStreamString()
126 throws Exception
127 {
128 InputStream is = new ByteArrayInputStream("foo=bar\n".getBytes());
129 params = new DefaultParameters(is, "ISO-8859-1");
130 assertEquals(params.get("foo"), "bar");
131 params = new DefaultParameters("foo=bar,buzz,foo\nbar=foo");
132 assertEquals(params.getStrings("foo").length, 3);
133 assertEquals(params.get("bar"), "foo");
134 params = new DefaultParameters("");
135 assertEquals(params.get("foo", "bar"), "bar");
136 }
137
138 /***
139 * Test for void ParametersImpl(Parameters)
140 */
141 public void testParametersImplParameters()
142 {
143 params = new DefaultParameters();
144 params.add("foo", "bar");
145 params.add("foo", "buz");
146 params.add("bar", "foo");
147 params = new DefaultParameters(params);
148 assertEquals(params.getParameterNames().length, 2);
149 }
150
151 /***
152 * Test for String get(String)
153 */
154 public void testGet()
155 {
156 try
157 {
158 assertEquals(params.get("foo"), "bar");
159 fail("Should throw UndefinedParameterException");
160 }
161 catch (UndefinedParameterException e)
162 {
163
164 }
165 params.add("foo", "bar");
166 assertEquals(params.get("foo"), "bar");
167 params.add("foo", "true");
168 try
169 {
170 assertEquals(params.get("foo"), "bar");
171 fail("Should throw AmbiguousParameterException");
172 }
173 catch (AmbiguousParameterException e)
174 {
175
176 }
177 }
178
179 /***
180 * Test for String get(String, String)
181 */
182 public void testGetStringString()
183 {
184 assertEquals(params.get("foo", "bar"), "bar");
185 params.add("foo", "bar");
186 assertEquals(params.get("foo", "buzz"), "bar");
187 params.add("foo", "buzz");
188 try
189 {
190 assertEquals(params.get("foo", "buzz"), "bar");
191 fail("Should throw AmbiguousParameterException");
192 }
193 catch (AmbiguousParameterException e)
194 {
195
196 }
197 }
198
199 /***
200 * Test for String getStrings(String)
201 */
202 public void testGetStrings()
203 {
204 assertEquals(params.getStrings("foo").length, 0);
205 params.add("foo", "bar");
206 params.add("foo", "true");
207 assertEquals(params.getStrings("foo").length, 2);
208 assertEquals(params.getStrings("foo")[0], "bar");
209 assertEquals(params.getStrings("foo")[1], "true");
210 }
211
212 /***
213 * Test for boolean getBoolean(String)
214 */
215 public void testGetBooleanString()
216 {
217 try
218 {
219 assertEquals(params.getBoolean("foo"), false);
220 fail("Should throw UndefinedParameterException");
221 }
222 catch (UndefinedParameterException e)
223 {
224
225 }
226 params.add("foo", "bar");
227 assertEquals(params.getBoolean("foo"), false);
228 params.set("foo", "true");
229 assertEquals(params.getBoolean("foo"), true);
230 }
231
232 /***
233 * Test for boolean getBoolean(String, boolean)
234 */
235 public void testGetBooleanStringboolean()
236 {
237 assertEquals(params.getBoolean("foo", false), false);
238 params.add("foo", "bar");
239 assertEquals(params.getBoolean("foo"), false);
240 params.set("foo", "true");
241 assertEquals(params.getBoolean("foo", false), true);
242 params.set("foo", true);
243 assertEquals(params.getBoolean("foo", false), true);
244 }
245
246 /***
247 * Test for boolean getBooleans(String)
248 */
249 public void testGetBooleans()
250 {
251 assertEquals(params.getBooleans("foo").length, 0);
252 params.add("foo", "bar");
253 params.add("foo", "true");
254 assertEquals(params.getBooleans("foo").length, 2);
255 assertEquals(params.getBooleans("foo")[0], false);
256 assertEquals(params.getBooleans("foo")[1], true);
257 }
258
259 /***
260 * Test for boolean getDate(String)
261 */
262 public void testGetDateString()
263 {
264 try
265 {
266 assertEquals(params.getDate("foo"), new Date());
267 fail("Should throw UndefinedParameterException");
268 }
269 catch (UndefinedParameterException e)
270 {
271
272 }
273 params.add("foo", new Date(anyTimeStamp));
274 assertEquals(params.getDate("foo"), new Date(anyTimeStamp));
275 params.add("foo", new Date(anyTimeStamp2));
276 try
277 {
278 params.getDate("foo");
279 fail("Should throw AmbiguousParameterException");
280 }
281 catch (AmbiguousParameterException e)
282 {
283
284 }
285 params.set("foo", "bar");
286 try
287 {
288 params.getDate("foo");
289 fail("Should throw NumberFormatException");
290 }
291 catch (NumberFormatException e)
292 {
293
294 }
295 }
296
297 /***
298 * Test for boolean getDate(String, Date)
299 */
300 public void testGetDateStringDate()
301 {
302 assertEquals(params.getDate("foo", new Date(anyTimeStamp)), new Date(anyTimeStamp));
303 params.add("foo", new Date(anyTimeStamp2));
304 assertEquals(params.getDate("foo", new Date(anyTimeStamp)), new Date(anyTimeStamp2));
305 params.add("boo", "");
306 assertEquals(params.getDate("boo", new Date(anyTimeStamp)), new Date(anyTimeStamp));
307 try
308 {
309 params.add("doo", "adasda");
310 params.getDate("doo", new Date(anyTimeStamp2));
311 fail("Should throw NumberFormatException");
312 }
313 catch (NumberFormatException e)
314 {
315
316 }
317 }
318
319 /***
320 * Test for boolean getDates(String)
321 */
322 public void testGetDates()
323 {
324 assertEquals(params.getDates("foo").length, 0);
325 params.add("foo", new Date(anyTimeStamp));
326 params.add("foo", new Date(anyTimeStamp2));
327 assertEquals(params.getDates("foo").length, 2);
328 assertEquals(params.getDates("foo")[0], new Date(anyTimeStamp));
329 assertEquals(params.getDates("foo")[1], new Date(anyTimeStamp2));
330 }
331
332 /***
333 * Test for float getFloat(String)
334 */
335 public void testGetFloatString()
336 {
337 try
338 {
339 assertEquals(params.getFloat("foo"), 1, 1);
340 fail("Should throw UndefinedParameterException");
341 }
342 catch (UndefinedParameterException e)
343 {
344
345 }
346 params.add("foo", 1);
347 assertEquals(params.getFloat("foo"), 1, 1);
348 params.add("foo", 2);
349 try
350 {
351 params.getFloat("foo");
352 fail("Should throw AmbiguousParameterException");
353 }
354 catch (AmbiguousParameterException e)
355 {
356
357 }
358 params.set("foo", "bar");
359 try
360 {
361 params.getFloat("foo");
362 fail("Should throw NumberFormatException");
363 }
364 catch (NumberFormatException e)
365 {
366
367 }
368 }
369
370 /***
371 * Test for float getFloat(String, float)
372 */
373 public void testGetFloatStringfloat()
374 {
375 assertEquals(params.getFloat("foo", 1.5F), 1.5F, 1.5F);
376 params.add("foo", 2.5F);
377 assertEquals(params.getFloat("foo", 1.5F), 2.5F, 2.5F);
378 }
379
380 /***
381 * Test for float getFloats(String)
382 */
383 public void testGetFloats()
384 {
385 params.add("foo", 2.5F);
386 assertEquals(params.getFloats("foo")[0], 2.5F, 3.5F);
387 params.add("foo", 2.5F);
388 assertEquals(params.getFloats("foo")[1], 2.5F, 2.5F);
389 }
390
391 /***
392 * Test for int getInt(String)
393 */
394 public void testGetIntString()
395 {
396 try
397 {
398 assertEquals(params.getInt("foo"), 1, 1);
399 fail("Should throw UndefinedParameterException");
400 }
401 catch (UndefinedParameterException e)
402 {
403
404 }
405 params.add("foo", 1);
406 assertEquals(params.getInt("foo"), 1, 1);
407 params.add("foo", 2);
408 try
409 {
410 params.getInt("foo");
411 fail("Should throw AmbiguousParameterException");
412 }
413 catch (AmbiguousParameterException e)
414 {
415
416 }
417 params.set("foo", "bar");
418 try
419 {
420 params.getInt("foo");
421 fail("Should throw NumberFormatException");
422 }
423 catch (NumberFormatException e)
424 {
425
426 }
427
428 }
429
430 /***
431 * Test for int getInt(String, int)
432 */
433 public void testGetIntStringint()
434 {
435 assertEquals(params.getInt("foo", 1), 1, 1);
436 params.add("foo", 1);
437 assertEquals(params.getInt("foo", 1), 1, 1);
438 }
439
440 /***
441 * Test for get ints.
442 */
443 public void testGetInts()
444 {
445 params.add("foo", 2);
446 assertEquals(params.getInts("foo")[0], 2, 2);
447 params.add("foo", 2);
448 assertEquals(params.getInts("foo")[1], 2, 2);
449 }
450
451 /***
452 * Test for long getLong(String)
453 */
454 public void testGetLongString()
455 {
456 try
457 {
458 assertEquals(params.getLong("foo"), 1, 1);
459 fail("Should throw UndefinedParameterException");
460 }
461 catch (UndefinedParameterException e)
462 {
463
464 }
465 params.add("foo", 1);
466 assertEquals(params.getLong("foo"), 1, 1);
467 params.add("foo", 2);
468 try
469 {
470 params.getLong("foo");
471 fail("Should throw AmbiguousParameterException");
472 }
473 catch (AmbiguousParameterException e)
474 {
475
476 }
477 params.set("foo", "bar");
478 try
479 {
480 params.getLong("foo");
481 fail("Should throw NumberFormatException");
482 }
483 catch (NumberFormatException e)
484 {
485
486 }
487 }
488
489 /***
490 * Test for long getLong(String, long)
491 */
492 public void testGetLongStringlong()
493 {
494 assertEquals(params.getLong("foo", 1), 1, 1);
495 params.add("foo", 1);
496 assertEquals(params.getLong("foo", 1), 1, 1);
497 }
498
499 /***
500 * Test for long getLongs()
501 */
502 public void testGetLongs()
503 {
504 params.add("foo", 2);
505 assertEquals(params.getLongs("foo")[0], 2, 2);
506 params.add("foo", 2);
507 assertEquals(params.getLongs("foo")[1], 2, 2);
508 }
509
510 /***
511 * Test for getParameterNames()
512 */
513 public void testGetParameterNames()
514 {
515 params.set("foo", "bar");
516 params.set("foo", "buzz");
517 params.set("bar", "buzz");
518 params.set("buzz", "bar");
519 assertEquals(params.getParameterNames().length, 3);
520 }
521
522 /***
523 * Test for boolean isDefined()
524 */
525 public void testIsDefined()
526 {
527 assertEquals(params.isDefined("foo"), false);
528 params.set("foo", "bar");
529 assertEquals(params.isDefined("foo"), true);
530 params.remove("foo");
531 assertEquals(params.isDefined("foo"), false);
532 }
533
534 /***
535 * Test for void remove()
536 */
537 public void testRemove()
538 {
539 params.set("foo", "bar");
540 params.set("bar", "foo");
541 params.remove();
542 assertEquals(params.isDefined("foo"), false);
543 assertEquals(params.isDefined("foo"), false);
544 }
545
546 /***
547 * Test for void remove(String)
548 */
549 public void testRemoveString()
550 {
551
552 }
553
554 /***
555 * Test for void remove(String, String)
556 */
557 public void testRemoveStringString()
558 {
559 params.set("foo", "bar");
560 params.set("foo", "foo");
561 params.remove("foo", "bar");
562 params.remove("bar", "foo");
563 assertEquals(params.isDefined("foo"), true);
564 assertEquals(params.get("foo", "bar"), "foo");
565 }
566
567 /***
568 * Test for void remove(String, float)
569 */
570 public void testRemoveStringfloat()
571 {
572 params.set("foo", 1F);
573 params.set("foo", 2F);
574 params.remove("foo", 1F);
575 assertEquals(params.isDefined("foo"), true);
576 assertEquals(params.getFloat("foo", 1F), 2F, 2F);
577 }
578
579 /***
580 * Test for void remove(String, int)
581 */
582 public void testRemoveStringint()
583 {
584 params.set("foo", 1);
585 params.set("foo", 2);
586 params.remove("foo", 1);
587 assertEquals(params.isDefined("foo"), true);
588 assertEquals(params.getFloat("foo", 1), 2, 2);
589 }
590
591 /***
592 * Test for void remove(String, long)
593 */
594 public void testRemoveStringlong()
595 {
596 params.set("foo", 1L);
597 params.set("foo", 2L);
598 params.remove("foo", 1L);
599 assertEquals(params.isDefined("foo"), true);
600 assertEquals(params.getFloat("foo", 1L), 2L, 2L);
601 }
602
603 /***
604 * Test for void remove(Set)
605 */
606 public void testRemoveSet()
607 {
608 params.set("foo", "bar");
609 params.set("bar", "foo");
610 Set<String> set = new HashSet<String>();
611 set.add("foo");
612 params.remove(set);
613 assertEquals(params.isDefined("foo"), false);
614 assertEquals(params.isDefined("bar"),true);
615 }
616
617 /***
618 * Test for void remove(Set)
619 */
620 public void testRemoveExcept()
621 {
622 params.set("foo", "bar");
623 params.set("bar", "foo");
624 Set<String> set = new HashSet<String>();
625 set.add("foo");
626 params.removeExcept(set);
627 assertEquals(params.isDefined("bar"), false);
628 assertEquals(params.isDefined("foo"),true);
629 }
630
631
632 /***
633 * Test for void set(String, String)
634 */
635 public void testSetStringString()
636 {
637 params.set("foo", "bar");
638 assertEquals("bar".equals(params.get("foo")), true);
639 params.set("foo", "buzz");
640 assertEquals("bar".equals(params.get("foo")), false);
641 params.add("foo", "bar");
642 try
643 {
644 params.get("foo");
645 fail("should throw the exception");
646 }
647 catch (AmbiguousParameterException e)
648 {
649
650 }
651 }
652
653 /***
654 * Test for void set(String, String[])
655 */
656 public void testSetStringStringArray()
657 {
658 params.set("foo", new String[] { "foo", "bar" });
659 String[] result = params.getStrings("foo");
660 if (result[0].equals("foo"))
661 {
662 assertEquals(result[1], "bar");
663 }
664 else
665 {
666 assertEquals(result[0], "bar");
667 assertEquals(result[1], "foo");
668 }
669 }
670
671 /***
672 * Test for void set(String, boolean)
673 */
674 public void testSetStringboolean()
675 {
676 params.set("foo", true);
677 assertEquals(params.getBoolean("foo", false), true);
678 params.set("foo", false);
679 assertEquals(params.getBoolean("foo", true), false);
680 }
681
682 /***
683 * Test for void set(String, boolean[])
684 */
685 public void testSetStringbooleanArray()
686 {
687 params.set("foo", new boolean[] { true, false, true });
688 boolean[] result = params.getBooleans("foo");
689 assertEquals(result.length, 3);
690 }
691
692 /***
693 * Test for void set(String, float)
694 */
695 public void testSetStringfloat()
696 {
697 params.set("foo", 1F);
698 assertEquals(params.getFloat("foo", 2F), 1F, 1F);
699 }
700
701 /***
702 * Test for void set(String, float[])
703 */
704 public void testSetStringfloatArray()
705 {
706 params.set("foo", new float[] { 1, 2, 3 });
707 assertEquals(params.getFloats("foo").length, 3);
708 }
709
710 /***
711 * Test for void set(String, int)
712 */
713 public void testSetStringint()
714 {
715 params.set("foo", 1);
716 assertEquals(params.getInt("foo", 2), 1);
717 }
718
719 /***
720 * Test for void set(String, int[])
721 */
722 public void testSetStringintArray()
723 {
724 params.set("foo", new int[] { 1, 2, 3 });
725 assertEquals(params.getInts("foo").length, 3);
726 }
727
728 /***
729 * Test for void set(String, long)
730 */
731 public void testSetStringlong()
732 {
733 params.set("foo", 1L);
734 assertEquals(params.getLong("foo", 2L), 1L, 1L);
735 }
736
737 /***
738 * Test for void set(String, long[])
739 */
740 public void testSetStringlongArray()
741 {
742 params.set("foo", new long[] { 1, 2, 3 });
743 assertEquals(params.getInts("foo").length, 3);
744 }
745
746 /***
747 * Test for void add(String, String)
748 */
749 public void testAddStringString()
750 {
751 params.add("foo", "bar");
752 params.add("foo", "bar");
753 params.add("bar", "foo");
754 assertEquals(params.getParameterNames().length, 2);
755 assertEquals(params.getStrings("foo").length, 2);
756 }
757
758 /***
759 * Test for void add(String, String[])
760 */
761 public void testAddStringStringArray()
762 {
763 params.add("foo", new String[] { "bar" });
764 params.add("foo", new String[] { "foo", "buz" });
765 params.add("bar", new String[] { "foo" });
766 assertEquals(params.getParameterNames().length, 2);
767 assertEquals(params.getStrings("foo").length, 3);
768 }
769
770 /***
771 * Test for void add(String, boolean)
772 */
773 public void testAddStringboolean()
774 {
775 params.add("foo", true);
776 params.add("foo", false);
777 params.add("bar", true);
778 assertEquals(params.getParameterNames().length, 2);
779 assertEquals(params.getBooleans("foo").length, 2);
780 assertEquals(params.getBoolean("bar",false),true);
781 }
782
783 /***
784 * Test for void add(String, boolean[])
785 */
786 public void testAddStringbooleanArray()
787 {
788 params.add("foo", new boolean[] { true });
789 params.add("foo", new boolean[] { false, true });
790 params.add("bar", new boolean[] { true });
791 assertEquals(params.getParameterNames().length, 2);
792 assertEquals(params.getBooleans("foo").length, 3);
793 }
794
795 /***
796 * Test for void add(String, float)
797 */
798 public void testAddStringfloat()
799 {
800 params.add("foo", 1F);
801 params.add("foo", 2F);
802 params.add("bar", 1F);
803 assertEquals(params.getParameterNames().length, 2);
804 assertEquals(params.getFloats("foo").length, 2);
805 assertEquals(params.getFloat("bar",2F),1F,1F);
806 }
807
808 /***
809 * Test for void add(String, float[])
810 */
811 public void testAddStringfloatArray()
812 {
813 params.add("foo", new float[] { 1 });
814 params.add("foo", new float[] { 2, 3 });
815 params.add("bar", new float[] { 1 });
816 assertEquals(params.getParameterNames().length, 2);
817 assertEquals(params.getFloats("foo").length, 3);
818 }
819
820 /***
821 * Test for void add(String, int)
822 */
823 public void testAddStringint()
824 {
825 params.add("foo", 1);
826 params.add("foo", 2);
827 params.add("bar", 1);
828 assertEquals(params.getParameterNames().length, 2);
829 assertEquals(params.getInts("foo").length, 2);
830 assertEquals(params.getInt("bar",2),1);
831 }
832
833 /***
834 * Test for void add(String, int[])
835 */
836 public void testAddStringintArray()
837 {
838 params.add("foo", new int[] { 1 });
839 params.add("foo", new int[] { 2, 3 });
840 params.add("bar", new int[] { 1 });
841 assertEquals(params.getParameterNames().length, 2);
842 assertEquals(params.getInts("foo").length, 3);
843 }
844
845 /***
846 * Test for void add(String, long)
847 */
848 public void testAddStringlong()
849 {
850 params.add("foo", 1L);
851 params.add("foo", 2L);
852 params.add("bar", 1L);
853 assertEquals(params.getParameterNames().length, 2);
854 assertEquals(params.getLongs("foo").length, 2);
855 assertEquals(params.getLong("bar",2L),1L,1L);
856 }
857
858 /***
859 * Test for void add(String, long[])
860 */
861 public void testAddStringlongArray()
862 {
863 params.add("foo", new long[] { 1 });
864 params.add("foo", new long[] { 2, 3 });
865 params.add("bar", new long[] { 1 });
866 assertEquals(params.getParameterNames().length, 2);
867 assertEquals(params.getLongs("foo").length, 3);
868 }
869
870 /***
871 * Test for void add(Parameters, boolean)
872 */
873 public void testAddParametersboolean()
874 {
875 Parameters temp = new DefaultParameters();
876 temp.add("foo",2);
877 temp.add("bar",2);
878 params.add("foo",1);
879 params.add("bar",1);
880 params.add(temp,false);
881 assertEquals(params.getInts("foo").length,2);
882 assertEquals(params.getInts("bar").length,2);
883 params = new DefaultParameters();
884 params.add("foo",1);
885 params.add("bar",1);
886 params.add(temp,true);
887 assertEquals(params.getInts("foo").length,1);
888 assertEquals(params.getInts("bar").length,1);
889 assertEquals(params.getInt("foo"),2);
890 assertEquals(params.getInt("bar"),2);
891 }
892
893 /***
894 * Test for void toString()
895 */
896 public void testToString()
897 {
898 String source = "foo=bar,foo=foo,bar=foo\n";
899 params = new DefaultParameters(source);
900 assertEquals(params.toString(),source);
901
902 source = "bar=foo\nfoo=bar,foo\n";
903 params = new DefaultParameters(source);
904 assertEquals(params.toString(),source);
905
906 source = "bar=foo\nfoo=ba//,r,foo\n";
907 params = new DefaultParameters(source);
908 System.out.println("WYNIK:"+params.toString());
909 assertEquals(params.toString(),source);
910 }
911
912 /***
913 * Test for void getChild(String)
914 */
915 public void testGetChild()
916 {
917 params.add("foo.bar","foo");
918 params.add("foo.buz","bar");
919 Parameters children = params.getChild("foo.");
920 assertEquals(children.get("bar"),"foo");
921 }
922 }