Skip to content

Commit eeae78a

Browse files
committed
use maven-model pom parser
1 parent 5031725 commit eeae78a

File tree

2 files changed

+31
-46
lines changed

2 files changed

+31
-46
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<image-comparison.version>4.4.0</image-comparison.version>
6868
<imageio-batik.version>3.13.0</imageio-batik.version>
6969
<jfreechart.version>1.5.6</jfreechart.version>
70+
<maven-model.version>3.9.12</maven-model.version>
7071

7172
<!-- quality -->
7273
<checkstyle.version>12.3.1</checkstyle.version>
@@ -1552,6 +1553,11 @@
15521553
<version>${jfreechart.version}</version>
15531554
<scope>test</scope>
15541555
</dependency>
1556+
<dependency>
1557+
<groupId>org.apache.maven</groupId>
1558+
<artifactId>maven-model</artifactId>
1559+
<version>${maven-model.version}</version>
1560+
</dependency>
15551561
<!-- Jetty -->
15561562
<dependency>
15571563
<groupId>org.eclipse.jetty</groupId>

src/test/java/org/htmlunit/ExternalTest.java

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static java.nio.charset.StandardCharsets.ISO_8859_1;
1818

1919
import java.io.File;
20+
import java.io.FileReader;
21+
import java.io.IOException;
2022
import java.net.UnknownHostException;
2123
import java.text.DateFormat;
2224
import java.text.SimpleDateFormat;
@@ -31,6 +33,8 @@
3133

3234
import org.apache.commons.io.FileUtils;
3335
import org.apache.commons.lang3.StringUtils;
36+
import org.apache.maven.model.Model;
37+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
3438
import org.htmlunit.html.DomNode;
3539
import org.htmlunit.html.DomNodeList;
3640
import org.htmlunit.html.HtmlAnchor;
@@ -75,62 +79,37 @@ public void testEnvironment() throws Exception {
7579
/**
7680
* Tests that POM dependencies are the latest.
7781
*
78-
* Currently it is configured to check every week.
82+
* Currently, it is configured to check every week.
7983
*
8084
* @throws Exception if an error occurs
8185
*/
8286
@Test
8387
public void pom() throws Exception {
84-
final Map<String, String> properties = new HashMap<>();
85-
final List<String> lines = FileUtils.readLines(new File("pom.xml"), ISO_8859_1);
88+
final File pomFile = new File("pom.xml");
89+
if (!pomFile.exists()) {
90+
throw new IOException("POM file not found: " + pomFile.getAbsolutePath());
91+
}
8692

87-
final List<String> wrongVersions = new LinkedList<>();
88-
boolean inComment = false;
89-
for (int i = 0; i < lines.size(); i++) {
90-
String line = lines.get(i);
91-
92-
String cleaned = "";
93-
if (!inComment) {
94-
final int startIdx = line.indexOf("<!--");
95-
if (startIdx != -1) {
96-
cleaned += line.substring(0, startIdx);
97-
inComment = true;
93+
MavenXpp3Reader reader = new MavenXpp3Reader();
94+
try (FileReader fileReader = new FileReader(pomFile)) {
95+
final Model model = reader.read(fileReader);
96+
97+
final List<String> wrongVersions = new LinkedList<>();
98+
for (var dep : model.getDependencies()) {
99+
String version = dep.getVersion();
100+
if (version.startsWith("${")) {
101+
version = "" + model.getProperties().get(version.substring(2, version.length() - 1));
98102
}
99-
}
100-
if (inComment) {
101-
final int endIdx = line.indexOf("-->");
102-
if (endIdx != -1) {
103-
cleaned += line.substring(endIdx + 3);
104-
inComment = false;
103+
try {
104+
assertVersion(dep.getGroupId(), dep.getArtifactId(), version);
105105
}
106-
line = cleaned;
107-
}
108-
109-
if (line.trim().equals("<properties>")) {
110-
processProperties(lines, i + 1, properties);
111-
}
112-
if (line.contains("artifactId")
113-
&& !line.contains(">htmlunit<")
114-
&& !line.contains(">selenium-devtools-v")) {
115-
final String artifactId = getValue(line);
116-
final String groupId = getValue(lines.get(i - 1));
117-
if (!lines.get(i + 1).contains("</exclusion>")) {
118-
String version = getValue(lines.get(i + 1));
119-
if (version.startsWith("${")) {
120-
version = properties.get(version.substring(2, version.length() - 1));
121-
}
122-
try {
123-
assertVersion(groupId, artifactId, version);
124-
}
125-
catch (final AssertionError e) {
126-
wrongVersions.add(e.getMessage());
127-
}
106+
catch (final AssertionError e) {
107+
wrongVersions.add(e.getMessage());
128108
}
129109
}
130-
}
131-
132-
if (!wrongVersions.isEmpty()) {
133-
Assertions.fail(String.join("\n ", wrongVersions));
110+
if (!wrongVersions.isEmpty()) {
111+
Assertions.fail(String.join("\n ", wrongVersions));
112+
}
134113
}
135114

136115
assertVersion("org.sonatype.oss", "oss-parent", "9");

0 commit comments

Comments
 (0)