Prefer Charsets.UTF_8 over string literal

Found with modernizer-maven-plugin.
This commit is contained in:
Andrew Gaul 2014-09-20 18:30:56 -07:00
parent 2db654fbe3
commit dc3011469b
3 changed files with 24 additions and 21 deletions

View File

@ -34,6 +34,7 @@ import org.jclouds.ContextBuilder;
import org.jclouds.concurrent.config.ExecutorServiceModule; import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.util.Strings2; import org.jclouds.util.Strings2;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.net.HttpHeaders; import com.google.common.net.HttpHeaders;
@ -223,7 +224,7 @@ public class BaseOpenStackMockTest<A extends Closeable> {
JsonElement requestJson = null; // to be compared JsonElement requestJson = null; // to be compared
JsonElement resourceJson; // to be compared JsonElement resourceJson; // to be compared
try { try {
requestJson = parser.parse(new String(request.getBody(), "UTF-8")); requestJson = parser.parse(new String(request.getBody(), Charsets.UTF_8));
} catch (Exception e) { } catch (Exception e) {
Throwables.propagate(e); Throwables.propagate(e);
} }

View File

@ -42,6 +42,7 @@ import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
@ -299,7 +300,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
String result = client.post("", "foo"); String result = client.post("", "foo");
// Verify that the body is properly populated // Verify that the body is properly populated
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
assertEquals(result, "fooPOST"); assertEquals(result, "fooPOST");
} finally { } finally {
close(client, true); close(client, true);
@ -315,7 +316,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
client.postNothing(""); client.postNothing("");
assertEquals(server.getRequestCount(), 1); assertEquals(server.getRequestCount(), 1);
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), ""); assertEquals(new String(request.getBody(), Charsets.UTF_8), "");
} finally { } finally {
close(client, true); close(client, true);
server.shutdown(); server.shutdown();
@ -333,9 +334,9 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
assertEquals(result, "fooPOST"); assertEquals(result, "fooPOST");
// Verify that the body was properly sent in the two requests // Verify that the body was properly sent in the two requests
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
request = server.takeRequest(); request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
} finally { } finally {
close(client, true); close(client, true);
server.shutdown(); server.shutdown();
@ -356,9 +357,9 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
assertEquals(redirectTarget.getRequestCount(), 1); assertEquals(redirectTarget.getRequestCount(), 1);
// Verify that the body was populated after the redirect // Verify that the body was populated after the redirect
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
request = redirectTarget.takeRequest(); request = redirectTarget.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
} finally { } finally {
close(client, true); close(client, true);
redirectTarget.shutdown(); redirectTarget.shutdown();
@ -374,7 +375,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
String result = client.postAsInputStream("", "foo"); String result = client.postAsInputStream("", "foo");
// Verify that the body is properly populated // Verify that the body is properly populated
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
assertEquals(result, "fooPOST"); assertEquals(result, "fooPOST");
} finally { } finally {
close(client, true); close(client, true);
@ -405,7 +406,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
String result = client.postJson("", "foo"); String result = client.postJson("", "foo");
// Verify that the body is properly populated // Verify that the body is properly populated
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "{\"key\":\"foo\"}"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "{\"key\":\"foo\"}");
assertEquals(result, "fooPOSTJSON"); assertEquals(result, "fooPOSTJSON");
} finally { } finally {
close(client, true); close(client, true);
@ -491,7 +492,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
String result = client.upload("", "foo"); String result = client.upload("", "foo");
// Verify that the body is properly populated // Verify that the body is properly populated
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
assertEquals(result, "fooPUT"); assertEquals(result, "fooPUT");
} finally { } finally {
close(client, true); close(client, true);
@ -513,9 +514,9 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
assertEquals(redirectTarget.getRequestCount(), 1); assertEquals(redirectTarget.getRequestCount(), 1);
// Verify that the body was populated after the redirect // Verify that the body was populated after the redirect
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
request = redirectTarget.takeRequest(); request = redirectTarget.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
} finally { } finally {
close(client, true); close(client, true);
redirectTarget.shutdown(); redirectTarget.shutdown();
@ -531,7 +532,7 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
client.putNothing(""); client.putNothing("");
assertEquals(server.getRequestCount(), 1); assertEquals(server.getRequestCount(), 1);
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), ""); assertEquals(new String(request.getBody(), Charsets.UTF_8), "");
} finally { } finally {
close(client, true); close(client, true);
server.shutdown(); server.shutdown();
@ -549,9 +550,9 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
assertEquals(result, "fooPUT"); assertEquals(result, "fooPUT");
// Verify that the body was properly sent in the two requests // Verify that the body was properly sent in the two requests
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
request = server.takeRequest(); request = server.takeRequest();
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
} finally { } finally {
close(client, true); close(client, true);
server.shutdown(); server.shutdown();

View File

@ -36,6 +36,7 @@ import org.jclouds.rest.annotations.PATCH;
import org.jclouds.rest.binders.BindToStringPayload; import org.jclouds.rest.binders.BindToStringPayload;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.inject.Module; import com.google.inject.Module;
import com.squareup.okhttp.mockwebserver.MockResponse; import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer; import com.squareup.okhttp.mockwebserver.MockWebServer;
@ -80,7 +81,7 @@ public class OkHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorSer
// Verify that the body is properly populated // Verify that the body is properly populated
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(request.getMethod(), "PATCH"); assertEquals(request.getMethod(), "PATCH");
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
assertEquals(result, "fooPATCH"); assertEquals(result, "fooPATCH");
} finally { } finally {
close(api, true); close(api, true);
@ -100,10 +101,10 @@ public class OkHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorSer
// Verify that the body was properly sent in the two requests // Verify that the body was properly sent in the two requests
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(request.getMethod(), "PATCH"); assertEquals(request.getMethod(), "PATCH");
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
request = server.takeRequest(); request = server.takeRequest();
assertEquals(request.getMethod(), "PATCH"); assertEquals(request.getMethod(), "PATCH");
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
} finally { } finally {
close(api, true); close(api, true);
server.shutdown(); server.shutdown();
@ -125,10 +126,10 @@ public class OkHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorSer
// Verify that the body was populated after the redirect // Verify that the body was populated after the redirect
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(request.getMethod(), "PATCH"); assertEquals(request.getMethod(), "PATCH");
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
request = redirectTarget.takeRequest(); request = redirectTarget.takeRequest();
assertEquals(request.getMethod(), "PATCH"); assertEquals(request.getMethod(), "PATCH");
assertEquals(new String(request.getBody(), "UTF-8"), "foo"); assertEquals(new String(request.getBody(), Charsets.UTF_8), "foo");
} finally { } finally {
close(api, true); close(api, true);
redirectTarget.shutdown(); redirectTarget.shutdown();
@ -145,7 +146,7 @@ public class OkHttpCommandExecutorServiceTest extends BaseHttpCommandExecutorSer
assertEquals(server.getRequestCount(), 1); assertEquals(server.getRequestCount(), 1);
RecordedRequest request = server.takeRequest(); RecordedRequest request = server.takeRequest();
assertEquals(request.getMethod(), "PATCH"); assertEquals(request.getMethod(), "PATCH");
assertEquals(new String(request.getBody(), "UTF-8"), ""); assertEquals(new String(request.getBody(), Charsets.UTF_8), "");
} finally { } finally {
close(api, true); close(api, true);
server.shutdown(); server.shutdown();