1 package test.thread; 2 3 import org.testng.Assert; 4 import org.testng.TestListenerAdapter; 5 import org.testng.TestNG; 6 import org.testng.annotations.Test; 7 import org.testng.xml.XmlSuite; 8 9 public class FactoryTest { 10 11 @Test 12 /** 13 * In non-parallel mode, we should only have one thread id 14 * for the two methods invoked on B. 15 */ verifyFactoryNotParallel()16 public void verifyFactoryNotParallel() { 17 runTest(null, 1); 18 } 19 20 /** 21 * In parallel mode "methods", we should have as many thread id's 22 * as there are test methods on B (2). 23 */ 24 @Test verifyFactoryParallelMethods()25 public void verifyFactoryParallelMethods() { 26 runTest(XmlSuite.ParallelMode.METHODS, 2); 27 } 28 29 @Test verifyFactoryParallelTests()30 public void verifyFactoryParallelTests() { 31 runTest(XmlSuite.ParallelMode.TESTS, 1); 32 } 33 runTest(XmlSuite.ParallelMode parallelMode, int expectedThreadIdCount)34 private void runTest(XmlSuite.ParallelMode parallelMode, int expectedThreadIdCount) { 35 TestNG tng = new TestNG(); 36 tng.setVerbose(0); 37 tng.setTestClasses(new Class[] { FactorySampleTest.class}); 38 if (parallelMode != null) { 39 tng.setParallel(parallelMode); 40 } 41 TestListenerAdapter tla = new TestListenerAdapter(); 42 tng.addListener(tla); 43 44 B.setUp(); 45 tng.run(); 46 47 Assert.assertEquals(tla.getPassedTests().size(), 2); 48 Assert.assertEquals(B.m_threadIds.size(), expectedThreadIdCount); 49 50 // ppp("# TESTS RUN " + tla.getPassedTests().size() 51 // + " ID:" + B.m_threadIds.size()); 52 } 53 ppp(String string)54 private void ppp(String string) { 55 System.out.println("[FactoryTest] " + string); 56 } 57 58 59 60 } 61