Skip to content

Commit

Permalink
Add a method to get the modified lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Jan 25, 2024
1 parent a2992cc commit a596e6f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +17,6 @@
*/
@SuppressWarnings("PMD.DataClass")
public class FileChanges implements Serializable {

private static final long serialVersionUID = 6135245877389921937L;

private final String fileName;
Expand Down Expand Up @@ -108,6 +108,30 @@ public void addChange(final Change change) {
}
}

/**
* Returns all modified lines in this changed file.
*
* @return the modified line
*/
public Set<Integer> getModifiedLines() {
return changes.values().stream()
.flatMap(Collection::stream)
.map(this::getModifiedLinesForChange)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}

private Set<Integer> getModifiedLinesForChange(final Change filter) {
if (filter.getEditType() == ChangeEditType.INSERT || filter.getEditType() == ChangeEditType.REPLACE) {
var lines = new HashSet<Integer>();
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,32 @@
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}.
*
* @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<ChangeEditType, Set<Change>> 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
Expand All @@ -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<ChangeEditType, Set<Change>> 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<Change> 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());
}
}

0 comments on commit a596e6f

Please sign in to comment.