Skip to content

Commit 4011d28

Browse files
author
Timothy G. Rundle
committed
LizardReportParser Unit Test exposing issue with space in folder and/or file names
1 parent 2fb3494 commit 4011d28

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* backelite-sonar-swift-plugin - Enables analysis of Swift and Objective-C projects into SonarQube.
3+
* Copyright © 2015 Backelite (${email})
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.backelite.sonarqube.swift;
19+
20+
import com.backelite.sonarqube.swift.complexity.LizardReportParser;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.ArgumentCaptor;
24+
import org.mockito.Captor;
25+
import org.mockito.Mock;
26+
import org.mockito.runners.MockitoJUnitRunner;
27+
import org.sonar.api.batch.fs.*;
28+
import org.sonar.api.batch.measure.Metric;
29+
import org.sonar.api.batch.sensor.SensorContext;
30+
import org.sonar.api.batch.sensor.measure.NewMeasure;
31+
32+
import java.io.File;
33+
import java.util.Arrays;
34+
35+
import static org.junit.Assert.assertEquals;
36+
import static org.mockito.Mockito.when;
37+
38+
@RunWith(MockitoJUnitRunner.class)
39+
public class LizardReportParserTest {
40+
41+
@Mock
42+
SensorContext sensorContext;
43+
44+
@Mock
45+
FileSystem fileSystem;
46+
47+
@Mock
48+
FilePredicates filePredicates;
49+
50+
@Mock
51+
FilePredicate filePredicate;
52+
53+
@Mock
54+
InputFile inputFile;
55+
56+
@Mock
57+
NewMeasure<Integer> newMeasure;
58+
59+
@Captor
60+
ArgumentCaptor<String> hasRelativePathCaptor;
61+
62+
@Captor
63+
ArgumentCaptor<InputComponent> onCaptor;
64+
65+
@Captor
66+
ArgumentCaptor<Metric<Integer>> forMetricCaptor;
67+
68+
@Captor
69+
ArgumentCaptor<Integer> withValueCaptor;
70+
71+
72+
@Test
73+
public void parseSimpleFile() {
74+
75+
LizardReportParser parser = new LizardReportParser(sensorContext);
76+
File xmlFile = new File("src/test/resources/lizard-report.xml");
77+
78+
when(sensorContext.<Integer>newMeasure()).thenReturn(newMeasure);
79+
when(newMeasure.on(onCaptor.capture())).thenReturn(newMeasure);
80+
when(newMeasure.forMetric(forMetricCaptor.capture())).thenReturn(newMeasure);
81+
when(newMeasure.withValue(withValueCaptor.capture())).thenReturn(newMeasure);
82+
83+
when(sensorContext.fileSystem()).thenReturn(fileSystem);
84+
when(fileSystem.predicates()).thenReturn(filePredicates);
85+
when(filePredicates.hasRelativePath(hasRelativePathCaptor.capture())).thenReturn(filePredicate);
86+
when(fileSystem.hasFiles(filePredicate)).thenReturn(true);
87+
when(fileSystem.inputFile(filePredicate)).thenReturn(inputFile);
88+
89+
parser.parseReport(xmlFile);
90+
91+
assertEquals(5, onCaptor.getAllValues().size());
92+
assertEquals(5, forMetricCaptor.getAllValues().size());
93+
94+
assertEquals(Arrays.asList(1, 4, 8, 5, 46), withValueCaptor.getAllValues());
95+
assertEquals(Arrays.asList("./Folder With Space/File With Space.swift"), hasRelativePathCaptor.getAllValues());
96+
}
97+
98+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" ?>
2+
<?xml-stylesheet type="text/xsl" href="https://raw.githubusercontent.com/terryyin/lizard/master/lizard.xsl"?>
3+
<cppncss>
4+
<measure type="Function">
5+
<labels>
6+
<label>Nr.</label>
7+
<label>NCSS</label>
8+
<label>CCN</label>
9+
</labels>
10+
<item name="methodName(...) at ./Folder With Space/File With Space.swift:14">
11+
<value>1</value>
12+
<value>4</value>
13+
<value>1</value>
14+
</item>
15+
<average label="NCSS" value="8"/>
16+
<average label="CCN" value="1"/>
17+
</measure>
18+
<measure type="File">
19+
<labels>
20+
<label>Nr.</label>
21+
<label>NCSS</label>
22+
<label>CCN</label>
23+
<label>Functions</label>
24+
</labels>
25+
<item name="./Folder With Space/File With Space.swift">
26+
<value>1</value>
27+
<value>46</value>
28+
<value>8</value>
29+
<value>5</value>
30+
</item>
31+
<average label="NCSS" value="80"/>
32+
<average label="CCN" value="13"/>
33+
<average label="Functions" value="5"/>
34+
<sum label="NCSS" value="19537"/>
35+
<sum label="CCN" value="3194"/>
36+
<sum label="Functions" value="1420"/>
37+
<average label="NCSS" value="13"/>
38+
<average label="CCN" value="2"/>
39+
</measure>
40+
</cppncss>
41+

0 commit comments

Comments
 (0)