Skip to content

Commit

Permalink
Hardening of sax parser (even if we only read files produced by the t…
Browse files Browse the repository at this point in the history
…ool (#36)

* Hardening of sax parser (even if we only read files produced by the tool

Signed-off-by: Olivier Lamy <olamy@apache.org>

* use xmlunit to compare xml files, fix junit assert as code have changed

Signed-off-by: Olivier Lamy <olamy@apache.org>

---------

Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy authored Jun 17, 2024
1 parent 125023f commit 6350059
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* #L%
*/

import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
Expand Down Expand Up @@ -74,7 +75,8 @@ public JDependXMLReportParser(File xmlFile) throws ParserConfigurationException,
stack = new Stack<>();

SAXParser saxParser = factory.newSAXParser();

saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "false");
saxParser.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "false");
saxParser.parse(xmlFile, this);
}

Expand Down
18 changes: 10 additions & 8 deletions src/test/java/org/codehaus/mojo/jdepend/JDependMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import jdepend.xmlui.JDepend;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;

public class JDependMojoTest {
JDependXMLReportParser parser;
Expand Down Expand Up @@ -58,9 +58,11 @@ public void testJDependReportContent() throws IOException {

JDepend.main(args);

assertTrue(
"Generated report xml from " + generatedReport + " is not equal to expected output " + reportXML,
IOUtil.contentEquals(
Files.newInputStream(generatedReport.toPath()), Files.newInputStream(reportXML.toPath())));
Diff myDiff = DiffBuilder.compare(Input.fromFile(reportXML))
.withTest(Input.fromFile(generatedReport))
.ignoreComments()
.build();

Assert.assertFalse(myDiff.toString(), myDiff.hasDifferences());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void testStats() {
assertEquals("Stats Concrete classes is not equal to expected output", "4", stats.getConcreteClasses());
assertEquals("Stats Abstract Classes is not equal to expected output", "1", stats.getAbstractClasses());
assertEquals("0", stats.getCa());
assertEquals("13", stats.getCe());
assertEquals("14", stats.getCe());
assertEquals("0.2", stats.getA());
assertEquals("1", stats.getI());
assertEquals("0.2", stats.getD());
Expand Down Expand Up @@ -137,7 +137,7 @@ public void testCountOfDependsUpon() {
for (JDPackage jdpackage : packages) {
if (jdpackage.getPackageName().equals("org.codehaus.mojo.jdepend")) {
int count = jdpackage.getDependsUpon().size();
assertEquals(13, count);
assertEquals(14, count);
}
if (jdpackage.getPackageName().equals("org.codehaus.mojo.jdepend.objects")) {
int count = jdpackage.getDependsUpon().size();
Expand Down
7 changes: 6 additions & 1 deletion src/test/resources/jdepend-report.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<error>No stats available: package referenced, but not analyzed.</error>
</Package>

<Package name="javax.xml">
<error>No stats available: package referenced, but not analyzed.</error>
</Package>

<Package name="javax.xml.parsers">
<error>No stats available: package referenced, but not analyzed.</error>
</Package>
Expand Down Expand Up @@ -52,7 +56,7 @@
<ConcreteClasses>4</ConcreteClasses>
<AbstractClasses>1</AbstractClasses>
<Ca>0</Ca>
<Ce>13</Ce>
<Ce>14</Ce>
<A>0.2</A>
<I>1</I>
<D>0.2</D>
Expand Down Expand Up @@ -84,6 +88,7 @@
<Package>java.io</Package>
<Package>java.lang</Package>
<Package>java.util</Package>
<Package>javax.xml</Package>
<Package>javax.xml.parsers</Package>
<Package>jdepend.xmlui</Package>
<Package>org.apache.maven.doxia.sink</Package>
Expand Down

0 comments on commit 6350059

Please sign in to comment.