...37 * 38 * THIS IS AN EXACT COPY OF THE CLASS IN org.testng.reporters39 * EXCEPT FOR THIS COMMENT AND log(String) CHANGED TO BE PROTECTED40 */41public class VerboseReporter extends TestListenerAdapter {42 /**43 * Default prefix for messages printed out by this reporter44 *45 */46 public static final String LISTENER_PREFIX = "[VerboseTestNG] ";47 private String suiteName;48 private final String prefix;49 private enum Status {50 SUCCESS(ITestResult.SUCCESS), FAILURE(ITestResult.FAILURE), SKIP(ITestResult.SKIP),51 SUCCESS_PERCENTAGE_FAILURE(ITestResult.SUCCESS_PERCENTAGE_FAILURE), STARTED(ITestResult.STARTED);52 private final int code;53 private Status(int i) {54 code = i;55 }56 57 @SuppressWarnings("unused")58 public int getCode() {59 return code;60 }61 }62 /**63 * Default constructor64 */65 public VerboseReporter() {66 this(LISTENER_PREFIX);67 }68 /**69 * Create VerboseReporter with custom prefix70 *71 * @param prefix prefix for messages printed out by this reporter72 */73 public VerboseReporter(String prefix) {74 this.prefix = prefix;75 }76 @Override77 public void beforeConfiguration(ITestResult tr) {78 super.beforeConfiguration(tr);79 logTestResult(Status.STARTED, tr, true);80 }81 @Override82 public void onConfigurationFailure(ITestResult tr) {83 super.onConfigurationFailure(tr);84 logTestResult(Status.FAILURE, tr, true);85 }86 @Override87 public void onConfigurationSkip(ITestResult tr) {88 super.onConfigurationSkip(tr);89 logTestResult(Status.SKIP, tr, true);90 }91 @Override92 public void onConfigurationSuccess(ITestResult tr) {93 super.onConfigurationSuccess(tr);94 logTestResult(Status.SUCCESS, tr, true);95 }96 @Override97 public void onTestStart(ITestResult tr) {98 logTestResult(Status.STARTED, tr, false);99 }100 @Override101 public void onTestFailure(ITestResult tr) {102 super.onTestFailure(tr);103 logTestResult(Status.FAILURE, tr, false);104 }105 @Override106 public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {107 super.onTestFailedButWithinSuccessPercentage(tr);108 logTestResult(Status.SUCCESS_PERCENTAGE_FAILURE, tr, false);109 }110 @Override111 public void onTestSkipped(ITestResult tr) {112 super.onTestSkipped(tr);113 logTestResult(Status.SKIP, tr, false);114 }115 @Override116 public void onTestSuccess(ITestResult tr) {117 super.onTestSuccess(tr);118 logTestResult(Status.SUCCESS, tr, false);119 }120 @Override121 public void onStart(ITestContext ctx) {122 suiteName = ctx.getName();//ctx.getSuite().getXmlSuite().getFileName();123 log("RUNNING: Suite: \"" + suiteName + "\" containing \"" + ctx.getAllTestMethods().length + "\" Tests (config: " + ctx.getSuite().getXmlSuite().getFileName() + ")");124 }125 @Override126 public void onFinish(ITestContext context) {127 logResults();128 suiteName = null;129 }130 private ITestNGMethod[] resultsToMethods(List<ITestResult> results) {131 ITestNGMethod[] result = new ITestNGMethod[results.size()];132 int i = 0;133 for (ITestResult tr : results) {134 result[i++] = tr.getMethod();135 }136 return result;137 }138 /**139 * Print out test summary140 */141 private void logResults() {142 //143 // Log test summary144 //145 ITestNGMethod[] ft = resultsToMethods(getFailedTests());146 StringBuilder sb = new StringBuilder("\n===============================================\n");147 sb.append(" ").append(suiteName).append("\n");148 sb.append(" Tests run: ").append(getAllTestMethods().length);149 sb.append(", Failures: ").append(ft.length);150 sb.append(", Skips: ").append(resultsToMethods(getSkippedTests()).length);151 int confFailures = getConfigurationFailures().size();152 int confSkips = getConfigurationSkips().size();153 if (confFailures > 0 || confSkips > 0) {154 sb.append("\n").append(" Configuration Failures: ").append(confFailures);155 sb.append(", Skips: ").append(confSkips);156 }157 sb.append("\n===============================================");158 log(sb.toString());159 }160 /**161 * Log meaningful message for passed in arguments.162 * Message itself is of form:163 * $status: "$suiteName" - $methodDeclaration ($actualArguments) finished in $x ms ($run of $totalRuns)164 *165 * @param st status of passed in itr166 * @param itr test result to be described167 * @param isConfMethod is itr describing configuration method168 */169 private void logTestResult(Status st, ITestResult itr, boolean isConfMethod) {170 StringBuilder sb = new StringBuilder();171 String stackTrace = "";172 switch (st) {173 case STARTED:174 sb.append("INVOKING");175 break;176 case SKIP:177 sb.append("SKIPPED");178 stackTrace = itr.getThrowable() != null179 ? Utils.shortStackTrace(itr.getThrowable(), false) : "";180 break;181 case FAILURE:182 sb.append("FAILED");183 stackTrace = itr.getThrowable() != null184 ? Utils.shortStackTrace(itr.getThrowable(), false) : "";185 break;186 case SUCCESS:187 sb.append("PASSED");188 break;189 case SUCCESS_PERCENTAGE_FAILURE:190 sb.append("PASSED with failures");191 break;192 default:193 //not happen194 throw new RuntimeException("Unsupported test status:" + itr.getStatus());195 }196 if (isConfMethod) {197 sb.append(" CONFIGURATION: ");198 } else {199 sb.append(": ");200 }201 ITestNGMethod tm = itr.getMethod();202 int identLevel = sb.length();203 sb.append(getMethodDeclaration(tm));204 Object[] params = itr.getParameters();205 Class<?>[] paramTypes = tm.getConstructorOrMethod().getParameterTypes();206 if (null != params && params.length > 0) {207 // The error might be a data provider parameter mismatch, so make208 // a special case here209 if (params.length != paramTypes.length) {210 sb.append("Wrong number of arguments were passed by the Data Provider: found ");211 sb.append(params.length);212 sb.append(" but expected ");213 sb.append(paramTypes.length);214 } else {215 sb.append("(value(s): ");216 for (int i = 0; i < params.length; i++) {217 if (i > 0) {218 sb.append(", ");219 }220 sb.append(Utils.toString(params[i], paramTypes[i]));221 }222 sb.append(")");223 }224 }225 if (Status.STARTED != st) {226 sb.append(" finished in ");227 sb.append(itr.getEndMillis() - itr.getStartMillis());228 sb.append(" ms");229 if (!Utils.isStringEmpty(tm.getDescription())) {230 sb.append("\n");231 for (int i = 0; i < identLevel; i++) {232 sb.append(" ");233 }234 sb.append(tm.getDescription());235 }236 if (tm.getInvocationCount() > 1) {237 sb.append(" (");238 sb.append(tm.getCurrentInvocationCount());239 sb.append(" of ");240 sb.append(tm.getInvocationCount());241 sb.append(")");242 }243 if (!Utils.isStringEmpty(stackTrace)) {244 sb.append("\n").append(stackTrace.substring(0, stackTrace.lastIndexOf(System.getProperty("line.separator"))));245 }246 } else {247 if (!isConfMethod && tm.getInvocationCount() > 1) {248 sb.append(" success: ");249 sb.append(tm.getSuccessPercentage());250 sb.append("%");251 }252 }253 log(sb.toString());254 }255 protected void log(String message) {256 //prefix all output lines257 System.out.println(message.replaceAll("(?m)^", prefix));258 }259 /**260 *261 * @param method method to be described262 * @return FQN of a class + method declaration for a method passed in263 * ie. test.triangle.CheckCount.testCheckCount(java.lang.String)264 */265 private String getMethodDeclaration(ITestNGMethod method) {266 //see Utils.detailedMethodName267 //perhaps should rather adopt the original method instead268 ConstructorOrMethod m = method.getConstructorOrMethod();269 StringBuilder buf = new StringBuilder();270 buf.append("\"");271 if (suiteName != null) {272 buf.append(suiteName);273 } else {274 buf.append("UNKNOWN");275 }276 buf.append("\"");277 buf.append(" - ");278 if (method.isBeforeSuiteConfiguration()) {279 buf.append("@BeforeSuite ");280 } else if (method.isBeforeTestConfiguration()) {281 buf.append("@BeforeTest ");282 } else if (method.isBeforeClassConfiguration()) {283 buf.append("@BeforeClass ");284 } else if (method.isBeforeGroupsConfiguration()) {285 buf.append("@BeforeGroups ");286 } else if (method.isBeforeMethodConfiguration()) {287 buf.append("@BeforeMethod ");288 } else if (method.isAfterMethodConfiguration()) {289 buf.append("@AfterMethod ");290 } else if (method.isAfterGroupsConfiguration()) {291 buf.append("@AfterGroups ");292 } else if (method.isAfterClassConfiguration()) {293 buf.append("@AfterClass ");294 } else if (method.isAfterTestConfiguration()) {295 buf.append("@AfterTest ");296 } else if (method.isAfterSuiteConfiguration()) {297 buf.append("@AfterSuite ");298 }299 buf.append(m.getDeclaringClass().getName());300 buf.append(".");301 buf.append(m.getName());302 buf.append("(");303 int i = 0;304 for (Class<?> p : m.getParameterTypes()) {305 if (i++ > 0) {306 buf.append(", ");307 }308 buf.append(p.getName());309 }310 buf.append(")");311 return buf.toString();312 }313 @Override314 public String toString() {315 return "VerboseReporter{" + "suiteName=" + suiteName + '}';316 }317}...