From e8da28498c51c65ec3fa200d382882219a9f3aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Crobin=2Egupta=E2=80=9D?= <“robin.gupta@singtel.com”> Date: Thu, 17 Mar 2022 16:04:02 +0800 Subject: [PATCH 1/7] Fix for basePath appending logic to fix Issue#1660 AppiumDriverLocalService is not able to start - checkStatus is failing --- .../service/local/AppiumDriverLocalService.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java index cc1c77ad5..b1e24bbf3 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java @@ -94,10 +94,23 @@ public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder } public AppiumDriverLocalService withBasePath(String basePath) { - this.basePath = basePath; + this.basePath = sanitizeBasePath(basePath); return this; } + @SneakyThrows private static String sanitizeBasePath(String basePath) { + if (null == basePath) { + LOG.warn("Base Path cannot be NULL -- ignoring the basepath configuration"); + return null; + } + basePath = basePath.trim(); + if (basePath.isBlank() || basePath.isEmpty()) { + LOG.warn("Base Path cannot be Empty or Blank -- ignoring the basepath configuration"); + return null; + } + return (basePath.endsWith("/") ? basePath : basePath + "/"); + } + public String getBasePath() { return this.basePath; } From 72cf29f942691861b33479c975837fd216f6ff9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Crobin=2Egupta=E2=80=9D?= <“robin.gupta@singtel.com”> Date: Thu, 17 Mar 2022 18:06:35 +0800 Subject: [PATCH 2/7] Added test to check the service instantiation with basepath --- .../java_client/service/local/ServerBuilderTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 7db741cdd..fd075e976 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -6,6 +6,7 @@ import static io.appium.java_client.service.local.AppiumServiceBuilder.APPIUM_PATH; import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS; import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; +import static io.appium.java_client.service.local.flags.GeneralServerFlag.BASEPATH; import static io.github.bonigarcia.wdm.WebDriverManager.chromedriver; import static java.lang.System.getProperty; import static java.lang.System.setProperty; @@ -312,4 +313,11 @@ public void checkAbilityToStartServiceWithLogFileUsingShortFlag() { service.start(); assertTrue(testLogFile.exists()); } + + @Test + public void checkAbilityToStartServiceUsingBasePath() { + service = new AppiumServiceBuilder().withArgument(BASEPATH, "/wd/hub").build(); + service.start(); + assertTrue(service.isRunning()); + } } From c553b917075af03a2ed9b43d4635b4c0e4f924bf Mon Sep 17 00:00:00 2001 From: robinGupta11392 <36239951+robinGupta11392@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:23:45 +0800 Subject: [PATCH 3/7] Update src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java Removing redundant parenthesis Co-authored-by: Valery Yatsynovich --- .../java_client/service/local/AppiumDriverLocalService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java index b1e24bbf3..6d6b5072e 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java @@ -108,7 +108,7 @@ public AppiumDriverLocalService withBasePath(String basePath) { LOG.warn("Base Path cannot be Empty or Blank -- ignoring the basepath configuration"); return null; } - return (basePath.endsWith("/") ? basePath : basePath + "/"); + return basePath.endsWith("/") ? basePath : basePath + "/"; } public String getBasePath() { From 69d2a4136490c615b85e618efd1669a09e671272 Mon Sep 17 00:00:00 2001 From: robinGupta11392 Date: Fri, 18 Mar 2022 14:48:12 +0800 Subject: [PATCH 4/7] fix: Modified the logic to validate basePath as per the pull request reviews - replaced String#isBlank with StringUtils#isBlank for Java language compatibility - introduced InvalidBasePathException to be thrown for blank/null/empty values of basepath - basePath validations moved to the step of storing server arguments to give forward compatibility with Appium v2 fix: added extra test cases for more precisely testing basepath validations --- .../local/AppiumDriverLocalService.java | 15 +---- .../service/local/AppiumServiceBuilder.java | 13 +++++ .../local/InvalidBasePathException.java | 31 +++++++++++ .../service/local/ServerBuilderTest.java | 55 ++++++++++++++++++- 4 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java diff --git a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java index 6d6b5072e..cc1c77ad5 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java @@ -94,23 +94,10 @@ public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder } public AppiumDriverLocalService withBasePath(String basePath) { - this.basePath = sanitizeBasePath(basePath); + this.basePath = basePath; return this; } - @SneakyThrows private static String sanitizeBasePath(String basePath) { - if (null == basePath) { - LOG.warn("Base Path cannot be NULL -- ignoring the basepath configuration"); - return null; - } - basePath = basePath.trim(); - if (basePath.isBlank() || basePath.isEmpty()) { - LOG.warn("Base Path cannot be Empty or Blank -- ignoring the basepath configuration"); - return null; - } - return basePath.endsWith("/") ? basePath : basePath + "/"; - } - public String getBasePath() { return this.basePath; } diff --git a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java index 0196579c2..7e6d46665 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java @@ -16,6 +16,7 @@ package io.appium.java_client.service.local; +import static com.google.common.base.Preconditions.checkNotNull; import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME; import com.google.common.collect.ImmutableList; @@ -217,6 +218,9 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) case "-g": withLogFile(new File(value)); break; + case "--base-path": + serverArguments.put(argName,sanitizeBasePath(value)); + break; default: serverArguments.put(argName, value); break; @@ -224,6 +228,15 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) return this; } + private static String sanitizeBasePath(String basePath) { + basePath = checkNotNull(basePath).trim(); + if (StringUtils.isBlank(basePath) || basePath.isEmpty()) { + throw new InvalidBasePathException( + "Given base path is not valid - blank or empty values are not allowed for base path"); + } + return basePath.endsWith("/") ? basePath : basePath + "/"; + } + /** * Adds capabilities. * diff --git a/src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java b/src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java new file mode 100644 index 000000000..e694a1f56 --- /dev/null +++ b/src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java @@ -0,0 +1,31 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://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 io.appium.java_client.service.local; + + +public class InvalidBasePathException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public InvalidBasePathException(String message, Throwable t) { + super(message, t); + } + + public InvalidBasePathException(String message) { + super(message); + } +} diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index fd075e976..31915ea8e 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -4,6 +4,8 @@ import static io.appium.java_client.TestUtils.getLocalIp4Address; import static io.appium.java_client.service.local.AppiumDriverLocalService.buildDefaultService; import static io.appium.java_client.service.local.AppiumServiceBuilder.APPIUM_PATH; +import static io.appium.java_client.service.local.AppiumServiceBuilder.BROADCAST_IP_ADDRESS; +import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT; import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS; import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; import static io.appium.java_client.service.local.flags.GeneralServerFlag.BASEPATH; @@ -20,6 +22,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import com.google.common.collect.ImmutableMap; import io.appium.java_client.android.options.UiAutomator2Options; @@ -55,6 +58,9 @@ public class ServerBuilderTest { private static final Path PATH_T0_TEST_MAIN_JS = ROOT_TEST_PATH .resolve("service").resolve("local").resolve("main.js"); + private static final String INVALID_BASE_PATH_ERROR_MESSAGE = + "Given base path is not valid - blank or empty values are not allowed for base path"; + private static String testIP; private AppiumDriverLocalService service; private File testLogFile; @@ -315,9 +321,54 @@ public void checkAbilityToStartServiceWithLogFileUsingShortFlag() { } @Test - public void checkAbilityToStartServiceUsingBasePath() { - service = new AppiumServiceBuilder().withArgument(BASEPATH, "/wd/hub").build(); + public void checkAbilityToStartServiceUsingValidBasePathWithMultiplePathParams() { + String baseUrl = String.format("http://%s:%d/", BROADCAST_IP_ADDRESS, DEFAULT_APPIUM_PORT); + String basePath = "wd/hub"; + service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build(); service.start(); assertTrue(service.isRunning()); + assertEquals(baseUrl + basePath + "/",service.getUrl().toString()); + } + + @Test + public void checkAbilityToStartServiceUsingValidBasePathWithSinglePathParams() { + String baseUrl = String.format("http://%s:%d/", BROADCAST_IP_ADDRESS, DEFAULT_APPIUM_PORT); + String basePath = "/wd/"; + service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build(); + service.start(); + assertTrue(service.isRunning()); + assertEquals(baseUrl + basePath.substring(1) ,service.getUrl().toString()); + } + + @Test + public void checkAbilityToValidateBasePathForEmptyBasePath() { + try { + service = new AppiumServiceBuilder().withArgument(BASEPATH, "").build(); + fail("Base path was not validated for Blank or Empty string"); + } catch (Exception e) { + assertEquals(InvalidBasePathException.class, e.getClass()); + assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, e.getMessage()); + } + } + + @Test + public void checkAbilityToValidateBasePathForBlankBasePath() { + try { + service = new AppiumServiceBuilder().withArgument(BASEPATH, " ").build(); + fail("Base path was not validated for Blank or Empty string"); + } catch (Exception e) { + assertEquals(InvalidBasePathException.class, e.getClass()); + assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, e.getMessage()); + } + } + + @Test + public void checkAbilityToValidateBasePathForNullBasePath() { + try { + service = new AppiumServiceBuilder().withArgument(BASEPATH, null).build(); + fail("Base path was not validated for a null value"); + } catch (Exception e) { + assertEquals(NullPointerException.class, e.getClass()); + } } } From e013f70785822e0e9923ef79343d55d9be5068fe Mon Sep 17 00:00:00 2001 From: robinGupta11392 Date: Fri, 18 Mar 2022 19:29:32 +0800 Subject: [PATCH 5/7] Formatted code to fix the spacing issue removed redundant validation of StringUtils#isBlank() on the basepath replaced Assert#fail with Assert#assertThrows for the test cases --- .../service/local/AppiumServiceBuilder.java | 32 +++++++-------- .../local/InvalidBasePathException.java | 31 -------------- .../service/local/ServerBuilderTest.java | 40 +++++++------------ 3 files changed, 29 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java diff --git a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java index 7e6d46665..d22259256 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java @@ -20,7 +20,6 @@ import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME; import com.google.common.collect.ImmutableList; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.appium.java_client.remote.AndroidMobileCapabilityType; @@ -28,22 +27,8 @@ import io.appium.java_client.remote.MobileCapabilityType; import io.appium.java_client.service.local.flags.GeneralServerFlag; import io.appium.java_client.service.local.flags.ServerArgument; - -import lombok.SneakyThrows; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.SystemUtils; -import org.apache.commons.validator.routines.InetAddressValidator; -import org.openqa.selenium.Capabilities; -import org.openqa.selenium.Platform; -import org.openqa.selenium.os.ExecutableFinder; -import org.openqa.selenium.remote.Browser; -import org.openqa.selenium.remote.service.DriverService; - -import javax.annotation.Nullable; import java.io.File; import java.io.IOException; - import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; @@ -55,6 +40,17 @@ import java.util.Map; import java.util.Set; import java.util.function.Function; +import javax.annotation.Nullable; +import lombok.SneakyThrows; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; +import org.apache.commons.validator.routines.InetAddressValidator; +import org.openqa.selenium.Capabilities; +import org.openqa.selenium.Platform; +import org.openqa.selenium.os.ExecutableFinder; +import org.openqa.selenium.remote.Browser; +import org.openqa.selenium.remote.service.DriverService; public final class AppiumServiceBuilder extends DriverService.Builder { @@ -219,7 +215,7 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) withLogFile(new File(value)); break; case "--base-path": - serverArguments.put(argName,sanitizeBasePath(value)); + serverArguments.put(argName, sanitizeBasePath(value)); break; default: serverArguments.put(argName, value); @@ -230,8 +226,8 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) private static String sanitizeBasePath(String basePath) { basePath = checkNotNull(basePath).trim(); - if (StringUtils.isBlank(basePath) || basePath.isEmpty()) { - throw new InvalidBasePathException( + if (basePath.isEmpty()) { + throw new IllegalArgumentException( "Given base path is not valid - blank or empty values are not allowed for base path"); } return basePath.endsWith("/") ? basePath : basePath + "/"; diff --git a/src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java b/src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java deleted file mode 100644 index e694a1f56..000000000 --- a/src/main/java/io/appium/java_client/service/local/InvalidBasePathException.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * You may obtain a copy of the License at - * - * http://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 io.appium.java_client.service.local; - - -public class InvalidBasePathException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - public InvalidBasePathException(String message, Throwable t) { - super(message, t); - } - - public InvalidBasePathException(String message) { - super(message); - } -} diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 31915ea8e..2d6fe9044 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -6,9 +6,9 @@ import static io.appium.java_client.service.local.AppiumServiceBuilder.APPIUM_PATH; import static io.appium.java_client.service.local.AppiumServiceBuilder.BROADCAST_IP_ADDRESS; import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT; +import static io.appium.java_client.service.local.flags.GeneralServerFlag.BASEPATH; import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS; import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE; -import static io.appium.java_client.service.local.flags.GeneralServerFlag.BASEPATH; import static io.github.bonigarcia.wdm.WebDriverManager.chromedriver; import static java.lang.System.getProperty; import static java.lang.System.setProperty; @@ -21,16 +21,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import com.google.common.collect.ImmutableMap; import io.appium.java_client.android.options.UiAutomator2Options; import io.github.bonigarcia.wdm.WebDriverManager; -import org.junit.After; -import org.junit.BeforeClass; -import org.junit.Test; - import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; @@ -38,6 +34,9 @@ import java.time.Duration; import java.util.ArrayList; import java.util.List; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; @SuppressWarnings("ResultOfMethodCallIgnored") public class ServerBuilderTest { @@ -327,7 +326,7 @@ public void checkAbilityToStartServiceUsingValidBasePathWithMultiplePathParams() service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build(); service.start(); assertTrue(service.isRunning()); - assertEquals(baseUrl + basePath + "/",service.getUrl().toString()); + assertEquals(baseUrl + basePath + "/", service.getUrl().toString()); } @Test @@ -337,38 +336,29 @@ public void checkAbilityToStartServiceUsingValidBasePathWithSinglePathParams() { service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build(); service.start(); assertTrue(service.isRunning()); - assertEquals(baseUrl + basePath.substring(1) ,service.getUrl().toString()); + assertEquals(baseUrl + basePath.substring(1), service.getUrl().toString()); } @Test public void checkAbilityToValidateBasePathForEmptyBasePath() { - try { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { service = new AppiumServiceBuilder().withArgument(BASEPATH, "").build(); - fail("Base path was not validated for Blank or Empty string"); - } catch (Exception e) { - assertEquals(InvalidBasePathException.class, e.getClass()); - assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, e.getMessage()); - } + }); + assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, exception.getMessage()); } @Test public void checkAbilityToValidateBasePathForBlankBasePath() { - try { + Exception exception = assertThrows(IllegalArgumentException.class, () -> { service = new AppiumServiceBuilder().withArgument(BASEPATH, " ").build(); - fail("Base path was not validated for Blank or Empty string"); - } catch (Exception e) { - assertEquals(InvalidBasePathException.class, e.getClass()); - assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, e.getMessage()); - } + }); + assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, exception.getMessage()); } @Test public void checkAbilityToValidateBasePathForNullBasePath() { - try { + assertThrows(NullPointerException.class, () -> { service = new AppiumServiceBuilder().withArgument(BASEPATH, null).build(); - fail("Base path was not validated for a null value"); - } catch (Exception e) { - assertEquals(NullPointerException.class, e.getClass()); - } + }); } } From 3061c9930b1dcdfc7c2278225b90503ea5396def Mon Sep 17 00:00:00 2001 From: robinGupta11392 <36239951+robinGupta11392@users.noreply.github.com> Date: Sat, 19 Mar 2022 09:24:59 +0800 Subject: [PATCH 6/7] Update src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java Co-authored-by: Valery Yatsynovich --- .../java_client/service/local/AppiumServiceBuilder.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java index d22259256..ebefe72bc 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java @@ -226,10 +226,7 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) private static String sanitizeBasePath(String basePath) { basePath = checkNotNull(basePath).trim(); - if (basePath.isEmpty()) { - throw new IllegalArgumentException( - "Given base path is not valid - blank or empty values are not allowed for base path"); - } + Preconditions.checkArgument(!basePath.isEmpty(), "Given base path is not valid - blank or empty values are not allowed for base path"); return basePath.endsWith("/") ? basePath : basePath + "/"; } From 5b76ab99cdc7753d14e061b7ed3f3f5cae7eb3df Mon Sep 17 00:00:00 2001 From: robinGupta11392 Date: Sat, 19 Mar 2022 09:44:07 +0800 Subject: [PATCH 7/7] Import order correction Removed validations of exception message in the tests Accepted the change from val to use checkArgument method from Preconditions class --- .../service/local/AppiumServiceBuilder.java | 30 +++++++++++-------- .../service/local/ServerBuilderTest.java | 22 ++++++-------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java index ebefe72bc..e0ce11520 100644 --- a/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java +++ b/src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java @@ -16,10 +16,12 @@ package io.appium.java_client.service.local; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME; import com.google.common.collect.ImmutableList; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import io.appium.java_client.remote.AndroidMobileCapabilityType; @@ -27,8 +29,22 @@ import io.appium.java_client.remote.MobileCapabilityType; import io.appium.java_client.service.local.flags.GeneralServerFlag; import io.appium.java_client.service.local.flags.ServerArgument; + +import lombok.SneakyThrows; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.SystemUtils; +import org.apache.commons.validator.routines.InetAddressValidator; +import org.openqa.selenium.Capabilities; +import org.openqa.selenium.Platform; +import org.openqa.selenium.os.ExecutableFinder; +import org.openqa.selenium.remote.Browser; +import org.openqa.selenium.remote.service.DriverService; + +import javax.annotation.Nullable; import java.io.File; import java.io.IOException; + import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; @@ -40,17 +56,6 @@ import java.util.Map; import java.util.Set; import java.util.function.Function; -import javax.annotation.Nullable; -import lombok.SneakyThrows; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.SystemUtils; -import org.apache.commons.validator.routines.InetAddressValidator; -import org.openqa.selenium.Capabilities; -import org.openqa.selenium.Platform; -import org.openqa.selenium.os.ExecutableFinder; -import org.openqa.selenium.remote.Browser; -import org.openqa.selenium.remote.service.DriverService; public final class AppiumServiceBuilder extends DriverService.Builder { @@ -226,7 +231,8 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value) private static String sanitizeBasePath(String basePath) { basePath = checkNotNull(basePath).trim(); - Preconditions.checkArgument(!basePath.isEmpty(), "Given base path is not valid - blank or empty values are not allowed for base path"); + checkArgument(!basePath.isEmpty(), + "Given base path is not valid - blank or empty values are not allowed for base path"); return basePath.endsWith("/") ? basePath : basePath + "/"; } diff --git a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java index 2d6fe9044..3c3fed15e 100644 --- a/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java +++ b/src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java @@ -27,6 +27,10 @@ import com.google.common.collect.ImmutableMap; import io.appium.java_client.android.options.UiAutomator2Options; import io.github.bonigarcia.wdm.WebDriverManager; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; + import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; @@ -34,9 +38,6 @@ import java.time.Duration; import java.util.ArrayList; import java.util.List; -import org.junit.After; -import org.junit.BeforeClass; -import org.junit.Test; @SuppressWarnings("ResultOfMethodCallIgnored") public class ServerBuilderTest { @@ -57,9 +58,6 @@ public class ServerBuilderTest { private static final Path PATH_T0_TEST_MAIN_JS = ROOT_TEST_PATH .resolve("service").resolve("local").resolve("main.js"); - private static final String INVALID_BASE_PATH_ERROR_MESSAGE = - "Given base path is not valid - blank or empty values are not allowed for base path"; - private static String testIP; private AppiumDriverLocalService service; private File testLogFile; @@ -341,24 +339,22 @@ public void checkAbilityToStartServiceUsingValidBasePathWithSinglePathParams() { @Test public void checkAbilityToValidateBasePathForEmptyBasePath() { - Exception exception = assertThrows(IllegalArgumentException.class, () -> { - service = new AppiumServiceBuilder().withArgument(BASEPATH, "").build(); + assertThrows(IllegalArgumentException.class, () -> { + new AppiumServiceBuilder().withArgument(BASEPATH, "").build(); }); - assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, exception.getMessage()); } @Test public void checkAbilityToValidateBasePathForBlankBasePath() { - Exception exception = assertThrows(IllegalArgumentException.class, () -> { - service = new AppiumServiceBuilder().withArgument(BASEPATH, " ").build(); + assertThrows(IllegalArgumentException.class, () -> { + new AppiumServiceBuilder().withArgument(BASEPATH, " ").build(); }); - assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, exception.getMessage()); } @Test public void checkAbilityToValidateBasePathForNullBasePath() { assertThrows(NullPointerException.class, () -> { - service = new AppiumServiceBuilder().withArgument(BASEPATH, null).build(); + new AppiumServiceBuilder().withArgument(BASEPATH, null).build(); }); } }