Skip to content

Commit

Permalink
If string is empty return empty string instead of null
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kwonye authored and maxme committed Apr 17, 2015
1 parent 2879710 commit bbfb8e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.wordpress.android.util;

import android.test.InstrumentationTestCase;

public class UrlUtilsTest extends InstrumentationTestCase {
public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() {
assertNotNull(UrlUtils.getDomainFromUrl(""));
}

public void testGetDomainFromUrlWithNoHostDoesNotReturnNull() {
assertNotNull(UrlUtils.getDomainFromUrl("wordpress"));
}

public void testGetDomainFromUrlWithHostReturnsHost() {
String url = "http://www.wordpress.com";
String host = UrlUtils.getDomainFromUrl(url);

assertTrue(host.equals("www.wordpress.com"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ public static String urlDecode(final String text) {
}

public static String getDomainFromUrl(final String urlString) {
if (urlString == null) {
return "";
if (urlString != null) {
Uri uri = Uri.parse(urlString);
if (uri.getHost() != null) {
return uri.getHost();
}
}
Uri uri = Uri.parse(urlString);
return uri.getHost();

return "";
}

/**
Expand Down

0 comments on commit bbfb8e3

Please sign in to comment.