Skip to content

Commit

Permalink
Parameterize max width of reporter table #30
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jan 13, 2018
1 parent ea3249a commit 87c5ab7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@

Changelog of Violations lib.

## Unreleased
### GitHub [#30](https://github.com/tomasbjerre/violations-lib/pull/30) Parameterize the max line length for the report table.

**Parameterize max width of reporter table**


[0be6456df10d23b](https://github.com/tomasbjerre/violations-lib/commit/0be6456df10d23b) Tomas Bjerre *2018-01-13 19:03:00*


## 1.50
### GitHub [#28](https://github.com/tomasbjerre/violations-lib/issues/28) Single quotes in xml are printed as '

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip
85 changes: 66 additions & 19 deletions src/main/java/se/bjurr/violations/lib/ViolationsReporterApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
public class ViolationsReporterApi {

private Iterable<Violation> violations;
private int maxReporterColumnWidth;
private int maxRuleColumnWidth = 10;
private int maxSeverityColumnWidth;
private int maxLineColumnWidth;
private int maxMessageColumnWidth = 50;

private ViolationsReporterApi() {}

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

public String getReport(ViolationsReporterDetailLevel level) {
public String getReport(final ViolationsReporterDetailLevel level) {
checkNotNull(violations, "violations");

final StringBuilder sb = new StringBuilder();
Expand All @@ -47,7 +52,7 @@ public String getReport(ViolationsReporterDetailLevel level) {
return sb.toString();
}

private StringBuilder toVerbose(Iterable<Violation> violations) {
private StringBuilder toVerbose(final Iterable<Violation> violations) {
final StringBuilder sb = new StringBuilder();
final Map<String, Set<Violation>> perFile = getViolationsPerFile(violations);
for (final Entry<String, Set<Violation>> perFileEntry : perFile.entrySet()) {
Expand All @@ -60,7 +65,7 @@ private StringBuilder toVerbose(Iterable<Violation> violations) {
return sb;
}

private StringBuilder toPerFile(Iterable<Violation> violations) {
private StringBuilder toPerFile(final Iterable<Violation> violations) {
final StringBuilder sb = new StringBuilder();
final Map<String, Set<Violation>> perFile = getViolationsPerFile(violations);
for (final Entry<String, Set<Violation>> fileEntry : perFile.entrySet()) {
Expand All @@ -72,22 +77,60 @@ private StringBuilder toPerFile(Iterable<Violation> violations) {
return sb;
}

public ViolationsReporterApi withViolations(List<Violation> violations) {
public ViolationsReporterApi withViolations(final List<Violation> violations) {
this.violations = violations;
return this;
}

private StringBuilder toDetailed(Iterable<Violation> violations, String summarySubject) {
/**
* Avoid wider column. Will add new lines if wider. A value of 0 or less will disable the feature.
*/
public ViolationsReporterApi withMaxMessageColumnWidth(final int maxMessageColumnWidth) {
this.maxMessageColumnWidth = maxMessageColumnWidth;
return this;
}

/**
* Avoid wider column. Will add new lines if wider. A value of 0 or less will disable the feature.
*/
public ViolationsReporterApi withMaxLineColumnWidth(final int maxLineColumnWidth) {
this.maxLineColumnWidth = maxLineColumnWidth;
return this;
}

/**
* Avoid wider column. Will add new lines if wider. A value of 0 or less will disable the feature.
*/
public ViolationsReporterApi withMaxReporterColumnWidth(final int maxReporterColumnWidth) {
this.maxReporterColumnWidth = maxReporterColumnWidth;
return this;
}

/**
* Avoid wider column. Will add new lines if wider. A value of 0 or less will disable the feature.
*/
public ViolationsReporterApi withMaxRuleColumnWidth(final int maxRuleColumnWidth) {
this.maxRuleColumnWidth = maxRuleColumnWidth;
return this;
}

/** Avoid wider column. Will add new lines if wider. */
public ViolationsReporterApi withMaxSeverityColumnWidth(final int maxSeverityColumnWidth) {
this.maxSeverityColumnWidth = maxSeverityColumnWidth;
return this;
}

private StringBuilder toDetailed(
final Iterable<Violation> violations, final String summarySubject) {
final StringBuilder sb = new StringBuilder();
final List<String[]> rows = new ArrayList<>();
for (final Violation violation : violations) {
final String[] row = {
violation.getReporter(),
violation.getRule().or(""),
violation.getSeverity().name(),
violation.getStartLine().toString(),
addNewlines(violation.getMessage())
};
final String message = addNewlines(violation.getMessage(), maxMessageColumnWidth);
final String line = addNewlines(violation.getStartLine().toString(), maxLineColumnWidth);
final String severity = addNewlines(violation.getSeverity().name(), maxSeverityColumnWidth);
final String rule = addNewlines(violation.getRule().or(""), maxRuleColumnWidth);
final String reporter = addNewlines(violation.getReporter(), maxReporterColumnWidth);
final String[] row = {reporter, rule, severity, line, message};
rows.add(row);
}

Expand All @@ -101,11 +144,14 @@ private StringBuilder toDetailed(Iterable<Violation> violations, String summaryS
return sb;
}

private String addNewlines(String message) {
private String addNewlines(final String message, final int maxLineLength) {
if (message == null) {
return "";
}
final int maxLineLength = 100;
if (maxLineLength <= 0) {
return message;
}

int noLineCounter = 0;
final StringBuilder withNewlines = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
Expand All @@ -124,7 +170,7 @@ private String addNewlines(String message) {
return withNewlines.toString().trim();
}

private StringBuilder toCompact(Iterable<Violation> violations, String subject) {
private StringBuilder toCompact(final Iterable<Violation> violations, final String subject) {
final StringBuilder sb = new StringBuilder();
final Map<String, Set<Violation>> perReporter = getViolationsPerReporter(violations);
final List<String[]> rows = new ArrayList<>();
Expand Down Expand Up @@ -166,7 +212,7 @@ private StringBuilder toCompact(Iterable<Violation> violations, String subject)
return sb;
}

private Map<SEVERITY, Set<Violation>> getViolationsPerSeverity(Set<Violation> violations) {
private Map<SEVERITY, Set<Violation>> getViolationsPerSeverity(final Set<Violation> violations) {
final Map<SEVERITY, Set<Violation>> violationsPerSeverity = new TreeMap<>();
for (final SEVERITY severity : SEVERITY.values()) {
violationsPerSeverity.put(severity, new TreeSet<Violation>());
Expand All @@ -180,7 +226,7 @@ private Map<SEVERITY, Set<Violation>> getViolationsPerSeverity(Set<Violation> vi
return violationsPerSeverity;
}

private Map<String, Set<Violation>> getViolationsPerFile(Iterable<Violation> violations) {
private Map<String, Set<Violation>> getViolationsPerFile(final Iterable<Violation> violations) {
final Map<String, Set<Violation>> violationsPerFile = new TreeMap<>();
for (final Violation violation : violations) {
final Set<Violation> perReporter = getOrCreate(violationsPerFile, violation.getFile());
Expand All @@ -189,7 +235,8 @@ private Map<String, Set<Violation>> getViolationsPerFile(Iterable<Violation> vio
return violationsPerFile;
}

private Map<String, Set<Violation>> getViolationsPerReporter(Iterable<Violation> violations) {
private Map<String, Set<Violation>> getViolationsPerReporter(
final Iterable<Violation> violations) {
final Map<String, Set<Violation>> violationsPerReporter = new TreeMap<>();
for (final Violation violation : violations) {
final Set<Violation> perReporter =
Expand All @@ -199,7 +246,7 @@ private Map<String, Set<Violation>> getViolationsPerReporter(Iterable<Violation>
return violationsPerReporter;
}

private <T, K> Set<T> getOrCreate(Map<K, Set<T>> container, K key) {
private <T, K> Set<T> getOrCreate(final Map<K, Set<T>> container, final K key) {
if (container.containsKey(key)) {
return container.get(key);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ public void testVerbose() {
LOG.info("\n" + report);
}

@Test
public void testVerboseLimitations() {
final String report =
violationsReporterApi() //
.withViolations(accumulatedViolations) //
.withMaxReporterColumnWidth(20) //
.withMaxRuleColumnWidth(10) //
.withMaxSeverityColumnWidth(20) //
.withMaxLineColumnWidth(10) //
.withMaxMessageColumnWidth(20) //
.getReport(VERBOSE);

LOG.info("\n" + report);
}

@Test
public void testCompactWithZeroViolations() {
final String report =
Expand Down

0 comments on commit 87c5ab7

Please sign in to comment.