1414 */
1515package org .htmlunit ;
1616
17- import static java .nio .charset .StandardCharsets .ISO_8859_1 ;
18-
1917import java .io .File ;
2018import java .io .FileReader ;
2119import java .io .IOException ;
2220import java .net .UnknownHostException ;
2321import java .text .DateFormat ;
2422import java .text .SimpleDateFormat ;
25- import java .util .HashMap ;
2623import java .util .LinkedList ;
2724import java .util .List ;
2825import java .util .Locale ;
29- import java .util .Map ;
3026import java .util .concurrent .TimeUnit ;
3127import java .util .regex .Matcher ;
3228import java .util .regex .Pattern ;
3329
34- import org .apache .commons .io .FileUtils ;
35- import org .apache .commons .lang3 .StringUtils ;
3630import org .apache .maven .model .Model ;
3731import org .apache .maven .model .io .xpp3 .MavenXpp3Reader ;
3832import org .htmlunit .html .DomNode ;
4438import org .junit .jupiter .api .Test ;
4539
4640/**
47- * Tests against external web sites , this should be done once every while.
41+ * Tests against external websites , this should be done once every while.
4842 *
4943 * @author Ahmed Ashour
5044 * @author Ronald Brill
@@ -69,18 +63,15 @@ public class ExternalTest {
6963
7064 /**
7165 * Tests the current environment matches the expected setup.
72- * @throws Exception if an error occurs
7366 */
7467 @ Test
75- public void testEnvironment () throws Exception {
68+ public void testEnvironment () {
7669 Assertions .assertEquals ("en_US" , Locale .getDefault ().toString ());
7770 }
7871
7972 /**
8073 * Tests that POM dependencies are the latest.
8174 *
82- * Currently, it is configured to check every week.
83- *
8475 * @throws Exception if an error occurs
8576 */
8677 @ Test
@@ -135,7 +126,7 @@ public void assertChromeDriver() throws Exception {
135126 break ;
136127 }
137128 }
138- Assertions .assertEquals (version , CHROME_DRIVER_ );
129+ Assertions .assertEquals (CHROME_DRIVER_ , version );
139130 }
140131 }
141132
@@ -166,7 +157,7 @@ public void assertEdgeDriver() throws Exception {
166157 break ;
167158 }
168159 }
169- Assertions .assertEquals (version , EDGE_DRIVER_ );
160+ Assertions .assertEquals (EDGE_DRIVER_ , version );
170161 }
171162 }
172163
@@ -180,7 +171,7 @@ public void assertGeckoDriver() throws Exception {
180171 try {
181172 final HtmlPage page = webClient .getPage (GECKO_DRIVER_URL_ );
182173 final DomNodeList <DomNode > divs = page .querySelectorAll ("li.breadcrumb-item-selected" );
183- Assertions .assertEquals (divs .get (0 ).asNormalizedText (), "v" + GECKO_DRIVER_ );
174+ Assertions .assertEquals ("v" + GECKO_DRIVER_ , divs .get (0 ).asNormalizedText ());
184175 }
185176 catch (final FailingHttpStatusCodeException e ) {
186177 // ignore
@@ -191,33 +182,35 @@ public void assertGeckoDriver() throws Exception {
191182 /**
192183 * Tests that the deployed snapshot is not more than two weeks old.
193184 *
194- * Currently, it is configured to check every week.
195- *
196185 * @throws Exception if an error occurs
197186 */
198187 @ Test
199188 public void snapshot () throws Exception {
200- final List <String > lines = FileUtils .readLines (new File ("pom.xml" ), ISO_8859_1 );
201- String version = null ;
202- for (int i = 0 ; i < lines .size (); i ++) {
203- if ("<artifactId>htmlunit</artifactId>" .equals (lines .get (i ).trim ())) {
204- version = getValue (lines .get (i + 1 ));
205- break ;
206- }
189+ final File pomFile = new File ("pom.xml" );
190+ if (!pomFile .exists ()) {
191+ throw new IOException ("POM file not found: " + pomFile .getAbsolutePath ());
207192 }
208- Assertions .assertNotNull (version );
209- if (version .contains ("SNAPSHOT" )) {
210- try (WebClient webClient = new WebClient (BrowserVersion .FIREFOX , false , null , -1 )) {
211- final String url = SONATYPE_SNAPSHOT_REPO_URL_
212- + "org/htmlunit/htmlunit/" + version + "/maven-metadata.xml" ;
213-
214- final XmlPage page = webClient .getPage (url );
215- final String timestamp = page .getElementsByTagName ("timestamp" ).get (0 ).getTextContent ();
216- final DateFormat format = new SimpleDateFormat ("yyyyMMdd.HHmmss" , Locale .ROOT );
217- final long snapshotMillis = format .parse (timestamp ).getTime ();
218- final long nowMillis = System .currentTimeMillis ();
219- final long days = TimeUnit .MILLISECONDS .toDays (nowMillis - snapshotMillis );
220- Assertions .assertTrue (days < 14 , "Snapshot not deployed for " + days + " days" );
193+
194+ MavenXpp3Reader reader = new MavenXpp3Reader ();
195+ try (FileReader fileReader = new FileReader (pomFile )) {
196+ final Model model = reader .read (fileReader );
197+
198+ final String version = model .getVersion ();
199+ Assertions .assertNotNull (version );
200+ if (version .contains ("SNAPSHOT" )) {
201+ try (WebClient webClient = new WebClient (BrowserVersion .FIREFOX , false , null , -1 )) {
202+ final String url = SONATYPE_SNAPSHOT_REPO_URL_
203+ + "org/htmlunit/htmlunit/" + version + "/maven-metadata.xml" ;
204+
205+ final XmlPage page = webClient .getPage (url );
206+ final String timestamp = page .getElementsByTagName ("timestamp" ).get (0 ).getTextContent ();
207+ final DateFormat format = new SimpleDateFormat ("yyyyMMdd.HHmmss" , Locale .ROOT );
208+ final long snapshotMillis = format .parse (timestamp ).getTime ();
209+ final long nowMillis = System .currentTimeMillis ();
210+ final long days = TimeUnit .MILLISECONDS .toDays (nowMillis - snapshotMillis );
211+
212+ Assertions .assertTrue (days < 14 , "Snapshot not deployed for " + days + " days" );
213+ }
221214 }
222215 }
223216 }
@@ -341,7 +334,7 @@ private static boolean isIgnored(@SuppressWarnings("unused") final String groupI
341334 return true ;
342335 }
343336
344- // really old common versions
337+ // ancient common versions
345338 if ("commons-io" .equals (artifactId ) && (version .startsWith ("2003" ))) {
346339 return true ;
347340 }
@@ -351,8 +344,4 @@ private static boolean isIgnored(@SuppressWarnings("unused") final String groupI
351344
352345 return false ;
353346 }
354-
355- private static String getValue (final String line ) {
356- return line .substring (line .indexOf ('>' ) + 1 , line .lastIndexOf ('<' ));
357- }
358347}
0 commit comments