From 4011d28e679ae581a14c357376ae81f8960cebfa Mon Sep 17 00:00:00 2001 From: "Timothy G. Rundle" Date: Tue, 30 Oct 2018 20:52:56 -0400 Subject: [PATCH] LizardReportParser Unit Test exposing issue with space in folder and/or file names --- .../swift/LizardReportParserTest.java | 98 +++++++++++++++++++ .../src/test/resources/lizard-report.xml | 41 ++++++++ 2 files changed, 139 insertions(+) create mode 100644 sonar-swift-plugin/src/test/java/com/backelite/sonarqube/swift/LizardReportParserTest.java create mode 100644 sonar-swift-plugin/src/test/resources/lizard-report.xml diff --git a/sonar-swift-plugin/src/test/java/com/backelite/sonarqube/swift/LizardReportParserTest.java b/sonar-swift-plugin/src/test/java/com/backelite/sonarqube/swift/LizardReportParserTest.java new file mode 100644 index 00000000..5b6e4531 --- /dev/null +++ b/sonar-swift-plugin/src/test/java/com/backelite/sonarqube/swift/LizardReportParserTest.java @@ -0,0 +1,98 @@ +/** + * backelite-sonar-swift-plugin - Enables analysis of Swift and Objective-C projects into SonarQube. + * Copyright © 2015 Backelite (${email}) + * + * 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, see . + */ +package com.backelite.sonarqube.swift; + +import com.backelite.sonarqube.swift.complexity.LizardReportParser; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.sonar.api.batch.fs.*; +import org.sonar.api.batch.measure.Metric; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.measure.NewMeasure; + +import java.io.File; +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class LizardReportParserTest { + + @Mock + SensorContext sensorContext; + + @Mock + FileSystem fileSystem; + + @Mock + FilePredicates filePredicates; + + @Mock + FilePredicate filePredicate; + + @Mock + InputFile inputFile; + + @Mock + NewMeasure newMeasure; + + @Captor + ArgumentCaptor hasRelativePathCaptor; + + @Captor + ArgumentCaptor onCaptor; + + @Captor + ArgumentCaptor> forMetricCaptor; + + @Captor + ArgumentCaptor withValueCaptor; + + + @Test + public void parseSimpleFile() { + + LizardReportParser parser = new LizardReportParser(sensorContext); + File xmlFile = new File("src/test/resources/lizard-report.xml"); + + when(sensorContext.newMeasure()).thenReturn(newMeasure); + when(newMeasure.on(onCaptor.capture())).thenReturn(newMeasure); + when(newMeasure.forMetric(forMetricCaptor.capture())).thenReturn(newMeasure); + when(newMeasure.withValue(withValueCaptor.capture())).thenReturn(newMeasure); + + when(sensorContext.fileSystem()).thenReturn(fileSystem); + when(fileSystem.predicates()).thenReturn(filePredicates); + when(filePredicates.hasRelativePath(hasRelativePathCaptor.capture())).thenReturn(filePredicate); + when(fileSystem.hasFiles(filePredicate)).thenReturn(true); + when(fileSystem.inputFile(filePredicate)).thenReturn(inputFile); + + parser.parseReport(xmlFile); + + assertEquals(5, onCaptor.getAllValues().size()); + assertEquals(5, forMetricCaptor.getAllValues().size()); + + assertEquals(Arrays.asList(1, 4, 8, 5, 46), withValueCaptor.getAllValues()); + assertEquals(Arrays.asList("./Folder With Space/File With Space.swift"), hasRelativePathCaptor.getAllValues()); + } + +} diff --git a/sonar-swift-plugin/src/test/resources/lizard-report.xml b/sonar-swift-plugin/src/test/resources/lizard-report.xml new file mode 100644 index 00000000..d8b444de --- /dev/null +++ b/sonar-swift-plugin/src/test/resources/lizard-report.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + 1 + 4 + 1 + + + + + + + + + + + + + 1 + 46 + 8 + 5 + + + + + + + + + + + +