Skip to content

Commit 5f27e4c

Browse files
SONARXML-218 Remove queries to non-persisted metrics
1 parent ee19f16 commit 5f27e4c

File tree

5 files changed

+102
-52
lines changed

5 files changed

+102
-52
lines changed

its/plugin/src/test/java/com/sonar/it/xml/XmlTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import org.junit.jupiter.api.BeforeAll;
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.api.extension.RegisterExtension;
25-
import org.sonarqube.ws.Measures.Measure;
2625

27-
import static com.sonar.it.xml.XmlTestSuite.getMeasure;
2826
import static com.sonar.it.xml.XmlTestSuite.getMeasureAsDouble;
2927
import static org.assertj.core.api.Assertions.assertThat;
3028

@@ -51,16 +49,6 @@ void testBaseMetrics() {
5149
assertThat(getProjectMeasureAsDouble("violations")).isEqualTo(13);
5250
}
5351

54-
@Test
55-
void should_be_compatible_with_DevCockpit() {
56-
assertThat(getFileMeasure("ncloc_data").getValue())
57-
.contains(";7=1")
58-
.doesNotContain("8=1")
59-
.doesNotContain("9=1")
60-
.doesNotContain("10=1")
61-
.contains(";11=1");
62-
}
63-
6452
@Test
6553
// SONARXML-19
6654
void should_correctly_count_lines_when_char_before_prolog() {
@@ -86,8 +74,4 @@ private Double getFileMeasureAsDouble(String metricKey) {
8674
return getMeasureAsDouble(FILE_TOKEN_PARSER, metricKey.trim());
8775
}
8876

89-
private Measure getFileMeasure(String metricKey) {
90-
return getMeasure(FILE_TOKEN_PARSER, metricKey.trim());
91-
}
92-
9377
}

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@
169169
<version>3.12.4</version>
170170
<scope>test</scope>
171171
</dependency>
172+
<dependency>
173+
<groupId>com.google.code.gson</groupId>
174+
<artifactId>gson</artifactId>
175+
<version>2.11.0</version>
176+
<scope>test</scope>
177+
</dependency>
172178
</dependencies>
173179
</dependencyManagement>
174180

175-
176181
<profiles>
177182
<profile>
178183
<id>its</id>

sonar-xml-plugin/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
<artifactId>sonar-plugin-api-impl</artifactId>
100100
<scope>test</scope>
101101
</dependency>
102+
<dependency>
103+
<groupId>com.google.code.gson</groupId>
104+
<artifactId>gson</artifactId>
105+
<scope>test</scope>
106+
</dependency>
102107
</dependencies>
103108

104109
<build>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* SonarQube XML Plugin
3+
* Copyright (C) 2010-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.plugins.xml;
18+
19+
import java.util.Map;
20+
import java.util.TreeMap;
21+
import org.sonar.api.batch.fs.InputFile;
22+
import org.sonar.api.measures.FileLinesContext;
23+
import org.sonar.api.measures.FileLinesContextFactory;
24+
25+
public class FileLinesContextTester implements FileLinesContextFactory {
26+
27+
// String inputFileKey -> String metricKey -> Integer line -> Object value
28+
private final Map<String, Map<String, Map<Integer, Object>>> storage = new TreeMap<>();
29+
30+
@Override
31+
public FileLinesContext createFor(InputFile inputFile) {
32+
return new Metrics(inputFile.key());
33+
}
34+
35+
public Map<String, Map<Integer, Object>> metrics(InputFile inputFile) {
36+
return storage.getOrDefault(inputFile.key(), Map.of());
37+
}
38+
39+
private class Metrics implements FileLinesContext {
40+
41+
// String metricKey -> Integer line -> Object value
42+
private final Map<String, Map<Integer, Object>> storageBuffer = new TreeMap<>();
43+
private final String inputFileKey;
44+
45+
public Metrics(String inputFileKey) {
46+
this.inputFileKey = inputFileKey;
47+
}
48+
49+
@Override
50+
public void setIntValue(String metricKey, int line, int value) {
51+
put(metricKey, line, value);
52+
}
53+
54+
@Override
55+
public void setStringValue(String metricKey, int line, String value) {
56+
put(metricKey, line, value);
57+
}
58+
59+
private void put(String metricKey, int line, Object value) {
60+
storageBuffer
61+
.computeIfAbsent(metricKey, k -> new TreeMap<>())
62+
.put(line, value);
63+
}
64+
65+
@Override
66+
public void save() {
67+
storage
68+
.computeIfAbsent(inputFileKey, k -> new TreeMap<>())
69+
.putAll(storageBuffer);
70+
storageBuffer.clear();
71+
}
72+
73+
}
74+
75+
}

sonar-xml-plugin/src/test/java/org/sonar/plugins/xml/LineCounterTest.java

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,77 +16,58 @@
1616
*/
1717
package org.sonar.plugins.xml;
1818

