From e677e10d6eaefcf1c029331c3b165114fb17f58d Mon Sep 17 00:00:00 2001 From: Giulio Longfils Date: Thu, 2 Nov 2023 18:45:03 +0100 Subject: [PATCH] leveraging buildUrl method in combination with avoidExternalConnections to replace placeholders in the provided url (#1158) --- .../bonigarcia/wdm/WebDriverManager.java | 5 +- .../bonigarcia/wdm/WebDriverManagerTest.java | 116 +++++++++++++----- 2 files changed, 85 insertions(+), 36 deletions(-) diff --git a/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java b/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java index 2ea66dbcb..b77598c9c 100644 --- a/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java +++ b/src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java @@ -1269,8 +1269,9 @@ protected String download(String driverVersion) throws IOException { } URL url; - if (config.isAvoidExternalConnections()) { - url = URI.create(config.getChromeDownloadUrlPattern()).toURL(); + Optional optionalURL = buildUrl(driverVersion); + if (config.isAvoidExternalConnections() && optionalURL.isPresent()) { + url = optionalURL.get(); downloadedDriverVersion = driverVersion; } else { UrlHandler urlHandler = createUrlHandler(driverVersion); diff --git a/src/test/java/io/github/bonigarcia/wdm/WebDriverManagerTest.java b/src/test/java/io/github/bonigarcia/wdm/WebDriverManagerTest.java index 26d8fd1a7..edb19a0b2 100644 --- a/src/test/java/io/github/bonigarcia/wdm/WebDriverManagerTest.java +++ b/src/test/java/io/github/bonigarcia/wdm/WebDriverManagerTest.java @@ -1,10 +1,8 @@ package io.github.bonigarcia.wdm; import io.github.bonigarcia.wdm.config.Config; -import io.github.bonigarcia.wdm.managers.VoidDriverManager; +import io.github.bonigarcia.wdm.config.DriverManagerType; import io.github.bonigarcia.wdm.online.Downloader; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -12,24 +10,24 @@ import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; import java.io.IOException; -import java.net.URI; +import java.net.MalformedURLException; import java.net.URL; +import java.util.List; +import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class WebDriverManagerTest { - @InjectMocks - private VoidDriverManager webDriverManager; + private static final String DUMMY_URL = "http://not.important"; - private MockedStatic uriMockedStatic; + @InjectMocks + private DummyWebDriverManager webDriverManager; @Mock private Config config; @@ -37,22 +35,6 @@ class WebDriverManagerTest { @Mock private Downloader downloader; - @Mock - private URI uri; - - @Mock - private URL url; - - @BeforeEach - public void beforeEach() { - uriMockedStatic = mockStatic(URI.class); - } - - @AfterEach - public void afterEach() { - uriMockedStatic.close(); - } - @Test @DisplayName("avoidExternalConnections() should fluently set to true the corresponding config parameter") void avoidExternalConnections() { @@ -64,22 +46,88 @@ void avoidExternalConnections() { @DisplayName("download") @ParameterizedTest(name = "with driver version {0} should download version {1}") - @ValueSource(strings = { "123", ".123" }) + @ValueSource(strings = {"123", ".123"}) void download(String driverVersion) throws IOException { - String chromeDownloadUrlPattern = "https://chromeDownloadUrlPattern"; String expected = "expected"; String cleanDriverVersion = "123"; when(config.isAvoidExternalConnections()).thenReturn(true); - when(config.getChromeDownloadUrlPattern()) - .thenReturn(chromeDownloadUrlPattern); - when(URI.create(chromeDownloadUrlPattern)).thenReturn(uri); - when(uri.toURL()).thenReturn(url); - - when(downloader.download(url, cleanDriverVersion, "", null)) + when(downloader.download(new URL(DUMMY_URL), cleanDriverVersion, "dummy", null)) .thenReturn(expected); assertEquals(expected, webDriverManager.download(driverVersion)); } -} \ No newline at end of file + + private static class DummyWebDriverManager extends WebDriverManager { + + @Override + protected List getDriverUrls(String driverVersion) { + return null; + } + + @Override + protected String getDriverName() { + return "dummy"; + } + + @Override + protected String getDriverVersion() { + return null; + } + + @Override + protected void setDriverVersion(String driverVersion) { + + } + + @Override + protected String getBrowserVersion() { + return null; + } + + @Override + protected void setBrowserVersion(String browserVersion) { + + } + + @Override + protected void setDriverUrl(URL url) { + + } + + @Override + protected URL getDriverUrl() { + return null; + } + + @Override + protected Optional getMirrorUrl() { + return Optional.empty(); + } + + @Override + protected Optional getExportParameter() { + return Optional.empty(); + } + + @Override + public DriverManagerType getDriverManagerType() { + return null; + } + + @Override + public WebDriverManager exportParameter(String exportParameter) { + return null; + } + + @Override + protected Optional buildUrl(String driverVersion) { + try { + return Optional.of(new URL(DUMMY_URL)); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + } + } +}