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.IChild;
25  import com.smhumayun.mi_plus.testdata.IChild2;
26  import com.smhumayun.mi_plus.testdata.IChild3;
27  import org.testng.Assert;
28  import org.testng.annotations.BeforeClass;
29  import org.testng.annotations.BeforeGroups;
30  import org.testng.annotations.BeforeMethod;
31  import org.testng.annotations.Test;
32  
33  import java.lang.reflect.Method;
34  import java.lang.reflect.Proxy;
35  
36  /**
37   * Multi Threaded Test cases against {@link com.smhumayun.mi_plus.MIFactory}
38   *
39   * @author smhumayun
40   * @since 1.0
41   */
42  @SuppressWarnings("UnusedDeclaration")
43  @Test(dependsOnGroups = {"MIFactory Basic Tests"})
44  public class MIFactory_MultiThreadTest {
45      MIFactory miFactory;
46  
47      @SuppressWarnings("UnusedDeclaration")
48      @BeforeClass
49      public void beforeClass () {
50          System.out.println("\n===================================[ " + this.getClass().getSimpleName() + " ]===================================");
51      }
52  
53      @SuppressWarnings("UnusedDeclaration")
54      @BeforeMethod
55      public void beforeMethod (Method method) throws MIException {
56          System.out.println("\n-----------------------------------[ " + method.getName() + " ]-----------------------------------");
57      }
58  
59      @SuppressWarnings("UnusedDeclaration")
60      @BeforeGroups(value = {"newInstance_SINGLETON_CONTAINER_SINGLETON_COMPOSED"
61              , "newInstance_PROTOTYPE_CONTAINER_SINGLETON_COMPOSED"
62              , "newInstance_PROTOTYPE_CONTAINER_PROTOTYPE_COMPOSED"})
63      public void beforeGroup () throws MIException {
64          miFactory = new MIFactory();
65          Assert.assertNotNull(miFactory, "NULL factory returned");
66      }
67  
68      @Test(enabled = true, groups = {"positive tests", "newInstance_SINGLETON_CONTAINER_SINGLETON_COMPOSED"}, threadPoolSize = 10, invocationCount = 50)
69      public void newInstance_SINGLETON_CONTAINER_SINGLETON_COMPOSED () throws MIException {
70          IChild child1 = (IChild) miFactory.newInstance(IChild.class);
71          Assert.assertNotNull(child1, "object returned by factory is NULL");
72          child1.parentOneMethod();
73          child1.parentTwoMethod();
74          IChild child2 = (IChild) miFactory.newInstance(IChild.class);
75          Assert.assertNotNull(child2, "object returned by factory is NULL");
76          child2.parentOneMethod();
77          child2.parentTwoMethod();
78          Assert.assertEquals(child1.hashCode(), child2.hashCode(), "hashCode() should be same");
79          Assert.assertTrue(child1.equals(child2), "equals() should be true");
80          Assert.assertEquals(child1, child2, "should be equal");
81      }
82  
83      @Test(enabled = true, groups = {"positive tests", "newInstance_PROTOTYPE_CONTAINER_SINGLETON_COMPOSED"}, threadPoolSize = 10, invocationCount = 50)
84      public void newInstance_PROTOTYPE_CONTAINER_SINGLETON_COMPOSED () throws MIException {
85          IChild2 child1 = (IChild2) miFactory.newInstance(IChild2.class);
86          Assert.assertNotNull(child1, "object returned by factory is NULL");
87          child1.parentOneMethod();
88          child1.parentTwoMethod();
89          IChild2 child2 = (IChild2) miFactory.newInstance(IChild2.class);
90          Assert.assertNotNull(child2, "object returned by factory is NULL");
91          child2.parentOneMethod();
92          child2.parentTwoMethod();
93          Assert.assertNotEquals(child1.hashCode(), child2.hashCode(), "hashCode() should not be same");
94          Assert.assertFalse(child1.equals(child2), "equals() should be false");
95          Assert.assertNotEquals(child1, child2, "should not be equal");
96          MIContainerSupport miContainerSupport1 = (MIContainerSupport) Proxy.getInvocationHandler(child1);
97          MIContainerSupport miContainerSupport2 = (MIContainerSupport) Proxy.getInvocationHandler(child2);
98          Assert.assertNotEquals(miContainerSupport1.hashCode(), miContainerSupport2.hashCode(), "MI Containers hashCode should not be same");
99          Assert.assertFalse(miContainerSupport1.equals(miContainerSupport2), "equals() for MI Containers should be false");
100         Assert.assertNotEquals(miContainerSupport1, miContainerSupport2, "MI Containers should not be equal");
101         Assert.assertEquals(miContainerSupport1.getMiContainerClass().hashCode(), miContainerSupport2.getMiContainerClass().hashCode()
102                 , "MI Container classes hashCode should be same");
103         Assert.assertTrue(miContainerSupport1.getMiContainerClass().equals(miContainerSupport2.getMiContainerClass())
104                 , "equals() for MI Container classes should be true");
105         Assert.assertEquals(miContainerSupport1.getMiContainerClass(), miContainerSupport2.getMiContainerClass()
106                 , "MI Container classes should be equal");
107         Assert.assertEquals(miContainerSupport1.getComposedObjects().hashCode(), miContainerSupport2.getComposedObjects().hashCode()
108                 , "Composed Objects hashCode should be same");
109         Assert.assertTrue(miContainerSupport1.getComposedObjects().equals(miContainerSupport2.getComposedObjects())
110                 , "equals() for Composed Objects should be true");
111         Assert.assertEquals(miContainerSupport1.getComposedObjects(), miContainerSupport2.getComposedObjects()
112                 , "Composed Objects should be equal");
113     }
114 
115     @Test(enabled = true, groups = {"positive tests", "newInstance_PROTOTYPE_CONTAINER_PROTOTYPE_COMPOSED"}, threadPoolSize = 10, invocationCount = 50)
116     public void newInstance_PROTOTYPE_CONTAINER_PROTOTYPE_COMPOSED () throws MIException {
117         IChild3 child1 = (IChild3) miFactory.newInstance(IChild3.class);
118         Assert.assertNotNull(child1, "object returned by factory is NULL");
119         child1.parentOneMethod();
120         child1.parentTwoMethod();
121         IChild3 child2 = (IChild3) miFactory.newInstance(IChild3.class);
122         Assert.assertNotNull(child2, "object returned by factory is NULL");
123         child2.parentOneMethod();
124         child2.parentTwoMethod();
125         Assert.assertNotEquals(child1.hashCode(), child2.hashCode(), "hashCode() should not be same");
126         Assert.assertFalse(child1.equals(child2), "equals() should be false");
127         Assert.assertNotEquals(child1, child2, "should not be equal");
128         MIContainerSupport miContainerSupport1 = (MIContainerSupport) Proxy.getInvocationHandler(child1);
129         MIContainerSupport miContainerSupport2 = (MIContainerSupport) Proxy.getInvocationHandler(child2);
130         Assert.assertNotEquals(miContainerSupport1.hashCode(), miContainerSupport2.hashCode(), "MI Containers hashCode should not be same");
131         Assert.assertFalse(miContainerSupport1.equals(miContainerSupport2), "equals() for MI Containers should be false");
132         Assert.assertNotEquals(miContainerSupport1, miContainerSupport2, "MI Containers should not be equal");
133         Assert.assertEquals(miContainerSupport1.getMiContainerClass().hashCode(), miContainerSupport2.getMiContainerClass().hashCode()
134                 , "MI Container classes hashCode should be same");
135         Assert.assertTrue(miContainerSupport1.getMiContainerClass().equals(miContainerSupport2.getMiContainerClass())
136                 , "equals() for MI Container classes should be true");
137         Assert.assertEquals(miContainerSupport1.getMiContainerClass(), miContainerSupport2.getMiContainerClass()
138                 , "MI Container classes should be equal");
139         Assert.assertNotEquals(miContainerSupport1.getComposedObjects().hashCode(), miContainerSupport2.getComposedObjects().hashCode()
140                 , "Composed Objects hashCode should not be same");
141         Assert.assertFalse(miContainerSupport1.getComposedObjects().equals(miContainerSupport2.getComposedObjects())
142                 , "equals() for Composed Objects should be false");
143         Assert.assertNotEquals(miContainerSupport1.getComposedObjects(), miContainerSupport2.getComposedObjects()
144                 , "Composed Objects should not be equal");
145     }
146 }