aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/testing/suite/CoverageTest.java
blob: 16c0be3945af847b4d44bbca1d05886707993a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (C) 2009-2014 SonarSource SA
 * All rights reserved
 * mailto:contact AT sonarsource DOT com
 */
package testing.suite;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarRunner;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;

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

public class CoverageTest {

  @ClassRule
  public static Orchestrator orchestrator = TestingTestSuite.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 unit_test_coverage() throws Exception {
    orchestrator.executeBuilds(SonarRunner.create(projectDir("testing/xoo-sample-ut-coverage")));

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-ut-coverage", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("line_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("lines_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("uncovered_lines")).isEqualTo(2);
    assertThat(project.getMeasureValue("branch_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("conditions_to_cover")).isEqualTo(2);
    assertThat(project.getMeasureValue("uncovered_conditions")).isEqualTo(1);
    assertThat(project.getMeasureValue("coverage")).isEqualTo(50.0);

    assertThat(project.getMeasureValue("it_coverage")).isNull();

    assertThat(project.getMeasureValue("overall_coverage")).isNull();

    String coverage = orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-ut-coverage:src/main/xoo/sample/Sample.xoo");
    JSONAssert.assertEquals(IOUtils.toString(this.getClass().getResourceAsStream("CoverageTest/unit_test_coverage-expected.json"), "UTF-8"), coverage, false);
  }

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

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-ut-coverage", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("line_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("lines_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("uncovered_lines")).isEqualTo(2);
    assertThat(project.getMeasureValue("branch_coverage")).isNull();
    assertThat(project.getMeasureValue("conditions_to_cover")).isNull();
    assertThat(project.getMeasureValue("uncovered_conditions")).isNull();
    assertThat(project.getMeasureValue("coverage")).isEqualTo(50.0);

    assertThat(project.getMeasureValue("it_coverage")).isNull();

    assertThat(project.getMeasureValue("overall_coverage")).isNull();

    String coverage = orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-ut-coverage:src/main/xoo/sample/Sample.xoo");
    JSONAssert.assertEquals(IOUtils.toString(this.getClass().getResourceAsStream("CoverageTest/unit_test_coverage_no_condition-expected.json"), "UTF-8"), coverage, false);
  }

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

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-it-coverage", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("coverage")).isNull();

    assertThat(project.getMeasureValue("it_line_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("it_lines_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("it_uncovered_lines")).isEqualTo(2);
    assertThat(project.getMeasureValue("it_branch_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("it_conditions_to_cover")).isEqualTo(2);
    assertThat(project.getMeasureValue("it_uncovered_conditions")).isEqualTo(1);
    assertThat(project.getMeasureValue("it_coverage")).isEqualTo(50.0);

    assertThat(project.getMeasureValue("overall_coverage")).isNull();

    String coverage = orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-it-coverage:src/main/xoo/sample/Sample.xoo");
    JSONAssert.assertEquals(IOUtils.toString(this.getClass().getResourceAsStream("CoverageTest/it_coverage-expected.json"), "UTF-8"), coverage, false);
  }

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

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample-overall-coverage", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("line_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("lines_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("uncovered_lines")).isEqualTo(2);
    assertThat(project.getMeasureValue("branch_coverage")).isEqualTo(25.0);
    assertThat(project.getMeasureValue("conditions_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("uncovered_conditions")).isEqualTo(3);
    assertThat(project.getMeasureValue("coverage")).isEqualTo(37.5);

    assertThat(project.getMeasureValue("it_line_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("it_lines_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("it_uncovered_lines")).isEqualTo(2);
    assertThat(project.getMeasureValue("it_branch_coverage")).isEqualTo(25.0);
    assertThat(project.getMeasureValue("it_conditions_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("it_uncovered_conditions")).isEqualTo(3);
    assertThat(project.getMeasureValue("it_coverage")).isEqualTo(37.5);

    assertThat(project.getMeasureValue("overall_line_coverage")).isEqualTo(75.0);
    assertThat(project.getMeasureValue("overall_lines_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("overall_uncovered_lines")).isEqualTo(1);
    assertThat(project.getMeasureValue("overall_branch_coverage")).isEqualTo(50.0);
    assertThat(project.getMeasureValue("overall_conditions_to_cover")).isEqualTo(4);
    assertThat(project.getMeasureValue("overall_uncovered_conditions")).isEqualTo(2);
    assertThat(project.getMeasureValue("overall_coverage")).isEqualTo(62.5);

    String coverage = orchestrator.getServer().adminWsClient().get("api/sources/lines", "key", "sample-overall-coverage:src/main/xoo/sample/Sample.xoo");
    JSONAssert.assertEquals(IOUtils.toString(this.getClass().getResourceAsStream("CoverageTest/ut_and_it_coverage-expected.json"), "UTF-8"), coverage, false);
  }

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

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("coverage")).isEqualTo(50.0);
  }

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

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("coverage")).isEqualTo(100.0);
  }

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

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("coverage")).isEqualTo(100.0);
  }

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

    Resource project = orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("xoo-half-covered", ALL_COVERAGE_METRICS));
    assertThat(project.getMeasureValue("coverage")).isNull();
  }

}