Skip to content

Commit

Permalink
Fixes square#2522 - Format String using Locale.US.
Browse files Browse the repository at this point in the history
  • Loading branch information
venilnoronha committed Apr 29, 2016
1 parent 48ff30b commit 4b0c09e
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ private void handleWebSocketUpgrade(Socket socket, BufferedSource source, Buffer

ThreadPoolExecutor replyExecutor =
new ThreadPoolExecutor(1, 1, 1, SECONDS, new LinkedBlockingDeque<Runnable>(),
Util.threadFactory(String.format("MockWebServer %s WebSocket", request.getPath()),
Util.threadFactory(Util.format("MockWebServer %s WebSocket", request.getPath()),
true));
replyExecutor.allowCoreThreadTimeOut(true);
final RealWebSocket webSocket =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import okhttp3.Headers;
import okhttp3.internal.Util;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -231,8 +232,8 @@ public final class MockWebServerTest {
long elapsedNanos = System.nanoTime() - startNanos;
long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos);

assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500);
assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000);
assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500);
assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000);
}

/**
Expand All @@ -257,8 +258,8 @@ public final class MockWebServerTest {
long elapsedNanos = System.nanoTime() - startNanos;
long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos);

assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500);
assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000);
assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 500);
assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis < 1000);
}

/** Delay the response body by sleeping 1s. */
Expand All @@ -273,7 +274,7 @@ public final class MockWebServerTest {
assertEquals('A', in.read());
long elapsedNanos = System.nanoTime() - startNanos;
long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos);
assertTrue(String.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 1000);
assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 1000);

in.close();
}
Expand Down
3 changes: 2 additions & 1 deletion okcurl/src/main/java/okhttp3/curl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.internal.Util;
import okhttp3.internal.framed.Http2;
import okhttp3.internal.http.StatusLine;
import okio.BufferedSource;
Expand Down Expand Up @@ -277,7 +278,7 @@ private static void enableHttp2FrameLogging() {
handler.setLevel(Level.FINE);
handler.setFormatter(new SimpleFormatter() {
@Override public String format(LogRecord record) {
return String.format("%s%n", record.getMessage());
return Util.format("%s%n", record.getMessage());
}
});
frameLogger.addHandler(handler);
Expand Down
2 changes: 1 addition & 1 deletion okhttp-tests/src/test/java/okhttp3/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exc
// Timed out as expected.
long elapsedNanos = System.nanoTime() - startNanos;
long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(elapsedNanos);
assertTrue(String.format("Timed out: %sms", elapsedMillis), elapsedMillis < 500);
assertTrue(Util.format("Timed out: %sms", elapsedMillis), elapsedMillis < 500);
} finally {
bodySource.close();
}
Expand Down
2 changes: 1 addition & 1 deletion okhttp-tests/src/test/java/okhttp3/URLConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ private void testEarlyDisconnectDoesntHarmPooling(TransferKind transferKind) thr
// If we're working correctly, this should be greater than 100ms, but less than double that.
// Previously we had a bug where we would download the entire response body as long as no
// individual read took longer than 100ms.
assertTrue(String.format("Time to close: %sms", elapsedMillis), elapsedMillis < 500);
assertTrue(Util.format("Time to close: %sms", elapsedMillis), elapsedMillis < 500);

// Do another request to confirm that the discarded connection was not pooled.
assertContent("A", urlFactory.open(server.url("/").url()));
Expand Down
21 changes: 11 additions & 10 deletions okhttp-tests/src/test/java/okhttp3/UrlComponentEncodingTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import okhttp3.internal.Util;
import okio.Buffer;
import okio.ByteString;

Expand Down Expand Up @@ -231,7 +232,7 @@ private void testParseAlreadyEncoded(int codePoint, Encoding encoding, Component
String urlString = component.urlString(encoded);
HttpUrl url = HttpUrl.parse(urlString);
if (!component.encodedValue(url).equals(encoded)) {
fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding));
}
}

Expand All @@ -242,7 +243,7 @@ private void testEncodeAndDecode(int codePoint, Component component) {
HttpUrl url = builder.build();
String actual = component.get(url);
if (!expected.equals(actual)) {
fail(String.format("Roundtrip %s %#x %s", component, codePoint, url));
fail(Util.format("Roundtrip %s %#x %s", component, codePoint, url));
}
}

Expand All @@ -255,7 +256,7 @@ private void testParseOriginal(int codePoint, Encoding encoding, Component compo

String s = component.encodedValue(url);
if (!s.equals(encoded)) {
fail(String.format("Encoding %s %#02x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#02x using %s", component, codePoint, encoding));
}
}

