• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.testng.remote;
2 
3 import java.util.List;
4 import java.util.Properties;
5 
6 import org.testng.ISuite;
7 import org.testng.TestNG;
8 import org.testng.TestNGException;
9 import org.testng.collections.Lists;
10 import org.testng.internal.PropertiesFile;
11 import org.testng.internal.Utils;
12 import org.testng.remote.adapter.DefaultWorkerAdapter;
13 import org.testng.remote.adapter.IWorkerAdapter;
14 import org.testng.xml.XmlSuite;
15 
16 /**
17  * Run test suits sent by the dispatcher.
18  *
19  *
20  * @author	Guy Korland
21  * @since 	April 20, 2007
22  */
23 public class SuiteSlave
24 {
25 
26 	/**
27 	 * Properties allowed in remote.properties
28 	 */
29 	public static final String VERBOSE = "testng.verbose";
30 	public static final String SLAVE_ADPATER = "testng.slave.adpter";
31 
32 
33 	final private int m_verbose;
34 	final private IWorkerAdapter m_slaveAdpter;
35 	final private TestNG m_testng;
36 
37 	/**
38 	 * Creates a new suite dispatcher.
39 	 */
SuiteSlave( String propertiesFile, TestNG testng)40 	public SuiteSlave( String propertiesFile, TestNG testng) throws TestNGException
41 	{
42 		try
43 		{
44 			m_testng = testng;
45 
46 			PropertiesFile file = new PropertiesFile( propertiesFile);
47 			Properties properties = file.getProperties();
48 
49 			m_verbose = Integer.parseInt( properties.getProperty(VERBOSE, "1"));
50 
51 			String adapter = properties.getProperty(SLAVE_ADPATER);
52 			if( adapter == null)
53 			{
54 				m_slaveAdpter = new DefaultWorkerAdapter();
55 			}
56 			else
57 			{
58 				Class clazz = Class.forName(adapter);
59 				m_slaveAdpter = (IWorkerAdapter)clazz.newInstance();
60 			}
61 			m_slaveAdpter.init(properties);
62 		}
63 		catch( Exception e)
64 		{
65 			throw new TestNGException( "Fail to initialize slave mode", e);
66 		}
67 	}
68 
69 	/**
70 	 * Invoked in client mode.  In this case, wait for a connection
71 	 * on the given port, run the XmlSuite we received and return the SuiteRunner
72 	 * created to run it.
73 	 */
waitForSuites()74 	public void waitForSuites() {
75 		try {
76 			while (true) {
77 				//TODO set timeout
78 				XmlSuite s = m_slaveAdpter.getSuite(Long.MAX_VALUE);
79 				if( s== null) {
80           continue;
81         }
82 				log("Processing " + s.getName());
83 				List<XmlSuite> suites = Lists.newArrayList();
84 				suites.add(s);
85 				m_testng.setXmlSuites(suites);
86 				List<ISuite> suiteRunners = m_testng.runSuitesLocally();
87 				ISuite sr = suiteRunners.get(0);
88 				log("Done processing " + s.getName());
89 				m_slaveAdpter.returnResult(sr);
90 			}
91 		}
92 		catch(Exception ex) {
93 			ex.printStackTrace(System.out);
94 		}
95 	}
96 
log(String string)97 	private static void log(String string) {
98 		Utils.log("", 2, string);
99 	}
100 
101 }
102