View Javadoc

1   /**
2    * Project MI+
3    *
4    * Copyright (c) 2013, Syed Muhammad Humayun - smhumayun@gmail.com
5    *
6    * This project includes software developed by Syed Muhammad Humayun - smhumayun@gmail.com
7    * http://www.smhumayun.com
8    *
9    * Licensed under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License. You may obtain a copy of the License at:
12   *
13   * http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on
17   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied. See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  package com.smhumayun.mi_plus;
23  
24  import com.smhumayun.mi_plus.testdata.*;
25  import org.testng.Assert;
26  import org.testng.annotations.BeforeClass;
27  import org.testng.annotations.BeforeMethod;
28  import org.testng.annotations.Test;
29  
30  import java.lang.reflect.Method;
31  import java.util.Arrays;
32  
33  /**
34   * Test cases against {@link MIFactory}
35   *
36   * @author smhumayun
37   * @since 1.0
38   */
39  @SuppressWarnings("UnusedDeclaration")
40  @Test(groups = {"MIFactory Basic Tests"})
41  public class MIFactoryTest {
42  
43      MIFactory miFactory;
44  
45      @SuppressWarnings("UnusedDeclaration")
46      @BeforeClass
47      public void beforeClass () {
48          System.out.println("\n===================================[ " + this.getClass().getSimpleName() + " ]===================================");
49      }
50  
51      @SuppressWarnings("UnusedDeclaration")
52      @BeforeMethod
53      public void beforeMethod (Method method) throws MIException {
54          System.out.println("\n-----------------------------------[ " + method.getName() + " ]-----------------------------------");
55          miFactory = new MIFactory();
56          Assert.assertNotNull(miFactory, "NULL factory returned");
57      }
58  
59      @Test(groups = {"negative tests"}, expectedExceptions = {MIException.class})
60      public void newInstance_missingAnnotation () throws MIException {
61          class MITest {}
62          miFactory.newInstance(MITest.class);
63      }
64  
65      @Test(groups = {"negative tests"}, expectedExceptions = {MIException.class})
66      public void newInstance_missingParentClasses () throws MIException {
67          @MISupport(parentClasses = {})
68          class MITest {}
69          miFactory.newInstance(MITest.class);
70      }
71  
72      @Test(groups = {"negative tests"}, expectedExceptions = {MIException.class})
73      public void newInstance_parentInterface () throws MIException {
74          @MISupport(parentClasses = {MIFactory.class})
75          class MITest {}
76          miFactory.newInstance(MITest.class);
77      }
78  
79      @Test(groups = {"negative tests"}, expectedExceptions = {MIException.class})
80      public void newInstance_parentAbstract () throws MIException {
81          abstract class AbstractParent {}
82          @MISupport(parentClasses = {AbstractParent.class})
83          class MITest {}
84          miFactory.newInstance(MITest.class);
85      }
86  
87      @Test(groups = {"positive tests"})
88      public void newInstance_parentStaticNestedClass () throws MIException {
89          miFactory.newInstance(IChild4.class);
90      }
91  
92      @Test(groups = {"negative tests"}, expectedExceptions = {MIException.class})
93      public void newInstance_parentNonStaticNestedClass () throws MIException {
94          miFactory.newInstance(IChild5.class);
95      }
96  
97      @Test(groups = {"negative tests"}, expectedExceptions = {MIException.class})
98      public void newInstance_missingNoArgConstructor () throws MIException {
99          class Parent { private Parent() {} }
100         @MISupport(parentClasses = {Parent.class})
101         class MITest {}
102         miFactory.newInstance(MITest.class);
103     }
104 
105     @Test(groups = {"positive tests"})
106     public void newInstance_callParentMethods () throws MIException {
107         IChild child = (IChild) miFactory.newInstance(IChild.class);
108         Assert.assertNotNull(child, "object returned by factory is NULL");
109         child.parentOneMethod();
110         child.parentTwoMethod();
111         Assert.assertTrue(child.sameMethodSignature().equals(ParentOne.class.getSimpleName()));
112     }
113 
114     @Test(groups = {"positive tests"})
115     public void newInstance_callDuplicateParentMethods () throws MIException {
116         IChild child = (IChild) miFactory.newInstance(IChild.class);
117         Assert.assertNotNull(child, "object returned by factory is NULL");
118         Assert.assertTrue(child.sameMethodSignature().equals(ParentOne.class.getSimpleName()));
119     }
120 
121     @Test(groups = {"positive tests"})
122     public void testScanPackagesAndProcessAnnotatedClasses () {
123         MIFactory miFactory = new MIFactory(Arrays.asList("com.smhumayun.mi_plus.testdata"));
124         Assert.assertNotNull(miFactory, "NULL factory returned");
125         Assert.assertNotNull(miFactory.getMiObjectCache().getCachedMiContainerProxy(IChild.class)
126                 , IChild.class.getName() + "'s instance not found in Container Proxy Cache");
127         Assert.assertNull(miFactory.getMiObjectCache().getCachedMiContainerProxy(IChild2.class)
128                 , IChild2.class.getName() + "'s instance found in Container Proxy Cache");
129         Assert.assertNotNull(miFactory.getMiObjectCache().getCachedComposedObjects(IChild2.class)
130                 , IChild2.class.getName() + "'s instance not found in Composed Object Cache");
131         Assert.assertNull(miFactory.getMiObjectCache().getCachedMiContainerProxy(IChild3.class)
132                 , IChild3.class.getName() + "'s instance found in Container Proxy Cache");
133         Assert.assertNull(miFactory.getMiObjectCache().getCachedComposedObjects(IChild3.class)
134                 , IChild3.class.getName() + "'s instance found in Composed Object Cache");
135         Assert.assertNotNull(miFactory.getMiObjectCache().getCachedMiContainerProxy(IChild4.class)
136                 , IChild4.class.getName() + "'s instance not found in Container Proxy Cache");
137     }
138 
139 }