-
-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathExternalTest.java
More file actions
291 lines (258 loc) · 11.3 KB
/
ExternalTest.java
File metadata and controls
291 lines (258 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2002-2026 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.htmlunit.html.DomNode;
import org.htmlunit.html.DomNodeList;
import org.htmlunit.html.HtmlAnchor;
import org.htmlunit.html.HtmlPage;
import org.htmlunit.xml.XmlPage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Tests against external websites, this should be done once every while.
*
* @author Ahmed Ashour
* @author Ronald Brill
*/
public class ExternalTest {
static final String SONATYPE_SNAPSHOT_REPO_URL_ =
"https://central.sonatype.com/repository/maven-snapshots/";
static final String MAVEN_REPO_URL_ = "https://repo1.maven.org/maven2/";
/** Chrome driver. */
static final String CHROME_DRIVER_ = "147.0.7727";
static final String CHROME_DRIVER_URL_ =
"https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json";
static final String EDGE_DRIVER_ = "147.0.3912";
static final String EDGE_DRIVER_URL_ = "https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/";
/** Gecko driver. */
static final String GECKO_DRIVER_ = "0.36.0";
static final String GECKO_DRIVER_URL_ = "https://github.com/mozilla/geckodriver/releases/latest";
/**
* Tests the current environment matches the expected setup.
*/
@Test
public void testEnvironment() {
Assertions.assertEquals("en_US", Locale.getDefault().toString());
}
/**
* Tests that POM dependencies are the latest.
*
* @throws Exception if an error occurs
*/
@Test
public void pom() throws Exception {
final File pomFile = new File("pom.xml");
if (!pomFile.exists()) {
throw new IOException("POM file not found: " + pomFile.getAbsolutePath());
}
MavenXpp3Reader reader = new MavenXpp3Reader();
try (FileReader fileReader = new FileReader(pomFile)) {
final Model model = reader.read(fileReader);
final Pattern ignorePattern = Pattern.compile("" + model.getProperties().get("maven.version.ignore"));
final List<String> wrongVersions = new LinkedList<>();
for (var dep : model.getDependencies()) {
String version = dep.getVersion();
if (version.startsWith("${")) {
version = "" + model.getProperties().get(version.substring(2, version.length() - 1));
}
try {
assertVersion(dep.getGroupId(), dep.getArtifactId(), version, ignorePattern);
}
catch (final AssertionError e) {
wrongVersions.add(e.getMessage());
}
}
if (!wrongVersions.isEmpty()) {
Assertions.fail(String.join("\n ", wrongVersions));
}
}
}
/**
* Tests that we use the latest chrome driver.
* @throws Exception if an error occurs
*/
@Test
public void assertChromeDriver() throws Exception {
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX, false, null, -1)) {
final Page page = webClient.getPage(CHROME_DRIVER_URL_);
final String content = page.getWebResponse().getContentAsString();
String version = "0.0.0.0";
final Pattern regex =
Pattern.compile("\"channels\":\\{\"Stable\":\\{.*?\"version\":\"(\\d*\\.\\d*\\.\\d*)\\.\\d*\"");
final Matcher matcher = regex.matcher(content);
while (matcher.find()) {
if (version.compareTo(matcher.group(1)) < 0) {
version = matcher.group(1);
break;
}
}
Assertions.assertEquals(CHROME_DRIVER_, version);
}
}
/**
* Tests that we use the latest edge driver.
* @throws Exception if an error occurs
*/
@Test
// @Disabled("javascript errors")
public void assertEdgeDriver() throws Exception {
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
webClient.getOptions().setThrowExceptionOnScriptError(false);
final HtmlPage page = webClient.getPage(EDGE_DRIVER_URL_);
String content = page.asNormalizedText();
content = content.substring(content.indexOf("Current general public release channel."));
content = content.replace("\r\n", "");
String version = "0.0.0.0";
final Pattern regex =
Pattern.compile("Version ("
+ BrowserVersion.EDGE.getBrowserVersionNumeric()
+ "\\.\\d*\\.\\d*)\\.\\d*\\s");
final Matcher matcher = regex.matcher(content);
while (matcher.find()) {
if (version.compareTo(matcher.group(1)) < 0) {
version = matcher.group(1);
break;
}
}
Assertions.assertEquals(EDGE_DRIVER_, version);
}
}
/**
* Tests that we use the latest gecko driver.
* @throws Exception if an error occurs
*/
@Test
public void assertGeckoDriver() throws Exception {
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX, false, null, -1)) {
try {
final HtmlPage page = webClient.getPage(GECKO_DRIVER_URL_);
final DomNodeList<DomNode> divs = page.querySelectorAll("li.breadcrumb-item-selected");
Assertions.assertEquals("v" + GECKO_DRIVER_, divs.get(0).asNormalizedText());
}
catch (final FailingHttpStatusCodeException e) {
// ignore
}
}
}
/**
* Tests that the deployed snapshot is not more than two weeks old.
*
* @throws Exception if an error occurs
*/
@Test
public void snapshot() throws Exception {
final File pomFile = new File("pom.xml");
if (!pomFile.exists()) {
throw new IOException("POM file not found: " + pomFile.getAbsolutePath());
}
MavenXpp3Reader reader = new MavenXpp3Reader();
try (FileReader fileReader = new FileReader(pomFile)) {
final Model model = reader.read(fileReader);
final String version = model.getVersion();
Assertions.assertNotNull(version);
if (version.contains("SNAPSHOT")) {
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX, false, null, -1)) {
final String url = SONATYPE_SNAPSHOT_REPO_URL_
+ "org/htmlunit/htmlunit/" + version + "/maven-metadata.xml";
final XmlPage page = webClient.getPage(url);
final String timestamp = page.getElementsByTagName("timestamp").get(0).getTextContent();
final DateFormat format = new SimpleDateFormat("yyyyMMdd.HHmmss", Locale.ROOT);
final long snapshotMillis = format.parse(timestamp).getTime();
final long nowMillis = System.currentTimeMillis();
final long days = TimeUnit.MILLISECONDS.toDays(nowMillis - snapshotMillis);
Assertions.assertTrue(days < 14, "Snapshot not deployed for " + days + " days");
}
}
}
}
private static void assertVersion(final String groupId, final String artifactId,
final String pomVersion, final Pattern ignorePattern)
throws Exception {
String latestMavenCentralVersion = null;
String url = MAVEN_REPO_URL_
+ groupId.replace('.', '/') + '/'
+ artifactId.replace('.', '/');
if (!url.endsWith("/")) {
url += "/";
}
try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX, false, null, -1)) {
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
try {
final HtmlPage page = webClient.getPage(url);
for (final HtmlAnchor anchor : page.getAnchors()) {
String mavenCentralVersion = anchor.getTextContent();
mavenCentralVersion = mavenCentralVersion.substring(0, mavenCentralVersion.length() - 1);
if (!isIgnored(groupId, artifactId, mavenCentralVersion, ignorePattern)) {
if (isVersionAfter(mavenCentralVersion, latestMavenCentralVersion)) {
latestMavenCentralVersion = mavenCentralVersion;
}
}
}
}
catch (final UnknownHostException e) {
// ignore because our ci machine sometimes fails
}
}
if (!pomVersion.endsWith("-SNAPSHOT")
|| !isVersionAfter(
pomVersion.substring(0, pomVersion.length() - "-SNAPSHOT".length()),
latestMavenCentralVersion)) {
// it is ok if the pom uses a more recent version
if (!isVersionAfter(pomVersion, latestMavenCentralVersion)) {
Assertions.assertEquals(latestMavenCentralVersion, pomVersion, groupId + ":" + artifactId);
}
}
}
private static boolean isVersionAfter(final String pomVersion, final String centralVersion) {
if (centralVersion == null) {
return true;
}
return new ComparableVersion(pomVersion).compareTo(new ComparableVersion(centralVersion)) > 0;
}
private static boolean isIgnored(final String groupId, final String artifactId,
final String version, final Pattern ignorePattern) {
// version > 3.12.0 does not work with our site.xml and also not with a refactored one
if ("maven-site-plugin".equals(artifactId)
&& (version.startsWith("3.12.1") || version.startsWith("3.20.") || version.startsWith("3.21."))) {
return true;
}
// ancient common versions
if ("commons-io".equals(artifactId) && (version.startsWith("2003"))) {
return true;
}
if ("commons-net".equals(artifactId) && (version.startsWith("2003"))) {
return true;
}
if (ignorePattern.matcher(version).matches()) {
return true;
}
return false;
}
}