*/
package org.sonar.scanner.externalissue.sarif;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.core.sarif.Region;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
+import static org.assertj.core.api.Fail.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-@RunWith(MockitoJUnitRunner.class)
+@RunWith(DataProviderRunner.class)
public class RegionMapperTest {
private static final int LINE_END_OFFSET = 10;
private static final DefaultInputFile INPUT_FILE = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "relative/path", null),
@InjectMocks
private RegionMapper regionMapper;
+ @Before
+ public void setup() {
+ MockitoAnnotations.openMocks(this);
+ }
+
@Test
public void mapRegion_whenNullRegion_returnsEmpty() {
assertThat(regionMapper.mapRegion(null, INPUT_FILE)).isEmpty();
.withMessage("No start line defined for the region.");
}
+ @Test
+ @UseDataProvider("index")
+ public void mapRegion_whenColumnsDefined_convert1BasedIndexTo0BasedIndex(int startColumn, int endColumn, int startOffsetExpected, int endOffsetExpected) {
+ Region fullRegion = mockRegion(startColumn, endColumn, 3, 4);
+
+ TextRange textRange = regionMapper.mapRegion(fullRegion, INPUT_FILE).orElseGet(() -> fail("No TextRange"));
+
+ assertThat(textRange.start().lineOffset()).isEqualTo(startOffsetExpected);
+ assertThat(textRange.end().lineOffset()).isEqualTo(endOffsetExpected);
+ }
+
+ @DataProvider
+ public static Object[][] index() {
+ return new Object[][]{
+ {1,3, 0,2},
+ {5,8, 4,7}
+ };
+ }
+
@Test
public void mapRegion_whenAllCoordinatesDefined() {
Region fullRegion = mockRegion(1, 2, 3, 4);
assertThat(optTextRange).isPresent();
TextRange textRange = optTextRange.get();
assertThat(textRange.start().line()).isEqualTo(fullRegion.getStartLine());
- assertThat(textRange.start().lineOffset()).isEqualTo(fullRegion.getStartColumn());
+ assertThat(textRange.start().lineOffset()).isZero();
assertThat(textRange.end().line()).isEqualTo(fullRegion.getEndLine());
- assertThat(textRange.end().lineOffset()).isEqualTo(fullRegion.getEndColumn());
+ assertThat(textRange.end().lineOffset()).isEqualTo(1);
}
@Test
assertThat(optTextRange).isPresent();
TextRange textRange = optTextRange.get();
assertThat(textRange.start().line()).isEqualTo(fullRegion.getStartLine());
- assertThat(textRange.start().lineOffset()).isEqualTo(1);
+ assertThat(textRange.start().lineOffset()).isZero();
assertThat(textRange.end().line()).isEqualTo(fullRegion.getEndLine());
assertThat(textRange.end().lineOffset()).isEqualTo(LINE_END_OFFSET);
}
assertThat(optTextRange).isPresent();
TextRange textRange = optTextRange.get();
assertThat(textRange.start().line()).isEqualTo(fullRegion.getStartLine());
- assertThat(textRange.start().lineOffset()).isEqualTo(fullRegion.getStartColumn());
+ assertThat(textRange.start().lineOffset()).isEqualTo(7);
assertThat(textRange.end().line()).isEqualTo(fullRegion.getEndLine());
assertThat(textRange.end().lineOffset()).isEqualTo(LINE_END_OFFSET);
}
assertThat(optTextRange).isPresent();
TextRange textRange = optTextRange.get();
assertThat(textRange.start().line()).isEqualTo(fullRegion.getStartLine());
- assertThat(textRange.start().lineOffset()).isEqualTo(1);
+ assertThat(textRange.start().lineOffset()).isZero();
assertThat(textRange.end().line()).isEqualTo(fullRegion.getEndLine());
- assertThat(textRange.end().lineOffset()).isEqualTo(fullRegion.getEndLine());
+ assertThat(textRange.end().lineOffset()).isEqualTo(7);
}
private static Region mockRegion(@Nullable Integer startColumn, @Nullable Integer endColumn, @Nullable Integer startLine, @Nullable Integer endLine) {