Skip to content

Commit

Permalink
Adding accumulated builder
Browse files Browse the repository at this point in the history
 * With sorting by file or severity.
 * With filtering of severity.
  • Loading branch information
tomasbjerre committed Feb 28, 2016
1 parent 57f3be8 commit 9b87b1d
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 27 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ Changelog of Git Changelog.
## Unreleased
### No issue

**Adding accumulated builder**

* With sorting by file or severity.
* With filtering of severity.

[3d6ec1ca77e0f45](https://github.com/tomasbjerre/git-changelog-lib/commit/3d6ec1ca77e0f45) Tomas Bjerre *2016-02-28 19:37:34*


## 1.0
### No issue

**Update README.md**


[d2da496ed772fd4](https://github.com/tomasbjerre/git-changelog-lib/commit/d2da496ed772fd4) Tomas Bjerre *2016-02-21 12:27:49*
[2dca003de3f0764](https://github.com/tomasbjerre/git-changelog-lib/commit/2dca003de3f0764) Tomas Bjerre *2016-02-21 13:09:41*

**JSHint and PMD parsers**

Expand Down
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Violations Lib [![Build Status](https://travis-ci.org/tomasbjerre/violations-lib.svg?branch=master)](https://travis-ci.org/tomasbjerre/violations-lib) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/se.bjurr.violations/violations-lib/badge.svg)](https://maven-badges.herokuapp.com/maven-central/se.bjurr.violations/violations-lib)

This is a library for parsing report files from static code analyzis.
This is a library for parsing report files from static code analysis.

It supports:
* [_PMD_](https://pmd.github.io/)
Expand All @@ -9,15 +9,38 @@ It supports:
* [_CSSLint_](https://github.com/CSSLint/csslint)
* [_JSHint_](http://jshint.com/)

Very easy to use with a nice builder patternn
Very easy to use with a nice builder pattern
```
List<Violation> actual = violationsApi() //
List<Violation> violations = violationsReporterApi() //
.withPattern(".*/findbugs/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(FINDBUGS) //
.violations();
```

Or

```
List<Violation> violations = violationsAccumulatedReporterApi() //
.withViolations( //
violationsReporterApi() //
.withPattern(".*/findbugs/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(FINDBUGS) //
.violations() //
) //
.withViolations( //
violationsReporterApi() //
.withPattern(".*/pmd/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(PMD) //
.violations() //
) //
.withAtLeastSeverity(ERROR)//
.orderedBy(FILE)//
.violations();
```

## Developer instructions

To build the code, have a look at `.travis.yml`.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ if (JavaVersion.current().isJava8Compatible()) {
modifyPom {
project {
name 'Violations Lib'
description 'Library for parsing report files from static code analyzis.'
description 'Library for parsing report files from static code analysis.'
url 'https://github.com/tomasbjerre/violations-lib'
inceptionYear '2015'
scm {
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
./gradlew clean build -i
./gradlew clean gitChangelogTask build -i
29 changes: 29 additions & 0 deletions src/main/java/se/bjurr/violations/lib/ORDERED_BY.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package se.bjurr.violations.lib;

import java.util.Comparator;

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

public enum ORDERED_BY {
FILE(new Comparator<Violation>() {
@Override
public int compare(Violation o1, Violation o2) {
return o1.getFile().compareTo(o2.getFile());
}
}), SEVERITY(new Comparator<Violation>() {
@Override
public int compare(Violation o1, Violation o2) {
return new Integer(o1.getSeverity().ordinal())//
.compareTo(new Integer(o2.getSeverity().ordinal()));
}
});
private Comparator<Violation> comparator;

private ORDERED_BY(Comparator<Violation> comparable) {
this.comparator = comparable;
}

public Comparator<Violation> getComparator() {
return comparator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package se.bjurr.violations.lib;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Ordering.from;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;

import java.util.List;

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

import com.google.common.base.Predicate;

public class ViolationsAccumulatedReporterApi {

private final List<Violation> violations = newArrayList();
private SEVERITY atLeastSeverity = INFO;
private ORDERED_BY orderedBy;

private ViolationsAccumulatedReporterApi() {
}

public ViolationsAccumulatedReporterApi withViolationsReporterApiList(List<Violation> violations) {
this.violations.addAll(violations);
return this;
}

public static ViolationsAccumulatedReporterApi violationsAccumulatedReporterApi() {
return new ViolationsAccumulatedReporterApi();
}

public ViolationsAccumulatedReporterApi withAtLeastSeverity(SEVERITY atLeastSeverity) {
this.atLeastSeverity = atLeastSeverity;
return this;
}

public ViolationsAccumulatedReporterApi orderedBy(ORDERED_BY orderedBy) {
this.orderedBy = orderedBy;
return this;
}

public List<Violation> violations() {
return from(orderedBy.getComparator())//
.sortedCopy(//
filter(violations, new Predicate<Violation>() {
@Override
public boolean apply(Violation input) {
return input.getSeverity().ordinal() >= atLeastSeverity.ordinal();
}
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.reports.Reporter;

public class ViolationsApi {
public class ViolationsReporterApi {
private String pattern;
private Reporter reporter;
private File startFile;

private ViolationsApi() {
private ViolationsReporterApi() {
}

public static ViolationsApi violationsApi() {
return new ViolationsApi();
public static ViolationsReporterApi violationsReporterApi() {
return new ViolationsReporterApi();
}

public ViolationsApi withPattern(String regularExpression) {
public ViolationsReporterApi withPattern(String regularExpression) {
this.pattern = regularExpression;
return this;
}

public ViolationsApi findAll(Reporter reporter) {
public ViolationsReporterApi findAll(Reporter reporter) {
this.reporter = reporter;
return this;
}

public ViolationsApi inFolder(String folder) {
public ViolationsReporterApi inFolder(String folder) {
this.startFile = new File(folder);
if (!startFile.exists()) {
throw new RuntimeException(folder + " not found");
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/se/bjurr/violations/lib/AccumulatedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package se.bjurr.violations.lib;

import static com.google.common.collect.Lists.reverse;
import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.ORDERED_BY.FILE;
import static se.bjurr.violations.lib.ORDERED_BY.SEVERITY;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsAccumulatedReporterApi.violationsAccumulatedReporterApi;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
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.reports.Reporter.CHECKSTYLE;
import static se.bjurr.violations.lib.reports.Reporter.JSHINT;

import org.junit.Test;

public class AccumulatedTest {

@Test
public void testThatViolationsCanBeFiltered() {
ViolationsAccumulatedReporterApi violationsAccumulatedReporterApi = getAccumulatedReporterApi();

assertThat(violationsAccumulatedReporterApi//
.withAtLeastSeverity(ERROR)//
.orderedBy(FILE)//
.violations())//
.hasSize(1);

assertThat(violationsAccumulatedReporterApi//
.withAtLeastSeverity(WARN)//
.orderedBy(FILE)//
.violations())//
.hasSize(8);

assertThat(violationsAccumulatedReporterApi//
.withAtLeastSeverity(INFO)//
.orderedBy(FILE)//
.violations())//
.hasSize(10);
}

@Test
public void testThatViolationsCanBeOrdered() {
ViolationsAccumulatedReporterApi violationsAccumulatedReporterApi = getAccumulatedReporterApi();

assertThat(violationsAccumulatedReporterApi//
.withAtLeastSeverity(INFO)//
.orderedBy(FILE)//
.violations()//
.get(0).getFile())//
.isEqualTo("../../../../example-reports/web/js-file.js");

assertThat(reverse(violationsAccumulatedReporterApi//
.withAtLeastSeverity(INFO)//
.orderedBy(FILE)//
.violations())//
.get(0).getFile())//
.isEqualTo("/src/main/java/se/bjurr/violations/lib/example/OtherClass.java");

assertThat(violationsAccumulatedReporterApi//
.withAtLeastSeverity(INFO)//
.orderedBy(SEVERITY)//
.violations()//
.get(0).getSeverity())//
.isEqualTo(INFO);

assertThat(reverse(violationsAccumulatedReporterApi//
.withAtLeastSeverity(INFO)//
.orderedBy(SEVERITY)//
.violations())//
.get(0).getSeverity())//
.isEqualTo(ERROR);

}

private ViolationsAccumulatedReporterApi getAccumulatedReporterApi() {
String rootFolder = getRootFolder();

return violationsAccumulatedReporterApi()//
.withViolationsReporterApiList(//
violationsReporterApi() //
.withPattern(".*/checkstyle/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(CHECKSTYLE) //
.violations()//
) //
.withViolationsReporterApiList(//
violationsReporterApi() //
.withPattern(".*/jshint/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(JSHINT) //
.violations()//
);
}
}
4 changes: 2 additions & 2 deletions src/test/java/se/bjurr/violations/lib/CSSLintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
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.CSSLINT;
Expand All @@ -19,7 +19,7 @@ public class CSSLintTest {
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsApi() //
List<Violation> actual = violationsReporterApi() //
.withPattern(".*/csslint/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(CSSLINT) //
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/se/bjurr/violations/lib/CheckstyleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
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;
Expand All @@ -21,7 +21,7 @@ public class CheckstyleTest {
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsApi() //
List<Violation> actual = violationsReporterApi() //
.withPattern(".*/checkstyle/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(CHECKSTYLE) //
Expand Down
33 changes: 27 additions & 6 deletions src/test/java/se/bjurr/violations/lib/FindbugsTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
package se.bjurr.violations.lib;

import static com.google.common.collect.Iterables.filter;
import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.violations.lib.TestUtils.filterRule;
import static se.bjurr.violations.lib.TestUtils.getRootFolder;
import static se.bjurr.violations.lib.ViolationsApi.violationsApi;
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.parsers.FindbugsParser.FINDBUGS_SPECIFIC_RANK;
import static se.bjurr.violations.lib.reports.Reporter.FINDBUGS;

import java.util.List;

import org.junit.Before;
import org.junit.Test;

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

public class FindbugsTest {

@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();
private List<Violation> actual;

List<Violation> actual = violationsApi() //
.withPattern(".*/findbugs/.*\\.xml$") //
@Before
public void before() {
String rootFolder = getRootFolder();
actual = violationsReporterApi() //
.withPattern(".*/findbugs/main\\.xml$") //
.inFolder(rootFolder) //
.findAll(FINDBUGS) //
.violations();

assertThat(actual)//
.hasSize(13);
}

@Test
public void testThatEqualsUseHashCodeCanBeParsed() {
Iterable<Violation> equalsUseHashCode = filter(actual, filterRule("HE_EQUALS_USE_HASHCODE"));
assertThat(equalsUseHashCode)//
.hasSize(2);
}

@Test
public void testThatNamingConventionCanBeParsed() {
Iterable<Violation> equalsUseHashCode = filter(actual, filterRule("NM_FIELD_NAMING_CONVENTION"));
assertThat(equalsUseHashCode)//
.hasSize(1);
}

@Test
public void testThatViolationsCanBeParsed() {
assertThat(actual.get(0).getFile())//
.isEqualTo("se/bjurr/violations/lib/example/MyClass.java");
assertThat(actual.get(0).getMessage())//
Expand Down
Loading

0 comments on commit 9b87b1d

Please sign in to comment.