001: package test.distributed;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Properties;
012: import java.util.Random;
013:
014: import org.testng.Assert;
015: import org.testng.ITestResult;
016: import org.testng.TestListenerAdapter;
017: import org.testng.TestNG;
018: import org.testng.annotations.Configuration;
019: import org.testng.annotations.Test;
020: import org.testng.remote.SuiteDispatcher;
021: import org.testng.xml.XmlClass;
022: import org.testng.xml.XmlSuite;
023: import org.testng.xml.XmlTest;
024:
025: import test.BaseDistributedTest;
026:
027: import testhelper.OutputDirectoryPatch;
028:
029: public class DistributedTest extends BaseDistributedTest {
030:
031: private List<Thread> m_hostThreads = new ArrayList<Thread>();
032:
033: protected Thread startSlave(final String filename) {
034: Thread result = new Thread(new Runnable() {
035: public void run() {
036: TestNG.main(new String[] { "-slave", filename, "-d",
037: OutputDirectoryPatch.getOutputDirectory() });
038: }
039: });
040: result.setName("Slave-" + filename);
041: result.start();
042: m_hostThreads.add(result);
043: return result;
044: }
045:
046: private File createMasterProperties(String strategy)
047: throws IOException {
048: String fileName = "remote";
049:
050: File result = File.createTempFile(fileName, ".properties");
051: result.deleteOnExit();
052: Properties p = new Properties();
053: p.setProperty("testng.hosts", "localhost:" + m_ports[0]
054: + " localhost:" + m_ports[1]);
055: p.setProperty("testng.master.strategy", strategy);
056: p.setProperty("testng.verbose", "0");
057: p.setProperty("testng.master.adpter",
058: "org.testng.remote.adapter.DefaultMastertAdapter");
059: FileOutputStream fos = new FileOutputStream(result);
060: p.store(fos, "Automatically generated by tests");
061: fos.close();
062:
063: return result;
064: }
065:
066: private File createSlaveProperties(String port) throws IOException {
067: String fileName = "remote";
068:
069: File result = File.createTempFile(fileName, ".properties");
070: result.deleteOnExit();
071: Properties p = new Properties();
072: p.setProperty("testng.verbose", "0");
073: p.setProperty("slave.port", port);
074: p.setProperty("testng.slave.adpter",
075: "org.testng.remote.adapter.DefaultWorkerAdapter");
076: FileOutputStream fos = new FileOutputStream(result);
077: p.store(fos, "Automatically generated by tests");
078: fos.close();
079:
080: return result;
081: }
082:
083: private XmlSuite createSuite(String name, Class[] classes) {
084: XmlSuite result = new XmlSuite();
085: result.setName(name);
086:
087: for (Class c : classes) {
088: XmlTest test1 = new XmlTest(result);
089: test1.setName(c.getName());
090: XmlClass class1 = new XmlClass(c);
091: test1.getXmlClasses().add(class1);
092: }
093:
094: return result;
095: }
096:
097: // @ Configuration(beforeTestClass = true)
098: private void startSlaves() throws IOException {
099: int port = new Random().nextInt(50000) + 2000;
100: m_ports = new String[] { Integer.toString(port),
101: Integer.toString(port + 1) };
102:
103: File slaveFile = createSlaveProperties(m_ports[0]);
104: startSlave(slaveFile.getCanonicalPath());
105:
106: slaveFile = createSlaveProperties(m_ports[1]);
107: startSlave(slaveFile.getCanonicalPath());
108: }
109:
110: private String[] m_ports = new String[2];
111:
112: public TestListenerAdapter twoHosts(String strategy)
113: throws IOException {
114: TestNG tng = new TestNG();
115: tng.setOutputDirectory(OutputDirectoryPatch
116: .getOutputDirectory());
117:
118: File masterFile = createMasterProperties(strategy);
119: tng.setMaster(masterFile.getAbsolutePath());
120:
121: XmlSuite suite = createSuite("DistributedSuite1", new Class[] {
122: Test1.class, Test2.class });
123: tng.setXmlSuites(Arrays.asList(new XmlSuite[] { suite }));
124:
125: TestListenerAdapter result = new TestListenerAdapter();
126: tng.addListener(result);
127: tng.run();
128:
129: String[] passed = { "f1", "f2" };
130: String[] failed = {};
131: String[] skipped = {};
132:
133: verifyTests("Passed", passed, toMap(result.getPassedTests()));
134: verifyTests("Failed", failed, toMap(result.getFailedTests()));
135: verifyTests("Skipped", skipped, toMap(result.getSkippedTests()));
136:
137: return result;
138: }
139:
140: @Test
141: public void twoHostsWithTestStrategy() throws IOException {
142: startSlaves();
143: TestListenerAdapter listener = twoHosts(SuiteDispatcher.STRATEGY_TEST);
144:
145: boolean found1 = false;
146: boolean found2 = false;
147: for (ITestResult tr : listener.getPassedTests()) {
148: String host = tr.getHost();
149: if (!found1)
150: found1 = host.contains(m_ports[0]);
151: if (!found2)
152: found2 = host.contains(m_ports[1]);
153: }
154: Assert.assertTrue(found1, "No tests ran on port " + m_ports[0]);
155: Assert.assertTrue(found2, "No tests ran on port " + m_ports[1]);
156: }
157:
158: @Test
159: public void twoHostsWithSuiteStrategy() throws IOException {
160: startSlaves();
161: TestListenerAdapter listener = twoHosts(SuiteDispatcher.STRATEGY_SUITE);
162: }
163:
164: private Map<String, ITestResult> toMap(List<ITestResult> results) {
165: Map<String, ITestResult> result = new HashMap<String, ITestResult>();
166: for (ITestResult tr : results) {
167: result.put(tr.getName(), tr);
168: }
169:
170: return result;
171: }
172:
173: private void ppp(String string) {
174: System.out.println("[DistributedTest] " + string);
175: }
176: }
|