From 125428398b1a6e524498a32f16a232c9dd5983de Mon Sep 17 00:00:00 2001 From: Will Kwon Date: Tue, 31 Mar 2015 20:34:18 -0700 Subject: [PATCH 1/2] 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 --- .../wordpress/android/util/UrlUtilsTest.java | 20 +++++++++++++++++++ .../org/wordpress/android/util/UrlUtils.java | 11 ++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java new file mode 100644 index 000000000000..7b0bb6beeca7 --- /dev/null +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java @@ -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")); + } +} diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java index 6bf59b0998f2..811cf6092350 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -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 ""; } /** From 8821cb2624c279ec161a2170a3a917ea8d18b543 Mon Sep 17 00:00:00 2001 From: Will Kwon Date: Wed, 1 Apr 2015 18:42:15 -0700 Subject: [PATCH 2/2] javadocs --- .../src/main/java/org/wordpress/android/util/UrlUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java index 811cf6092350..7ac4dbaf089d 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -32,6 +32,11 @@ public static String urlDecode(final String text) { } } + /** + * + * @param urlString url to get domain from + * @return domain of uri if available. Empty string otherwise. + */ public static String getDomainFromUrl(final String urlString) { if (urlString != null) { Uri uri = Uri.parse(urlString);