1package test.factory; 2 3 4import static org.testng.Assert.assertEquals; 5import static org.testng.Assert.assertTrue; 6 7import org.testng.annotations.Factory; 8import org.testng.annotations.Test; 9 10public class NestedStaticFactoryTest { 11 private int m_capacity = 2; 12 private float m_loadFactor = 0.4f; 13 14 static public class NestedStaticFactory { 15 @Factory 16 public Object[] createInstances() { 17 return new NestedStaticFactoryTest[] { 18 new NestedStaticFactoryTest(1, 0.1f), 19 new NestedStaticFactoryTest(10, 0.5f), 20 }; 21 } 22 }; 23 24 private static int m_instanceCount = 0; 25 public NestedStaticFactoryTest() { 26 m_instanceCount++; 27 } 28 29 public NestedStaticFactoryTest(int capacity, float loadFactor) { 30 m_instanceCount++; 31 this.m_capacity=capacity; 32 this.m_loadFactor=loadFactor; 33 } 34 35 @Test 36 public void verify() { 37 // Should have three instances: the default one created by TestNG 38 // and two created by the factory 39 assertEquals(m_instanceCount, 2); 40 assertTrue((m_capacity == 1 && m_loadFactor == 0.1f) || 41 m_capacity == 10 && m_loadFactor == 0.5f); 42 } 43 44 private static void ppp(String s) { 45 System.out.println("[NestedStaticFactoryTest] " + s); 46 } 47} 48