-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added ccmCalculus especially to find ncc value for ccm metric. The lo… #548
base: master
Are you sure you want to change the base?
Changes from 6 commits
aebacaa
c9293ab
fd730b6
4443ead
e0f8a2f
0ce54fd
aaa584a
9557ee8
3b47211
144e4e1
1f7f100
dd92650
61dbe52
cdc7ae4
5017e4a
1071ee1
5f37cbb
5258ae7
346d3d2
5a5a305
83a8137
d051565
d62314d
0874e82
d06c483
d4ffcde
0a64225
4dc4951
fe2b446
969c63e
f8e30c1
0bfe7a7
cb0d747
e0f6596
71dedfe
db39bd7
6bbc1ee
8084432
d26f4e7
47254cc
a8684bd
f6489cb
93668c3
4f56dfb
762b62f
ddce255
b4cf48b
debef9d
39b8f2f
da5370e
69c8980
202e99e
bb8e09f
ec8dd38
b6404bc
4b0e358
52c404c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,26 @@ | |
*/ | ||
package org.jpeek.calculus.java; | ||
|
||
import com.jcabi.xml.Sources; | ||
import com.jcabi.xml.XML; | ||
import com.jcabi.xml.XMLDocument; | ||
import com.jcabi.xml.XSLDocument; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import org.cactoos.io.ResourceOf; | ||
import org.cactoos.io.UncheckedInput; | ||
import org.cactoos.text.FormattedText; | ||
import org.cactoos.text.Joined; | ||
import org.cactoos.text.TextOf; | ||
import org.cactoos.text.UncheckedText; | ||
import org.jpeek.calculus.Calculus; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NamedNodeMap; | ||
import org.w3c.dom.Node; | ||
|
||
/** | ||
* CCM metric Java calculus. | ||
|
@@ -52,14 +63,19 @@ public XML node( | |
).toString() | ||
); | ||
} | ||
return Ccm.withFixedNcc( | ||
new XSLDocument( | ||
new UncheckedInput( | ||
new ResourceOf("org/jpeek/metrics/CCM.xsl") | ||
).stream() | ||
).transform(skeleton), | ||
skeleton | ||
final XSLDocument doc = new XSLDocument( | ||
new UncheckedText( | ||
new TextOf( | ||
new ResourceOf( | ||
new FormattedText("org/jpeek/metrics/%s.xsl", metric) | ||
) | ||
) | ||
).asString(), | ||
Sources.DUMMY, | ||
params | ||
); | ||
final XML meta = addMetaTag(skeleton); | ||
return doc.transform(meta); | ||
} | ||
|
||
/** | ||
|
@@ -68,6 +84,7 @@ public XML node( | |
* @param skeleton XML Skeleton | ||
* @return XML with fixed NCC. | ||
*/ | ||
@SuppressWarnings("PMD.UnusedPrivateMethod") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. really bed suppression, method is not used and confusing for all, why do we need it |
||
private static XML withFixedNcc(final XML transformed, final XML skeleton) { | ||
final List<XML> packages = transformed.nodes("//package"); | ||
for (final XML elt : packages) { | ||
|
@@ -105,4 +122,92 @@ private static void updateNcc( | |
).toString() | ||
); | ||
} | ||
|
||
/** | ||
* Adds a meta tag to the skeleton XML. | ||
* @param skeleton The skeleton XML to which the meta tag will be added | ||
* @return The modified XML with the meta tag added | ||
*/ | ||
private static XML addMetaTag(final XML skeleton) { | ||
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = null; | ||
final Document doc; | ||
try { | ||
builder = factory.newDocumentBuilder(); | ||
doc = builder.newDocument(); | ||
final Element meta = doc.createElement("meta"); | ||
Node packages = skeleton.node().getFirstChild().getFirstChild().getNextSibling() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really near to impossible to support this code, I can not remember the order of the nodes in XML and nobody wants, that's why there are a lot of ways to work with XML using XSL, XPATH, tag names. |
||
.getFirstChild(); | ||
while (Objects.nonNull(packages)) { | ||
if (packages.getTextContent().matches("\\s*")) { | ||
packages = packages.getNextSibling(); | ||
continue; | ||
} | ||
final Node child = createPackageTag(doc, packages); | ||
meta.appendChild(child); | ||
packages = packages.getNextSibling(); | ||
} | ||
doc.appendChild(meta); | ||
} catch (final ParserConfigurationException ex) { | ||
throw new IllegalStateException(ex); | ||
} | ||
final Node repr = skeleton.node().getFirstChild(); | ||
final Node node = repr.getOwnerDocument() | ||
.importNode(doc.getDocumentElement(), true); | ||
repr.appendChild(node); | ||
return new XMLDocument(repr); | ||
} | ||
|
||
/** | ||
* Creates a package tag element in the XML document. | ||
* @param doc The XML document to which the package tag will be added | ||
* @param packages The node representing the package | ||
* @return The package tag element created in the XML document | ||
*/ | ||
private static Node createPackageTag(final Document doc, final Node packages) { | ||
final NamedNodeMap map = packages.getAttributes(); | ||
final Element root = doc.createElement("package"); | ||
final Node id = map.getNamedItem("id"); | ||
root.setAttribute("id", id.getNodeValue()); | ||
Node classes = packages.getFirstChild(); | ||
while (Objects.nonNull(classes)) { | ||
if (classes.getTextContent().matches("\\s*")) { | ||
classes = classes.getNextSibling(); | ||
continue; | ||
} | ||
final Node child = createClassTag(doc, classes); | ||
root.appendChild(child); | ||
classes = classes.getNextSibling(); | ||
} | ||
return root; | ||
} | ||
|
||
/** | ||
* Creates a class tag element in the XML document. | ||
* | ||
* This method constructs a class tag element in the XML document based on the | ||
* information provided by the given node representing a class. It traverses | ||
* through the attributes of the class node to extract necessary information | ||
* and constructs the class tag element accordingly. | ||
* | ||
* Additionally, this method adds a child element representing a metric called | ||
* "ncc" to the class tag. | ||
* | ||
* @param doc The XML document to which the class tag will be added | ||
* @param classes The node representing the class | ||
* @return The class tag element created in the XML document | ||
* @todo #522:30m/DEV Implement method that would build graph where methods are nodes and | ||
* field references are edges and find connected components in that graph. | ||
*/ | ||
private static Node createClassTag(final Document doc, final Node classes) { | ||
final NamedNodeMap map = classes.getAttributes(); | ||
final Element root = doc.createElement("class"); | ||
final Node id = map.getNamedItem("id"); | ||
root.setAttribute("id", id.getNodeValue()); | ||
final Integer value = 1; | ||
final Element ncc = doc.createElement("ncc"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not only ncc metric should be presented, you can check current implementation |
||
ncc.setTextContent(value.toString()); | ||
root.appendChild(ncc); | ||
return root; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2024 Yegor Bugayenko | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.jpeek.calculus.java; | ||
|
||
import com.jcabi.matchers.XhtmlMatchers; | ||
import com.jcabi.xml.XML; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import org.jpeek.FakeBase; | ||
import org.jpeek.skeleton.Skeleton; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.llorllale.cactoos.matchers.Assertion; | ||
import org.llorllale.cactoos.matchers.IsTrue; | ||
|
||
/** | ||
* Test class for CCM calculus. | ||
* @since 0.30.9 | ||
*/ | ||
final class CcmTest { | ||
@Test | ||
@Disabled | ||
void createsSkeletonWithMeta() throws IOException { | ||
final XML result = new Ccm().node( | ||
"CCM", new HashMap<>(0), new Skeleton( | ||
new FakeBase( | ||
"Foo", "Bar" | ||
) | ||
).xml() | ||
); | ||
new Assertion<>( | ||
"Must create CCM report", | ||
result.toString(), | ||
XhtmlMatchers.hasXPath( | ||
"/metric/app/package/class/vars/var" | ||
) | ||
).affirm(); | ||
new Assertion<>( | ||
"Must have 2 ncc vars", | ||
result.xpath("/metric/app/package/class/vars/var[@id=\'ncc\']/text()").size() == 2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While we have a specific problem with specific class, lets create a tests to check metric values, not just their existing. |
||
new IsTrue() | ||
).affirm(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not clear why do we need to apply xsl for dymmy sources, while ccm contains 5 columns for now and we have a problem with 2 of them only
https://user-images.githubusercontent.com/63350301/117303386-4ff0da00-ae85-11eb-82c3-869f1a66521d.png