• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test;
2 
3 
4 import java.io.BufferedReader;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Collection;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.regex.Pattern;
16 
17 import org.testng.Assert;
18 import org.testng.IClassListener;
19 import org.testng.IInvokedMethodListener;
20 import org.testng.ISuite;
21 import org.testng.ITestResult;
22 import org.testng.ITestRunnerFactory;
23 import org.testng.SuiteRunner;
24 import org.testng.TestListenerAdapter;
25 import org.testng.TestRunner;
26 import org.testng.annotations.BeforeMethod;
27 import org.testng.collections.Lists;
28 import org.testng.internal.Configuration;
29 import org.testng.internal.IConfiguration;
30 import org.testng.reporters.JUnitXMLReporter;
31 import org.testng.reporters.TestHTMLReporter;
32 import org.testng.xml.XmlClass;
33 import org.testng.xml.XmlInclude;
34 import org.testng.xml.XmlMethodSelector;
35 import org.testng.xml.XmlPackage;
36 import org.testng.xml.XmlSuite;
37 import org.testng.xml.XmlTest;
38 
39 /**
40  * Base class for tests
41  *
42  * @author Cedric Beust, May 5, 2004
43  *
44  */
45 public class BaseTest extends BaseDistributedTest {
46   private static final String m_outputDirectory= "test-output-tests";
47 
48   private XmlSuite m_suite= null;
49   private ITestRunnerFactory m_testRunnerFactory;
50   private IConfiguration m_configuration;
51 
52   private Integer m_verbose = null;
53 
BaseTest()54   public BaseTest() {
55     m_testRunnerFactory= new InternalTestRunnerFactory(this);
56     m_configuration = new Configuration();
57   }
58 
getConfiguration()59   private IConfiguration getConfiguration() {
60     return m_configuration;
61   }
62 
setDebug()63   protected void setDebug() {
64     getTest().setVerbose(9);
65   }
66 
setParallel(XmlSuite.ParallelMode parallel)67   protected void setParallel(XmlSuite.ParallelMode parallel) {
68     getTest().setParallel(parallel);
69   }
70 
setVerbose(int n)71   protected void setVerbose(int n) {
72     m_verbose = n;
73   }
74 
setTestTimeOut(long n)75   protected void setTestTimeOut(long n) {
76       getTest().setTimeOut(n);
77   }
78 
setSuiteTimeOut(long n)79   protected void setSuiteTimeOut(long n) {
80       m_suite.setTimeOut(Long.toString(n));
81   }
82 
setJUnit(boolean f)83   protected void setJUnit(boolean f) {
84     getTest().setJUnit(f);
85   }
86 
setThreadCount(int count)87   protected void setThreadCount(int count) {
88     getTest().getSuite().setThreadCount(count);
89   }
90 
91   private Map<Long, XmlTest> m_tests= new HashMap<>();
92   private Map<Long, Map> m_passedTests= new HashMap<>();
93   private Map<Long, Map> m_failedTests= new HashMap<>();
94   private Map<Long, Map> m_skippedTests= new HashMap<>();
95   private Map<Long, XmlTest> m_testConfigs= new HashMap<>();
96   private Map<Long, Map> m_passedConfigs= new HashMap<>();
97   private Map<Long, Map> m_failedConfigs= new HashMap<>();
98   private Map<Long, Map> m_skippedConfigs= new HashMap<>();
99   private Map<Long, Map> m_failedButWithinSuccessPercentageTests= new HashMap<>();
100 
getTests(Map<Long, Map> map)101   protected Map<String, List<ITestResult>> getTests(Map<Long, Map> map) {
102     Map<String, List<ITestResult>> result= map.get(getId());
103     if(null == result) {
104       result= new HashMap<>();
105       map.put(getId(), result);
106     }
107 
108     return result;
109   }
110 
getTest()111   protected XmlTest getTest() {
112     return m_tests.get(getId());
113   }
114 
setTests(Map<Long, Map> map, Map m)115   protected void setTests(Map<Long, Map> map, Map m) {
116     map.put(getId(), m);
117   }
118 
getFailedTests()119   public Map<String, List<ITestResult>> getFailedTests() {
120     return getTests(m_failedTests);
121   }
122 
getFailedButWithinSuccessPercentageTests()123   public Map<String, List<ITestResult>> getFailedButWithinSuccessPercentageTests() {
124     return getTests(m_failedButWithinSuccessPercentageTests);
125   }
126 
getPassedTests()127   public Map<String, List<ITestResult>> getPassedTests() {
128     return getTests(m_passedTests);
129   }
130 
getSkippedTests()131   public Map<String, List<ITestResult>> getSkippedTests() {
132     return getTests(m_skippedTests);
133   }
134 
getFailedConfigs()135   public Map<String, List<ITestResult>> getFailedConfigs() {
136     return getTests(m_failedConfigs);
137   }
138 
getPassedConfigs()139   public Map<String, List<ITestResult>> getPassedConfigs() {
140     return getTests(m_passedConfigs);
141   }
142 
getSkippedConfigs()143   public Map<String, List<ITestResult>> getSkippedConfigs() {
144     return getTests(m_skippedConfigs);
145   }
146 
setSkippedTests(Map m)147   public void setSkippedTests(Map m) {
148     setTests(m_skippedTests, m);
149   }
150 
setPassedTests(Map m)151   public void setPassedTests(Map m) {
152     setTests(m_passedTests, m);
153   }
154 
setFailedTests(Map m)155   public void setFailedTests(Map m) {
156     setTests(m_failedTests, m);
157   }
158 
setFailedButWithinSuccessPercentageTests(Map m)159   public void setFailedButWithinSuccessPercentageTests(Map m) {
160     setTests(m_failedButWithinSuccessPercentageTests, m);
161   }
162 
setSkippedConfigs(Map m)163   public void setSkippedConfigs(Map m) {
164     setTests(m_skippedConfigs, m);
165   }
166 
setPassedConfigs(Map m)167   public void setPassedConfigs(Map m) {
168     setTests(m_passedConfigs, m);
169   }
170 
setFailedConfigs(Map m)171   public void setFailedConfigs(Map m) {
172     setTests(m_failedConfigs, m);
173   }
174 
175 
run()176   protected void run() {
177     assert null != getTest() : "Test wasn't set, maybe @Configuration methodSetUp() was never called";
178     setPassedTests(new HashMap());
179     setFailedTests(new HashMap());
180     setSkippedTests(new HashMap());
181     setPassedConfigs(new HashMap());
182     setFailedConfigs(new HashMap());
183     setSkippedConfigs(new HashMap());
184     setFailedButWithinSuccessPercentageTests(new HashMap());
185 
186     m_suite.setVerbose(m_verbose != null ? m_verbose : 0);
187     SuiteRunner suite = new SuiteRunner(m_configuration,
188         m_suite, m_outputDirectory, m_testRunnerFactory);
189 
190     suite.run();
191   }
192 
addMethodSelector(String className, int priority)193   protected void addMethodSelector(String className, int priority) {
194     XmlMethodSelector methodSelector= new XmlMethodSelector();
195     methodSelector.setName(className);
196     methodSelector.setPriority(priority);
197     getTest().getMethodSelectors().add(methodSelector);
198   }
199 
addClass(Class<?> cls)200   protected XmlClass addClass(Class<?> cls) {
201     return addClass(cls.getName());
202   }
203 
addClass(String className)204   protected XmlClass addClass(String className) {
205     XmlClass result= new XmlClass(className);
206     getTest().getXmlClasses().add(result);
207 
208     return result;
209   }
210 
setBeanShellExpression(String expression)211   protected void setBeanShellExpression(String expression) {
212     getTest().setBeanShellExpression(expression);
213   }
214 
addPackage(String pkgName, String[] included, String[] excluded)215   protected void addPackage(String pkgName, String[] included, String[] excluded) {
216     XmlPackage pkg= new XmlPackage();
217     pkg.setName(pkgName);
218     pkg.getInclude().addAll(Arrays.asList(included));
219     pkg.getExclude().addAll(Arrays.asList(excluded));
220     getTest().getSuite().getXmlPackages().add(pkg);
221   }
222 
findClass(String className)223   private XmlClass findClass(String className) {
224     for(XmlClass cl : getTest().getXmlClasses()) {
225       if(cl.getName().equals(className)) {
226         return cl;
227       }
228     }
229 
230     XmlClass result= addClass(className);
231 
232     return result;
233   }
234 
addIncludedMethod(String className, String m)235   public void addIncludedMethod(String className, String m) {
236     XmlClass xmlClass= findClass(className);
237     xmlClass.getIncludedMethods().add(new XmlInclude(m));
238     getTest().getXmlClasses().add(xmlClass);
239   }
240 
addExcludedMethod(String className, String m)241   public void addExcludedMethod(String className, String m) {
242     XmlClass xmlClass= findClass(className);
243     xmlClass.getExcludedMethods().add(m);
244     getTest().getXmlClasses().add(xmlClass);
245   }
246 
addIncludedGroup(String g)247   public void addIncludedGroup(String g) {
248     getTest().addIncludedGroup(g);
249   }
250 
addExcludedGroup(String g)251   public void addExcludedGroup(String g) {
252     getTest().addExcludedGroup(g);
253   }
254 
addMetaGroup(String mg, List<String> l)255   public void addMetaGroup(String mg, List<String> l) {
256     getTest().getMetaGroups().put(mg, l);
257   }
258 
addMetaGroup(String mg, String n)259   public void addMetaGroup(String mg, String n) {
260     List<String> l= new ArrayList<>();
261     l.add(n);
262     addMetaGroup(mg, l);
263   }
264 
setParameter(String key, String value)265   public void setParameter(String key, String value) {
266     getTest().addParameter(key, value);
267   }
268 
269 //  @Configuration(beforeTestMethod = true, groups = { "init", "initTest"})
270   @BeforeMethod(groups= { "init", "initTest" })
methodSetUp()271   public void methodSetUp() {
272     m_suite= new XmlSuite();
273     m_suite.setName("Internal_suite");
274     XmlTest xmlTest= new XmlTest(m_suite);
275     xmlTest.setName("Internal_test_failures_are_expected");
276     m_tests.put(getId(), xmlTest);
277   }
278 
addTest(Map<String, List<ITestResult>> tests, ITestResult t)279   private void addTest(Map<String, List<ITestResult>> tests, ITestResult t) {
280     List<ITestResult> l= tests.get(t.getMethod().getMethodName());
281     if(null == l) {
282       l= new ArrayList<>();
283       tests.put(t.getMethod().getMethodName(), l);
284     }
285     l.add(t);
286   }
287 
addPassedTest(ITestResult t)288   public void addPassedTest(ITestResult t) {
289     addTest(getPassedTests(), t);
290   }
291 
addFailedTest(ITestResult t)292   public void addFailedTest(ITestResult t) {
293     addTest(getFailedTests(), t);
294   }
295 
addFailedButWithinSuccessPercentageTest(ITestResult t)296   public void addFailedButWithinSuccessPercentageTest(ITestResult t) {
297     addTest(getFailedButWithinSuccessPercentageTests(), t);
298   }
299 
addSkippedTest(ITestResult t)300   public void addSkippedTest(ITestResult t) {
301     addTest(getSkippedTests(), t);
302   }
303 
addPassedConfig(ITestResult t)304   public void addPassedConfig(ITestResult t) {
305     addTest(getPassedConfigs(), t);
306   }
307 
addFailedConfig(ITestResult t)308   public void addFailedConfig(ITestResult t) {
309     addTest(getFailedConfigs(), t);
310   }
311 
addSkippedConfig(ITestResult t)312   public void addSkippedConfig(ITestResult t) {
313     addTest(getSkippedConfigs(), t);
314   }
315 
ppp(String s)316   private void ppp(String s) {
317     System.out.println("[BaseTest " + getId() + "] " + s);
318   }
319 
getId()320   protected Long getId() {
321     return 42L;
322 //    long result = Thread.currentThread().getId();
323 ////    System.out.println("RETURNING ID " + result);
324 //    return result;
325   }
326 
getSuite()327   public XmlSuite getSuite() {
328     return m_suite;
329   }
330 
setSuite(XmlSuite suite)331   public void setSuite(XmlSuite suite) {
332     m_suite = suite;
333   }
334 
335   /**
336    * Used for instanceCount testing, when we need to look inside the
337    * TestResult to count the various SUCCESS/FAIL/FAIL_BUT_OK
338    */
verifyResults(Map<String, List<ITestResult>> tests, int expected, String message)339   protected void verifyResults(Map<String, List<ITestResult>> tests,
340                                int expected,
341                                String message) {
342     if(tests.size() > 0) {
343       Set keys= tests.keySet();
344       Object firstKey= keys.iterator().next();
345       List<ITestResult> passedResult= tests.get(firstKey);
346       int n= passedResult.size();
347       assert n == expected : "Expected " + expected + " " + message + " but found " + n;
348     }
349     else {
350       assert expected == 0 : "Expected " + expected + " " + message + " but found "
351         + tests.size();
352     }
353   }
354 
dumpResults(String name, Map<String, List<ITestResult>> tests)355   protected void dumpResults(String name, Map<String, List<ITestResult>> tests) {
356     ppp("============= " + name);
357     for(Map.Entry<String, List<ITestResult>> entry : tests.entrySet()) {
358       ppp("TEST:" + entry.getKey());
359       List<ITestResult> l= entry.getValue();
360       for(ITestResult tr : l) {
361         ppp("   " + tr);
362       }
363     }
364   }
365 
verifyInstanceNames(String title, Map<String, List<ITestResult>> actual, String[] expected)366   protected static void verifyInstanceNames(String title, Map<String, List<ITestResult>> actual,
367       String[] expected)
368   {
369     List<String> actualNames = Lists.newArrayList();
370     for (Map.Entry<String, List<ITestResult>> es : actual.entrySet()) {
371       for (ITestResult tr : es.getValue()) {
372         Object instance = tr.getInstance();
373         actualNames.add(es.getKey() + "#" + (instance != null ? instance.toString() : ""));
374       }
375     }
376     Assert.assertEqualsNoOrder(actualNames.toArray(), expected);
377   }
378 
verifyPassedTests(String... expectedPassed)379   protected void verifyPassedTests(String... expectedPassed) {
380     verifyTests("Passed", expectedPassed, getPassedTests());
381   }
382 
verifyFailedTests(String... expectedFailed)383   protected void verifyFailedTests(String... expectedFailed) {
384     verifyTests("Failed", expectedFailed, getFailedTests());
385   }
386 
387   /**
388      *
389      * @param fileName The filename to parse
390      * @param regexp The regular expression
391      * @param resultLines An out parameter that will contain all the lines
392      * that matched the regexp
393      * @return A List<Integer> containing the lines of all the matches
394      *
395      * Note that the size() of the returned valuewill always be equal to
396      * result.size() at the end of this function.
397      */
grep(File fileName, String regexp, List<String> resultLines)398     public static List<Integer> grep(File fileName, String regexp, List<String> resultLines) {
399       List<Integer> resultLineNumbers = new ArrayList<>();
400       BufferedReader fr = null;
401       try {
402         fr = new BufferedReader(new FileReader(fileName));
403         String line = fr.readLine();
404         int currentLine = 0;
405         Pattern p = Pattern.compile(".*" + regexp + ".*");
406 
407         while(null != line) {
408   //        ppp("COMPARING " + p + " TO @@@" + line + "@@@");
409           if(p.matcher(line).matches()) {
410             resultLines.add(line);
411             resultLineNumbers.add(currentLine);
412           }
413 
414           line = fr.readLine();
415           currentLine++;
416         }
417       } catch(IOException e) {
418         e.printStackTrace();
419       }
420       finally {
421         if(null != fr) {
422           try {
423             fr.close();
424           }
425           catch(IOException ex) {
426             ex.printStackTrace();
427           }
428         }
429       }
430 
431       return resultLineNumbers;
432 
433     }
434 
435   private static class InternalTestRunnerFactory implements ITestRunnerFactory {
436     private final BaseTest m_baseTest;
437 
InternalTestRunnerFactory(final BaseTest baseTest)438     public InternalTestRunnerFactory(final BaseTest baseTest) {
439       m_baseTest= baseTest;
440     }
441 
442     /**
443      * @see org.testng.ITestRunnerFactory#newTestRunner(org.testng.ISuite, org.testng.xml.XmlTest)
444      */
445     @Override
newTestRunner(ISuite suite, XmlTest test, Collection<IInvokedMethodListener> listeners, List<IClassListener> classListeners)446     public TestRunner newTestRunner(ISuite suite, XmlTest test,
447         Collection<IInvokedMethodListener> listeners, List<IClassListener> classListeners) {
448       TestRunner testRunner= new TestRunner(m_baseTest.getConfiguration(), suite, test, false,
449           listeners, classListeners);
450 
451       testRunner.addTestListener(new TestHTMLReporter());
452       testRunner.addTestListener(new JUnitXMLReporter());
453       testRunner.addListener(new TestListener(m_baseTest));
454       if (listeners != null) {
455         for (IInvokedMethodListener l : listeners) {
456           testRunner.addListener(l);
457         }
458       }
459 
460       return testRunner;
461     }
462   }
463 
464   /**
465    *  Deletes all files and subdirectories under dir.
466 
467    *  @return true if all deletions were successful.
468    *  If a deletion fails, the method stops attempting to delete and returns false.
469    */
deleteDir(File dir)470   public static boolean deleteDir(File dir) {
471     if (dir.isDirectory()) {
472       String[] children = dir.list();
473       for (String element : children) {
474         boolean success = deleteDir(new File(dir, element));
475         if (!success) {
476           return false;
477         }
478       }
479     }
480 
481     // The directory is now empty so delete it
482     return dir.delete();
483   }
484 
runTest(String cls, String[] passed, String[] failed, String[] skipped)485   protected void runTest(String cls, String[] passed, String[] failed, String[] skipped) {
486     addClass(cls);
487     run();
488     verifyTests("Passed", passed, getPassedTests());
489     verifyTests("Failed", failed, getFailedTests());
490     verifyTests("Skipped", skipped, getSkippedTests());
491   }
492 
493 } // BaseTest
494 
495 ////////////////////////////
496 
497 class TestListener extends TestListenerAdapter {
498   private static BaseTest m_test= null;
499 
TestListener(BaseTest t1)500   public TestListener(BaseTest t1) {
501     m_test= t1;
502   }
503 
504   @Override
onTestSuccess(ITestResult tr)505   public void onTestSuccess(ITestResult tr) {
506     m_test.addPassedTest(tr);
507   }
508 
509   @Override
onTestFailure(ITestResult tr)510   public void onTestFailure(ITestResult tr) {
511     m_test.addFailedTest(tr);
512   }
513 
514   @Override
onTestFailedButWithinSuccessPercentage(ITestResult result)515   public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
516     m_test.addFailedButWithinSuccessPercentageTest(result);
517   }
518 
519   @Override
onTestSkipped(ITestResult tr)520   public void onTestSkipped(ITestResult tr) {
521     m_test.addSkippedTest(tr);
522   }
523 
524   @Override
onConfigurationSuccess(ITestResult tr)525   public void onConfigurationSuccess(ITestResult tr) {
526     m_test.addPassedConfig(tr);
527   }
528 
529   @Override
onConfigurationFailure(ITestResult tr)530   public void onConfigurationFailure(ITestResult tr) {
531     m_test.addFailedConfig(tr);
532   }
533 
534   @Override
onConfigurationSkip(ITestResult tr)535   public void onConfigurationSkip(ITestResult tr) {
536     m_test.addSkippedConfig(tr);
537   }
538 
539 } // TestListener
540