001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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     */
020    package org.sonar.plugins.surefire.data;
021    
022    import com.google.common.collect.Lists;
023    
024    import java.util.Collections;
025    import java.util.List;
026    
027    public final class UnitTestClassReport {
028      private long errors = 0L, failures = 0L, skipped = 0L, tests = 0L, durationMilliseconds = 0L;
029      private List<UnitTestResult> results = null;
030    
031      public UnitTestClassReport add(UnitTestClassReport other) {
032        for (UnitTestResult otherResult : other.getResults()) {
033          add(otherResult);
034        }
035        return this;
036      }
037    
038      public UnitTestClassReport add(UnitTestResult result) {
039        initResults();
040        results.add(result);
041        if (result.getStatus().equals(UnitTestResult.STATUS_SKIPPED)) {
042          skipped += 1;
043    
044        } else if (result.getStatus().equals(UnitTestResult.STATUS_FAILURE)) {
045          failures += 1;
046    
047        } else if (result.getStatus().equals(UnitTestResult.STATUS_ERROR)) {
048          errors += 1;
049        }
050        tests += 1;
051        durationMilliseconds += result.getDurationMilliseconds();
052        return this;
053      }
054    
055      private void initResults() {
056        if (results == null) {
057          results = Lists.newArrayList();
058        }
059      }
060    
061      public long getErrors() {
062        return errors;
063      }
064    
065      public long getFailures() {
066        return failures;
067      }
068    
069      public long getSkipped() {
070        return skipped;
071      }
072    
073      public long getTests() {
074        return tests;
075      }
076    
077      public long getDurationMilliseconds() {
078        return durationMilliseconds;
079      }
080    
081      public List<UnitTestResult> getResults() {
082        if (results==null) {
083          return Collections.emptyList();
084        }
085        return results;
086      }
087    
088      public String toXml() {
089        StringBuilder sb = new StringBuilder(256);
090        sb.append("<tests-details>");
091        for (UnitTestResult result : results) {
092          result.appendXml(sb);
093        }
094        sb.append("</tests-details>");
095        return sb.toString();
096      }
097    }