Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ SOFTWARE.
<version>0.14.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eolang</groupId>
<artifactId>jeo-maven-plugin</artifactId>
<version>0.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
58 changes: 56 additions & 2 deletions src/main/java/org/eolang/lints/LtByXsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@
import com.jcabi.xml.XSL;
import com.jcabi.xml.XSLDocument;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import org.cactoos.Input;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.IoCheckedText;
import org.cactoos.text.TextOf;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
* Lint by XSL.
Expand Down Expand Up @@ -101,7 +105,7 @@ public String name() {
public Collection<Defect> defects(final XML xmir) {
final XML report = this.sheet.transform(xmir);
final Collection<Defect> defects = new LinkedList<>();
for (final XML defect : report.nodes("/defects/defect")) {
for (final XML defect : LtByXsl.findDefects(report)) {
final List<String> severity = defect.xpath("@severity");
if (severity.isEmpty()) {
throw new IllegalStateException(
Expand All @@ -112,7 +116,7 @@ public Collection<Defect> defects(final XML xmir) {
new Defect.Default(
this.rule,
Severity.parsed(severity.get(0)),
xmir.xpath("/program/@name").stream().findFirst().orElse("unknown"),
LtByXsl.findName(xmir),
this.lineno(defect),
defect.xpath("text()").get(0)
)
Expand Down Expand Up @@ -159,4 +163,54 @@ private int lineno(final XML defect) {
return lineno;
}

/**
* Find the name of the program.
* @param program XML program
* @return Name of the program.
* @todo #199:30min Use {@link XML#xpath(String)} Method Instead.
* This method is using a custom implementation to find the name of the program.
* We should replace it with the {@link XML#xpath(String)} method to make the code cleaner.
* You can use `program.xpath("/program/@name").stream().findFirst().orElse("unknown")`
* to find the name.
* This issue is blocked by
* <a href="https://github.com/jcabi/jcabi-xml/issues/289">jcabi/jcabi-xml#289</a>.
*/
private static String findName(final XML program) {
return Optional.of(program.inner().getFirstChild())
.map(Node::getAttributes).map(attrs -> attrs.getNamedItem("name"))
.map(Node::getTextContent).orElse("unknown");
}

/**
* Find defects in the report.
* @param report XML report.
* @return Collection of defects.
* @todo #199:30min Use {@link XML#nodes(String)} Method Instead.
* This method is using a custom implementation to find defects in the
* report. We should replace it with the {@link XML#nodes(String)} method
* to make the code cleaner.
* You can use `report.nodes("/defects/defect")` to find the defects.
* This issue is blocked by
* <a href="https://github.com/jcabi/jcabi-xml/issues/288">jcabi/jcabi-xml#288</a>.
*/
private static Collection<XML> findDefects(final XML report) {
final NodeList nodes = report.inner().getChildNodes();
final int length = nodes.getLength();
final List<XML> defects = new ArrayList<>(0);
for (int index = 0; index < length; ++index) {
final Node element = nodes.item(index);
if ("defects".equals(element.getNodeName())) {
final NodeList dnodes;
dnodes = element.getChildNodes();
final int all = dnodes.getLength();
for (int idx = 0; idx < all; ++idx) {
final Node defect = dnodes.item(idx);
if ("defect".equals(defect.getNodeName())) {
defects.add(new XMLDocument(defect.cloneNode(true)));
}
}
}
}
return defects;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ SOFTWARE.
<xsl:text>anonymous object</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text> is empty, it doesn't have any attributes, neither void nor attached</xsl:text>
<xsl:text> is empty. It doesn't have any attributes, neither void nor attached</xsl:text>
</xsl:element>
</xsl:for-each>
</defects>
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/eolang/lints/LtByXslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
*/
package org.eolang.lints;

import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.stream.Collectors;
import org.cactoos.io.InputOf;
import org.eolang.jeo.Disassembler;
import org.eolang.jucs.ClasspathSource;
import org.eolang.parser.EoSyntax;
import org.eolang.xax.XtSticky;
Expand All @@ -38,9 +40,12 @@
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;

/**
Expand Down Expand Up @@ -171,4 +176,24 @@ void checksFileNaming() throws IOException {
);
}

@Test
@Timeout(30)
void checksEmptyObjectOnLargeXmirInReasonableTime(@TempDir final Path tmp) throws IOException {
final Path path = Paths.get("com/sun/jna");
final String clazz = "Pointer.class";
Files.copy(
Paths.get("target")
.resolve("jna-classes")
.resolve(path)
.resolve(clazz),
tmp.resolve(clazz)
);
new Disassembler(tmp, tmp).disassemble();
Assertions.assertDoesNotThrow(
() -> new LtByXsl("errors/empty-object").defects(
new XMLDocument(tmp.resolve(path).resolve("Pointer.xmir"))
),
"Huge XMIR must pass in reasonable time. See the timeout value."
);
}
}
Loading