1 package org.testng.reporters.jq; 2 3 import org.testng.IResultMap; 4 import org.testng.ISuite; 5 import org.testng.ISuiteResult; 6 import org.testng.ITestContext; 7 import org.testng.ITestResult; 8 import org.testng.collections.ListMultiMap; 9 import org.testng.collections.Lists; 10 import org.testng.collections.Maps; 11 import org.testng.collections.SetMultiMap; 12 import org.testng.internal.Utils; 13 14 import java.util.Arrays; 15 import java.util.Collections; 16 import java.util.List; 17 import java.util.Map; 18 import java.util.Set; 19 20 public class Model { 21 private ListMultiMap<ISuite, ITestResult> m_model = Maps.newListMultiMap(); 22 private List<ISuite> m_suites = null; 23 private Map<String, String> m_testTags = Maps.newHashMap(); 24 private Map<ITestResult, String> m_testResultMap = Maps.newHashMap(); 25 private Map<ISuite, ResultsByClass> m_failedResultsByClass = Maps.newHashMap(); 26 private Map<ISuite, ResultsByClass> m_skippedResultsByClass = Maps.newHashMap(); 27 private Map<ISuite, ResultsByClass> m_passedResultsByClass = Maps.newHashMap(); 28 private List<ITestResult> m_allFailedResults = Lists.newArrayList(); 29 // Each suite is mapped to failed.png, skipped.png or nothing (which means passed.png) 30 private Map<String, String> m_statusBySuiteName = Maps.newHashMap(); 31 private SetMultiMap<String, String> m_groupsBySuiteName = Maps.newSetMultiMap(); 32 private SetMultiMap<String, String> m_methodsByGroup = Maps.newSetMultiMap(); 33 Model(List<ISuite> suites)34 public Model(List<ISuite> suites) { 35 m_suites = suites; 36 init(); 37 } 38 getSuites()39 public List<ISuite> getSuites() { 40 return m_suites; 41 } 42 init()43 private void init() { 44 int testCounter = 0; 45 for (ISuite suite : m_suites) { 46 List<ITestResult> passed = Lists.newArrayList(); 47 List<ITestResult> failed = Lists.newArrayList(); 48 List<ITestResult> skipped = Lists.newArrayList(); 49 for (ISuiteResult sr : suite.getResults().values()) { 50 ITestContext context = sr.getTestContext(); 51 m_testTags.put(context.getName(), "test-" + testCounter++); 52 failed.addAll(context.getFailedTests().getAllResults()); 53 skipped.addAll(context.getSkippedTests().getAllResults()); 54 passed.addAll(context.getPassedTests().getAllResults()); 55 IResultMap[] map = new IResultMap[] { 56 context.getFailedTests(), 57 context.getSkippedTests(), 58 context.getPassedTests() 59 }; 60 for (IResultMap m : map) { 61 for (ITestResult tr : m.getAllResults()) { 62 m_testResultMap.put(tr, getTestResultName(tr)); 63 } 64 } 65 } 66 67 // Process them in the order passed, skipped and failed, so that the failed 68 // icon overrides all the others and the skipped icon overrides passed. 69 70 // Passed 71 { 72 ResultsByClass rbc = new ResultsByClass(); 73 for (ITestResult tr : passed) { 74 rbc.addResult(tr.getTestClass().getRealClass(), tr); 75 updateGroups(suite, tr); 76 } 77 m_passedResultsByClass.put(suite, rbc); 78 } 79 80 // Skipped 81 { 82 ResultsByClass rbc = new ResultsByClass(); 83 for (ITestResult tr : skipped) { 84 m_statusBySuiteName.put(suite.getName(), "skipped"); 85 rbc.addResult(tr.getTestClass().getRealClass(), tr); 86 updateGroups(suite, tr); 87 } 88 m_skippedResultsByClass.put(suite, rbc); 89 } 90 91 // Failed 92 { 93 ResultsByClass rbc = new ResultsByClass(); 94 for (ITestResult tr : failed) { 95 m_statusBySuiteName.put(suite.getName(), "failed"); 96 rbc.addResult(tr.getTestClass().getRealClass(), tr); 97 m_allFailedResults.add(tr); 98 updateGroups(suite, tr); 99 } 100 m_failedResultsByClass.put(suite, rbc); 101 } 102 103 m_model.putAll(suite, failed); 104 m_model.putAll(suite, skipped); 105 m_model.putAll(suite, passed); 106 } 107 } 108 updateGroups(ISuite suite, ITestResult tr)109 private void updateGroups(ISuite suite, ITestResult tr) { 110 String[] groups = tr.getMethod().getGroups(); 111 m_groupsBySuiteName.putAll(suite.getName(), 112 Arrays.asList(groups)); 113 for (String group : groups) { 114 m_methodsByGroup.put(group, tr.getMethod().getMethodName()); 115 } 116 } 117 getFailedResultsByClass(ISuite suite)118 public ResultsByClass getFailedResultsByClass(ISuite suite) { 119 return m_failedResultsByClass.get(suite); 120 } 121 getSkippedResultsByClass(ISuite suite)122 public ResultsByClass getSkippedResultsByClass(ISuite suite) { 123 return m_skippedResultsByClass.get(suite); 124 } 125 getPassedResultsByClass(ISuite suite)126 public ResultsByClass getPassedResultsByClass(ISuite suite) { 127 return m_passedResultsByClass.get(suite); 128 } 129 getTag(ITestResult tr)130 public String getTag(ITestResult tr) { 131 return m_testResultMap.get(tr); 132 } 133 getTestResults(ISuite suite)134 public List<ITestResult> getTestResults(ISuite suite) { 135 return nonnullList(m_model.get(suite)); 136 } 137 getTestResultName(ITestResult tr)138 public static String getTestResultName(ITestResult tr) { 139 StringBuilder result = new StringBuilder(tr.getMethod().getMethodName()); 140 Object[] parameters = tr.getParameters(); 141 if (parameters.length > 0) { 142 result.append("("); 143 StringBuilder p = new StringBuilder(); 144 for (int i = 0; i < parameters.length; i++) { 145 if (i > 0) p.append(", "); 146 p.append(Utils.toString(parameters[i])); 147 } 148 if (p.length() > 100) { 149 String s = p.toString().substring(0, 100); 150 s = s + "..."; 151 result.append(s); 152 } else { 153 result.append(p.toString()); 154 } 155 result.append(")"); 156 } 157 158 return result.toString(); 159 } 160 getAllFailedResults()161 public List<ITestResult> getAllFailedResults() { 162 return m_allFailedResults; 163 } 164 getImage(String tagClass)165 public static String getImage(String tagClass) { 166 return tagClass + ".png"; 167 } 168 getStatusForSuite(String suiteName)169 public String getStatusForSuite(String suiteName) { 170 String result = m_statusBySuiteName.get(suiteName); 171 return result != null ? result : "passed"; 172 } 173 nonnullSet(Set<T> l)174 public <T> Set<T> nonnullSet(Set<T> l) { 175 return l != null ? l : Collections.<T>emptySet(); 176 } 177 nonnullList(List<T> l)178 public <T> List<T> nonnullList(List<T> l) { 179 return l != null ? l : Collections.<T>emptyList(); 180 } 181 getGroups(String name)182 public List<String> getGroups(String name) { 183 List<String> result = Lists.newArrayList(nonnullSet(m_groupsBySuiteName.get(name))); 184 Collections.sort(result); 185 return result; 186 } 187 getMethodsInGroup(String groupName)188 public List<String> getMethodsInGroup(String groupName) { 189 List<String> result = Lists.newArrayList(nonnullSet(m_methodsByGroup.get(groupName))); 190 Collections.sort(result); 191 return result; 192 } 193 getAllTestResults(ISuite suite)194 public List<ITestResult> getAllTestResults(ISuite suite) { 195 return getAllTestResults(suite, true /* tests only */); 196 } 197 getAllTestResults(ISuite suite, boolean testsOnly)198 public List<ITestResult> getAllTestResults(ISuite suite, boolean testsOnly) { 199 List<ITestResult> result = Lists.newArrayList(); 200 for (ISuiteResult sr : suite.getResults().values()) { 201 result.addAll(sr.getTestContext().getPassedTests().getAllResults()); 202 result.addAll(sr.getTestContext().getFailedTests().getAllResults()); 203 result.addAll(sr.getTestContext().getSkippedTests().getAllResults()); 204 if (! testsOnly) { 205 result.addAll(sr.getTestContext().getPassedConfigurations().getAllResults()); 206 result.addAll(sr.getTestContext().getFailedConfigurations().getAllResults()); 207 result.addAll(sr.getTestContext().getSkippedConfigurations().getAllResults()); 208 } 209 } 210 return result; 211 } 212 } 213