예제 #1
0
  @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);
  }
예제 #2
0
  @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);
  }
  /**
   * 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;
  }
예제 #4
0
 private Measure getMeasure(String resourceKey, String metricKey) {
   Resource resource =
       orchestrator
           .getServer()
           .getWsClient()
           .find(ResourceQuery.createForMetrics(resourceKey, metricKey));
   return resource != null ? resource.getMeasure(metricKey) : null;
 }
예제 #5
0
 private Measure getFileMeasure(String metricKey) {
   Resource resource =
       orchestrator
           .getServer()
           .getWsClient()
           .find(ResourceQuery.createForMetrics("project:file.js", metricKey));
   return resource == null ? null : resource.getMeasure(metricKey);
 }
예제 #6
0
 private int getNumberOfViolations(String projectKey) {
   Resource resource =
       ORCHESTRATOR
           .getServer()
           .getWsClient()
           .find(ResourceQuery.createForMetrics(projectKey, "violations"));
   return resource != null ? resource.getMeasure("violations").getValue().intValue() : -1;
 }
  /** SONAR-6787 */
  @Test
  public void ensure_differential_period_4_and_5_defined_at_project_level_is_taken_into_account()
      throws Exception {
    orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);
    setServerProperty(orchestrator, PROJECT_KEY, "sonar.timemachine.period4", "30");
    setServerProperty(orchestrator, PROJECT_KEY, "sonar.timemachine.period5", "previous_analysis");

    // Execute an analysis in the past to have a past snapshot without any issues
    orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "empty");
    orchestrator.executeBuild(
        SonarRunner.create(projectDir("shared/xoo-sample"))
            .setProperty("sonar.projectDate", formatDate(addDays(new Date(), -60))));

    // Second analysis -> issues will be created
    orchestrator
        .getServer()
        .restoreProfile(FileLocation.ofClasspath("/measureHistory/one-issue-per-line-profile.xml"));
    orchestrator
        .getServer()
        .associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line");
    orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample")));

    // New technical debt only comes from new issues
    Resource newTechnicalDebt =
        orchestrator
            .getServer()
            .getWsClient()
            .find(
                ResourceQuery.createForMetrics(
                        "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt")
                    .setIncludeTrends(true));
    List<Measure> measures = newTechnicalDebt.getMeasures();
    assertThat(measures.get(0).getVariation4()).isEqualTo(17);
    assertThat(measures.get(0).getVariation5()).isEqualTo(17);

    // Third analysis, with exactly the same profile -> no new issues so no new technical debt
    orchestrator
        .getServer()
        .associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line");
    orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample")));

    newTechnicalDebt =
        orchestrator
            .getServer()
            .getWsClient()
            .find(
                ResourceQuery.createForMetrics(
                        "sample:src/main/xoo/sample/Sample.xoo", "new_technical_debt")
                    .setIncludeTrends(true));

    // No variation => measure is purged
    assertThat(newTechnicalDebt).isNull();
  }
 private static void verifyDuplicationMeasures(
     String componentKey,
     int duplicatedBlocks,
     int duplicatedLines,
     int duplicatedFiles,
     double duplicatedLinesDensity) {
   Resource file = getComponent(componentKey);
   assertThat(file.getMeasureValue("duplicated_blocks").intValue()).isEqualTo(duplicatedBlocks);
   assertThat(file.getMeasureValue("duplicated_lines").intValue()).isEqualTo(duplicatedLines);
   assertThat(file.getMeasureValue("duplicated_files").intValue()).isEqualTo(duplicatedFiles);
   assertThat(file.getMeasureValue("duplicated_lines_density")).isEqualTo(duplicatedLinesDensity);
 }
