Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed unmotivated throwing of UnsupportedEncodingException #75

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand All @@ -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;
}
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/com/auth0/client/mgmt/filter/QueryFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ 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);
}
}

/**
Expand Down Expand Up @@ -59,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");
}

}
Original file line number Diff line number Diff line change
@@ -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.
*/
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}

@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);
}

}