Expand All @@ -264,7 +265,7 @@ private void testToUrl(int codePoint, Encoding encoding, Component component) {
HttpUrl httpUrl = HttpUrl.parse(component.urlString(encoded));
URL javaNetUrl = httpUrl.url();
if (!javaNetUrl.toString().equals(javaNetUrl.toString())) {
fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding));
}
}

Expand All @@ -273,7 +274,7 @@ private void testFromUrl(int codePoint, Encoding encoding, Component component)
HttpUrl httpUrl = HttpUrl.parse(component.urlString(encoded));
HttpUrl toAndFromJavaNetUrl = HttpUrl.get(httpUrl.url());
if (!toAndFromJavaNetUrl.equals(httpUrl)) {
fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding));
}
}

Expand All @@ -287,18 +288,18 @@ private void testUri(
if (uriEscaped) {
// The URI has more escaping than the HttpURL. Check that the decoded values still match.
if (uri.toString().equals(httpUrl.toString())) {
fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding));
}
if (!component.get(toAndFromUri).equals(string)) {
fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding));
}
} else {
// Check that the URI and HttpURL have the exact same escaping.
if (!toAndFromUri.equals(httpUrl)) {
fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding));
}
if (!uri.toString().equals(httpUrl.toString())) {
fail(String.format("Encoding %s %#x using %s", component, codePoint, encoding));
fail(Util.format("Encoding %s %#x using %s", component, codePoint, encoding));
}
}
}
Expand All @@ -315,7 +316,7 @@ public String encode(int codePoint) {
ByteString utf8 = ByteString.encodeUtf8(IDENTITY.encode(codePoint));
Buffer percentEncoded = new Buffer();
for (int i = 0; i < utf8.size(); i++) {
percentEncoded.writeUtf8(String.format("%%%02X", utf8.getByte(i) & 0xff));
percentEncoded.writeUtf8(Util.format("%%%02X", utf8.getByte(i) & 0xff));
}
return percentEncoded.readUtf8();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.internal.Util;
import okio.Buffer;
import okio.BufferedSource;

Expand Down Expand Up @@ -81,7 +82,7 @@ private void set(String name, String value) {
}

@Override public String toString() {
return String.format("Parsing: <%s> against <%s>", input, base);
return Util.format("Parsing: <%s> against <%s>", input, base);
}

public static List<WebPlatformUrlTestData> load(BufferedSource source) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import okhttp3.internal.Util;

import static org.junit.Assert.assertEquals;

Expand All @@ -45,7 +46,7 @@ public void assertRequests(URI... expectedUris) {
@Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
InetSocketAddress socketAddress = (InetSocketAddress) sa;
failures.add(
String.format("%s %s:%d %s", uri, socketAddress.getHostName(), socketAddress.getPort(),
Util.format("%s %s:%d %s", uri, socketAddress.getHostName(), socketAddress.getPort(),
ioe.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import okhttp3.ResponseBody;
import okhttp3.internal.Util;
import okhttp3.ws.WebSocketRecorder;
import okhttp3.ws.WebSocketRecorder.MessageDelegate;
import okio.Buffer;
Expand Down Expand Up @@ -385,7 +386,7 @@ public final class WebSocketReaderTest {
data.write(ByteString.decodeHex("880203ed")); // Close with code 1005
data.write(ByteString.decodeHex("880203ee")); // Close with code 1006
for (int i = 1012; i <= 2999; i++) {
data.write(ByteString.decodeHex("8802" + String.format("%04X", i))); // Close with code 'i'
data.write(ByteString.decodeHex("8802" + Util.format("%04X", i))); // Close with code 'i'
}

int count = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.Random;
import okhttp3.RequestBody;
import okhttp3.internal.Util;
import okio.Buffer;
import okio.BufferedSink;
import okio.ByteString;
Expand Down Expand Up @@ -99,7 +100,7 @@ public final class WebSocketWriterTest {
sink.close();

assertData("817e");
assertData(String.format("%04x", length));
assertData(Util.format("%04x", length));
assertData(bytes);
assertTrue(data.exhausted());
}
Expand Down Expand Up @@ -199,7 +200,7 @@ public final class WebSocketWriterTest {
// Write directly to the unbuffered sink. This ensures it will become single frame.
sink.write(payload.clone(), byteCount);
assertData("027e"); // 'e' == 4-byte follow-up length.
assertData(String.format("%04X", payload.completeSegmentByteCount()));
assertData(Util.format("%04X", payload.completeSegmentByteCount()));
assertData(payload.readByteArray());

sink.close();
Expand All @@ -219,7 +220,7 @@ public final class WebSocketWriterTest {
// Write directly to the unbuffered sink. This ensures it will become single frame.
sink.write(payload.clone(), byteCount);
assertData("027f"); // 'f' == 16-byte follow-up length.
assertData(String.format("%016X", byteCount));
assertData(Util.format("%016X", byteCount));
assertData(payload.readByteArray(byteCount));

sink.close();
Expand Down
2 changes: 1 addition & 1 deletion okhttp-ws/src/main/java/okhttp3/ws/WebSocketCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ static RealWebSocket create(StreamAllocation streamAllocation, Response response
String url = response.request().url().toString();
ThreadPoolExecutor replyExecutor =
new ThreadPoolExecutor(1, 1, 1, SECONDS, new LinkedBlockingDeque<Runnable>(),
Util.threadFactory(String.format("OkHttp %s WebSocket", url), true));
Util.threadFactory(Util.format("OkHttp %s WebSocket", url), true));
replyExecutor.allowCoreThreadTimeOut(true);

return new StreamWebSocket(streamAllocation, random, replyExecutor, listener, url);
Expand Down
5 changes: 3 additions & 2 deletions okhttp/src/main/java/okhttp3/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import okhttp3.internal.Util;
import okhttp3.internal.http.HttpDate;

/**
Expand Down Expand Up @@ -271,15 +272,15 @@ private void checkNameAndValue(String name, String value) {
for (int i = 0, length = name.length(); i < length; i++) {
char c = name.charAt(i);
if (c <= '\u001f' || c >= '\u007f') {
throw new IllegalArgumentException(String.format(
throw new IllegalArgumentException(Util.format(
"Unexpected char %#04x at %d in header name: %s", (int) c, i, name));
}
}
if (value == null) throw new NullPointerException("value == null");
for (int i = 0, length = value.length(); i < length; i++) {
char c = value.charAt(i);
if (c <= '\u001f' || c >= '\u007f') {
throw new IllegalArgumentException(String.format(
throw new IllegalArgumentException(Util.format(
"Unexpected char %#04x at %d in %s value: %s", (int) c, i, name, value));
}
}
Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/java/okhttp3/internal/NamedRunnable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class NamedRunnable implements Runnable {
protected final String name;

public NamedRunnable(String format, Object... args) {
this.name = String.format(format, args);
this.name = Util.format(format, args);
}

@Override public final void run() {
Expand Down
5 changes: 5 additions & 0 deletions okhttp/src/main/java/okhttp3/internal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,4 +446,9 @@ private static boolean containsInvalidHostnameAsciiCodes(String hostnameAscii) {
public static boolean verifyAsIpAddress(String host) {
return VERIFY_AS_IP_ADDRESS.matcher(host).matches();
}

/** Returns a {@link Locale#US} formatted {@link String}. */
public static String format(String format, Object... args) {
return String.format(Locale.US, format, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private FramedConnection(Builder builder) throws IOException {
// Like newSingleThreadExecutor, except lazy creates the thread.
pushExecutor = new ThreadPoolExecutor(0, 1, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
Util.threadFactory(String.format("OkHttp %s Push Observer", hostname), true));
Util.threadFactory(Util.format("OkHttp %s Push Observer", hostname), true));
// 1 less than SPDY http://tools.ietf.org/html/draft-ietf-httpbis-http2-17#section-6.9.2
peerSettings.set(Settings.INITIAL_WINDOW_SIZE, 0, 65535);
peerSettings.set(Settings.MAX_FRAME_SIZE, 0, Http2.INITIAL_MAX_FRAME_SIZE);
Expand Down
3 changes: 2 additions & 1 deletion okhttp/src/main/java/okhttp3/internal/framed/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package okhttp3.internal.framed;

import okhttp3.internal.Util;
import okio.ByteString;

/** HTTP header: the name is an ASCII string, but the value can be UTF-8. */
Expand Down Expand Up @@ -66,6 +67,6 @@ public Header(ByteString name, ByteString value) {
}

@Override public String toString() {
return String.format("%s: %s", name.utf8(), value.utf8());
return Util.format("%s: %s", name.utf8(), value.utf8());
}
}
2 changes: 1 addition & 1 deletion okhttp/src/main/java/okhttp3/internal/framed/Http2.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import okio.Source;
import okio.Timeout;

import static java.lang.String.format;
import static java.util.logging.Level.FINE;
import static okhttp3.internal.Util.format;
import static okhttp3.internal.framed.Http2.FrameLogger.formatHeader;
import static okio.ByteString.EMPTY;

Expand Down
2 changes: 1 addition & 1 deletion okhttp/src/main/java/okhttp3/internal/framed/Spdy3.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private void readSettings(Handler handler, int flags, int length) throws IOExcep
}

private static IOException ioException(String message, Object... args) throws IOException {
throw new IOException(String.format(message, args));
throw new IOException(Util.format(message, args));
}

@Override public void close() throws IOException {
Expand Down

0 comments on commit 4b0c09e

Please sign in to comment.