Skip to content

Commit

Permalink
cqfn#481 - Pass metric name via constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
vzurauskas committed Aug 1, 2020
1 parent 847ef28 commit 01b38c5
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 65 deletions.
32 changes: 15 additions & 17 deletions src/main/java/org/jpeek/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.cactoos.scalar.AndInThreads;
import org.cactoos.scalar.IoChecked;
import org.cactoos.scalar.LengthOf;
import org.jpeek.calculus.Calculus;
import org.jpeek.calculus.xsl.XslCalculus;
import org.jpeek.skeleton.Skeleton;
import org.xembly.Directives;
Expand Down Expand Up @@ -171,123 +170,122 @@ public void analyze() throws IOException {
final XSL chain = new XSLChain(layers);
this.save(skeleton.toString(), "skeleton.xml");
final Collection<Report> reports = new LinkedList<>();
final Calculus xsl = new XslCalculus();
if (this.params.containsKey("LCOM")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("LCOM"),
new ReportData("LCOM", this.params, 10.0d, -5.0d)
)
);
}
if (this.params.containsKey("CAMC")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("CAMC"),
new ReportData("CAMC", this.params)
)
);
}
if (this.params.containsKey("MMAC")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("MMAC"),
new ReportData("MMAC", this.params, 0.5d, 0.1d)
)
);
}
if (this.params.containsKey("LCOM5")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("LCOM5"),
new ReportData("LCOM5", this.params, 0.5d, -0.1d)
)
);
}
if (this.params.containsKey("LCOM4")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("LCOM4"),
new ReportData("LCOM4", this.params, 0.5d, -0.1d)
)
);
}
if (this.params.containsKey("NHD")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("NHD"),
new ReportData("NHD")
)
);
}
if (this.params.containsKey("LCOM2")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("LCOM2"),
new ReportData("LCOM2", this.params)
)
);
}
if (this.params.containsKey("LCOM3")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("LCOM3"),
new ReportData("LCOM3", this.params)
)
);
}
if (this.params.containsKey("SCOM")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("SCOM"),
new ReportData("SCOM", this.params)
)
);
}
if (this.params.containsKey("OCC")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("OCC"),
new ReportData("OCC", this.params)
)
);
}
if (this.params.containsKey("PCC")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("PCC"),
new ReportData("PCC")
)
);
}
if (this.params.containsKey("TCC")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("TCC"),
new ReportData("TCC")
)
);
}
if (this.params.containsKey("LCC")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("LCC"),
new ReportData("LCC")
)
);
}
if (this.params.containsKey("CCM")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("CCM"),
new ReportData("CCM")
)
);
}
if (this.params.containsKey("MWE")) {
reports.add(
new XslReport(
chain.transform(skeleton), xsl,
chain.transform(skeleton), new XslCalculus("MWE"),
new ReportData("MWE")
)
);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/jpeek/XslReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ final class XslReport implements Report {
* @param target Target dir
* @throws IOException If fails
*/
@Override
@SuppressWarnings("PMD.GuardLogStatement")
public void save(final Path target) throws IOException {
final long start = System.currentTimeMillis();
Expand Down Expand Up @@ -192,9 +193,7 @@ private XML xml() throws IOException {
XslReport.SCHEMA_FILE
)
).applyQuietly(
this.calculus.node(
this.metric, this.params, this.skeleton
).node()
this.calculus.node(this.params, this.skeleton).node()
)
);
}
Expand Down
16 changes: 1 addition & 15 deletions src/main/java/org/jpeek/calculus/Calculus.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,16 @@
* only we should also remove LCOM4.xsl part that is doing the calculus and let the xsl here just
* to build the structure of the xml result. This part is especially the one calculating
* "xsl:variable name='E'" L73->L89, and the one doing the division L97 -> L99
* @todo #449:30min The `node` method in this interface was designed with only
* XSL implementation in mind - it uses the `metric` parameter to select the
* XSL file and uses that file to transform the `skeleton`. This makes Java
* based implementations a little awkward because the `metric` parameter
* becomes redundant: there is a Java implementation for each metric, and these
* implementations already know which metric they are for. The question becomes
* - how to select correct java implementation for a given metric and integrate
* it seamlessly with XSL calculus in `XslReport`. One option could be removing
* the `metric` parameter from the method and injecting a Calculus for a
* concrete metric in `XslReport` directly. Another could be implementing
* Chain Of Responsibility pattern. Decide on the best way to integrate Java
* based Calculus with XSL based Calculus in `XslReport` and implement it.
*/
public interface Calculus {

/**
* Produces {@link XML} representing metrics values.
* @param metric Desired metric to calculate
* @param params Params
* @param skeleton Package input
* @return XML document giving metrics values for classes
* @throws IOException If fails
*/
XML node(String metric, Map<String, Object> params, XML skeleton)
throws IOException;
XML node(Map<String, Object> params, XML skeleton) throws IOException;

}
14 changes: 1 addition & 13 deletions src/main/java/org/jpeek/calculus/java/Ccm.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Map;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.UncheckedInput;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.jpeek.calculus.Calculus;

