/** * 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; }
private Measure getMeasure(String resourceKey, String metricKey) { Resource resource = orchestrator .getServer() .getWsClient() .find(ResourceQuery.createForMetrics(resourceKey, metricKey)); return resource != null ? resource.getMeasure(metricKey) : null; }
private Measure getFileMeasure(String metricKey) { Resource resource = orchestrator .getServer() .getWsClient() .find(ResourceQuery.createForMetrics("project:file.js", metricKey)); return resource == null ? null : resource.getMeasure(metricKey); }
private int getNumberOfViolations(String projectKey) { Resource resource = ORCHESTRATOR .getServer() .getWsClient() .find(ResourceQuery.createForMetrics(projectKey, "violations")); return resource != null ? resource.getMeasure("violations").getValue().intValue() : -1; }
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)); } }
public Measure findMeasure(String artifactId, String measureKey) throws SonarMeasureNotFoundException { Preconditions.checkState( !Strings.isNullOrEmpty(artifactId), "artifactId is a mandatory parameter"); Preconditions.checkNotNull(measureKey, "measureKey is a mandatory parameter"); try { ResourceQuery query = ResourceQuery.createForMetrics(artifactId, measureKey); Resource resource = sonar.find(query); if (resource == null) { throw new SonarMeasureNotFoundException( "Metric " + measureKey + " not found for project " + artifactId + " in Sonar " + sonarUrl); } Measure measure = resource.getMeasure(measureKey); if (measure == null) { throw new SonarMeasureNotFoundException( "Measure [" + measureKey + "] not found for project " + artifactId + " in Sonar " + sonarUrl); } return measure; } catch (ConnectionException e) { throw new SonarMeasureNotFoundException( "Metric " + measureKey + " not found for project " + artifactId + " in Sonar " + sonarUrl, e); } }