|
| 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 | +} |
0 commit comments