Expand All @@ -40,18 +39,7 @@
public final class Ccm implements Calculus {

@Override
public XML node(
final String metric,
final Map<String, Object> params,
final XML skeleton
) {
if (!"ccm".equalsIgnoreCase(metric)) {
throw new IllegalArgumentException(
new FormattedText(
"This metric is CCM, not %s.", metric
).toString()
);
}
public XML node(final Map<String, Object> params, final XML skeleton) {
return Ccm.withFixedNcc(
new XSLDocument(
new UncheckedInput(
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jpeek/calculus/java/Lcom4.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
public final class Lcom4 implements Calculus {

@Override
public XML node(final String metric, final Map<String, Object> params,
final XML skeleton) throws IOException {
public XML node(
final Map<String, Object> params, final XML skeleton
) throws IOException {
final XML result = new XSLDocument(
new TextOf(
new ResourceOf("org/jpeek/metrics/LCOM4.xsl")
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/org/jpeek/calculus/xsl/XslCalculus.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,27 @@
*/
public final class XslCalculus implements Calculus {

/**
* Name of the metric this Calculus is for.
*/
private final String metric;

/**
* Ctor.
* @param metric Name of the metric this Calculus is for.
*/
public XslCalculus(final String metric) {
this.metric = metric;
}

@Override
public XML node(final String metric, final Map<String, Object> params,
final XML skeleton) throws IOException {
public XML node(
final Map<String, Object> params, final XML skeleton
) throws IOException {
return new XSLDocument(
new TextOf(
new ResourceOf(
new FormattedText("org/jpeek/metrics/%s.xsl", metric)
new FormattedText("org/jpeek/metrics/%s.xsl", this.metric)
)
).asString(),
Sources.DUMMY,
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jpeek/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void testsTarget(final String target, final String metric, final double v
@TempDir final Path output)
throws Exception {
new XslReport(
new Skeleton(new FakeBase(target)).xml(), new XslCalculus(),
new Skeleton(new FakeBase(target)).xml(), new XslCalculus(metric),
new ReportData(metric)
).save(output);
final String xpath;
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/org/jpeek/XslReportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public final class XslReportTest {
@Test
public void createsXmlReport(@TempDir final Path output) throws IOException {
new XslReport(
new Skeleton(new FakeBase()).xml(), new XslCalculus(), new ReportData("LCOM")
new Skeleton(new FakeBase()).xml(),
new XslCalculus("LCOM"),
new ReportData("LCOM")
).save(output);
new Assertion<>(
"Must LCOM.xml file exists",
Expand All @@ -72,7 +74,7 @@ public void createsXmlReportWithXpaths(@TempDir final Path output) throws IOExce
"NoMethods", "Bar", "OverloadMethods",
"OnlyOneMethodWithParams", "WithoutAttributes"
)
).xml(), new XslCalculus(), new ReportData("LCOM")
).xml(), new XslCalculus("LCOM"), new ReportData("LCOM")
).save(output);
new Assertion<>(
"Must create LCOM report",
Expand All @@ -89,7 +91,9 @@ public void createsXmlReportWithXpaths(@TempDir final Path output) throws IOExce
@Test
public void createsXmlReportWithEmptyProject(@TempDir final Path output) throws IOException {
new XslReport(
new Skeleton(new FakeBase()).xml(), new XslCalculus(), new ReportData("LCOM")
new Skeleton(new FakeBase()).xml(),
new XslCalculus("LCOM"),
new ReportData("LCOM")
).save(output);
new Assertion<>(
"Report for empty project created",
Expand Down Expand Up @@ -118,7 +122,7 @@ public void createsFullXmlReport(@TempDir final Path output) throws IOException
.add("class").attr("id", "D").attr("value", "0.7").up()
.add("class").attr("id", "E").attr("value", "NaN").up()
).xmlQuietly()
), new XslCalculus(), new ReportData("LCOM")
), new XslCalculus("LCOM"), new ReportData("LCOM")
).save(output);
new Assertion<>(
"Must create full report",
Expand All @@ -140,7 +144,9 @@ public void createsFullXmlReport(@TempDir final Path output) throws IOException
@Test
public void setsCorrectSchemaLocation(@TempDir final Path output) throws IOException {
new XslReport(
new Skeleton(new FakeBase()).xml(), new XslCalculus(), new ReportData("LCOM")
new Skeleton(new FakeBase()).xml(),
new XslCalculus("LCOM"),
new ReportData("LCOM")
).save(output);
new Assertion<>(
"Must have correct schema location",
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jpeek/calculus/java/Lcom4Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class Lcom4Test {
@Disabled
public void createsXmlCalculusWithXpaths() throws IOException {
final XML result = new Lcom4().node(
"LCOM", new HashMap<>(0), new Skeleton(
new HashMap<>(0), new Skeleton(
new FakeBase(
"NoMethods", "Bar", "OverloadMethods",
"OnlyOneMethodWithParams", "WithoutAttributes"
Expand All @@ -70,7 +70,7 @@ public void createsXmlCalculusWithXpaths() throws IOException {
@CsvFileSource(resources = "/org/jpeek/calculus/java/lcom4-params.csv")
public void calculatesValue(final String file, final String value) throws Exception {
final XML result = new Lcom4().node(
"", new HashMap<>(0), new Skeleton(
new HashMap<>(0), new Skeleton(
new FakeBase(file)
).xml()
);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/jpeek/calculus/xsl/XslCalculusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public final class XslCalculusTest {

@Test
public void createsXmlCalculusWithXpaths() throws IOException {
final XML result = new XslCalculus().node(
"LCOM", new HashMap<>(0), new Skeleton(
final XML result = new XslCalculus("LCOM").node(
new HashMap<>(0), new Skeleton(
new FakeBase(
"NoMethods", "Bar", "OverloadMethods",
"OnlyOneMethodWithParams", "WithoutAttributes"
Expand All @@ -61,8 +61,8 @@ public void createsXmlCalculusWithXpaths() throws IOException {

@Test
public void createsXmlCalculusWithEmptyProject() throws IOException {
final XML result = new XslCalculus().node(
"LCOM2", new HashMap<>(0), new Skeleton(new FakeBase()).xml()
final XML result = new XslCalculus("LCOM2").node(
new HashMap<>(0), new Skeleton(new FakeBase()).xml()
);
new Assertion<>(
"Report for empty project created",
Expand Down

0 comments on commit 01b38c5

Please # to comment.