1 package com.sourceforge.jpatterns.patterns.factory;
2
3 import com.sourceforge.jpatterns.core.IJPEngine;
4 import com.sourceforge.jpatterns.core.JPConstants;
5 import com.sourceforge.jpatterns.core.JPEngineFactory;
6 import com.sourceforge.jpatterns.core.configuration.exceptions.JPConfigException;
7 import com.sourceforge.jpatterns.core.configuration.model.JPatternsConfigsBean;
8 import com.sourceforge.jpatterns.patterns.factory.junit.IOperator;
9 import com.sourceforge.jpatterns.patterns.factory.junit.IProduct;
10 import com.sourceforge.jpatterns.patterns.factory.junit.OperatorImpl1;
11 import com.sourceforge.jpatterns.patterns.factory.junit.OperatorImpl2;
12 import com.sourceforge.jpatterns.patterns.factory.junit.ProductImpl1;
13 import com.sourceforge.jpatterns.patterns.factory.junit.ProductImpl2;
14 import com.sourceforge.jpatterns.patterns.factory.junit.ProductImpl3;
15 import com.sourceforge.jpatterns.schema.CastorSectionType;
16 import com.sourceforge.jpatterns.schema.Factory;
17 import com.sourceforge.jpatterns.utils.junit.JPatternsTestUtils;
18 import com.zmicer.utils.LoggingUtils;
19 import com.zmicer.utils.MagicNumbers;
20 import com.zmicer.utils.ReflexionUtils;
21 import com.zmicer.utils.junit.JUnitUtils;
22 import com.zmicer.utils.junit.ZmicerTestUtils;
23 import com.zmicer.utils.model.junit.IJustInterface;
24 import com.zmicer.utils.model.junit.IJustInterface2;
25 import com.zmicer.utils.model.junit.IJustInterface3;
26 import com.zmicer.utils.model.junit.JustInterfaceImpl1;
27 import com.zmicer.utils.model.junit.JustInterfaceImpl2;
28 import com.zmicer.utils.model.junit.JustInterfaceImplThrowingExceptionInConstructor;
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32 import org.apache.log4j.Logger;
33
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39
40
41
42
43
44 public class JPFactoryImplTest extends TestCase
45 {
46
47
48
49 final public static Logger LOG = Logger.getLogger(JPFactoryImplTest.class);
50
51
52
53
54 private JPFactoryImpl m_jpFactoryImpl = null;
55
56
57
58
59 private static final String FACTORY_NAME = "JPFactoryImplTest";
60
61
62
63
64 private static final String NOT_DEFAULT_SCOPE = "NOT_DEFAULT_SCOPE";
65
66 interface TestKeys
67 {
68 String KEY1 = "string.key";
69 }
70
71
72
73
74 public static final int testMethodsNumber = JUnitUtils.getTestMethodsNumber(JPFactoryImplTest.class);
75
76
77
78
79
80
81 public static int counter = -1;
82
83
84
85
86
87
88 private void initFactory(final String testCaseName)
89 {
90 JPatternsTestUtils.setJVMXmlParams(JPFactoryImplTest.class, false, testCaseName, null, true);
91 final IJPEngine engine = JPEngineFactory.getJPEngine(JPConstants.EngineConfigCaregory.CONSUMER);
92 assertNotNull(engine);
93 m_jpFactoryImpl = (JPFactoryImpl) engine.getFactory(FACTORY_NAME, JPConstants.DEFAULT_SCOPE_NAME);
94 assertNotNull(m_jpFactoryImpl);
95 }
96
97
98
99
100
101
102 public JPFactoryImplTest(String name)
103 {
104 super(name);
105 }
106
107
108
109
110
111
112 public void globalSetUp() throws Exception
113 {
114 LOG.debug("Global setup is running.");
115 }
116
117
118
119
120
121
122 public void globalTearDown() throws Exception
123 {
124 LOG.debug("Global teardown is running.");
125 }
126
127
128
129
130
131
132 public void setUp() throws Exception
133 {
134
135 counter = ((counter == testMethodsNumber) ? 0 : counter + 1);
136 if (counter == 0)
137 {
138 globalSetUp();
139 }
140 super.setUp();
141 LOG.debug("Setup is running " + counter);
142
143 JPatternsTestUtils.setJVMPropsParams(ZmicerTestUtils.CommonTestCases.COMMON_PROPS, null, null, true);
144 initFactory(null);
145 }
146
147
148
149
150
151
152 public void tearDown() throws Exception
153 {
154 super.tearDown();
155 LOG.debug("Teardown is running " + counter);
156 if (counter == (testMethodsNumber - 1))
157 {
158 globalTearDown();
159 }
160 JPatternsTestUtils.resetAll();
161 }
162
163
164
165
166
167
168 public static Test suite()
169 {
170 return new TestSuite(JPFactoryImplTest.class);
171 }
172
173
174
175
176
177
178
179 public void testGetImplementation() throws Exception
180 {
181 assertNotNull(m_jpFactoryImpl);
182
183 try
184 {
185 m_jpFactoryImpl.getImplementation((Class) null, null);
186 fail("Exception should have been occuried before this line.");
187 }
188 catch (Exception ex)
189 {
190 if (!(ex instanceof IllegalArgumentException))
191 {
192 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
193 }
194 assertTrue(ex instanceof IllegalArgumentException);
195 }
196
197 try
198 {
199 m_jpFactoryImpl.getImplementation((String) null, null);
200 fail("Exception should have been occuried before this line.");
201 }
202 catch (Exception ex)
203 {
204 if (!(ex instanceof IllegalArgumentException))
205 {
206 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
207 }
208 assertTrue(ex instanceof IllegalArgumentException);
209 }
210 Object object = null;
211
212 try
213 {
214
215 object = m_jpFactoryImpl.getImplementation(IJustInterface.class, null);
216 assertNotNull(object);
217 assertTrue(object instanceof IJustInterface);
218 assertTrue(object instanceof JustInterfaceImpl1);
219
220 object = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IJustInterface.class), null);
221 assertNotNull(object);
222 assertTrue(object instanceof IJustInterface);
223 assertTrue(object instanceof JustInterfaceImpl1);
224
225
226 object = m_jpFactoryImpl.getImplementation(IJustInterface.class, NOT_DEFAULT_SCOPE);
227 assertNotNull(object);
228 assertTrue(object instanceof JustInterfaceImpl1);
229
230 object = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IJustInterface.class), NOT_DEFAULT_SCOPE);
231 assertNotNull(object);
232 assertTrue(object instanceof JustInterfaceImpl1);
233
234
235 object = m_jpFactoryImpl.getImplementation(TestKeys.KEY1, null);
236 assertNotNull(object);
237 assertTrue(object instanceof JustInterfaceImpl2);
238
239 object = m_jpFactoryImpl.getImplementation(TestKeys.KEY1, NOT_DEFAULT_SCOPE);
240 assertNotNull(object);
241 assertTrue(object instanceof JustInterfaceImpl2);
242 }
243 catch (Exception ex)
244 {
245 final String excMessage = "Exception should not occur for that case ";
246 LoggingUtils.logException(LOG, ex, excMessage, null);
247 fail(excMessage + ex.getMessage());
248 }
249
250
251 try
252 {
253 m_jpFactoryImpl.getImplementation(IJustInterface2.class, null);
254 fail("Exception should have been occuried before this line.");
255 }
256 catch (Exception ex)
257 {
258 if (!(ex instanceof JPConfigException))
259 {
260 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
261 }
262 assertTrue(ex instanceof JPConfigException);
263 }
264
265 try
266 {
267 m_jpFactoryImpl.getImplementation(IJustInterface3.class, null);
268 fail("Exception should have been occuried before this line.");
269 }
270 catch (Exception ex)
271 {
272 if (!(ex instanceof JPConfigException))
273 {
274 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
275 }
276 assertTrue(ex instanceof JPConfigException);
277 }
278
279 try
280 {
281 m_jpFactoryImpl.getImplementation(IJustInterface.class, NOT_DEFAULT_SCOPE + "notdefined");
282 fail("Exception should have been occuried before this line.");
283 }
284 catch (Exception ex)
285 {
286 if (!(ex instanceof JPConfigException))
287 {
288 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
289 }
290 assertTrue(ex instanceof JPConfigException);
291 }
292
293 try
294 {
295 m_jpFactoryImpl.getImplementation(IJustInterface2.class, NOT_DEFAULT_SCOPE);
296 fail("Exception should have been occuried before this line.");
297 }
298 catch (Exception ex)
299 {
300 if (!(ex instanceof JPConfigException))
301 {
302 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
303 }
304 assertTrue(ex instanceof JPConfigException);
305 }
306
307 try
308 {
309 m_jpFactoryImpl.getImplementation(IJustInterface2.class, NOT_DEFAULT_SCOPE + "_1");
310 fail("Exception should have been occuried before this line.");
311 }
312 catch (Exception ex)
313 {
314 if (!(ex instanceof JPConfigException))
315 {
316 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
317 }
318 assertTrue(ex instanceof JPConfigException);
319 }
320
321 try
322 {
323 m_jpFactoryImpl.getImplementation(IJustInterface2.class, NOT_DEFAULT_SCOPE + "_2");
324 fail("Exception should have been occuried before this line.");
325 }
326 catch (Exception ex)
327 {
328 if (!(ex instanceof JPConfigException))
329 {
330 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
331 }
332 assertTrue(ex instanceof JPConfigException);
333 }
334
335
336 try
337 {
338 object = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IJustInterface2.class), null);
339 assertNotNull(object);
340 assertTrue(object instanceof JustInterfaceImpl1);
341 }
342 catch (Exception ex)
343 {
344 final String excMessage = "Exception should not occur for that case ";
345 LoggingUtils.logException(LOG, ex, excMessage, null);
346 fail(excMessage + ex.getMessage());
347 }
348
349
350 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
351 new Class[]{Class.class, String.class},
352 IOperator.class, null);
353 }
354
355
356
357
358
359
360
361 public void testInit() throws Exception
362 {
363
364 try
365 {
366 m_jpFactoryImpl.init(null, null);
367 fail("Exception should have been occuried before this line.");
368 }
369 catch (Exception ex)
370 {
371 if (!(ex instanceof IllegalArgumentException))
372 {
373 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
374 }
375 assertTrue(ex instanceof IllegalArgumentException);
376 }
377
378 try
379 {
380 m_jpFactoryImpl.init(null, new JPatternsConfigsBean());
381 fail("Exception should have been occuried before this line.");
382 }
383 catch (Exception ex)
384 {
385 if (!(ex instanceof IllegalArgumentException))
386 {
387 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
388 }
389 assertTrue(ex instanceof IllegalArgumentException);
390 }
391
392 try
393 {
394 m_jpFactoryImpl.init(new Factory(), null);
395 fail("Exception should have been occuried before this line.");
396 }
397 catch (Exception ex)
398 {
399 if (!(ex instanceof IllegalArgumentException))
400 {
401 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
402 }
403 assertTrue(ex instanceof IllegalArgumentException);
404 }
405
406 try
407 {
408 m_jpFactoryImpl.init(new CastorSectionType(), new JPatternsConfigsBean());
409 fail("Exception should have been occuried before this line.");
410 }
411 catch (Exception ex)
412 {
413 if (!(ex instanceof JPConfigException))
414 {
415 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
416 }
417 assertTrue(ex instanceof JPConfigException);
418 }
419
420 try
421 {
422 final CastorSectionType castor = new Factory();
423 final JPatternsConfigsBean bean = new JPatternsConfigsBean();
424 m_jpFactoryImpl.init(castor, bean);
425 }
426 catch (Exception ex)
427 {
428 final String excMessage = "Exception should not occur for that case ";
429 LoggingUtils.logException(LOG, ex, excMessage, null);
430 fail(excMessage + ex.getMessage());
431 }
432 }
433
434
435
436
437
438
439
440 public void testGetImplementationFullName() throws Exception
441 {
442 assertNotNull(m_jpFactoryImpl);
443
444 try
445 {
446 m_jpFactoryImpl.getImplementationFullName((Class) null, null);
447 fail("Exception should have been occuried before this line.");
448 }
449 catch (Exception ex)
450 {
451 if (!(ex instanceof IllegalArgumentException))
452 {
453 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
454 }
455 assertTrue(ex instanceof IllegalArgumentException);
456 }
457
458 try
459 {
460 m_jpFactoryImpl.getImplementationFullName((String) null, null);
461 fail("Exception should have been occuried before this line.");
462 }
463 catch (Exception ex)
464 {
465 if (!(ex instanceof IllegalArgumentException))
466 {
467 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
468 }
469 assertTrue(ex instanceof IllegalArgumentException);
470 }
471
472
473 String result = null;
474 try
475 {
476
477 result = m_jpFactoryImpl.getImplementationFullName(IJustInterface.class, null);
478 assertNotNull(result);
479 assertEquals(JustInterfaceImpl1.class.getName(), result);
480
481 result = m_jpFactoryImpl.getImplementationFullName(ReflexionUtils.getBaseName(IJustInterface.class), null);
482 assertNotNull(result);
483 assertEquals(JustInterfaceImpl1.class.getName(), result);
484
485
486 result = m_jpFactoryImpl.getImplementationFullName(IJustInterface.class, NOT_DEFAULT_SCOPE);
487 assertNotNull(result);
488 assertEquals(JustInterfaceImpl1.class.getName(), result);
489
490 result = m_jpFactoryImpl.getImplementationFullName(ReflexionUtils.getBaseName(IJustInterface.class), NOT_DEFAULT_SCOPE);
491 assertNotNull(result);
492 assertEquals(JustInterfaceImpl1.class.getName(), result);
493
494
495 result = m_jpFactoryImpl.getImplementationFullName(TestKeys.KEY1, null);
496 assertNotNull(result);
497 assertEquals(JustInterfaceImpl2.class.getName(), result);
498
499 result = m_jpFactoryImpl.getImplementationFullName(TestKeys.KEY1, NOT_DEFAULT_SCOPE);
500 assertNotNull(result);
501 assertEquals(JustInterfaceImpl2.class.getName(), result);
502
503
504 result = m_jpFactoryImpl.getImplementationFullName(IJustInterface2.class, null);
505 assertNotNull(result);
506 assertEquals(JustInterfaceImpl1.class.getName(), result);
507
508 result = m_jpFactoryImpl.getImplementationFullName(IJustInterface2.class, NOT_DEFAULT_SCOPE);
509 assertNotNull(result);
510 assertEquals(JustInterfaceImpl1.class.getName() + "_incorrect", result);
511 }
512 catch (Exception ex)
513 {
514 final String excMessage = "Exception should not occur for that case ";
515 LoggingUtils.logException(LOG, ex, excMessage, null);
516 fail(excMessage + ex.getMessage());
517 }
518 }
519
520
521
522
523
524
525
526 public void testGetImplementationByType() throws Exception
527 {
528 assertNotNull(m_jpFactoryImpl);
529
530 JUnitUtils.checkOnWrongArgs(m_jpFactoryImpl, "getImplementation", new boolean[]{true, true, false},
531 new Class[]{Class.class, String.class, String.class},
532 IOperator.class, "string", "string");
533 JUnitUtils.checkOnWrongArgs(m_jpFactoryImpl, "getImplementation", new boolean[]{true, true, false},
534 new Class[]{Class.class, String.class, String.class},
535 IOperator.class, "string", "string");
536
537
538 Object result = null;
539 try
540 {
541
542 result = m_jpFactoryImpl.getImplementation(IOperator.class, "ONE", null);
543 assertNotNull(result);
544 assertTrue(result instanceof OperatorImpl1);
545
546 result = m_jpFactoryImpl.getImplementation(IOperator.class, "TWO", null);
547 assertNotNull(result);
548 assertTrue(result instanceof OperatorImpl2);
549
550 result = m_jpFactoryImpl.getImplementation(IOperator.class, "THREE", null);
551 assertNotNull(result);
552 assertTrue(result instanceof OperatorImpl2);
553
554 result = m_jpFactoryImpl.getImplementation(IOperator.class, "FOUR", null);
555 assertNotNull(result);
556 assertTrue(result instanceof OperatorImpl2);
557
558
559 result = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IOperator.class), "ONE", null);
560 assertNotNull(result);
561 assertTrue(result instanceof OperatorImpl1);
562
563 result = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IOperator.class), "TWO", null);
564 assertNotNull(result);
565 assertTrue(result instanceof OperatorImpl2);
566
567
568 result = m_jpFactoryImpl.getImplementation(IOperator.class, "ONE", NOT_DEFAULT_SCOPE);
569 assertNotNull(result);
570 assertTrue(result instanceof OperatorImpl1);
571
572 result = m_jpFactoryImpl.getImplementation(IOperator.class, "TWO", NOT_DEFAULT_SCOPE);
573 assertNotNull(result);
574 assertTrue(result instanceof OperatorImpl2);
575
576 result = m_jpFactoryImpl.getImplementation(IOperator.class, "THREE", NOT_DEFAULT_SCOPE);
577 assertNotNull(result);
578 assertTrue(result instanceof OperatorImpl2);
579
580 result = m_jpFactoryImpl.getImplementation(IOperator.class, "FOUR", NOT_DEFAULT_SCOPE);
581 assertNotNull(result);
582 assertTrue(result instanceof OperatorImpl2);
583
584
585 result = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IOperator.class), "THREE", NOT_DEFAULT_SCOPE);
586 assertNotNull(result);
587 assertTrue(result instanceof OperatorImpl2);
588
589 result = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IOperator.class), "FOUR", NOT_DEFAULT_SCOPE);
590 assertNotNull(result);
591 assertTrue(result instanceof OperatorImpl2);
592
593
594 result = m_jpFactoryImpl.getImplementation(TestKeys.KEY1, "ONE", NOT_DEFAULT_SCOPE + "_1");
595 assertNotNull(result);
596 assertTrue(result instanceof OperatorImpl1);
597
598 result = m_jpFactoryImpl.getImplementation(TestKeys.KEY1, "ONE", NOT_DEFAULT_SCOPE + "_2");
599 assertNotNull(result);
600 assertTrue(result instanceof OperatorImpl1);
601
602 }
603 catch (Exception ex)
604 {
605 final String excMessage = "Exception should not occur for that case ";
606 LoggingUtils.logException(LOG, ex, excMessage, null);
607 fail(excMessage + ex.getMessage());
608 }
609
610
611 try
612 {
613 m_jpFactoryImpl.getImplementation(OperatorImpl1.class, "ONE", null);
614 fail("Exception should have been occuried before this line.");
615 }
616 catch (Exception ex)
617 {
618 if (!(ex instanceof JPConfigException))
619 {
620 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
621 }
622 assertTrue(ex instanceof JPConfigException);
623 }
624
625 try
626 {
627 m_jpFactoryImpl.getImplementation(OperatorImpl1.class, "ONE", "not_defined_scope");
628 fail("Exception should have been occuried before this line.");
629 }
630 catch (Exception ex)
631 {
632 if (!(ex instanceof JPConfigException))
633 {
634 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
635 }
636 assertTrue(ex instanceof JPConfigException);
637 }
638
639
640
641 try
642 {
643 m_jpFactoryImpl.getImplementation(IOperator.class, "ONE", NOT_DEFAULT_SCOPE + "_1");
644 fail("Exception should have been occuried before this line.");
645 }
646 catch (Exception ex)
647 {
648 if (!(ex instanceof JPConfigException))
649 {
650 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
651 }
652 assertTrue(ex instanceof JPConfigException);
653 }
654
655 try
656 {
657 m_jpFactoryImpl.getImplementation(IOperator.class, "TWO", NOT_DEFAULT_SCOPE + "_1");
658 fail("Exception should have been occuried before this line.");
659 }
660 catch (Exception ex)
661 {
662 if (!(ex instanceof JPConfigException))
663 {
664 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
665 }
666 assertTrue(ex instanceof JPConfigException);
667 }
668
669 try
670 {
671 m_jpFactoryImpl.getImplementation(IOperator.class, "THREE", NOT_DEFAULT_SCOPE + "_1");
672 fail("Exception should have been occuried before this line.");
673 }
674 catch (Exception ex)
675 {
676 if (!(ex instanceof JPConfigException))
677 {
678 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
679 }
680 assertTrue(ex instanceof JPConfigException);
681 }
682
683 try
684 {
685 m_jpFactoryImpl.getImplementation(IOperator.class, "FOUR", NOT_DEFAULT_SCOPE + "_1");
686 fail("Exception should have been occuried before this line.");
687 }
688 catch (Exception ex)
689 {
690 if (!(ex instanceof JPConfigException))
691 {
692 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
693 }
694 assertTrue(ex instanceof JPConfigException);
695 }
696
697
698
699 try
700 {
701 result = m_jpFactoryImpl.getImplementation(ReflexionUtils.getBaseName(IOperator.class), "TWO", NOT_DEFAULT_SCOPE + "_1");
702 assertNotNull(result);
703 assertTrue(result instanceof ProductImpl2);
704 }
705 catch (Exception ex)
706 {
707 final String excMessage = "Exception should not occur for that case ";
708 LoggingUtils.logException(LOG, ex, excMessage, null);
709 fail(excMessage + ex.getMessage());
710 }
711
712 try
713 {
714 m_jpFactoryImpl.getImplementations(IJustInterface.class, null);
715 fail("Exception should have been occuried before this line.");
716 }
717 catch (Exception ex)
718 {
719 if (!(ex instanceof JPConfigException))
720 {
721 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
722 }
723 assertTrue(ex instanceof JPConfigException);
724 }
725 }
726
727
728
729
730
731
732
733 public void testGetImplementations() throws Exception
734 {
735 assertNotNull(m_jpFactoryImpl);
736
737 JUnitUtils.checkOnWrongArgs(m_jpFactoryImpl, "getImplementations", new boolean[]{true, false},
738 new Class[]{Class.class, String.class},
739 IOperator.class, "string");
740 JUnitUtils.checkOnWrongArgs(m_jpFactoryImpl, "getImplementations", new boolean[]{true, false},
741 new Class[]{Class.class, String.class},
742 IOperator.class, "string");
743
744 JUnitUtils.checkOnWrongArgs(m_jpFactoryImpl, "getImplementationsFullNames", new boolean[]{true, false},
745 new Class[]{Class.class, String.class},
746 IOperator.class, "string");
747 JUnitUtils.checkOnWrongArgs(m_jpFactoryImpl, "getImplementationsFullNames", new boolean[]{true, false},
748 new Class[]{Class.class, String.class},
749 IOperator.class, "string");
750
751
752
753 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
754 new Class[]{Class.class, String.class},
755 OperatorImpl1.class, null);
756 JUnitUtils.runFailure(m_jpFactoryImpl, "getImplementationsFullNames", JPConfigException.class,
757 new Class[]{Class.class, String.class},
758 OperatorImpl1.class, null);
759
760
761 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
762 new Class[]{Class.class, String.class},
763 OperatorImpl1.class, "not_defined_scope");
764 JUnitUtils.runFailure(m_jpFactoryImpl, "getImplementationsFullNames", JPConfigException.class,
765 new Class[]{Class.class, String.class},
766 OperatorImpl1.class, "not_defined_scope");
767
768
769 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
770 new Class[]{Class.class, String.class},
771 IJustInterface.class, null);
772 JUnitUtils.runFailure(m_jpFactoryImpl, "getImplementationsFullNames", JPConfigException.class,
773 new Class[]{Class.class, String.class},
774 IJustInterface.class, null);
775 JUnitUtils.runFailure(m_jpFactoryImpl, "getImplementationsFullNames", JPConfigException.class,
776 new Class[]{String.class, String.class},
777 ReflexionUtils.getBaseName(IJustInterface.class), null);
778
779
780 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
781 new Class[]{Class.class, String.class},
782 IOperator.class, NOT_DEFAULT_SCOPE + "_2");
783
784
785 try
786 {
787 Map<String, String> smap = m_jpFactoryImpl.getImplementationsFullNames(IOperator.class, null);
788 assertNotNull(smap);
789 smap.containsKey("ONE");
790 smap.containsKey("TWO");
791 smap.containsKey("THREE");
792 smap.containsKey("FOUR");
793 assertEquals(smap.get("ONE"), OperatorImpl1.class.getName());
794 assertEquals(smap.get("FOUR"), OperatorImpl2.class.getName());
795
796 smap = m_jpFactoryImpl.getImplementationsFullNames(IOperator.class, NOT_DEFAULT_SCOPE);
797 assertNotNull(smap);
798 smap.containsKey("ONE");
799 smap.containsKey("TWO");
800 smap.containsKey("THREE");
801 smap.containsKey("FOUR");
802 assertEquals(smap.get("ONE"), OperatorImpl1.class.getName());
803 assertEquals(smap.get("FOUR"), OperatorImpl2.class.getName());
804
805 smap = m_jpFactoryImpl.getImplementationsFullNames(TestKeys.KEY1, NOT_DEFAULT_SCOPE + "_1");
806 assertNotNull(smap);
807 smap.containsKey("ONE");
808 assertEquals(smap.get("ONE"), OperatorImpl1.class.getName());
809
810 smap = m_jpFactoryImpl.getImplementationsFullNames(TestKeys.KEY1, NOT_DEFAULT_SCOPE + "_2");
811 assertNotNull(smap);
812 smap.containsKey("ONE");
813 assertEquals(smap.get("ONE"), OperatorImpl1.class.getName());
814
815
816 smap = m_jpFactoryImpl.getImplementationsFullNames(IOperator.class, NOT_DEFAULT_SCOPE + "_1");
817 assertNotNull(smap);
818 smap.containsKey("ONE");
819 smap.containsKey("TWO");
820 smap.containsKey("THREE");
821 smap.containsKey("FOUR");
822 assertTrue(smap.get("ONE").contains("OperatorImpl1_incorrect"));
823 assertEquals(smap.get("THREE"), JustInterfaceImplThrowingExceptionInConstructor.class.getName());
824
825
826 smap = m_jpFactoryImpl.getImplementationsFullNames(IOperator.class, NOT_DEFAULT_SCOPE + "_2");
827 assertNotNull(smap);
828 smap.containsKey("ONE");
829 smap.containsKey("TWO");
830 assertEquals(smap.get("ONE"), OperatorImpl1.class.getName());
831 assertEquals(smap.get("TWO"), ProductImpl1.class.getName());
832 }
833 catch (Exception ex)
834 {
835 final String excMessage = "Exception should not occur for that case ";
836 LoggingUtils.logException(LOG, ex, excMessage, null);
837 fail(excMessage + ex.getMessage());
838 }
839
840
841 Map<String, Object> result = null;
842 try
843 {
844 result = m_jpFactoryImpl.getImplementations(ReflexionUtils.getBaseName(IOperator.class), NOT_DEFAULT_SCOPE + "_2");
845 assertNotNull(result);
846 assertEquals(result.size(), MagicNumbers.TWO);
847 assertTrue(result.get("ONE") instanceof OperatorImpl1);
848 assertTrue(result.get("TWO") instanceof ProductImpl1);
849 }
850 catch (Exception ex)
851 {
852 final String excMessage = "Exception should not occur for that case ";
853 LoggingUtils.logException(LOG, ex, excMessage, null);
854 fail(excMessage + ex.getMessage());
855 }
856
857 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
858 new Class[]{Class.class, String.class},
859 IOperator.class, NOT_DEFAULT_SCOPE + "_1");
860
861
862 try
863 {
864 makeAssertion(m_jpFactoryImpl.getImplementations(IOperator.class, null));
865 makeAssertion(m_jpFactoryImpl.getImplementations(IOperator.class, NOT_DEFAULT_SCOPE));
866 makeAssertion(m_jpFactoryImpl.getImplementations(ReflexionUtils.getBaseName(IOperator.class), null));
867 makeAssertion(m_jpFactoryImpl.getImplementations(ReflexionUtils.getBaseName(IOperator.class), NOT_DEFAULT_SCOPE));
868
869 result = m_jpFactoryImpl.getImplementations(TestKeys.KEY1, NOT_DEFAULT_SCOPE + "_1");
870 assertNotNull(result);
871 assertEquals(result.size(), MagicNumbers.ONE);
872 assertTrue(result.get("ONE") instanceof OperatorImpl1);
873
874 result = m_jpFactoryImpl.getImplementations(TestKeys.KEY1, NOT_DEFAULT_SCOPE + "_2");
875 assertNotNull(result);
876 assertEquals(result.size(), MagicNumbers.ONE);
877 assertTrue(result.get("ONE") instanceof OperatorImpl1);
878 }
879 catch (Exception ex)
880 {
881 final String excMessage = "Exception should not occur for that case ";
882 LoggingUtils.logException(LOG, ex, excMessage, null);
883 fail(excMessage + ex.getMessage());
884 }
885
886
887 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
888 new Class[]{Class.class, String.class},
889 IJustInterface.class, null);
890 }
891
892
893
894
895
896
897 private void makeAssertion(final Map<String, Object> result)
898 {
899 assertNotNull(result);
900 assertEquals(result.size(), MagicNumbers.FOUR);
901 assertTrue(result.containsKey("ONE"));
902 assertTrue(result.containsKey("TWO"));
903 assertTrue(result.containsKey("THREE"));
904 assertTrue(result.containsKey("FOUR"));
905 assertTrue(result.get("ONE") instanceof OperatorImpl1);
906 assertTrue(result.get("TWO") instanceof OperatorImpl2);
907 assertTrue(result.get("THREE") instanceof OperatorImpl2);
908 assertTrue(result.get("FOUR") instanceof OperatorImpl2);
909 }
910
911
912
913
914
915
916
917 public void testGetOperator() throws Exception
918 {
919 assertNotNull(m_jpFactoryImpl);
920
921 JUnitUtils.checkOnWrongArgs(m_jpFactoryImpl,
922 JUnitUtils.getRunningMethodName(), new boolean[]{true, true, true, false},
923 new Class[]{Class.class, Class.class, Object.class, String.class},
924 IProduct.class, IOperator.class, new ProductImpl1(), "string");
925
926 Object result = null;
927
928 try
929 {
930
931
932 result = m_jpFactoryImpl.getOperator(IProduct.class, IOperator.class, new ProductImpl1(), null);
933 assertNotNull(result);
934 assertTrue(result instanceof OperatorImpl1);
935
936 result = m_jpFactoryImpl.getOperator(IProduct.class, IOperator.class, new ProductImpl2(), null);
937 assertNotNull(result);
938 assertTrue(result instanceof OperatorImpl2);
939
940
941 result = m_jpFactoryImpl.getOperator(IProduct.class, IOperator.class, new ProductImpl1(), NOT_DEFAULT_SCOPE);
942 assertNotNull(result);
943 assertTrue(result instanceof OperatorImpl1);
944
945 result = m_jpFactoryImpl.getOperator(IProduct.class, IOperator.class, new ProductImpl2(), NOT_DEFAULT_SCOPE);
946 assertNotNull(result);
947 assertTrue(result instanceof OperatorImpl2);
948 }
949 catch (Exception ex)
950 {
951 final String excMessage = "Exception should not occur for that case ";
952 LoggingUtils.logException(LOG, ex, excMessage, null);
953 fail(excMessage + ex.getMessage());
954 }
955
956 runFailure(IProduct.class, IOperator.class, new ProductImpl3(), null);
957 runFailure(IProduct.class, IOperator.class, new ProductImpl2(), "not_defined");
958 runFailure(IOperator.class, IOperator.class, new ProductImpl1(), null);
959
960
961 runFailure(IProduct.class, IOperator.class, new ProductImpl1(), NOT_DEFAULT_SCOPE + "_1");
962 runFailure(IProduct.class, IOperator.class, new ProductImpl1(), NOT_DEFAULT_SCOPE + "_2");
963 runFailure(IProduct.class, IOperator.class, new ProductImpl1(), NOT_DEFAULT_SCOPE + "_3");
964 runFailure(IProduct.class, IOperator.class, new ProductImpl1(), NOT_DEFAULT_SCOPE + "_4");
965
966
967 runFailure(IJustInterface.class, IOperator.class, new ProductImpl1(), null);
968 }
969
970
971
972
973
974
975 private void runFailure(final Object... args)
976 {
977 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
978 new Class[]{Class.class, Class.class, Object.class, String.class},
979 args);
980 }
981
982
983
984
985
986
987
988 public void testGetOperators() throws Exception
989 {
990 assertNotNull(m_jpFactoryImpl);
991
992 try
993 {
994 m_jpFactoryImpl.getOperators(null, IOperator.class, new ArrayList<Object>(), null);
995 fail("Exception should have been occuried before this line.");
996 }
997 catch (Exception ex)
998 {
999 if (!(ex instanceof IllegalArgumentException))
1000 {
1001 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
1002 }
1003 assertTrue(ex instanceof IllegalArgumentException);
1004 }
1005
1006 try
1007 {
1008 m_jpFactoryImpl.getOperators(IProduct.class, null, new ArrayList<Object>(), null);
1009 fail("Exception should have been occuried before this line.");
1010 }
1011 catch (Exception ex)
1012 {
1013 if (!(ex instanceof IllegalArgumentException))
1014 {
1015 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
1016 }
1017 assertTrue(ex instanceof IllegalArgumentException);
1018 }
1019
1020 try
1021 {
1022 m_jpFactoryImpl.getOperators(IProduct.class, IOperator.class, null, null);
1023 fail("Exception should have been occuried before this line.");
1024 }
1025 catch (Exception ex)
1026 {
1027 if (!(ex instanceof IllegalArgumentException))
1028 {
1029 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
1030 }
1031 assertTrue(ex instanceof IllegalArgumentException);
1032 }
1033
1034 Map<Object, Object> result = null;
1035 ProductImpl1 productImpl1 = null;
1036 ProductImpl2 productImpl2 = null;
1037 List<Object> workingList = new ArrayList<Object>();
1038
1039 try
1040 {
1041
1042 workingList.clear();
1043 productImpl1 = new ProductImpl1();
1044 workingList.add(productImpl1);
1045 result = m_jpFactoryImpl.getOperators(IProduct.class, IOperator.class, workingList, null);
1046 assertNotNull(result);
1047 assertTrue(result.containsKey(productImpl1));
1048 assertTrue(result.get(productImpl1) instanceof OperatorImpl1);
1049
1050
1051 workingList.clear();
1052 productImpl1 = new ProductImpl1();
1053 workingList.add(productImpl1);
1054 productImpl2 = new ProductImpl2();
1055 workingList.add(productImpl2);
1056 result = m_jpFactoryImpl.getOperators(IProduct.class, IOperator.class, workingList, null);
1057 assertNotNull(result);
1058 assertTrue(result.containsKey(productImpl1));
1059 assertTrue(result.get(productImpl1) instanceof OperatorImpl1);
1060 assertTrue(result.containsKey(productImpl2));
1061 assertTrue(result.get(productImpl2) instanceof OperatorImpl2);
1062 }
1063 catch (Exception ex)
1064 {
1065 final String excMessage = "Exception should not occur for that case ";
1066 LoggingUtils.logException(LOG, ex, excMessage, null);
1067 fail(excMessage + ex.getMessage());
1068 }
1069
1070 try
1071 {
1072
1073 workingList.clear();
1074 ProductImpl3 productImpl3 = new ProductImpl3();
1075 workingList.add(productImpl3);
1076 productImpl2 = new ProductImpl2();
1077 workingList.add(productImpl2);
1078 result = m_jpFactoryImpl.getOperators(IProduct.class, IOperator.class, workingList, null);
1079 fail("Exception should have been occuried before this line.");
1080 }
1081 catch (Exception ex)
1082 {
1083 if (!(ex instanceof JPConfigException))
1084 {
1085 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
1086 }
1087 assertTrue(ex instanceof JPConfigException);
1088 }
1089
1090 workingList.clear();
1091 workingList.add(new ProductImpl1());
1092 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
1093 new Class[]{Class.class, Class.class, List.class, String.class},
1094 IProduct.class, IOperator.class, workingList, NOT_DEFAULT_SCOPE + "_1");
1095 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
1096 new Class[]{Class.class, Class.class, List.class, String.class},
1097 IProduct.class, IOperator.class, workingList, NOT_DEFAULT_SCOPE + "_2");
1098 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
1099 new Class[]{Class.class, Class.class, List.class, String.class},
1100 IProduct.class, IOperator.class, workingList, NOT_DEFAULT_SCOPE + "_3");
1101 JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
1102 new Class[]{Class.class, Class.class, List.class, String.class},
1103 IProduct.class, IOperator.class, workingList, NOT_DEFAULT_SCOPE + "_4");
1104 }
1105
1106
1107
1108
1109
1110
1111
1112 public void testValidateImplementationsMap() throws Exception
1113 {
1114 assertNotNull(m_jpFactoryImpl);
1115
1116
1117
1118 Map<String, Object> input = new HashMap<String, Object>();
1119 input.put("key", new ProductImpl1());
1120 try
1121 {
1122 m_jpFactoryImpl.validateImplementationsMap(IOperator.class, input);
1123 fail("Exception should have been occuried before this line.");
1124 }
1125 catch (Exception ex)
1126 {
1127 if (!(ex instanceof JPConfigException))
1128 {
1129 LoggingUtils.logException(LOG, ex, "Incorrect Exception Occuried", JPFactoryImplTest.class);
1130 }
1131 assertTrue(ex instanceof JPConfigException);
1132 }
1133
1134 try
1135 {
1136 m_jpFactoryImpl.validateImplementationsMap(IProduct.class, input);
1137 }
1138 catch (Exception ex)
1139 {
1140 final String excMessage = "Exception should not occur for that case ";
1141 LoggingUtils.logException(LOG, ex, excMessage, null);
1142 fail(excMessage + ex.getMessage());
1143 }
1144 }
1145 }