001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.plugins.surefire.data;
021
022import org.apache.commons.lang.StringEscapeUtils;
023
024public final class UnitTestResult {
025  public static final String STATUS_OK = "ok";
026  public static final String STATUS_ERROR = "error";
027  public static final String STATUS_FAILURE = "failure";
028  public static final String STATUS_SKIPPED = "skipped";
029
030  private String name, status, stackTrace, message;
031  private long durationMilliseconds = 0L;
032
033  public String getName() {
034    return name;
035  }
036
037  public UnitTestResult setName(String name) {
038    this.name = name;
039    return this;
040  }
041
042  public String getStatus() {
043    return status;
044  }
045
046  public UnitTestResult setStatus(String status) {
047    this.status = status;
048    return this;
049  }
050
051  public UnitTestResult setStackTrace(String stackTrace) {
052    this.stackTrace = stackTrace;
053    return this;
054  }
055
056  public String getMessage() {
057    return message;
058  }
059
060  public UnitTestResult setMessage(String message) {
061    this.message = message;
062    return this;
063  }
064
065  public long getDurationMilliseconds() {
066    return durationMilliseconds;
067  }
068
069  public UnitTestResult setDurationMilliseconds(long l) {
070    this.durationMilliseconds = l;
071    return this;
072  }
073
074  public boolean isErrorOrFailure() {
075    return STATUS_ERROR.equals(status) || STATUS_FAILURE.equals(status);
076  }
077
078  public boolean isError() {
079    return STATUS_ERROR.equals(status);
080  }
081
082  public String toXml() {
083    StringBuilder sb = new StringBuilder();
084    return appendXml(sb).toString();
085  }
086  public StringBuilder appendXml(StringBuilder sb) {
087    sb
088        .append("<testcase status=\"")
089        .append(status)
090        .append("\" time=\"")
091        .append(durationMilliseconds)
092        .append("\" name=\"")
093        .append(StringEscapeUtils.escapeXml(name))
094        .append("\"");
095
096    if (isErrorOrFailure()) {
097      sb
098          .append(">")
099          .append(isError() ? "<error message=\"" : "<failure message=\"")
100          .append(StringEscapeUtils.escapeXml(message))
101          .append("\">")
102          .append("<![CDATA[")
103          .append(StringEscapeUtils.escapeXml(stackTrace))
104          .append("]]>")
105          .append(isError() ? "</error>" : "</failure>")
106          .append("</testcase>");
107    } else {
108      sb.append("/>");
109    }
110    return sb;
111  }
112
113}