diff --git a/src/main/java/org/jpeek/App.java b/src/main/java/org/jpeek/App.java index 27b6e495..9361c9ad 100644 --- a/src/main/java/org/jpeek/App.java +++ b/src/main/java/org/jpeek/App.java @@ -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; @@ -171,11 +170,10 @@ public void analyze() throws IOException { final XSL chain = new XSLChain(layers); this.save(skeleton.toString(), "skeleton.xml"); final Collection 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) ) ); @@ -183,7 +181,7 @@ public void analyze() throws IOException { if (this.params.containsKey("CAMC")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("CAMC"), new ReportData("CAMC", this.params) ) ); @@ -191,7 +189,7 @@ public void analyze() throws IOException { 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) ) ); @@ -199,7 +197,7 @@ public void analyze() throws IOException { 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) ) ); @@ -207,7 +205,7 @@ public void analyze() throws IOException { 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) ) ); @@ -215,7 +213,7 @@ public void analyze() throws IOException { if (this.params.containsKey("NHD")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("NHD"), new ReportData("NHD") ) ); @@ -223,7 +221,7 @@ public void analyze() throws IOException { if (this.params.containsKey("LCOM2")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("LCOM2"), new ReportData("LCOM2", this.params) ) ); @@ -231,7 +229,7 @@ public void analyze() throws IOException { if (this.params.containsKey("LCOM3")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("LCOM3"), new ReportData("LCOM3", this.params) ) ); @@ -239,7 +237,7 @@ public void analyze() throws IOException { if (this.params.containsKey("SCOM")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("SCOM"), new ReportData("SCOM", this.params) ) ); @@ -247,7 +245,7 @@ public void analyze() throws IOException { if (this.params.containsKey("OCC")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("OCC"), new ReportData("OCC", this.params) ) ); @@ -255,7 +253,7 @@ public void analyze() throws IOException { if (this.params.containsKey("PCC")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("PCC"), new ReportData("PCC") ) ); @@ -263,7 +261,7 @@ public void analyze() throws IOException { if (this.params.containsKey("TCC")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("TCC"), new ReportData("TCC") ) ); @@ -271,7 +269,7 @@ public void analyze() throws IOException { if (this.params.containsKey("LCC")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("LCC"), new ReportData("LCC") ) ); @@ -279,7 +277,7 @@ public void analyze() throws IOException { if (this.params.containsKey("CCM")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("CCM"), new ReportData("CCM") ) ); @@ -287,7 +285,7 @@ public void analyze() throws IOException { if (this.params.containsKey("MWE")) { reports.add( new XslReport( - chain.transform(skeleton), xsl, + chain.transform(skeleton), new XslCalculus("MWE"), new ReportData("MWE") ) ); diff --git a/src/main/java/org/jpeek/XslReport.java b/src/main/java/org/jpeek/XslReport.java index 14c6dbb9..69573211 100644 --- a/src/main/java/org/jpeek/XslReport.java +++ b/src/main/java/org/jpeek/XslReport.java @@ -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(); @@ -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() ) ); } diff --git a/src/main/java/org/jpeek/calculus/Calculus.java b/src/main/java/org/jpeek/calculus/Calculus.java index 7fdd1895..53bbb4d7 100644 --- a/src/main/java/org/jpeek/calculus/Calculus.java +++ b/src/main/java/org/jpeek/calculus/Calculus.java @@ -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 params, XML skeleton) - throws IOException; + XML node(Map params, XML skeleton) throws IOException; } diff --git a/src/main/java/org/jpeek/calculus/java/Ccm.java b/src/main/java/org/jpeek/calculus/java/Ccm.java index c4c1693e..35d736eb 100644 --- a/src/main/java/org/jpeek/calculus/java/Ccm.java +++ b/src/main/java/org/jpeek/calculus/java/Ccm.java @@ -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; @@ -40,18 +39,7 @@ public final class Ccm implements Calculus { @Override - public XML node( - final String metric, - final Map 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 params, final XML skeleton) { return Ccm.withFixedNcc( new XSLDocument( new UncheckedInput( diff --git a/src/main/java/org/jpeek/calculus/java/Lcom4.java b/src/main/java/org/jpeek/calculus/java/Lcom4.java index a960d216..1f073a56 100644 --- a/src/main/java/org/jpeek/calculus/java/Lcom4.java +++ b/src/main/java/org/jpeek/calculus/java/Lcom4.java @@ -41,8 +41,9 @@ public final class Lcom4 implements Calculus { @Override - public XML node(final String metric, final Map params, - final XML skeleton) throws IOException { + public XML node( + final Map params, final XML skeleton + ) throws IOException { final XML result = new XSLDocument( new TextOf( new ResourceOf("org/jpeek/metrics/LCOM4.xsl") diff --git a/src/main/java/org/jpeek/calculus/xsl/XslCalculus.java b/src/main/java/org/jpeek/calculus/xsl/XslCalculus.java index 544a1b98..72775240 100644 --- a/src/main/java/org/jpeek/calculus/xsl/XslCalculus.java +++ b/src/main/java/org/jpeek/calculus/xsl/XslCalculus.java @@ -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 params, - final XML skeleton) throws IOException { + public XML node( + final Map 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, diff --git a/src/test/java/org/jpeek/MetricsTest.java b/src/test/java/org/jpeek/MetricsTest.java index e0b90e56..421f6cbc 100644 --- a/src/test/java/org/jpeek/MetricsTest.java +++ b/src/test/java/org/jpeek/MetricsTest.java @@ -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; diff --git a/src/test/java/org/jpeek/XslReportTest.java b/src/test/java/org/jpeek/XslReportTest.java index ce9ea791..2dd2539f 100644 --- a/src/test/java/org/jpeek/XslReportTest.java +++ b/src/test/java/org/jpeek/XslReportTest.java @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/src/test/java/org/jpeek/calculus/java/Lcom4Test.java b/src/test/java/org/jpeek/calculus/java/Lcom4Test.java index 5cceb811..517ebce2 100644 --- a/src/test/java/org/jpeek/calculus/java/Lcom4Test.java +++ b/src/test/java/org/jpeek/calculus/java/Lcom4Test.java @@ -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" @@ -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() ); diff --git a/src/test/java/org/jpeek/calculus/xsl/XslCalculusTest.java b/src/test/java/org/jpeek/calculus/xsl/XslCalculusTest.java index e39d8fdd..b128d316 100644 --- a/src/test/java/org/jpeek/calculus/xsl/XslCalculusTest.java +++ b/src/test/java/org/jpeek/calculus/xsl/XslCalculusTest.java @@ -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" @@ -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",