/**
   * Obtains the list of all classes of a project (or module) with their metrics per class.
   *
   * @param resourceKey the project or module to analyse.
   * @return the list of classes of such project or module with their metrics, -1 if metric value
   *     not found.
   */
  public static ArrayList<ClassInfo> getStatisticsPerClass(
      String host, String resourceKey, String[] metricNames) {
    ArrayList<ClassInfo> list = new ArrayList<ClassInfo>();
    Sonar sonar = Sonar.create(host);
    ResourceQuery query = new ResourceQuery(resourceKey);
    query.setMetrics(metricNames);
    query.setScopes("FIL");
    query.setDepth(-1);
    // **************************************************put above and control!
    for (Resource resource : sonar.findAll(query)) {
      ClassInfo classInfo = new ClassInfo();
      classInfo.setClassName(resource.getName());

      for (String metricName : metricNames) {
        Measure measure = resource.getMeasure(metricName);
        //                System.err.println(measure);
        if (measure != null) {
          classInfo.setMetric(metricName, measure.getValue());
        } else {
          System.err.print(
              "Warning[getClassInfos]: "
                  + metricName
                  + " of "
                  + resource.getName()
                  + " is empty. ");
          classInfo.setMetric(metricName, new Double(-1.0));
        }
      }
      list.add(classInfo);
    }
    System.err.println();
    return list;
  }
  public Double findMeasureValue(String artifactId, String measureKey)
      throws SonarMeasureNotFoundException {
    Preconditions.checkState(
        !Strings.isNullOrEmpty(artifactId), "artifactId is a mandatory parameter");

    Measure measure = findMeasure(artifactId, measureKey);
    return measure.getValue();
  }
  public QualityMeasure findQualityMeasure(String artifactId, String measureKey)
      throws SonarMeasureNotFoundException {
    Preconditions.checkState(
        !Strings.isNullOrEmpty(artifactId), "artifactId is a mandatory parameter");
    Preconditions.checkNotNull(measureKey, "measureKey is a mandatory parameter");

    Measure measure = findMeasure(artifactId, measureKey);
    if (measure != null) {
      Double value = measure.getValue();
      if (value != null) {
        QualityMeasure qualityMeasure = new QualityMeasure();
        qualityMeasure.setKey(measureKey);
        qualityMeasure.setValue(value);
        qualityMeasure.setFormattedValue(measure.getFormattedValue());
        return qualityMeasure;
      }
    }
    throw new SonarMeasureNotFoundException(
        "metric '" + measureKey + "' is not found for project '" + artifactId + "'");
  }
Example #4
0
 private void verifyMeasure(Resource resource, String metricKey, String expectedValue) {
   Measure measure = resource.getMeasure(metricKey);
   if (measure == null) {
     throw new AssertionError(
         String.format(
             "Measure mismatch for '%s' on metric '%s'. Expected '%s' but was null.",
             resourceKey, metricKey, expectedValue));
   }
   Object expected = expectedValue;
   Object got = measure.getData();
   try {
     expected = Double.valueOf(expectedValue);
     got = measure.getValue();
   } catch (NumberFormatException e) {
     // expected is not a numeric value, check below will fail
   }
   if (!expected.equals(got)) {
     throw new AssertionError(
         String.format(
             "Measure mismatch for '%s' on metric '%s'. Expected '%s' but was '%s'.",
             resourceKey, metricKey, expected, got));
   }
 }