diff --git a/src/main/java/se/bjurr/violations/lib/util/Filtering.java b/src/main/java/se/bjurr/violations/lib/util/Filtering.java new file mode 100644 index 00000000..cff5fe78 --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/util/Filtering.java @@ -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 withAtLEastSeverity(List unfiltered, final SEVERITY severity) { +List 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; + } + +} diff --git a/src/test/java/se/bjurr/violations/lib/util/FilteringTest.java b/src/test/java/se/bjurr/violations/lib/util/FilteringTest.java new file mode 100644 index 00000000..674f508b --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/util/FilteringTest.java @@ -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 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 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 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); + } + +}