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   * $Author::                    $<br/>
41   * $Rev::                       $<br/>
42   * $Date::                      $<br/>
43   */
44  public class JPFactoryImplTest extends TestCase
45  {
46      /**
47       * Logger instance.
48       */
49      final public static Logger LOG = Logger.getLogger(JPFactoryImplTest.class);
50  
51      /**
52       * Instance of the covered class to be used below in the tests
53       */
54      private JPFactoryImpl m_jpFactoryImpl = null;
55  
56      /**
57       * The factory name to be used here. The scope of the factory is supposed to be JPConstants.DEFAULT_SCOPE_NAME
58       */
59      private static final String FACTORY_NAME = "JPFactoryImplTest";
60  
61      /**
62       * Npt default scope value to be used.
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       * Count of the test methods here
73       */
74      public static final int testMethodsNumber = JUnitUtils.getTestMethodsNumber(JPFactoryImplTest.class);
75  
76      /**
77       * The counter which point to the test method is running for now. It is set to zero during the initializing the class at the JVM,
78       * at each call of setup it is increased, in the case it is equal to the <code>testMethodsNumber</code> it's value is set to the 0 again.
79       * The static variable allows us to run the global setup and tear down where it is necessary
80       */
81      public static int counter = -1;
82  
83      /**
84       * Init the factory instance.
85       *
86       * @param testCaseName the test case name.
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       * Contructor with name of test attribute.
99       *
100      * @param name name of the test
101      */
102     public JPFactoryImplTest(String name)
103     {
104         super(name);
105     }
106 
107     /**
108      * Perform the set up functionality for the test.
109      *
110      * @throws Exception may occur in the case of some problems
111      */
112     public void globalSetUp() throws Exception
113     {
114         LOG.debug("Global setup is running.");
115     }
116 
117     /**
118      * Perform the tear down functionality for the test
119      *
120      * @throws Exception may occur in the case of some problems
121      */
122     public void globalTearDown() throws Exception
123     {
124         LOG.debug("Global teardown is running.");
125     }
126 
127     /**
128      * Perform the set up functionality for the test.
129      *
130      * @throws Exception may occur in the case of some problems
131      */
132     public void setUp() throws Exception
133     {
134         // note [zmicer]: think here over where the super.setUp should be called.
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         // default configuration
143         JPatternsTestUtils.setJVMPropsParams(ZmicerTestUtils.CommonTestCases.COMMON_PROPS, null, null, true);
144         initFactory(null);
145     }
146 
147     /**
148      * Perform the tear down functionality for the test
149      *
150      * @throws Exception may occur in the case of some problems
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      * Test suite method
165      *
166      * @return the built test suite
167      */
168     public static Test suite()
169     {
170         return new TestSuite(JPFactoryImplTest.class);
171     }
172 
173     /**
174      * Tests {@link JPFactoryImpl#getImplementation}
175      *
176      * @throws Exception in the case smth. wrong occuried.
177      * @forversion 1.0
178      */
179     public void testGetImplementation() throws Exception
180     {
181         assertNotNull(m_jpFactoryImpl);
182         // 1. failed sub-case: exception occuried: null as the interface class
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         // failed sub-case: exception occuried: null as the interface class
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         // 2. normal sub-case: no exception: the given interface has appropriate implementation defined.
212         try
213         {
214             // A: without scope
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             // B: with scope
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             // C: check string key - with scope and without
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         // 3. illegal cases in the definitions of the patterns
250         // failed sub-case: exception occuried: interface and implementation are of the different types
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         // failed sub-case: exception occuried: can not find such item as this interface is not specified at the JPatterns config file
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         // failed sub-case: exception occuried: can not find such item as the scope is not specified
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         // failed sub-case: exception occuried: incorrect implementation specified
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         // failed sub-case: exception occuried: exception on init: constructor
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         // failed sub-case: exception occuried: exception on init: static initialization
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         // 4. normal sub-case: no exception: no problems (see item 3) if use the String as the key
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         // 5: trying to use the item not purposed for this (e.g. map of the implementations)
350         JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
351             new Class[]{Class.class, String.class},
352             IOperator.class, null);
353     }
354 
355     /**
356      * Tests {@link JPFactoryImpl#init}
357      *
358      * @throws Exception in the case smth. wrong occuried.
359      * @forversion 1.0
360      */
361     public void testInit() throws Exception
362     {
363         // failed sub-case: exception occuried: null as the argument
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         // failed sub-case: exception occuried: null as the argument
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         // failed sub-case: exception occuried: null as the argument
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         // failed sub-case: exception occuried: null as the argument
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         // normal sub-case: no exception: normal case
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      * Tests {@link JPFactoryImpl#getImplementationFullName}
436      *
437      * @throws Exception in the case smth. wrong occuried.
438      * @forversion 1.0
439      */
440     public void testGetImplementationFullName() throws Exception
441     {
442         assertNotNull(m_jpFactoryImpl);
443         // 1. failed sub-case: exception occuried: null arguments
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         // failed sub-case: exception occuried: null arguments
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         // 2. normal sub-case: no exception: normal cases
473         String result = null;
474         try
475         {
476             // A: default scope
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             // B: not default scope
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             // C: strings              
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             // D: incorrect implementations are got
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      * Tests {@link JPFactoryImpl#getImplementation}
522      *
523      * @throws Exception in the case smth. wrong occuried.
524      * @forversion 1.0
525      */
526     public void testGetImplementationByType() throws Exception
527     {
528         assertNotNull(m_jpFactoryImpl);
529         // 1: null input arguments
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         // 2: normal cases: juts checking all goes correctly
538         Object result = null;
539         try
540         {
541             // a: default scope
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             // a:i
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             // b: not default scope
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             // b: i
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             //  c: string key for several scopes
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         // 3: logically invalid cases
610         // failed sub-case: exception occuried: a: can not find such config element - because of such operator is not specified for such method
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         // failed sub-case: exception occuried: a: can not find such config element - because of incorrect scope
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         // 4: failed due to the errors at the configuration
640         // failed sub-case: exception occuried: a: just invalid string where the implementation should be specified
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         // failed sub-case: exception occuried: b: the interface and implementation do not correlate
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         // failed sub-case: exception occuried: c: exception in constructor
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         // failed sub-case: exception occuried: d: exception in static initialization
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         // 5: normal cases for the stirng key
698         // normal sub-case: no exception: a: for string we do not have validation for the interface and implementation speciofied
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         // 6: the root config element doesn't specify the mapping inself
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      * Tests {@link JPFactoryImpl#getImplementations}, {@link JPFactoryImpl#getImplementationsFullNames}
729      *
730      * @throws Exception in the case smth. wrong occuried.
731      * @forversion 1.0
732      */
733     public void testGetImplementations() throws Exception
734     {
735         assertNotNull(m_jpFactoryImpl);
736         // 1: null input arguments
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         // 2: invalid cases: logically can not find the appropriate config item to retrieve the implementations
752         // failed sub-case: exception occuried: not such item
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         // failed sub-case: exception occuried: not such item
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         // 3: the case when this config item for this appropriate factory item is of invalid type - do not specify the mapping itself.
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         // 4: the case when the some implementations defined at the config file do not correlate to the interface provided
780         JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
781             new Class[]{Class.class, String.class},
782             IOperator.class, NOT_DEFAULT_SCOPE + "_2");
783         // 4.b Still in the case of the just implementation full names it is working and allows us the chance to study the implementations names before
784         // the running + here is the check of all the normal getImplementationsFullNames functionality.
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             // if to return the implementation error would be here
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             // if to return the implementation error would be here
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         // 4;a in the case string is used as the key - all is ok
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         // 5: the case of incorrect implementations in the jpatterns config file
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         // 6. normal case: all is ok
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         // 7: the appropriate config definition is not for this method
887         JUnitUtils.runFailure(m_jpFactoryImpl, JUnitUtils.getRunningMethodName(), JPConfigException.class,
888             new Class[]{Class.class, String.class},
889             IJustInterface.class, null);
890     }
891 
892     /**
893      * Make the assertion of the provided map
894      *
895      * @param result the map to be checked on the structure
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      * Tests {@link JPFactoryImpl#getOperator}
913      *
914      * @throws Exception in the case smth. wrong occuried.
915      * @forversion 1.0
916      */
917     public void testGetOperator() throws Exception
918     {
919         assertNotNull(m_jpFactoryImpl);
920         // 1: null input arguments
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         // 2: correct cases
928         try
929         {
930             // A: just ordinary working case to be used
931             // default scope
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             //  not default scope
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         // 3. can not find the appropriate definition
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         // 4: incorrect definitions in the jpatterns config (study appropriate XML for details or just take a look at the parameters passed)
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         // 5: the appropriate config definition is not for this method
967         runFailure(IJustInterface.class, IOperator.class, new ProductImpl1(), null);
968     }
969 
970     /**
971      * Run failure case
972      *
973      * @param args the variables to be run
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      * Tests {@link JPFactoryImpl#getOperators}
984      *
985      * @throws Exception in the case smth. wrong occuried.
986      * @forversion 1.0
987      */
988     public void testGetOperators() throws Exception
989     {
990         assertNotNull(m_jpFactoryImpl);
991         // failed sub-case: exception occuried: null pointer
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         // failed sub-case: exception occuried: null pointer
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         // failed sub-case: exception occuried: null pointer
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         // 2: correct cases
1039         try
1040         {
1041             // A: one product is passed
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             // B: several products are passed
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         // 3: failed sub-case: exception occuried: product passed, but is hasnot appropriate implementation
1070         try
1071         {
1072             // B: several products are passed
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         // 4: wrong definitions at the bean policies
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      * Tests {@link JPFactoryImpl#validateImplementationsMap}
1108      *
1109      * @throws Exception in the case smth. wrong occuried.
1110      * @forversion 1.0
1111      */
1112     public void testValidateImplementationsMap() throws Exception
1113     {
1114         assertNotNull(m_jpFactoryImpl);
1115         // 1. won't test the null args - tha ppropriate call of InputArgumentUtils is put here
1116 
1117         // 2. validation is wrong
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         // 3: normal sub-case: no exception: all is ok
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 }