Skip to content

Commit efe7e97

Browse files
kwonyemaxme
authored andcommitted
If string is empty return empty string instead of null
Verify that passing in an empty string does not return null Handling cases where the url could be malformed so there are no hosts even with a non-empty string
1 parent fd92adf commit efe7e97

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.wordpress.android.util;
2+
3+
import android.test.InstrumentationTestCase;
4+
5+
public class UrlUtilsTest extends InstrumentationTestCase {
6+
public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() {
7+
assertNotNull(UrlUtils.getDomainFromUrl(""));
8+
}
9+
10+
public void testGetDomainFromUrlWithNoHostDoesNotReturnNull() {
11+
assertNotNull(UrlUtils.getDomainFromUrl("wordpress"));
12+
}
13+
14+
public void testGetDomainFromUrlWithHostReturnsHost() {
15+
String url = "http://www.wordpress.com";
16+
String host = UrlUtils.getDomainFromUrl(url);
17+
18+
assertTrue(host.equals("www.wordpress.com"));
19+
}
20+
}

WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ public static String urlDecode(final String text) {
3333
}
3434

3535
public static String getDomainFromUrl(final String urlString) {
36-
if (urlString == null) {
37-
return "";
36+
if (urlString != null) {
37+
Uri uri = Uri.parse(urlString);
38+
if (uri.getHost() != null) {
39+
return uri.getHost();
40+
}
3841
}
39-
Uri uri = Uri.parse(urlString);
40-
return uri.getHost();
42+
43+
return "";
4144
}
4245

4346
/**

0 commit comments

Comments
 (0)