001: package test;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Set;
010:
011: import org.testng.ISuite;
012: import org.testng.ITestResult;
013: import org.testng.ITestRunnerFactory;
014: import org.testng.SuiteRunner;
015: import org.testng.TestListenerAdapter;
016: import org.testng.TestRunner;
017: import org.testng.internal.annotations.DefaultAnnotationTransformer;
018: import org.testng.internal.annotations.IAnnotationFinder;
019: import org.testng.internal.annotations.IAnnotationTransformer;
020: import org.testng.internal.annotations.JDK14AnnotationFinder;
021: import org.testng.reporters.JUnitXMLReporter;
022: import org.testng.reporters.TestHTMLReporter;
023: import org.testng.xml.XmlClass;
024: import org.testng.xml.XmlSuite;
025: import org.testng.xml.XmlTest;
026:
027: /**
028: * Base class for tests
029: *
030: * @author Cedric Beust, May 5, 2004
031: * @author <a href='mailto:the_mindstorm@evolva.ro'>Alexandru Popescu</a>
032: */
033: public class BaseTest {
034: private static final String m_outputDirectory = "test-output-tests";
035:
036: private XmlSuite m_suite = null;
037: private ITestRunnerFactory m_testRunnerFactory;
038: private IAnnotationTransformer m_defaultAnnotationTransformer = new DefaultAnnotationTransformer();
039: private IAnnotationFinder m_javadocAnnotationFinder;
040:
041: public BaseTest() {
042: m_testRunnerFactory = new InternalTestRunnerFactory(this );
043: }
044:
045: protected void setDebug() {
046: getTest().setVerbose(9);
047: }
048:
049: protected void setParallel(String parallel) {
050: getTest().setParallel(parallel);
051: }
052:
053: public void setThreadCount(int threadCount) {
054: getTest().getSuite().setThreadCount(threadCount);
055: }
056:
057: protected void setVerbose(int n) {
058: getTest().setVerbose(n);
059: }
060:
061: private Map m_tests = new HashMap();
062: private Map m_passedTests = new HashMap();
063: private Map m_failedTests = new HashMap();
064: private Map m_skippedTests = new HashMap();
065: private Map m_failedButWithinSuccessPercentageTests = new HashMap();
066:
067: protected Map getTests(Map map) {
068: Map result = (Map) map.get(getId());
069: if (null == result) {
070: result = new HashMap();
071: map.put(getId(), result);
072: }
073: return result;
074: }
075:
076: protected XmlTest getTest() {
077: return (XmlTest) m_tests.get(getId());
078: }
079:
080: protected void setTests(Map map, Map m) {
081: map.put(getId(), m);
082: }
083:
084: public Map getFailedTests() {
085: return getTests(m_failedTests);
086: }
087:
088: // Map<String, List<ITestResult>>
089: public Map getFailedButWithinSuccessPercentageTests() {
090: return getTests(m_failedButWithinSuccessPercentageTests);
091: }
092:
093: public Map getPassedTests() {
094: return getTests(m_passedTests);
095: }
096:
097: public Map getSkippedTests() {
098: return getTests(m_skippedTests);
099: }
100:
101: public void setSkippedTests(Map m) {
102: setTests(m_skippedTests, m);
103: }
104:
105: public void setPassedTests(Map m) {
106: setTests(m_passedTests, m);
107: }
108:
109: public void setFailedTests(Map m) {
110: setTests(m_failedTests, m);
111: }
112:
113: public void setFailedButWithinSuccessPercentageTests(Map m) {
114: setTests(m_failedButWithinSuccessPercentageTests, m);
115: }
116:
117: protected void run() {
118: assert null != getTest() : "Test wasn't set, maybe @Configuration methodSetUp() was never called";
119: setPassedTests(new HashMap());
120: setFailedTests(new HashMap());
121: setSkippedTests(new HashMap());
122: setFailedButWithinSuccessPercentageTests(new HashMap());
123:
124: m_suite.setVerbose(new Integer(0));
125: SuiteRunner suite = new SuiteRunner(m_suite, m_outputDirectory,
126: m_testRunnerFactory, new IAnnotationFinder[] {
127: m_javadocAnnotationFinder, null });
128:
129: suite.run();
130: }
131:
132: public void addIncludedMethod(String className, String m) {
133: XmlClass xmlClass = findClass(className);
134: xmlClass.getIncludedMethods().add(m);
135: getTest().getXmlClasses().add(xmlClass);
136: }
137:
138: public void addExcludedMethod(String className, String m) {
139: XmlClass xmlClass = findClass(className);
140: xmlClass.getExcludedMethods().add(m);
141: getTest().getXmlClasses().add(xmlClass);
142: }
143:
144: private XmlClass findClass(String className) {
145: for (Iterator it = getTest().getXmlClasses().iterator(); it
146: .hasNext();) {
147: XmlClass cl = (XmlClass) it.next();
148: if (cl.getName().equals(className)) {
149: return cl;
150: }
151: }
152:
153: XmlClass result = addClass(className);
154: return result;
155: }
156:
157: public void addIncludedGroup(String g) {
158: getTest().getIncludedGroups().add(g);
159: }
160:
161: public void addExcludedGroup(String g) {
162: getTest().getExcludedGroups().add(g);
163: }
164:
165: public void addMetaGroup(String mg, List l) {
166: getTest().getMetaGroups().put(mg, l);
167: }
168:
169: public void addMetaGroup(String mg, String n) {
170: List l = new ArrayList();
171: l.add(n);
172: addMetaGroup(mg, l);
173: }
174:
175: public void setParameter(String key, String value) {
176: getTest().addParameter(key, value);
177: }
178:
179: protected XmlClass addClass(String className) {
180: XmlClass result = new XmlClass(className);
181: getTest().getXmlClasses().add(result);
182: return result;
183: }
184:
185: private void setTest(XmlTest test) {
186: XmlTest t = (XmlTest) m_tests.get(getId());
187: if (null == t) {
188: m_tests.put(getId(), t);
189: }
190: }
191:
192: /**
193: * @testng.before-method groups = "init, initTest, current"
194: * @ testng.configuration beforeTestMethod="true" groups="init,initTest,current"
195: */
196: public void methodSetUp() {
197: m_javadocAnnotationFinder = new JDK14AnnotationFinder(
198: m_defaultAnnotationTransformer);
199: m_javadocAnnotationFinder
200: .addSourceDirs(new String[] { "./test-14/src" });
201: m_suite = new XmlSuite();
202: m_suite.setAnnotations("javadoc");
203: m_tests.put(getId(), new XmlTest(m_suite));
204: getTest().setName("Internal test, failures are expected");
205: }
206:
207: protected void verifyResults(Map tests, int expected, String message) {
208: // protected void verifyResults(Map<String, List<ITestResult>> tests, int expected, String message) {
209: if (tests.size() > 0) {
210: Set keys = tests.keySet();
211: Object firstKey = keys.iterator().next();
212: List passedResult = (List) tests.get(firstKey);
213: int n = passedResult.size();
214: assert n == expected : "Expected " + expected + " "
215: + message + " but found " + n;
216: } else {
217: assert expected == 0 : "Expected " + expected + " "
218: + message + " but found " + tests.size();
219: }
220: }
221:
222: protected void verifyTests(String title, String[] exp, Map found) {
223: Map expected = new HashMap();
224: for (int i = 0; i < exp.length; i++) {
225: expected.put(exp[i], exp[i]);
226: }
227:
228: assert expected.size() == found.size() : "Expected "
229: + expected.size() + " " + title + " tests but found "
230: + found.size();
231:
232: for (Iterator it = expected.values().iterator(); it.hasNext();) {
233: String name = (String) it.next();
234: if (null == found.get(name)) {
235: dumpMap("Expected", expected);
236: dumpMap("Found", found);
237: }
238: assert null != found.get(name) : "Expected to find method "
239: + name + " in " + title + " but didn't find it.";
240: }
241: }
242:
243: private void dumpMap(String title, Map m) {
244: System.out.println("==== " + title);
245: for (Iterator it = m.keySet().iterator(); it.hasNext();) {
246: Object key = it.next();
247: Object value = m.get(key);
248: ppp(key + " => " + value);
249: }
250:
251: }
252:
253: private Collection computeDifferences(Map m1, Map m2) {
254: List result = new ArrayList();
255:
256: for (Iterator it = m1.keySet().iterator(); it.hasNext();) {
257: Object key = it.next();
258: }
259:
260: return result;
261: }
262:
263: /** Map<String, List<ITestResult>> */
264: private void addTest(Map tests, ITestResult t) {
265: List l = (List) tests.get(t.getName());
266: if (null == l) {
267: l = new ArrayList();
268: tests.put(t.getName(), l);
269: }
270: l.add(t);
271: }
272:
273: public void addPassedTest(ITestResult t) {
274: addTest(getPassedTests(), t);
275: }
276:
277: public void addFailedTest(ITestResult t) {
278: addTest(getFailedTests(), t);
279: }
280:
281: public void addFailedButWithinSuccessPercentageTest(ITestResult t) {
282: addTest(getFailedButWithinSuccessPercentageTests(), t);
283: }
284:
285: public void addSkippedTest(ITestResult t) {
286: addTest(getSkippedTests(), t);
287: }
288:
289: private void ppp(String s) {
290: System.out.println("[BaseTest " + getId() + "] " + s);
291: }
292:
293: protected Long getId() {
294: return new Long(Thread.currentThread().hashCode());
295: }
296:
297: public XmlSuite getSuite() {
298: return m_suite;
299: }
300:
301: private static class InternalTestRunnerFactory implements
302: ITestRunnerFactory {
303: private final BaseTest m_baseTest;
304:
305: public InternalTestRunnerFactory(final BaseTest baseTest) {
306: m_baseTest = baseTest;
307: }
308:
309: /**
310: * @see org.testng.ITestRunnerFactory#newTestRunner(org.testng.ISuite, org.testng.xml.XmlTest)
311: */
312: public TestRunner newTestRunner(ISuite suite, XmlTest test) {
313: TestRunner testRunner = new TestRunner(suite, test, false);
314:
315: testRunner.addTestListener(new TestHTMLReporter());
316: testRunner.addTestListener(new JUnitXMLReporter());
317: testRunner.addTestListener(new TestListener(m_baseTest));
318:
319: return testRunner;
320: }
321: }
322:
323: private static class TestListener extends TestListenerAdapter {
324: private BaseTest m_test = null;
325:
326: public TestListener(BaseTest t1) {
327: m_test = t1;
328: }
329:
330: public void onTestSuccess(ITestResult tr) {
331: m_test.addPassedTest(tr);
332: }
333:
334: public void onTestFailure(ITestResult tr) {
335: m_test.addFailedTest(tr);
336: }
337:
338: public void onTestSkipped(ITestResult tr) {
339: m_test.addSkippedTest(tr);
340: }
341:
342: public void onTestFailedButWithinSuccessPercentage(
343: ITestResult result) {
344: m_test.addFailedButWithinSuccessPercentageTest(result);
345: }
346: } // TestListener
347:
348: } // BaseTest
|