19+
import com.google.gson.Gson;
1920
import java.io.File;
2021
import java.io.IOException;
2122
import java.nio.charset.StandardCharsets;
2223
import java.nio.file.Path;
23-
import java.util.Arrays;
24-
import org.junit.jupiter.api.BeforeEach;
24+
import java.util.Map;
2525
import org.junit.jupiter.api.Test;
2626
import org.sonar.api.batch.fs.InputFile;
2727
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
2828
import org.sonar.api.batch.sensor.internal.SensorContextTester;
2929
import org.sonar.api.measures.CoreMetrics;
30-
import org.sonar.api.measures.FileLinesContext;
31-
import org.sonar.api.measures.FileLinesContextFactory;
3230
import org.sonarsource.analyzer.commons.xml.ParseException;
3331
import org.sonarsource.analyzer.commons.xml.XmlFile;
3432

3533
import static org.assertj.core.api.Assertions.assertThat;
3634
import static org.junit.jupiter.api.Assertions.assertThrows;
37-
import static org.mockito.ArgumentMatchers.any;
38-
import static org.mockito.Mockito.mock;
39-
import static org.mockito.Mockito.verify;
40-
import static org.mockito.Mockito.verifyNoMoreInteractions;
41-
import static org.mockito.Mockito.when;
4235

4336
class LineCounterTest {
4437

45-
private FileLinesContextFactory fileLinesContextFactory;
46-
private FileLinesContext fileLinesContext;
47-
48-
@BeforeEach
49-
void setUp() {
50-
fileLinesContextFactory = mock(FileLinesContextFactory.class);
51-
fileLinesContext = mock(FileLinesContext.class);
52-
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
53-
}
54-
5538
@Test
5639
void test_simple_file() throws IOException {
57-
verifyMetrics("simple.xml", 1,
58-
1, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18);
40+
verifyMetrics("simple.xml", 1, """
41+
{"ncloc_data":{"1":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"12":1,"13":1,"14":1,"15":1,"16":1,"17":1,"18":1}}""");
5942
}
6043

6144
@Test
6245
void test_complex_file() throws IOException {
63-
verifyMetrics("complex.xml", 17,
64-
1, 2, 3, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 25, 26, 27, 34, 35, 36, 37, 38, 39);
46+
verifyMetrics("complex.xml", 17, """
47+
{"ncloc_data":{"1":1,"2":1,"3":1,"11":1,"12":1,"13":1,"14":1,"15":1,"16":1,"17":1,"18":1,"19":1,"20":1,"22":1,"25":1,"26":1,"27":1,"34":1,"35":1,"36":1,"37":1,"38":1,"39":1}}""");
6548
}
6649

6750
@Test
6851
void test_invalid_file() {
69-
assertThrows(ParseException.class, () -> verifyMetrics("invalid.xml", -1));
52+
assertThrows(ParseException.class, () -> verifyMetrics("invalid.xml", -1, ""));
7053
}
7154

7255
@Test // SONARXML-19
7356
void test_file_with_char_before_prolog() throws Exception {
74-
verifyMetrics("char_before_prolog.xml", 1, 3, 6, 7, 8, 9, 10, 11, 12, 13);
57+
verifyMetrics("char_before_prolog.xml", 1, """
58+
{"ncloc_data":{"3":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":1}}""");
7559
}
7660

77-
private void verifyMetrics(String filename, int commentLinesNumber, int... linesOfCode) throws IOException {
61+
private void verifyMetrics(String filename, int commentLinesNumber, String linesMetrics) throws IOException {
7862
File moduleBaseDir = new File("src/test/resources/parsers/linecount");
7963
SensorContextTester context = SensorContextTester.create(moduleBaseDir);
8064
InputFile inputFile = createInputFile(moduleBaseDir.toPath(), filename);
81-
LineCounter.analyse(context, fileLinesContextFactory, XmlFile.create(inputFile));
82-
83-
Arrays.stream(linesOfCode).forEach(line ->
84-
verify(fileLinesContext).setIntValue(CoreMetrics.NCLOC_DATA_KEY, line, 1));
85-
86-
verify(fileLinesContext).save();
87-
verifyNoMoreInteractions(fileLinesContext);
88-
89-
assertThat(context.measure(inputFile.key(), CoreMetrics.NCLOC).value()).isEqualTo(linesOfCode.length);
65+
var fileLinesContext = new FileLinesContextTester();
66+
LineCounter.analyse(context, fileLinesContext, XmlFile.create(inputFile));
67+
Map<String, Map<Integer, Object>> metrics = fileLinesContext.metrics(inputFile);
68+
assertThat(new Gson().toJson(metrics)).isEqualTo(linesMetrics);
69+
int expectedNcloc = metrics.get(CoreMetrics.NCLOC_DATA_KEY).size();
70+
assertThat(context.measure(inputFile.key(), CoreMetrics.NCLOC).value()).isEqualTo(expectedNcloc);
9071
assertThat(context.measure(inputFile.key(), CoreMetrics.COMMENT_LINES).value()).isEqualTo(commentLinesNumber);
9172
}
9273

0 commit comments

Comments
 (0)