it.test.CoverageTest.java Source code

Java tutorial

Introduction

Here is the source code for it.test.CoverageTest.java

Source

/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * 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  02110-1301, USA.
 */
package it.test;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarScanner;
import it.Category2Suite;
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.lang.StringUtils;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import static org.assertj.core.api.Assertions.assertThat;
import static util.ItUtils.getMeasuresAsDoubleByMetricKey;
import static util.ItUtils.projectDir;

public class CoverageTest {

    @ClassRule
    public static Orchestrator orchestrator = Category2Suite.ORCHESTRATOR;

    private static final String[] ALL_COVERAGE_METRICS = new String[] { "line_coverage", "lines_to_cover",
            "uncovered_lines", "branch_coverage", "conditions_to_cover", "uncovered_conditions", "coverage",
            "it_line_coverage", "it_lines_to_cover", "it_uncovered_lines", "it_branch_coverage",
            "it_conditions_to_cover", "it_uncovered_conditions", "it_coverage", "overall_line_coverage",
            "overall_lines_to_cover", "overall_uncovered_lines", "overall_branch_coverage",
            "overall_conditions_to_cover", "overall_uncovered_conditions", "overall_coverage" };

    @Before
    public void delete_data() {
        orchestrator.resetData();
    }

    @Test
    public void coverage() throws Exception {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-ut-coverage")));

        Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-ut-coverage",
                ALL_COVERAGE_METRICS);
        assertThat(measures.get("line_coverage")).isEqualTo(50.0);
        assertThat(measures.get("lines_to_cover")).isEqualTo(4d);
        assertThat(measures.get("uncovered_lines")).isEqualTo(2d);
        assertThat(measures.get("branch_coverage")).isEqualTo(50.0);
        assertThat(measures.get("conditions_to_cover")).isEqualTo(2d);
        assertThat(measures.get("uncovered_conditions")).isEqualTo(1d);
        assertThat(measures.get("coverage")).isEqualTo(50.0);

        assertThat(measures.get("it_coverage")).isNull();

        assertThat(measures.get("overall_coverage")).isNull();

