From 77f936dccdbdf5facddabf1e8043689304a7edd5 Mon Sep 17 00:00:00 2001 From: Johannes Schneider Date: Tue, 5 Sep 2017 08:14:26 +0200 Subject: [PATCH 1/2] removed unmotivated throwing of UnsupportedEncodingException --- .../client/mgmt/filter/LogEventFilter.java | 4 +-- .../auth0/client/mgmt/filter/QueryFilter.java | 19 +++++++++++--- .../auth0/client/mgmt/filter/UserFilter.java | 4 +-- .../client/mgmt/filter/QueryFilterTest.java | 25 ++++++++++++++++++- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/auth0/client/mgmt/filter/LogEventFilter.java b/src/main/java/com/auth0/client/mgmt/filter/LogEventFilter.java index dd265c1e..1b3e04b3 100644 --- a/src/main/java/com/auth0/client/mgmt/filter/LogEventFilter.java +++ b/src/main/java/com/auth0/client/mgmt/filter/LogEventFilter.java @@ -1,7 +1,5 @@ package com.auth0.client.mgmt.filter; -import java.io.UnsupportedEncodingException; - /** * Class used to filter the results received when calling the Logs endpoint. Related to the {@link com.auth0.client.mgmt.LogEventsEntity} entity. */ @@ -27,7 +25,7 @@ public LogEventFilter withTotals(boolean includeTotals) { } @Override - public LogEventFilter withQuery(String query) throws UnsupportedEncodingException { + public LogEventFilter withQuery(String query) { super.withQuery(query); return this; } diff --git a/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java b/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java index b8a06db4..def7abe3 100644 --- a/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java +++ b/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java @@ -13,10 +13,21 @@ public class QueryFilter extends FieldsFilter { * @param query the query expression using the following syntax https://auth0.com/docs/api/management/v2/query-string-syntax. * @return this filter instance */ - public QueryFilter withQuery(String query) throws UnsupportedEncodingException { - String encodedQuery = URLEncoder.encode(query, "UTF-8"); - parameters.put(KEY_QUERY, encodedQuery); - return this; + public QueryFilter withQuery(String query) { + try { + String encodedQuery = urlEncode(query); + parameters.put(KEY_QUERY, encodedQuery); + return this; + } catch (UnsupportedEncodingException ex) { + //"Every implementation of the Java platform is required to support the following standard charsets [...]: UTF-8" + //cf. https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html + throw new IllegalStateException("UTF-8 encoding not supported by current Java platform implementation.", ex); + } + + } + + String urlEncode(String query) throws UnsupportedEncodingException { + return URLEncoder.encode(query, "UTF-8"); } /** diff --git a/src/main/java/com/auth0/client/mgmt/filter/UserFilter.java b/src/main/java/com/auth0/client/mgmt/filter/UserFilter.java index 33141bbc..7315452b 100644 --- a/src/main/java/com/auth0/client/mgmt/filter/UserFilter.java +++ b/src/main/java/com/auth0/client/mgmt/filter/UserFilter.java @@ -1,7 +1,5 @@ package com.auth0.client.mgmt.filter; -import java.io.UnsupportedEncodingException; - /** * Class used to filter the results received when calling the Users endpoint. Related to the {@link com.auth0.client.mgmt.UsersEntity} entity. */ @@ -21,7 +19,7 @@ public UserFilter withTotals(boolean includeTotals) { } @Override - public UserFilter withQuery(String query) throws UnsupportedEncodingException { + public UserFilter withQuery(String query) { super.withQuery(query); return this; } diff --git a/src/test/java/com/auth0/client/mgmt/filter/QueryFilterTest.java b/src/test/java/com/auth0/client/mgmt/filter/QueryFilterTest.java index 782bc1b5..9df799a8 100644 --- a/src/test/java/com/auth0/client/mgmt/filter/QueryFilterTest.java +++ b/src/test/java/com/auth0/client/mgmt/filter/QueryFilterTest.java @@ -6,10 +6,21 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.Matchers.isA; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; + +import java.io.UnsupportedEncodingException; + +import org.junit.Rule; +import org.junit.rules.ExpectedException; public class QueryFilterTest { + @Rule + public ExpectedException exception = ExpectedException.none(); + private QueryFilter filter; @Before @@ -73,4 +84,16 @@ public void shouldFilterWithoutFields() throws Exception { assertThat(filter.getAsMap(), Matchers.hasEntry("fields", (Object) "a,b,c")); assertThat(filter.getAsMap(), Matchers.hasEntry("include_fields", (Object) false)); } -} \ No newline at end of file + + @Test + public void shouldThrowIllegalStateExceptionWhenUtf8IsNotSupported() throws Exception { + exception.expect(IllegalStateException.class); + exception.expectMessage("UTF-8 encoding not supported by current Java platform implementation."); + exception.expectCause(isA(UnsupportedEncodingException.class)); + String value = "my test value"; + QueryFilter filter = spy(new QueryFilter()); + doThrow(UnsupportedEncodingException.class).when(filter).urlEncode(value); + filter.withQuery(value); + } + +} From fa55a13acbb7f37115cf4091f3e9830435ff079c Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Fri, 8 Sep 2017 10:17:18 -0300 Subject: [PATCH 2/2] add "visible for testing" note --- .../java/com/auth0/client/mgmt/filter/QueryFilter.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java b/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java index def7abe3..930f9fdd 100644 --- a/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java +++ b/src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java @@ -23,11 +23,6 @@ public QueryFilter withQuery(String query) { //cf. https://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html throw new IllegalStateException("UTF-8 encoding not supported by current Java platform implementation.", ex); } - - } - - String urlEncode(String query) throws UnsupportedEncodingException { - return URLEncoder.encode(query, "UTF-8"); } /** @@ -70,5 +65,10 @@ public QueryFilter withFields(String fields, boolean includeFields) { super.withFields(fields, includeFields); return this; } + + //Visible for testing + String urlEncode(String query) throws UnsupportedEncodingException { + return URLEncoder.encode(query, "UTF-8"); + } }