1 package org.testng.remote; 2 3 import java.util.Collection; 4 import java.util.List; 5 import java.util.Properties; 6 7 import org.testng.ISuite; 8 import org.testng.ISuiteResult; 9 import org.testng.ITestListener; 10 import org.testng.ITestResult; 11 import org.testng.SuiteRunner; 12 import org.testng.TestNGException; 13 import org.testng.collections.Lists; 14 import org.testng.internal.IConfiguration; 15 import org.testng.internal.Invoker; 16 import org.testng.internal.PropertiesFile; 17 import org.testng.remote.adapter.DefaultMastertAdapter; 18 import org.testng.remote.adapter.IMasterAdapter; 19 import org.testng.remote.adapter.RemoteResultListener; 20 import org.testng.xml.XmlSuite; 21 import org.testng.xml.XmlTest; 22 23 /** 24 * Dispatches test suits according to the strategy defined. 25 * 26 * 27 * @author Guy Korland 28 * @since April 20, 2007 29 */ 30 public class SuiteDispatcher 31 { 32 /** 33 * Properties allowed in remote.properties 34 */ 35 public static final String MASTER_STRATEGY = "testng.master.strategy"; 36 public static final String VERBOSE = "testng.verbose"; 37 public static final String MASTER_ADPATER = "testng.master.adpter"; 38 39 /** 40 * Values allowed for STRATEGY 41 */ 42 public static final String STRATEGY_TEST = "test"; 43 public static final String STRATEGY_SUITE = "suite"; 44 45 final private int m_verbose; 46 final private boolean m_isStrategyTest; 47 48 final private IMasterAdapter m_masterAdpter; 49 50 51 /** 52 * Creates a new suite dispatcher. 53 */ SuiteDispatcher( String propertiesFile)54 public SuiteDispatcher( String propertiesFile) throws TestNGException 55 { 56 try 57 { 58 PropertiesFile file = new PropertiesFile( propertiesFile); 59 Properties properties = file.getProperties(); 60 61 m_verbose = Integer.parseInt( properties.getProperty(VERBOSE, "1")); 62 63 String strategy = properties.getProperty(MASTER_STRATEGY, STRATEGY_SUITE); 64 m_isStrategyTest = STRATEGY_TEST.equalsIgnoreCase(strategy); 65 66 String adapter = properties.getProperty(MASTER_ADPATER); 67 if( adapter == null) 68 { 69 m_masterAdpter = new DefaultMastertAdapter(); 70 } 71 else 72 { 73 Class clazz = Class.forName(adapter); 74 m_masterAdpter = (IMasterAdapter)clazz.newInstance(); 75 } 76 m_masterAdpter.init(properties); 77 } 78 catch( Exception e) 79 { 80 throw new TestNGException( "Fail to initialize master mode", e); 81 } 82 } 83 84 /** 85 * Dispatch test suites 86 * @return suites result 87 */ dispatch(IConfiguration configuration, List<XmlSuite> suites, String outputDir, List<ITestListener> testListeners)88 public List<ISuite> dispatch(IConfiguration configuration, 89 List<XmlSuite> suites, String outputDir, List<ITestListener> testListeners){ 90 List<ISuite> result = Lists.newArrayList(); 91 try 92 { 93 // 94 // Dispatch the suites/tests 95 // 96 97 for (XmlSuite suite : suites) { 98 suite.setVerbose(m_verbose); 99 SuiteRunner suiteRunner = new SuiteRunner(configuration, suite, outputDir); 100 RemoteResultListener listener = new RemoteResultListener( suiteRunner); 101 if (m_isStrategyTest) { 102 for (XmlTest test : suite.getTests()) { 103 XmlSuite tmpSuite = new XmlSuite(); 104 tmpSuite.setXmlPackages(suite.getXmlPackages()); 105 tmpSuite.setJUnit(suite.isJUnit()); 106 tmpSuite.setSkipFailedInvocationCounts(suite.skipFailedInvocationCounts()); 107 tmpSuite.setName("Temporary suite for " + test.getName()); 108 tmpSuite.setParallel(suite.getParallel()); 109 tmpSuite.setParentModule(suite.getParentModule()); 110 tmpSuite.setGuiceStage(suite.getGuiceStage()); 111 tmpSuite.setParameters(suite.getParameters()); 112 tmpSuite.setThreadCount(suite.getThreadCount()); 113 tmpSuite.setDataProviderThreadCount(suite.getDataProviderThreadCount()); 114 tmpSuite.setVerbose(suite.getVerbose()); 115 tmpSuite.setObjectFactory(suite.getObjectFactory()); 116 XmlTest tmpTest = new XmlTest(tmpSuite); 117 tmpTest.setBeanShellExpression(test.getExpression()); 118 tmpTest.setXmlClasses(test.getXmlClasses()); 119 tmpTest.setExcludedGroups(test.getExcludedGroups()); 120 tmpTest.setIncludedGroups(test.getIncludedGroups()); 121 tmpTest.setJUnit(test.isJUnit()); 122 tmpTest.setMethodSelectors(test.getMethodSelectors()); 123 tmpTest.setName(test.getName()); 124 tmpTest.setParallel(test.getParallel()); 125 tmpTest.setParameters(test.getLocalParameters()); 126 tmpTest.setVerbose(test.getVerbose()); 127 tmpTest.setXmlClasses(test.getXmlClasses()); 128 tmpTest.setXmlPackages(test.getXmlPackages()); 129 130 m_masterAdpter.runSuitesRemotely(tmpSuite, listener); 131 } 132 } 133 else 134 { 135 m_masterAdpter.runSuitesRemotely(suite, listener); 136 } 137 result.add(suiteRunner); 138 } 139 140 m_masterAdpter.awaitTermination(100000); 141 142 // 143 // Run test listeners 144 // 145 for (ISuite suite : result) { 146 for (ISuiteResult suiteResult : suite.getResults().values()) { 147 Collection<ITestResult> allTests[] = new Collection[] { 148 suiteResult.getTestContext().getPassedTests().getAllResults(), 149 suiteResult.getTestContext().getFailedTests().getAllResults(), 150 suiteResult.getTestContext().getSkippedTests().getAllResults(), 151 suiteResult.getTestContext().getFailedButWithinSuccessPercentageTests().getAllResults(), 152 }; 153 for (Collection<ITestResult> all : allTests) { 154 for (ITestResult tr : all) { 155 Invoker.runTestListeners(tr, testListeners); 156 } 157 } 158 } 159 } 160 } 161 catch( Exception ex) 162 { 163 //TODO add to logs 164 ex.printStackTrace(); 165 } 166 return result; 167 } 168 } 169