All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.coverage.generic.UnitTestMeasuresBuilder Maven / Gradle / Ivy

Go to download

Imports coverage reports defined in the specified format. Coverage applied to files of any language.

The newest version!
/*
 * SonarQube Generic Coverage Plugin
 * Copyright (C) 2014 SonarSource
 * [email protected]
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.plugins.coverage.generic;

import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.utils.ParsingUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public final class UnitTestMeasuresBuilder {

  private final Map index = new HashMap<>();
  private int test = 0;
  private int failure = 0;
  private int error = 0;
  private int skipped = 0;
  private long duration = 0L;

  private UnitTestMeasuresBuilder() {
    // Use the factory
  }

  public boolean setTestCase(String name, String status, long duration, String message, String stacktrace) {
    if (index.containsKey(name)) {
      return false;
    } else {
      TestCase testCase = new TestCase();
      testCase
        .setName(name)
        .setStatus(status)
        .setDuration(duration)
        .setMessage(message)
        .setStackTrace(stacktrace);
      index.put(name, testCase);

      setCounter(status);
      this.duration += duration;
      return true;
    }
  }

  private void setCounter(String status) {
    if (TestCase.ERROR.equals(status)) {
      error++;
    } else if (TestCase.FAILURE.equals(status)) {
      failure++;
    } else if (TestCase.SKIPPED.equals(status)) {
      skipped++;
    }
    test++;
  }

  public static UnitTestMeasuresBuilder create() {
    return new UnitTestMeasuresBuilder();
  }

  public java.util.Collection createMeasures() {
    Collection measures = new ArrayList<>();
    if (test > 0) {
      measures.add(new Measure(CoreMetrics.SKIPPED_TESTS, (double) skipped));
      measures.add(new Measure(CoreMetrics.TESTS, (double) test));
      measures.add(new Measure(CoreMetrics.TEST_ERRORS, (double) error));
      measures.add(new Measure(CoreMetrics.TEST_FAILURES, (double) failure));
      measures.add(new Measure(CoreMetrics.TEST_EXECUTION_TIME, (double) duration));
      double passedTests = (double) test - error - failure;
      double percentage = passedTests * 100d / test;
      measures.add(new Measure(CoreMetrics.TEST_SUCCESS_DENSITY, ParsingUtils.scaleValue(percentage)));
    }
    return measures;
  }

  public Collection getTestCases() {
    return Collections.unmodifiableCollection(index.values());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy