@Test public void should_exclude_source_files() { assumeTrue(AndroidTestSuite.isAtLeastPlugin1_1()); SonarRunner analysis = SonarRunner.create() .setProfile("it-profile") .setProjectName("SonarAndroidSample") .setProjectKey("SonarAndroidSample") .setProjectVersion("1.0") .setSourceDirs("app/src/main") .setProjectDir(new File("projects/SonarAndroidSample")) .setProperty("skipTests", "true") .setProperty("sonar.global.exclusions", "**/TestViolations.java") .setProperty("sonar.android.lint.report", "lint-results.xml") .setProperty("sonar.import_unknown_files", "true"); orchestrator.executeBuild(analysis); Resource project = sonar.find(ResourceQuery.createForMetrics("SonarAndroidSample", "violations")); int expectedViolations = 0; if (AndroidTestSuite.sonarqube_version_is_after_5_1()) { // After version 5.1 xml files will be indexed thanks to sonar.import_unknown_files parameter // and so issues can be reported on them. expectedViolations = 4; } assertThat(project.getMeasureIntValue("violations")).isEqualTo(expectedViolations); }
@Test @Ignore("Deactivated awaiting resolution of http://jira.sonarsource.com/browse/JC-145") public void should_run_lint_after_export_and_import_results() throws Exception { assumeTrue(AndroidTestSuite.isAtLeastPlugin1_1()); String response = exportProfile("it-profile"); File baseDir = new File("projects/SonarAndroidSample/app"); FileUtils.write(new File(baseDir, "lint.xml"), response, Charsets.UTF_8); ProcessBuilder pb = new ProcessBuilder("CMD", "/C", "gradle lint"); pb.directory(baseDir); pb.inheritIO(); Process gradleProcess = pb.start(); int exitStatus = gradleProcess.waitFor(); if (exitStatus != 0) { fail("Failed to execute gradle lint."); } SonarRunner analysis = SonarRunner.create() .setProfile("it-profile") .setProjectName("SonarAndroidSample2") .setProjectKey("SonarAndroidSample2") .setProjectVersion("1.0") .setSourceDirs("src/main") .setProjectDir(baseDir) .setProperty("sonar.android.lint.report", "lint-report-build.xml") .setProperty("sonar.import_unknown_files", "true"); orchestrator.executeBuild(analysis); Resource project = sonar.find(ResourceQuery.createForMetrics("SonarAndroidSample2", "violations")); assertThat(project.getMeasureIntValue("violations")).isEqualTo(2); }
@VisibleForTesting void verifyMeasures(Sonar client) { ResourceQuery query = ResourceQuery.create(resourceKey); query.setMetrics( (String[]) expectedMeasures.keySet().toArray(new String[expectedMeasures.size()])); Resource resource = client.find(query); if (resource == null) { throw new AssertionError("Resource does not exist: " + resourceKey); } for (Map.Entry<String, String> assertion : expectedMeasures.entrySet()) { verifyMeasure(resource, assertion.getKey(), assertion.getValue()); } }
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); } }