        String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines",
                "key", "sample-ut-coverage:src/main/xoo/sample/Sample.xoo"));
        // Use strict checking to be sure IT coverage is not present
        JSONAssert.assertEquals(IOUtils.toString(
                this.getClass().getResourceAsStream("/test/CoverageTest/unit_test_coverage-expected.json"),
                "UTF-8"), coverage, true);

        verifyComputeEngineTempDirIsEmpty();
    }

    private String cleanupScmAndDuplication(String coverage) {
        coverage = StringUtils.remove(coverage, ",\"scmAuthor\":\"\"");
        coverage = StringUtils.remove(coverage, ",\"scmRevision\":\"\"");
        coverage = StringUtils.remove(coverage, ",\"duplicated\":false");
        return coverage;
    }

    @Test
    public void coverage_no_condition() throws Exception {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-ut-coverage-no-condition")));

        Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-ut-coverage",
                ALL_COVERAGE_METRICS);
        assertThat(measures.get("line_coverage")).isEqualTo(50.0);
        assertThat(measures.get("lines_to_cover")).isEqualTo(4d);
        assertThat(measures.get("uncovered_lines")).isEqualTo(2d);
        assertThat(measures.get("branch_coverage")).isNull();
        assertThat(measures.get("conditions_to_cover")).isNull();
        assertThat(measures.get("uncovered_conditions")).isNull();
        assertThat(measures.get("coverage")).isEqualTo(50.0);

        assertThat(measures.get("it_coverage")).isNull();

        assertThat(measures.get("overall_coverage")).isNull();

        String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines",
                "key", "sample-ut-coverage:src/main/xoo/sample/Sample.xoo"));
        // Use strict checking to be sure IT coverage is not present
        JSONAssert.assertEquals(
                IOUtils.toString(this.getClass().getResourceAsStream(
                        "/test/CoverageTest/unit_test_coverage_no_condition-expected.json"), "UTF-8"),
                coverage, true);

        verifyComputeEngineTempDirIsEmpty();
    }

    @Test
    public void it_coverage_imported_as_coverage() throws Exception {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-it-coverage")));

        Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-it-coverage",
                ALL_COVERAGE_METRICS);

        // Since SQ 6.2 all coverage reports are merged as coverage

        assertThat(measures.get("line_coverage")).isEqualTo(50.0);
        assertThat(measures.get("lines_to_cover")).isEqualTo(4d);
        assertThat(measures.get("uncovered_lines")).isEqualTo(2d);
        assertThat(measures.get("branch_coverage")).isEqualTo(50.0);
        assertThat(measures.get("conditions_to_cover")).isEqualTo(2d);
        assertThat(measures.get("uncovered_conditions")).isEqualTo(1d);
        assertThat(measures.get("coverage")).isEqualTo(50.0);

        assertThat(measures.get("it_coverage")).isNull();

        assertThat(measures.get("overall_coverage")).isNull();

        String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines",
                "key", "sample-it-coverage:src/main/xoo/sample/Sample.xoo"));
        // Use strict checking to be sure UT coverage is not present
        JSONAssert.assertEquals(IOUtils.toString(
                this.getClass().getResourceAsStream("/test/CoverageTest/it_coverage-expected.json"), "UTF-8"),
                coverage, true);

        verifyComputeEngineTempDirIsEmpty();
    }

    @Test
    public void ut_and_it_coverage_merged_in_coverage() throws Exception {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-sample-overall-coverage")));

        // Since SQ 6.2 all coverage reports are merged as coverage

        Map<String, Double> measures = getMeasuresAsDoubleByMetricKey(orchestrator, "sample-overall-coverage",
                ALL_COVERAGE_METRICS);
        assertThat(measures.get("line_coverage")).isEqualTo(75.0);
        assertThat(measures.get("lines_to_cover")).isEqualTo(4);
        assertThat(measures.get("uncovered_lines")).isEqualTo(1);
        assertThat(measures.get("branch_coverage")).isEqualTo(50.0);
        assertThat(measures.get("conditions_to_cover")).isEqualTo(4);
        assertThat(measures.get("uncovered_conditions")).isEqualTo(2);
        assertThat(measures.get("coverage")).isEqualTo(62.5);

        assertThat(measures.get("it_coverage")).isNull();

        assertThat(measures.get("overall_coverage")).isNull();

        String coverage = cleanupScmAndDuplication(orchestrator.getServer().adminWsClient().get("api/sources/lines",
                "key", "sample-overall-coverage:src/main/xoo/sample/Sample.xoo"));
        // Use strict checking to be sure no extra coverage is present
        JSONAssert.assertEquals(IOUtils.toString(
                this.getClass().getResourceAsStream("/test/CoverageTest/ut_and_it_coverage-expected.json"),
                "UTF-8"), coverage, true);

        verifyComputeEngineTempDirIsEmpty();
    }

    /**
     * SONAR-766
     */
    @Test
    public void should_compute_coverage_on_project() {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered")));

        assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS)
                .get("coverage")).isEqualTo(50.0);

        verifyComputeEngineTempDirIsEmpty();
    }

    /**
     * SONAR-766
     */
    @Test
    public void should_ignore_coverage_on_full_path() {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered")).setProperty(
                "sonar.coverage.exclusions", "src/main/xoo/org/sonar/tests/halfcovered/UnCovered.xoo"));

        assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS)
                .get("coverage")).isEqualTo(100.0);

        verifyComputeEngineTempDirIsEmpty();
    }

    /**
     * SONAR-766
     */
    @Test
    public void should_ignore_coverage_on_pattern() {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered"))
                .setProperty("sonar.coverage.exclusions", "**/UnCovered*"));

        assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS)
                .get("coverage")).isEqualTo(100.0);

        verifyComputeEngineTempDirIsEmpty();
    }

    /**
     * SONAR-766
     */
    @Test
    public void should_not_have_coverage_at_all() {
        orchestrator.executeBuilds(SonarScanner.create(projectDir("testing/xoo-half-covered"))
                .setProperty("sonar.coverage.exclusions", "**/*"));

        assertThat(getMeasuresAsDoubleByMetricKey(orchestrator, "xoo-half-covered", ALL_COVERAGE_METRICS)
                .get("coverage")).isNull();

        verifyComputeEngineTempDirIsEmpty();
    }

    private void verifyComputeEngineTempDirIsEmpty() {
        File ceTempDirectory = new File(new File(orchestrator.getServer().getHome(), "temp"), "ce");
        assertThat(FileUtils.listFiles(ceTempDirectory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE))
                .isEmpty();
    }

}