예제 #9
0
  @Test
  public void exclude_source_files() {
    scan(
        "sonar.global.exclusions", "**/*Ignore*.xoo",
        "sonar.exclusions", "**/*Exclude*.xoo,src/main/xoo/org/sonar/tests/packageToExclude/**",
        "sonar.test.exclusions", "**/ClassTwoTest.xoo");

    Resource project = projectWithMetrics("ncloc", "files", "directories");

    assertThat(project.getMeasureIntValue("files")).isEqualTo(4);
    assertThat(project.getMeasureIntValue("ncloc")).isEqualTo(60);
    assertThat(project.getMeasureIntValue("directories")).isEqualTo(3);
  }
예제 #10
0
  /** SONAR-1896 */
  @Test
  public void include_test_files() {
    scan("sonar.test.inclusions", "src/test/xoo/**/*One*.xoo,src/test/xoo/**/*Two*.xoo");

    Resource project = projectWithMetrics("tests");
    assertThat(project.getMeasureIntValue("tests")).isEqualTo(2);

    List<Resource> testFiles =
        orchestrator
            .getServer()
            .getWsClient()
            .findAll(new ResourceQuery(PROJECT).setQualifiers("UTS").setDepth(-1));

    assertThat(testFiles).hasSize(2);
    assertThat(testFiles).extracting("name").containsOnly("ClassOneTest.xoo", "ClassTwoTest.xoo");
  }
예제 #11
0
  /** SONAR-1896 */
  @Test
  public void include_source_files() {
    scan(
        "sonar.dynamicAnalysis", "false",
        "sonar.inclusions", "**/*One.xoo,**/*Two.xoo");

    Resource project = projectWithMetrics("files");
    assertThat(project.getMeasureIntValue("files")).isEqualTo(2);

    List<Resource> sourceFiles =
        orchestrator
            .getServer()
            .getWsClient()
            .findAll(new ResourceQuery(PROJECT).setQualifiers("FIL").setDepth(-1));

    assertThat(sourceFiles).hasSize(2);
    assertThat(sourceFiles).extracting("name").containsOnly("ClassOne.xoo", "ClassTwo.xoo");
  }
  /** SONAR-7093 */
  @Test
  public void ensure_leak_period_defined_at_project_level_is_taken_into_account() throws Exception {
    orchestrator.getServer().provisionProject(PROJECT_KEY, PROJECT_KEY);

    // Set a global property and a project property to ensure project property is used
    setServerProperty(orchestrator, "sonar.timemachine.period1", "previous_analysis");
    setServerProperty(orchestrator, PROJECT_KEY, "sonar.timemachine.period1", "30");

    // Execute an analysis in the past to have a past snapshot without any issues
    orchestrator.getServer().associateProjectToQualityProfile(PROJECT_KEY, "xoo", "empty");
    orchestrator.executeBuild(
        SonarRunner.create(projectDir("shared/xoo-sample"))
            .setProperty("sonar.projectDate", formatDate(addDays(new Date(), -15))));

    // Second analysis -> issues will be created
    orchestrator
        .getServer()
        .restoreProfile(FileLocation.ofClasspath("/measureHistory/one-issue-per-line-profile.xml"));
    orchestrator
        .getServer()
        .associateProjectToQualityProfile(PROJECT_KEY, "xoo", "one-issue-per-line");
    orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample")));

    // Third analysis -> There's no new issue from previous analysis
    orchestrator.executeBuild(SonarRunner.create(projectDir("shared/xoo-sample")));

    // Project should have 17 new issues for period 1
    Resource newTechnicalDebt =
        orchestrator
            .getServer()
            .getWsClient()
            .find(ResourceQuery.createForMetrics(PROJECT_KEY, "violations").setIncludeTrends(true));
    List<Measure> measures = newTechnicalDebt.getMeasures();
    assertThat(measures.get(0).getVariation1()).isEqualTo(17);

    // Check on ui that it's possible to define leak period on project
    new SeleneseTest(
            Selenese.builder()
                .setHtmlTestsInClasspath(
                    "define-leak-period-on-project",
                    "/measureHistory/DifferentialPeriodsTest/define-leak-period-on-project.html")
                .build())
        .runOn(orchestrator);
  }
