From 577e0253e89bc5ef89d52578022edcb54886759b Mon Sep 17 00:00:00 2001 From: Will Kwon Date: Thu, 3 Sep 2015 15:22:58 -0700 Subject: [PATCH 01/10] Updating build tools to 23.0.1 --- WordPressUtils/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPressUtils/build.gradle b/WordPressUtils/build.gradle index 4c77b82bc0e8..06e48011763c 100644 --- a/WordPressUtils/build.gradle +++ b/WordPressUtils/build.gradle @@ -20,7 +20,7 @@ dependencies { exclude group: 'commons-logging' } compile 'com.mcxiaoke.volley:library:1.0.18' - compile 'com.android.support:support-v13:23.0.0' + compile 'com.android.support:support-v13:23.0.1' } android { @@ -29,7 +29,7 @@ android { publishNonDefault true compileSdkVersion 23 - buildToolsVersion '23.0.0' + buildToolsVersion '23.0.1' defaultConfig { versionName "1.5.0" From 167f96de993360a6bdf1a911dac718ad0d6c4ad7 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Wed, 9 Sep 2015 12:18:46 +0200 Subject: [PATCH 02/10] new UrlUtils method: appendPreviewURLParameter --- .../src/main/java/org/wordpress/android/util/UrlUtils.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 7ac4dbaf089d..c2529526a898 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -10,7 +10,6 @@ import java.io.UnsupportedEncodingException; import java.net.IDN; import java.net.URI; -import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.Charset; @@ -214,4 +213,8 @@ public static boolean isImageUrl(String url) { return cleanedUrl.endsWith("jpg") || cleanedUrl.endsWith("jpeg") || cleanedUrl.endsWith("gif") || cleanedUrl.endsWith("png"); } + + public static String appendPreviewURLParameter(String url) { + return Uri.parse(url).buildUpon().appendQueryParameter("preview", "true").build().toString(); + } } From 05310091a61a465b6e39ab54d127252130cbed1e Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Wed, 9 Sep 2015 15:35:49 +0200 Subject: [PATCH 03/10] generic approach to add parameters to a URL --- .../wordpress/android/util/UrlUtilsTest.java | 54 +++++++++++++++++++ .../org/wordpress/android/util/UrlUtils.java | 22 +++++--- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java index 7b0bb6beeca7..8174d7004c61 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java @@ -2,6 +2,9 @@ import android.test.InstrumentationTestCase; +import java.util.HashMap; +import java.util.Map; + public class UrlUtilsTest extends InstrumentationTestCase { public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() { assertNotNull(UrlUtils.getDomainFromUrl("")); @@ -17,4 +20,55 @@ public void testGetDomainFromUrlWithHostReturnsHost() { assertTrue(host.equals("www.wordpress.com")); } + + public void testAppendUrlParameter1() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test", "preview", "true"); + assertEquals("http://wp.com/test?preview=true", url); + } + + public void testAppendUrlParameter2() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony", "preview", "true"); + assertEquals("http://wp.com/test?q=pony&preview=true", url); + } + + public void testAppendUrlParameter3() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony#unicorn", "preview", "true"); + assertEquals("http://wp.com/test?q=pony&preview=true#unicorn", url); + } + + public void testAppendUrlParameter4() { + String url = UrlUtils.appendUrlParameter("/relative/test", "preview", "true"); + assertEquals("/relative/test?preview=true", url); + } + + public void testAppendUrlParameter5() { + String url = UrlUtils.appendUrlParameter("/relative/", "preview", "true"); + assertEquals("/relative/?preview=true", url); + } + + public void testAppendUrlParameter6() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test/", "preview", "true"); + assertEquals("http://wp.com/test/?preview=true", url); + } + + public void testAppendUrlParameter7() { + String url = UrlUtils.appendUrlParameter("http://wp.com/test/?q=pony", "preview", "true"); + assertEquals("http://wp.com/test/?q=pony&preview=true", url); + } + + public void testAppendUrlParameters1() { + Map params = new HashMap<>(); + params.put("w", "200"); + params.put("h", "300"); + String url = UrlUtils.appendUrlParameters("http://wp.com/test", params); + assertEquals("http://wp.com/test?h=300&w=200", url); + } + + public void testAppendUrlParameters2() { + Map params = new HashMap<>(); + params.put("h", "300"); + params.put("w", "200"); + String url = UrlUtils.appendUrlParameters("/relative/test", params); + assertEquals("/relative/test?h=300&w=200", url); + } } 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 c2529526a898..834fd8b48c6a 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -13,6 +13,8 @@ import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; public class UrlUtils { public static String urlEncode(final String text) { @@ -143,11 +145,7 @@ public static String removeQuery(final String urlString) { if (urlString == null) { return null; } - int pos = urlString.indexOf("?"); - if (pos == -1) { - return urlString; - } - return urlString.substring(0, pos); + return Uri.parse(urlString).buildUpon().clearQuery().toString(); } /** @@ -214,7 +212,17 @@ public static boolean isImageUrl(String url) { cleanedUrl.endsWith("gif") || cleanedUrl.endsWith("png"); } - public static String appendPreviewURLParameter(String url) { - return Uri.parse(url).buildUpon().appendQueryParameter("preview", "true").build().toString(); + public static String appendUrlParameter(String url, String paramName, String paramValue) { + Map parameters = new HashMap<>(); + parameters.put(paramName, paramValue); + return appendUrlParameters(url, parameters); + } + + public static String appendUrlParameters(String url, Map parameters) { + Uri.Builder uriBuilder = Uri.parse(url).buildUpon(); + for (Map.Entry parameter : parameters.entrySet()) { + uriBuilder.appendQueryParameter(parameter.getKey(), parameter.getValue()); + } + return uriBuilder.build().toString(); } } From fd8a557189aaad9cc13eee519c9008bdb5cfa090 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Thu, 10 Sep 2015 10:03:57 +0200 Subject: [PATCH 04/10] URL Utils test fix --- .../java/org/wordpress/android/util/UrlUtilsTest.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java index 8174d7004c61..b72e94821203 100644 --- a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java @@ -61,7 +61,9 @@ public void testAppendUrlParameters1() { params.put("w", "200"); params.put("h", "300"); String url = UrlUtils.appendUrlParameters("http://wp.com/test", params); - assertEquals("http://wp.com/test?h=300&w=200", url); + if (!url.equals("http://wp.com/test?h=300&w=200") && !url.equals("http://wp.com/test?w=200&h=300")) { + assertTrue("failed test on url: " + url, false); + } } public void testAppendUrlParameters2() { @@ -69,6 +71,8 @@ public void testAppendUrlParameters2() { params.put("h", "300"); params.put("w", "200"); String url = UrlUtils.appendUrlParameters("/relative/test", params); - assertEquals("/relative/test?h=300&w=200", url); + if (!url.equals("/relative/test?h=300&w=200") && !url.equals("/relative/test?w=200&h=300")) { + assertTrue("failed test on url: " + url, false); + } } } From 9b31cb940c14abdafe5d3330c3408281af128f80 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Tue, 15 Sep 2015 12:44:27 +0200 Subject: [PATCH 05/10] Use homeURL instead of hostName to show the blog address. WordPress can be installed in a sub-directory, and so the host name will be the same for two subdir installations on the same host. --- .../org/wordpress/android/util/BlogUtils.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java index 0e07040a97c6..db40f18800e1 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java @@ -8,19 +8,19 @@ public class BlogUtils { public int compare(Object blog1, Object blog2) { Map blogMap1 = (Map) blog1; Map blogMap2 = (Map) blog2; - String blogName1 = getBlogNameOrHostNameFromAccountMap(blogMap1); - String blogName2 = getBlogNameOrHostNameFromAccountMap(blogMap2); + String blogName1 = getBlogNameOrHomeURLFromAccountMap(blogMap1); + String blogName2 = getBlogNameOrHomeURLFromAccountMap(blogMap2); return blogName1.compareToIgnoreCase(blogName2); } }; /** - * Return a blog name or blog url (host part only) if trimmed name is an empty string + * Return a blog name or blog home URL if trimmed name is an empty string */ - public static String getBlogNameOrHostNameFromAccountMap(Map account) { + public static String getBlogNameOrHomeURLFromAccountMap(Map account) { String blogName = getBlogNameFromAccountMap(account); if (blogName.trim().length() == 0) { - blogName = StringUtils.getHost(MapUtils.getMapStr(account, "url")); + blogName = BlogUtils.getHomeURLOrHostNameFromAccountMap(account); } return blogName; } @@ -33,9 +33,18 @@ public static String getBlogNameFromAccountMap(Map account) { } /** - * Return blog url (host part only) if trimmed name is an empty string + * Return the blog home URL setting or the host name if home URL is an empty string. */ - public static String getHostNameFromAccountMap(Map account) { - return StringUtils.getHost(MapUtils.getMapStr(account, "url")); + public static String getHomeURLOrHostNameFromAccountMap(Map account) { + String homeURL = MapUtils.getMapStr(account, "homeURL").replace("http://", "").replace("https://", "").trim(); + if (homeURL.endsWith("/")) { + homeURL = homeURL.substring(0, homeURL.length() -1); + } + + if (homeURL.length() == 0) { + return StringUtils.getHost(MapUtils.getMapStr(account, "url")); + } + + return homeURL; } } From f53bd94dbdcb3e4f84687ccd3db5f8fb8a036e92 Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Tue, 15 Sep 2015 18:11:41 +0200 Subject: [PATCH 06/10] Add two new functions to Utils that remove the traling slash in a string, and remove the scheme from URL string. --- .../org/wordpress/android/util/BlogUtils.java | 6 ++---- .../wordpress/android/util/StringUtils.java | 8 ++++++++ .../org/wordpress/android/util/UrlUtils.java | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java index db40f18800e1..685aabf9ec53 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/BlogUtils.java @@ -36,10 +36,8 @@ public static String getBlogNameFromAccountMap(Map account) { * Return the blog home URL setting or the host name if home URL is an empty string. */ public static String getHomeURLOrHostNameFromAccountMap(Map account) { - String homeURL = MapUtils.getMapStr(account, "homeURL").replace("http://", "").replace("https://", "").trim(); - if (homeURL.endsWith("/")) { - homeURL = homeURL.substring(0, homeURL.length() -1); - } + String homeURL = UrlUtils.removeScheme(MapUtils.getMapStr(account, "homeURL")); + homeURL = StringUtils.removeTrailingSlash(homeURL); if (homeURL.length() == 0) { return StringUtils.getHost(MapUtils.getMapStr(account, "url")); diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java index 68e3113c3d5d..136cc4e541a4 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java @@ -180,6 +180,14 @@ public static String capitalize(final String str) { return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString(); } + public static String removeTrailingSlash(final String str) { + if (TextUtils.isEmpty(str) || !str.endsWith("/")) { + return str; + } + + return str.substring(0, str.length() -1); + } + /* * Wrap an image URL in a photon URL * Check out http://developer.wordpress.com/docs/photon/ 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 834fd8b48c6a..f4b5756d25ee 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java @@ -138,6 +138,25 @@ public static String normalizeUrl(final String urlString) { } } + + /** + * returns the passed url without the scheme + */ + public static String removeScheme(final String urlString) { + if (urlString == null) { + return null; + } + + int doubleslash = urlString.indexOf("//"); + if (doubleslash == -1) { + doubleslash = 0; + } else { + doubleslash += 2; + } + + return urlString.substring(doubleslash, urlString.length()); + } + /** * returns the passed url without the query parameters */ From 696c325d7ac89570f58c7e2e480dcb57f51569c4 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Thu, 17 Sep 2015 11:18:52 +0200 Subject: [PATCH 07/10] update ProfilingUtils usage --- .../org/wordpress/android/util/ProfilingUtils.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java index 991c7680b478..4660a3500b25 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/ProfilingUtils.java @@ -28,6 +28,10 @@ public static void dump() { getInstance().dumpToLog(); } + public static void stop() { + getInstance().reset(null); + } + private static ProfilingUtils getInstance() { if (sInstance == null) { sInstance = new ProfilingUtils(); @@ -56,12 +60,18 @@ public void reset() { } public void addSplit(String splitLabel) { + if (mLabel == null) { + return; + } long now = SystemClock.elapsedRealtime(); mSplits.add(now); mSplitLabels.add(splitLabel); } public void dumpToLog() { + if (mLabel == null) { + return; + } AppLog.d(T.PROFILING, mLabel + ": begin"); final long first = mSplits.get(0); long now = first; From 1735ccbf43b5cbb48933635340fd63fd0ab01a58 Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Mon, 28 Sep 2015 15:08:05 +0200 Subject: [PATCH 08/10] check null source in JSONUtils.queryJSON() --- .../java/org/wordpress/android/util/JSONUtils.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java index 4c92478cb4d7..6df9e0b0aa9d 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java @@ -8,7 +8,6 @@ import org.wordpress.android.util.AppLog.T; import java.util.ArrayList; -import java.util.Iterator; public class JSONUtils { private static String QUERY_SEPERATOR="."; @@ -25,6 +24,10 @@ public class JSONUtils { * traverse the object graph and pull out the desired property */ public static U queryJSON(JSONObject source, String query, U defaultObject) { + if (source == null) { + AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); + return defaultObject; + } int nextSeperator = query.indexOf(QUERY_SEPERATOR); int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START); if (nextSeperator == -1 && nextIndexStart == -1) { @@ -89,7 +92,11 @@ public static U queryJSON(JSONObject source, String query, U defaultObject) * Acceptable indexes include negative numbers to reference items from the end of * the list as well as "last" and "first" as more explicit references to "0" and "-1" */ - public static U queryJSON(JSONArray source, String query, U defaultObject){ + public static U queryJSON(JSONArray source, String query, U defaultObject) { + if (source == null) { + AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); + return defaultObject; + } // query must start with [ have an index and then have ] int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START); int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END); From a9b52cee6beb66559b6e74c3d896a5595b7219ec Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Tue, 29 Sep 2015 09:01:33 +0200 Subject: [PATCH 09/10] Check null query string --- .../org/wordpress/android/util/JSONUtils.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java index 6df9e0b0aa9d..8738acd24384 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtils.java @@ -10,15 +10,15 @@ import java.util.ArrayList; public class JSONUtils { - private static String QUERY_SEPERATOR="."; - private static String QUERY_ARRAY_INDEX_START="["; - private static String QUERY_ARRAY_INDEX_END="]"; - private static String QUERY_ARRAY_FIRST="first"; - private static String QUERY_ARRAY_LAST="last"; + private static String QUERY_SEPERATOR = "."; + private static String QUERY_ARRAY_INDEX_START = "["; + private static String QUERY_ARRAY_INDEX_END = "]"; + private static String QUERY_ARRAY_FIRST = "first"; + private static String QUERY_ARRAY_LAST = "last"; private static final String JSON_NULL_STR = "null"; + private static final String TAG = "JSONUtils"; - private static final String TAG="JSONUtils"; /** * Given a JSONObject and a key path (e.g property.child) and a default it will * traverse the object graph and pull out the desired property @@ -28,6 +28,10 @@ public static U queryJSON(JSONObject source, String query, U defaultObject) AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); return defaultObject; } + if (query == null) { + AppLog.e(T.UTILS, "Parameter query is null"); + return defaultObject; + } int nextSeperator = query.indexOf(QUERY_SEPERATOR); int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START); if (nextSeperator == -1 && nextIndexStart == -1) { @@ -59,9 +63,6 @@ public static U queryJSON(JSONObject source, String query, U defaultObject) String nextQuery = query.substring(endQuery); String key = query.substring(0, endQuery); try { - if (source == null) { - return defaultObject; - } if (nextQuery.indexOf(QUERY_SEPERATOR) == 0) { return queryJSON(source.getJSONObject(key), nextQuery.substring(1), defaultObject); } else if (nextQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) { @@ -97,6 +98,10 @@ public static U queryJSON(JSONArray source, String query, U defaultObject) { AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); return defaultObject; } + if (query == null) { + AppLog.e(T.UTILS, "Parameter query is null"); + return defaultObject; + } // query must start with [ have an index and then have ] int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START); int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END); From 8f65aa2580c106a56114f25d2770be1391c7eddd Mon Sep 17 00:00:00 2001 From: Maxime Biais Date: Tue, 29 Sep 2015 09:02:11 +0200 Subject: [PATCH 10/10] test queryJSON calls with null params --- .../wordpress/android/util/JSONUtilsTest.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 WordPressUtils/src/androidTest/java/org/wordpress/android/util/JSONUtilsTest.java diff --git a/WordPressUtils/src/androidTest/java/org/wordpress/android/util/JSONUtilsTest.java b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/JSONUtilsTest.java new file mode 100644 index 000000000000..f7c747ff7024 --- /dev/null +++ b/WordPressUtils/src/androidTest/java/org/wordpress/android/util/JSONUtilsTest.java @@ -0,0 +1,32 @@ +package org.wordpress.android.util; + +import android.test.InstrumentationTestCase; + +import org.json.JSONArray; +import org.json.JSONObject; + +public class JSONUtilsTest extends InstrumentationTestCase { + public void testQueryJSONNullSource1() { + JSONUtils.queryJSON((JSONObject) null, "", ""); + } + + public void testQueryJSONNullSource2() { + JSONUtils.queryJSON((JSONArray) null, "", ""); + } + + public void testQueryJSONNullQuery1() { + JSONUtils.queryJSON(new JSONObject(), null, ""); + } + + public void testQueryJSONNullQuery2() { + JSONUtils.queryJSON(new JSONArray(), null, ""); + } + + public void testQueryJSONNullReturnValue1() { + JSONUtils.queryJSON(new JSONObject(), "", null); + } + + public void testQueryJSONNullReturnValue2() { + JSONUtils.queryJSON(new JSONArray(), "", null); + } +}