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