예제 #13
0
  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);
    }
  }
예제 #14
0
  /** SONAR-926 SONAR-5069 */
  @Test
  public void test_sonar_runner_inspection() {
    orchestrator
        .getServer()
        .restoreProfile(
            FileLocation.ofClasspath("/analysis/MultiLanguageTest/one-issue-per-line.xml"));
    orchestrator
        .getServer()
        .restoreProfile(
            FileLocation.ofClasspath("/analysis/MultiLanguageTest/one-issue-per-line-xoo2.xml"));

    orchestrator.getServer().provisionProject("multi-language-sample", "multi-language-sample");

    orchestrator
        .getServer()
        .associateProjectToQualityProfile("multi-language-sample", "xoo", "one-issue-per-line");
    orchestrator
        .getServer()
        .associateProjectToQualityProfile(
            "multi-language-sample", "xoo2", "one-issue-per-line-xoo2");

    SonarRunner build =
        SonarRunner.create().setProjectDir(ItUtils.projectDir("analysis/xoo-multi-languages"));
    BuildResult result = orchestrator.executeBuild(build);

    assertThat(result.getLogs()).contains("2 files indexed");
    assertThat(result.getLogs()).contains("Quality profile for xoo: one-issue-per-line");
    assertThat(result.getLogs()).contains("Quality profile for xoo2: one-issue-per-line-xoo2");

    // modules
    Resource project = getResource("multi-language-sample", "files", "violations");
    assertThat(project.getMeasureIntValue("files")).isEqualTo(2);
    assertThat(project.getMeasureIntValue("violations")).isEqualTo(26);

    Resource xooFile = getResource("multi-language-sample:src/sample/Sample.xoo", "violations");
    assertThat(xooFile.getMeasureIntValue("violations")).isEqualTo(13);

    Resource xoo2File = getResource("multi-language-sample:src/sample/Sample.xoo2", "violations");
    assertThat(xoo2File.getMeasureIntValue("violations")).isEqualTo(13);
  }
예제 #15
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));
   }
 }
  public SonarResource apply(org.sonar.wsclient.services.Resource input) {

    io.gmind7.devops.ldap.sonar.resource.SonarResource output =
        new io.gmind7.devops.ldap.sonar.resource.SonarResource();

    output.setId(String.valueOf(input.getId()));
    output.setKey(StringUtils.defaultIfBlank(input.getKey(), ""));
    output.setName(StringUtils.defaultIfBlank(input.getName(), ""));
    output.setLongName(StringUtils.defaultIfBlank(input.getLongName(), ""));
    output.setScope(StringUtils.defaultIfBlank(input.getScope(), ""));
    output.setQualifier(StringUtils.defaultIfBlank(input.getQualifier(), ""));
    output.setLanguage(StringUtils.defaultIfBlank(input.getLanguage(), ""));
    output.setVersion(StringUtils.defaultIfBlank(input.getVersion(), ""));
    output.setCopy(StringUtils.defaultIfBlank(String.valueOf(input.getCopy()), ""));
    output.setDescription(StringUtils.defaultIfBlank(input.getDescription(), ""));
    output.setDate(input.getDate());

    List<org.sonar.wsclient.services.Measure> sonarMeasures = input.getMeasures();

    if (sonarMeasures != null) {
      List<io.gmind7.devops.ldap.sonar.measure.SonarMeasure> measures = Lists.newArrayList();
      for (org.sonar.wsclient.services.Measure sonarMeasure : sonarMeasures) {
        measures.add(sonarMeasureMapper.apply(input, sonarMeasure));
      }
      output.setMeasures(measures);
    } else {
      output.setMeasures(
          Lists.newArrayList(new io.gmind7.devops.ldap.sonar.measure.SonarMeasure()));
    }

    return output;
  }