From a596e6fcc7f159bbfff5bb81116ef02153bdcf21 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Thu, 25 Jan 2024 09:17:38 +0100 Subject: [PATCH] Add a method to get the modified lines. --- .../plugins/forensics/delta/FileChanges.java | 26 ++++++++- .../forensics/delta/FileChangesTest.java | 55 ++++++++++--------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/forensics/delta/FileChanges.java b/src/main/java/io/jenkins/plugins/forensics/delta/FileChanges.java index 728fe545..ca4b91cd 100644 --- a/src/main/java/io/jenkins/plugins/forensics/delta/FileChanges.java +++ b/src/main/java/io/jenkins/plugins/forensics/delta/FileChanges.java @@ -1,6 +1,7 @@ package io.jenkins.plugins.forensics.delta; import java.io.Serializable; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -16,7 +17,6 @@ */ @SuppressWarnings("PMD.DataClass") public class FileChanges implements Serializable { - private static final long serialVersionUID = 6135245877389921937L; private final String fileName; @@ -108,6 +108,30 @@ public void addChange(final Change change) { } } + /** + * Returns all modified lines in this changed file. + * + * @return the modified line + */ + public Set getModifiedLines() { + return changes.values().stream() + .flatMap(Collection::stream) + .map(this::getModifiedLinesForChange) + .flatMap(Collection::stream) + .collect(Collectors.toSet()); + } + + private Set getModifiedLinesForChange(final Change filter) { + if (filter.getEditType() == ChangeEditType.INSERT || filter.getEditType() == ChangeEditType.REPLACE) { + var lines = new HashSet(); + for (int line = filter.getFromLine(); line <= filter.getToLine(); line++) { + lines.add(line); + } + return lines; + } + return Set.of(); + } + @Override public boolean equals(final Object o) { if (this == o) { diff --git a/src/test/java/io/jenkins/plugins/forensics/delta/FileChangesTest.java b/src/test/java/io/jenkins/plugins/forensics/delta/FileChangesTest.java index 758765e2..7e040276 100644 --- a/src/test/java/io/jenkins/plugins/forensics/delta/FileChangesTest.java +++ b/src/test/java/io/jenkins/plugins/forensics/delta/FileChangesTest.java @@ -5,11 +5,11 @@ import java.util.Set; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import nl.jqno.equalsverifier.EqualsVerifier; import static io.jenkins.plugins.forensics.assertions.Assertions.*; +import static org.mockito.Mockito.*; /** * Tests the class {@link FileChanges}. @@ -17,21 +17,20 @@ * @author Florian Orendi */ class FileChangesTest { - private static final String FILE_NAME = "test"; private static final String OLD_FILE_NAME = "testOld"; private static final String FILE_CONTENT = "test"; private static final FileEditType FILE_EDIT_TYPE = FileEditType.ADD; - private static final Map> FILE_CHANGES = Collections.emptyMap(); @Test - void testFileChangesGetter() { + void shouldCreateEmptyChanges() { FileChanges fileChanges = createFileChanges(); - assertThat(fileChanges.getFileName()).isEqualTo(FILE_NAME); - assertThat(fileChanges.getOldFileName()).isEqualTo(OLD_FILE_NAME); - assertThat(fileChanges.getFileContent()).isEqualTo(FILE_CONTENT); - assertThat(fileChanges.getFileEditType()).isEqualTo(FILE_EDIT_TYPE); - assertThat(fileChanges.getChanges()).isEqualTo(FILE_CHANGES); + assertThat(fileChanges).hasFileName(FILE_NAME) + .hasOldFileName(OLD_FILE_NAME) + .hasFileContent(FILE_CONTENT) + .hasFileEditType(FILE_EDIT_TYPE) + .hasNoModifiedLines() + .hasChanges(Map.of()); } @Test @@ -48,27 +47,29 @@ void shouldAddChange() { assertThat(fileChanges.getChanges()).isEmpty(); assertThat(fileChanges.getChangesByType(changeEditType)).isEmpty(); - Change change = Mockito.mock(Change.class); - Mockito.when(change.getEditType()).thenReturn(changeEditType); - - fileChanges.addChange(change); - fileChanges.addChange(change); - - Map> changesMap = fileChanges.getChanges(); - assertThat(changesMap.size()).isEqualTo(1); - assertThat(changesMap).containsKey(changeEditType); + var first = createChange(changeEditType, 10, 14); + fileChanges.addChange(first); + var second = createChange(changeEditType, 100, 100); + fileChanges.addChange(second); + var unrelated = createChange(ChangeEditType.DELETE, 1000, 2000); + fileChanges.addChange(unrelated); + + assertThat(fileChanges).hasChanges( + Map.of(changeEditType, Set.of(first, second), + ChangeEditType.DELETE, Set.of(unrelated))); + assertThat(fileChanges.getChangesByType(changeEditType)).containsExactly(first, second); + assertThat(fileChanges).hasModifiedLines(10, 11, 12, 13, 14, 100); + } - Set changes = fileChanges.getChangesByType(changeEditType); - assertThat(changes.size()).isEqualTo(1); - assertThat(changes).contains(change); + private Change createChange(final ChangeEditType changeEditType, final int start, final int end) { + Change change = mock(Change.class); + when(change.getEditType()).thenReturn(changeEditType); + when(change.getFromLine()).thenReturn(start); + when(change.getToLine()).thenReturn(end); + return change; } - /** - * Factory method which creates an instance of {@link FileChanges}. - * - * @return the created instance - */ private FileChanges createFileChanges() { - return new FileChanges(FILE_NAME, OLD_FILE_NAME, FILE_CONTENT, FILE_EDIT_TYPE, FILE_CHANGES); + return new FileChanges(FILE_NAME, OLD_FILE_NAME, FILE_CONTENT, FILE_EDIT_TYPE, Collections.emptyMap()); } }