|
17 | 17 | import static java.nio.charset.StandardCharsets.ISO_8859_1; |
18 | 18 |
|
19 | 19 | import java.io.File; |
| 20 | +import java.io.FileReader; |
| 21 | +import java.io.IOException; |
20 | 22 | import java.net.UnknownHostException; |
21 | 23 | import java.text.DateFormat; |
22 | 24 | import java.text.SimpleDateFormat; |
|
31 | 33 |
|
32 | 34 | import org.apache.commons.io.FileUtils; |
33 | 35 | import org.apache.commons.lang3.StringUtils; |
| 36 | +import org.apache.maven.model.Model; |
| 37 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
34 | 38 | import org.htmlunit.html.DomNode; |
35 | 39 | import org.htmlunit.html.DomNodeList; |
36 | 40 | import org.htmlunit.html.HtmlAnchor; |
@@ -75,62 +79,37 @@ public void testEnvironment() throws Exception { |
75 | 79 | /** |
76 | 80 | * Tests that POM dependencies are the latest. |
77 | 81 | * |
78 | | - * Currently it is configured to check every week. |
| 82 | + * Currently, it is configured to check every week. |
79 | 83 | * |
80 | 84 | * @throws Exception if an error occurs |
81 | 85 | */ |
82 | 86 | @Test |
83 | 87 | 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 | + } |
86 | 92 |
|
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)); |
98 | 102 | } |
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); |
105 | 105 | } |
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()); |
128 | 108 | } |
129 | 109 | } |
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 | + } |
134 | 113 | } |
135 | 114 |
|
136 | 115 | assertVersion("org.sonatype.oss", "oss-parent", "9"); |
|
0 commit comments