Skip to content

Commit 7ab56af

Browse files
authored
Merge pull request #2303 from jnicol31/close-2302
Close #2302
2 parents b6a8f3a + c6f1745 commit 7ab56af

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

cxx-sensors/src/main/java/org/sonar/cxx/sensors/tests/xunit/XunitReportParser.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
*/
2020
package org.sonar.cxx.sensors.tests.xunit;
2121

22+
import java.nio.file.Path;
23+
import java.nio.file.Paths;
2224
import java.text.ParseException;
2325
import java.util.Collection;
2426
import java.util.HashMap;
2527
import java.util.Locale;
2628
import java.util.Map;
29+
import java.util.Optional;
30+
2731
import javax.xml.stream.XMLStreamException;
32+
2833
import org.codehaus.staxmate.in.ElementFilter;
2934
import org.codehaus.staxmate.in.SMHierarchicCursor;
3035
import org.codehaus.staxmate.in.SMInputCursor;
@@ -39,7 +44,7 @@
3944
public class XunitReportParser implements XmlStreamHandler {
4045

4146
private final String baseDir;
42-
private final Map<String, TestFile> testFiles = new HashMap<>();
47+
private final Map<Path, TestFile> testFiles = new HashMap<>();
4348

4449
public XunitReportParser(String baseDir) {
4550
this.baseDir = baseDir;
@@ -127,15 +132,15 @@ private void parseTestCaseTag(SMInputCursor testCaseCursor, String tsName, Strin
127132
}
128133

129134
private TestFile getTestFile(String filename) {
130-
String absolute = CxxUtils.resolveAntPath(baseDir, filename);
131-
if (absolute != null) {
132-
absolute = absolute.toLowerCase();
133-
}
134-
var file = testFiles.get(absolute);
135+
var absolute = Optional.ofNullable(CxxUtils.resolveAntPath(baseDir, filename))
136+
.map(p -> Paths.get(p));
137+
138+
var file = testFiles.get(absolute.orElse(null));
135139
if (file == null) {
136-
file = new TestFile(absolute);
137-
testFiles.put(absolute, file);
140+
file = new TestFile(absolute.map(Object::toString).orElse(null));
141+
testFiles.put(absolute.orElse(null), file);
138142
}
143+
139144
return file;
140145
}
141146

cxx-sensors/src/test/java/org/sonar/cxx/sensors/tests/xunit/XunitReportParserTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,17 @@
1919
*/
2020
package org.sonar.cxx.sensors.tests.xunit;
2121

22+
import static org.junit.Assert.assertEquals;
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
2225
import java.io.File;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
2328
import java.util.TreeMap;
24-
import static org.junit.Assert.assertEquals;
29+
import java.util.stream.Collectors;
30+
import java.util.stream.Stream;
31+
32+
import org.assertj.core.util.Strings;
2533
import org.junit.Test;
2634
import org.sonar.cxx.sensors.utils.StaxParser;
2735
import org.sonar.cxx.sensors.utils.TestUtils;
@@ -68,4 +76,27 @@ public void shouldThrowWhenGivenInvalidTime() throws javax.xml.stream.XMLStreamE
6876
parser.parse(report);
6977
}
7078

79+
@Test
80+
public void testFilePaths() throws javax.xml.stream.XMLStreamException {
81+
parserHandler = new XunitReportParser("");
82+
parser = new StaxParser(parserHandler, false);
83+
File report = TestUtils.loadResource(pathPrefix + "xunit-result-SAMPLE-inconsistent-case.xml");
84+
parser.parse(report);
85+
86+
var actualPaths = parserHandler.getTestFiles()
87+
.stream()
88+
.map(TestFile::getFilename)
89+
.filter(p -> !Strings.isNullOrEmpty(p))
90+
.map(s -> Paths.get(s))
91+
.collect(Collectors.toList());
92+
93+
var expectPaths = Stream.of(
94+
Paths.get("/test/file.cpp"),
95+
Paths.get("/test/File.cpp"),
96+
Paths.get("/TEST/file.cpp"))
97+
.distinct()
98+
.toArray(n -> new Path[n]);
99+
100+
assertThat(actualPaths).containsExactlyInAnyOrder(expectPaths);
101+
}
71102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<testsuite errors="0" failures="0" tests="3" name="SAMPLEtest">
3+
<properties>
4+
<property value="4.6.2" name="QTestVersion"/>
5+
<property value="4.6.2" name="QtVersion"/>
6+
</properties>
7+
<testcase result="pass" name="initTestCase" filename="test/file.cpp" />
8+
<testcase result="pass" name="testCase1" filename="test/File.cpp" />
9+
<testcase result="pass" name="testCase2" filename="test/File.cpp" />
10+
<testcase result="pass" name="cleanupTestCase" filename="TEST/file.cpp" />
11+
<system-err/>
12+
</testsuite>

0 commit comments

Comments
 (0)