Skip to content

Commit 69d2a41

Browse files
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
1 parent c553b91 commit 69d2a41

File tree

4 files changed

+98
-16
lines changed

4 files changed

+98
-16
lines changed

src/main/java/io/appium/java_client/service/local/AppiumDriverLocalService.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,10 @@ public static AppiumDriverLocalService buildService(AppiumServiceBuilder builder
9494
}
9595

9696
public AppiumDriverLocalService withBasePath(String basePath) {
97-
this.basePath = sanitizeBasePath(basePath);
97+
this.basePath = basePath;
9898
return this;
9999
}
100100

101-
@SneakyThrows private static String sanitizeBasePath(String basePath) {
102-
if (null == basePath) {
103-
LOG.warn("Base Path cannot be NULL -- ignoring the basepath configuration");
104-
return null;
105-
}
106-
basePath = basePath.trim();
107-
if (basePath.isBlank() || basePath.isEmpty()) {
108-
LOG.warn("Base Path cannot be Empty or Blank -- ignoring the basepath configuration");
109-
return null;
110-
}
111-
return basePath.endsWith("/") ? basePath : basePath + "/";
112-
}
113-
114101
public String getBasePath() {
115102
return this.basePath;
116103
}

src/main/java/io/appium/java_client/service/local/AppiumServiceBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.appium.java_client.service.local;
1818

19+
import static com.google.common.base.Preconditions.checkNotNull;
1920
import static org.openqa.selenium.remote.CapabilityType.PLATFORM_NAME;
2021

2122
import com.google.common.collect.ImmutableList;
@@ -217,13 +218,25 @@ public AppiumServiceBuilder withArgument(ServerArgument argument, String value)
217218
case "-g":
218219
withLogFile(new File(value));
219220
break;
221+
case "--base-path":
222+
serverArguments.put(argName,sanitizeBasePath(value));
223+
break;
220224
default:
221225
serverArguments.put(argName, value);
222226
break;
223227
}
224228
return this;
225229
}
226230

231+
private static String sanitizeBasePath(String basePath) {
232+
basePath = checkNotNull(basePath).trim();
233+
if (StringUtils.isBlank(basePath) || basePath.isEmpty()) {
234+
throw new InvalidBasePathException(
235+
"Given base path is not valid - blank or empty values are not allowed for base path");
236+
}
237+
return basePath.endsWith("/") ? basePath : basePath + "/";
238+
}
239+
227240
/**
228241
* Adds capabilities.
229242
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.service.local;
18+
19+
20+
public class InvalidBasePathException extends RuntimeException {
21+
22+
private static final long serialVersionUID = 1L;
23+
24+
public InvalidBasePathException(String message, Throwable t) {
25+
super(message, t);
26+
}
27+
28+
public InvalidBasePathException(String message) {
29+
super(message);
30+
}
31+
}

src/test/java/io/appium/java_client/service/local/ServerBuilderTest.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static io.appium.java_client.TestUtils.getLocalIp4Address;
55
import static io.appium.java_client.service.local.AppiumDriverLocalService.buildDefaultService;
66
import static io.appium.java_client.service.local.AppiumServiceBuilder.APPIUM_PATH;
7+
import static io.appium.java_client.service.local.AppiumServiceBuilder.BROADCAST_IP_ADDRESS;
8+
import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT;
79
import static io.appium.java_client.service.local.flags.GeneralServerFlag.CALLBACK_ADDRESS;
810
import static io.appium.java_client.service.local.flags.GeneralServerFlag.SESSION_OVERRIDE;
911
import static io.appium.java_client.service.local.flags.GeneralServerFlag.BASEPATH;
@@ -20,6 +22,7 @@
2022
import static org.junit.Assert.assertFalse;
2123
import static org.junit.Assert.assertThat;
2224
import static org.junit.Assert.assertTrue;
25+
import static org.junit.Assert.fail;
2326

2427
import com.google.common.collect.ImmutableMap;
2528
import io.appium.java_client.android.options.UiAutomator2Options;
@@ -55,6 +58,9 @@ public class ServerBuilderTest {
5558
private static final Path PATH_T0_TEST_MAIN_JS = ROOT_TEST_PATH
5659
.resolve("service").resolve("local").resolve("main.js");
5760

61+
private static final String INVALID_BASE_PATH_ERROR_MESSAGE =
62+
"Given base path is not valid - blank or empty values are not allowed for base path";
63+
5864
private static String testIP;
5965
private AppiumDriverLocalService service;
6066
private File testLogFile;
@@ -315,9 +321,54 @@ public void checkAbilityToStartServiceWithLogFileUsingShortFlag() {
315321
}
316322

317323
@Test
318-
public void checkAbilityToStartServiceUsingBasePath() {
319-
service = new AppiumServiceBuilder().withArgument(BASEPATH, "/wd/hub").build();
324+
public void checkAbilityToStartServiceUsingValidBasePathWithMultiplePathParams() {
325+
String baseUrl = String.format("http://%s:%d/", BROADCAST_IP_ADDRESS, DEFAULT_APPIUM_PORT);
326+
String basePath = "wd/hub";
327+
service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build();
320328
service.start();
321329
assertTrue(service.isRunning());
330+
assertEquals(baseUrl + basePath + "/",service.getUrl().toString());
331+
}
332+
333+
@Test
334+
public void checkAbilityToStartServiceUsingValidBasePathWithSinglePathParams() {
335+
String baseUrl = String.format("http://%s:%d/", BROADCAST_IP_ADDRESS, DEFAULT_APPIUM_PORT);
336+
String basePath = "/wd/";
337+
service = new AppiumServiceBuilder().withArgument(BASEPATH, basePath).build();
338+
service.start();
339+
assertTrue(service.isRunning());
340+
assertEquals(baseUrl + basePath.substring(1) ,service.getUrl().toString());
341+
}
342+
343+
@Test
344+
public void checkAbilityToValidateBasePathForEmptyBasePath() {
345+
try {
346+
service = new AppiumServiceBuilder().withArgument(BASEPATH, "").build();
347+
fail("Base path was not validated for Blank or Empty string");
348+
} catch (Exception e) {
349+
assertEquals(InvalidBasePathException.class, e.getClass());
350+
assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, e.getMessage());
351+
}
352+
}
353+
354+
@Test
355+
public void checkAbilityToValidateBasePathForBlankBasePath() {
356+
try {
357+
service = new AppiumServiceBuilder().withArgument(BASEPATH, " ").build();
358+
fail("Base path was not validated for Blank or Empty string");
359+
} catch (Exception e) {
360+
assertEquals(InvalidBasePathException.class, e.getClass());
361+
assertEquals(INVALID_BASE_PATH_ERROR_MESSAGE, e.getMessage());
362+
}
363+
}
364+
365+
@Test
366+
public void checkAbilityToValidateBasePathForNullBasePath() {
367+
try {
368+
service = new AppiumServiceBuilder().withArgument(BASEPATH, null).build();
369+
fail("Base path was not validated for a null value");
370+
} catch (Exception e) {
371+
assertEquals(NullPointerException.class, e.getClass());
372+
}
322373
}
323374
}

0 commit comments

Comments
 (0)