Skip to content

Commit

Permalink
#174 --metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Feb 12, 2018
1 parent 7033094 commit 81f9554
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 38 deletions.
121 changes: 86 additions & 35 deletions src/main/java/org/jpeek/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.cactoos.collection.CollectionOf;
import org.cactoos.io.LengthOf;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.TeeInput;
import org.cactoos.list.ListOf;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.cactoos.scalar.And;
import org.cactoos.scalar.AndInThreads;
import org.cactoos.scalar.IoCheckedScalar;
Expand All @@ -59,6 +60,8 @@
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @checkstyle ClassFanOutComplexityCheck (500 lines)
* @checkstyle ExecutableStatementCountCheck (500 lines)
* @checkstyle NPathComplexityCheck (500 lines)
* @checkstyle MagicNumberCheck (500 lines)
*
* @todo #9:30min TCC metric has impediments (see puzzles in TCC.xml).
* Once they are resolved, cover the metric with autotests and add it
Expand All @@ -80,7 +83,16 @@
* to reports list.
* (details on how to test the metrics are to be negotiated here - #107)
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@SuppressWarnings
(
{
"PMD.AvoidDuplicateLiterals",
"PMD.NPathComplexity",
"PMD.CyclomaticComplexity",
"PMD.StdCyclomaticComplexity",
"PMD.ModifiedCyclomaticComplexity"
}
)
public final class App {

/**
Expand All @@ -104,7 +116,19 @@ public final class App {
* @param target Target dir
*/
public App(final Path source, final Path target) {
this(source, target, new HashMap<>(0));
this(
source, target,
new MapOf<String, Object>(
new MapEntry<>("LCOM", true),
new MapEntry<>("LCOM2", true),
new MapEntry<>("LCOM3", true),
new MapEntry<>("LCOM4", true),
new MapEntry<>("LCOM5", true),
new MapEntry<>("SCOM", true),
new MapEntry<>("NHD", true),
new MapEntry<>("MMAC", true)
)
);
}

/**
Expand Down Expand Up @@ -132,44 +156,71 @@ public void analyze() throws IOException {
final Base base = new DefaultBase(this.input);
final XML skeleton = new Skeleton(base).xml();
final Collection<XSL> layers = new LinkedList<>();
if (this.params.get("include-ctors") == null) {
if (!this.params.containsKey("include-ctors")) {
layers.add(App.xsl("layers/no-ctors.xsl"));
}
if (this.params.get("include-static-methods") == null) {
if (!this.params.containsKey("include-static-methods")) {
layers.add(App.xsl("layers/no-static-methods.xsl"));
}
final XSL chain = new XSLChain(layers);
this.save(skeleton.toString(), "skeleton.xml");
final Iterable<Report> reports = new ListOf<>(
new Report(
chain.transform(skeleton),
"LCOM", this.params, 10.0d, -5.0d
),
new Report(
chain.transform(skeleton),
"MMAC", this.params, 0.5d, 0.25d
),
new Report(
chain.transform(skeleton),
"LCOM5", this.params
),
new Report(
chain.transform(skeleton),
"NHD"
),
new Report(
chain.transform(skeleton),
"LCOM2", this.params
),
new Report(
chain.transform(skeleton),
"LCOM3", this.params
),
new Report(
chain.transform(skeleton),
"SCOM", this.params
)
);
final Collection<Report> reports = new LinkedList<>();
if (this.params.containsKey("LCOM")) {
reports.add(
new Report(
chain.transform(skeleton),
"LCOM", this.params, 10.0d, -5.0d
)
);
}
if (this.params.containsKey("MMAC")) {
reports.add(
new Report(
chain.transform(skeleton),
"MMAC", this.params, 0.5d, 0.25d
)
);
}
if (this.params.containsKey("LCOM5")) {
reports.add(
new Report(
chain.transform(skeleton),
"LCOM5", this.params
)
);
}
if (this.params.containsKey("NHD")) {
reports.add(
new Report(
chain.transform(skeleton),
"NHD"
)
);
}
if (this.params.containsKey("LCOM2")) {
reports.add(
new Report(
chain.transform(skeleton),
"LCOM2", this.params
)
);
}
if (this.params.containsKey("LCOM3")) {
reports.add(
new Report(
chain.transform(skeleton),
"LCOM3", this.params
)
);
}
if (this.params.containsKey("SCOM")) {
reports.add(
new Report(
chain.transform(skeleton),
"SCOM", this.params
)
);
}
new IoCheckedScalar<>(
new AndInThreads(
report -> {
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/org/jpeek/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public final class Main {
)
private boolean statics;

@SuppressWarnings("PMD.ImmutableField")
@Parameter(
names = "--metrics",
description = "Comma-separated list of metrics to include"
)
private String metrics;

@Parameter(
names = "--overwrite",
// @checkstyle LineLength (1 line)
Expand All @@ -87,7 +94,7 @@ public final class Main {
* Ctor.
*/
private Main() {
// intentionally
this.metrics = "LCOM5,NHD,MMAC,SCOM";
}

/**
Expand Down Expand Up @@ -132,6 +139,14 @@ private void run() throws IOException {
if (this.statics) {
params.put("include-static-methods", 1);
}
for (final String metric : this.metrics.split(",")) {
if (!metric.matches("[A-Z]+[0-9]?")) {
throw new IllegalArgumentException(
String.format("Invalid metric name: '%s'", metric)
);
}
params.put(metric, true);
}
new App(this.sources.toPath(), this.target.toPath(), params).analyze();
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jpeek/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void createsXmlReports() throws IOException {
final Path input = Paths.get(".");
Main.main("--sources", input.toString(), "--target", output.toString());
MatcherAssert.assertThat(
Files.exists(output.resolve("LCOM.xml")),
Files.exists(output.resolve("LCOM5.xml")),
Matchers.equalTo(true)
);
}
Expand Down Expand Up @@ -77,7 +77,7 @@ public void createsXmlReportsIfOverwriteAndTargetExists()
"--overwrite"
);
MatcherAssert.assertThat(
Files.exists(target.resolve("LCOM.xml")),
Files.exists(target.resolve("LCOM5.xml")),
Matchers.equalTo(true)
);
}
Expand Down

0 comments on commit 81f9554

Please sign in to comment.