Skip to content

Commit

Permalink
Adding filtering util
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Mar 17, 2017
1 parent 71746a1 commit 30788ad
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/java/se/bjurr/violations/lib/util/Filtering.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package se.bjurr.violations.lib.util;

import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;

import java.util.ArrayList;
import java.util.List;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public final class Filtering {
private Filtering() {
}
public static List<Violation> withAtLEastSeverity(List<Violation> unfiltered, final SEVERITY severity) {
List<Violation> filtered = new ArrayList<>();
for (Violation candidate : unfiltered) {
if (isSeverer(candidate.getSeverity(),severity)) {
filtered.add(candidate);
}
}
return filtered ;
}

private static boolean isSeverer(SEVERITY candiate,SEVERITY atLeast) {
return candiate == ERROR//
|| atLeast == INFO//
|| candiate == WARN && atLeast == WARN;
}

}
79 changes: 79 additions & 0 deletions src/test/java/se/bjurr/violations/lib/util/FilteringTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package se.bjurr.violations.lib.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.util.Lists.newArrayList;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Reporter.CHECKSTYLE;

import java.util.List;

import org.junit.Test;

import se.bjurr.violations.lib.model.Violation;

public class FilteringTest {

@Test
public void testFilteringINFO() {
List<Violation> unfiltered = newArrayList(//
violationBuilder()//
.setFile("file")//
.setStartLine(2)//
.setMessage("message")//
.setReporter(CHECKSTYLE)//
.setSeverity(INFO)//
.build()//
);

assertThat(Filtering.withAtLEastSeverity(unfiltered, ERROR))//
.hasSize(0);
assertThat(Filtering.withAtLEastSeverity(unfiltered, WARN))//
.hasSize(0);
assertThat(Filtering.withAtLEastSeverity(unfiltered, INFO))//
.hasSize(1);
}

@Test
public void testFilteringWARN() {
List<Violation> unfiltered = newArrayList(//
violationBuilder()//
.setFile("file")//
.setStartLine(2)//
.setMessage("message")//
.setReporter(CHECKSTYLE)//
.setSeverity(WARN)//
.build()//
);

assertThat(Filtering.withAtLEastSeverity(unfiltered, ERROR))//
.hasSize(0);
assertThat(Filtering.withAtLEastSeverity(unfiltered, WARN))//
.hasSize(1);
assertThat(Filtering.withAtLEastSeverity(unfiltered, INFO))//
.hasSize(1);
}

@Test
public void testFilteringERROR() {
List<Violation> unfiltered = newArrayList(//
violationBuilder()//
.setFile("file")//
.setStartLine(2)//
.setMessage("message")//
.setReporter(CHECKSTYLE)//
.setSeverity(ERROR)//
.build()//
);

assertThat(Filtering.withAtLEastSeverity(unfiltered, ERROR))//
.hasSize(1);
assertThat(Filtering.withAtLEastSeverity(unfiltered, WARN))//
.hasSize(1);
assertThat(Filtering.withAtLEastSeverity(unfiltered, INFO))//
.hasSize(1);
}

}

0 comments on commit 30788ad

Please sign in to comment.