Skip to content

Commit

Permalink
correctly format Strings with ENGLISH locale
Browse files Browse the repository at this point in the history
to be independent of user locale settings
  • Loading branch information
msbt committed Mar 30, 2015
1 parent 17dc057 commit 9b073a4
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 29 deletions.
4 changes: 3 additions & 1 deletion blob/src/main/java/io/crate/blob/BlobWriteException.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@

import org.elasticsearch.ElasticsearchException;

import java.util.Locale;

public class BlobWriteException extends ElasticsearchException {

public BlobWriteException(String digest, long size, Throwable cause) {
super(String.format("digest: %s size: %s", digest, size), cause);
super(String.format(Locale.ENGLISH, "digest: %s size: %s", digest, size), cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected String blobUri(String index, String digest){

protected CloseableHttpResponse put(String uri, String body) throws IOException {

HttpPut httpPut = new HttpPut(String.format("http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
HttpPut httpPut = new HttpPut(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
if(body != null){
StringEntity bodyEntity = new StringEntity(body);
httpPut.setEntity(bodyEntity);
Expand Down Expand Up @@ -117,7 +118,7 @@ public void run() {
Integer statusCode = res.getStatusLine().getStatusCode();
String resultContent = EntityUtils.toString(res.getEntity());
if (!resultContent.equals(expected)) {
logger.warn(String.format("incorrect response %d -- length: %d expected: %d\n",
logger.warn(String.format(Locale.ENGLISH, "incorrect response %d -- length: %d expected: %d\n",
indexerId, resultContent.length(), expected.length()));
}
results.put(indexerId, (statusCode >= 200 && statusCode < 300 && expected.equals(resultContent)));
Expand All @@ -140,20 +141,20 @@ public boolean apply(Boolean input) {
}

protected CloseableHttpResponse get(String uri, Header[] headers) throws IOException {
HttpGet httpGet = new HttpGet(String.format("http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
HttpGet httpGet = new HttpGet(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
if(headers != null){
httpGet.setHeaders(headers);
}
return httpClient.execute(httpGet);
}

protected CloseableHttpResponse head(String uri) throws IOException {
HttpHead httpHead = new HttpHead(String.format("http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
HttpHead httpHead = new HttpHead(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
return httpClient.execute(httpHead);
}

protected CloseableHttpResponse delete(String uri) throws IOException {
HttpDelete httpDelete = new HttpDelete(String.format("http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
HttpDelete httpDelete = new HttpDelete(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
return httpClient.execute(httpDelete);
}

Expand All @@ -163,7 +164,7 @@ public int getNumberOfRedirects(String uri, InetSocketAddress address) throws Cl

try {
HttpClientContext context = HttpClientContext.create();
HttpHead httpHead = new HttpHead(String.format("http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
HttpHead httpHead = new HttpHead(String.format(Locale.ENGLISH, "http://%s:%s/_blobs/%s", address.getHostName(), address.getPort(), uri));
response = httpClient.execute(httpHead, context);
// get all redirection locations
if(context.getRedirectLocations() != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.junit.Test;

import java.io.IOException;
import java.util.Locale;

@CrateIntegrationTest.ClusterScope(scope = CrateIntegrationTest.Scope.SUITE, numNodes = 2)
public class BlobIntegrationTest extends BlobHttpIntegrationTest {
Expand Down Expand Up @@ -223,7 +224,7 @@ public void testEmptyFile() throws IOException {
@Test
public void testIndexOnNonBlobTable() throws IOException {
// this test works only if ES API is enabled
HttpPut httpPut = new HttpPut(String.format("http://%s:%s/test_no_blobs/default/1",
HttpPut httpPut = new HttpPut(String.format(Locale.ENGLISH, "http://%s:%s/test_no_blobs/default/1",
address.getHostName(), address.getPort()));
String blobData = String.format("{\"content\": \"%s\"}", StringUtils.repeat("a", 1024 * 64));
httpPut.setEntity(new StringEntity(blobData, ContentType.APPLICATION_OCTET_STREAM));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.google.common.collect.ImmutableList;
import org.testng.annotations.Test;

import java.util.Locale;

import static io.crate.sql.SqlFormatter.formatSql;
import static io.crate.sql.tree.QueryUtil.selectList;
import static io.crate.sql.tree.QueryUtil.table;
Expand Down Expand Up @@ -73,7 +75,7 @@ public void testDouble()
public void testParameter() throws Exception {
assertExpression("?", new ParameterExpression(1));
for (int i=0;i<1000;i++) {
assertExpression(String.format("$%d", i), new ParameterExpression(i));
assertExpression(String.format(Locale.ENGLISH, "$%d", i), new ParameterExpression(i));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@
import org.elasticsearch.common.inject.Singleton;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;

@Singleton
public class RelationAnalyzer extends DefaultTraversalVisitor<AnalyzedRelation, RelationAnalysisContext> {
Expand Down Expand Up @@ -302,13 +299,13 @@ private Symbol ordinalOutputReference(List<Symbol> outputSymbols, Literal longLi
assert longLiteral.valueType().equals(DataTypes.LONG) : "longLiteral must have valueType long";
int idx = ((Long) longLiteral.value()).intValue() - 1;
if (idx < 0) {
throw new IllegalArgumentException(String.format(
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"%s position %s is not in select list", clauseName, idx + 1));
}
try {
return outputSymbols.get(idx);
} catch (IndexOutOfBoundsException e) {
throw new IllegalArgumentException(String.format(
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"%s position %s is not in select list", clauseName, idx + 1));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@

import org.elasticsearch.index.shard.ShardId;

import java.util.Locale;

public class UnavailableShardsException extends RuntimeException implements CrateException {

public UnavailableShardsException(ShardId shardId) {
super(genMessage(shardId));
}

private static String genMessage(ShardId shardId) {
return String.format("the shard %s of table %s is not available",
return String.format(Locale.ENGLISH, "the shard %s of table %s is not available",
shardId.getId(), shardId.getIndex());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ protected ListenableFuture<Bucket> handleShardCollect(CollectNode collectNode, R
shardCollectors.add(collector);
} catch (IndexShardMissingException e) {
throw new UnhandledServerException(
String.format("unknown shard id %d on index '%s'",
String.format(Locale.ENGLISH, "unknown shard id %d on index '%s'",
shardId, entry.getKey()), e);
} catch (Exception e) {
logger.error("Error while getting collector", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private void executeRequests() {
}

private void execute(ShardUpsertRequest updateRequest) {
trace(String.format("execute shard request %d", updateRequest.shardId()));
trace(String.format(Locale.ENGLISH, "execute shard request %d", updateRequest.shardId()));
transportShardUpsertActionDelegate.execute(updateRequest, new ResponseListener(updateRequest));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private void executeRequests() {
}

private void execute(SymbolBasedShardUpsertRequest updateRequest) {
trace(String.format("execute shard request %d", updateRequest.shardId()));
trace(String.format(Locale.ENGLISH, "execute shard request %d", updateRequest.shardId()));
transportShardUpsertActionDelegate.execute(updateRequest, new ResponseListener(updateRequest));
}

Expand Down
5 changes: 3 additions & 2 deletions sql/src/test/java/io/crate/benchmark/SelectBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -176,7 +177,7 @@ public void testGetSingleResultApi() {
for (int i=0; i<NUM_REQUESTS_PER_TEST; i++) {
GetRequest request = getApiGetRequest(false);
GetResponse response = getClient(false).execute(GetAction.INSTANCE, request).actionGet();
assertTrue(String.format("Queried row '%s' does not exist (API). Round: %d", request.id(), apiGetRound), response.isExists());
assertTrue(String.format(Locale.ENGLISH, "Queried row '%s' does not exist (API). Round: %d", request.id(), apiGetRound), response.isExists());
apiGetRound++;
}
}
Expand All @@ -188,7 +189,7 @@ public void testGetSingleResultSql() {
SQLRequest request = getSqlGetRequest(false);
SQLResponse response = getClient(false).execute(SQLAction.INSTANCE, request).actionGet();
assertEquals(
String.format("Queried row '%s' does not exist (SQL). Round: %d", request.args()[0], sqlGetRound),
String.format(Locale.ENGLISH, "Queried row '%s' does not exist (SQL). Round: %d", request.args()[0], sqlGetRound),
1,
response.rows().length
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@
import org.junit.rules.ExpectedException;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.*;

import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.core.Is.is;
Expand Down Expand Up @@ -572,7 +569,7 @@ public void testRandomQTFPaging() throws Exception {

for (int i = 0 ; i<expected.length && i < actual.length; i++) {
assertThat(
String.format("elements at index %d differ for page (%s) %d", i, pageInfo, run),
String.format(Locale.ENGLISH, "elements at index %d differ for page (%s) %d", i, pageInfo, run),
actual[i], is(expected[i]));
i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ExecutionException;

Expand Down Expand Up @@ -71,11 +72,11 @@ public void testWriteRawToFile() throws Exception {

projector.registerUpstream(null);
for (int i = 0; i < 5; i++) {
projector.setNextRow(new Row1(new BytesRef(String.format("input line %02d", i))));
projector.setNextRow(new Row1(new BytesRef(String.format(Locale.ENGLISH, "input line %02d", i))));
}
projector.finish();

Bucket rows = ((ResultProvider) downstream).result().get();
Bucket rows = downstream.result().get();
assertThat(rows, contains(isRow(5L)));

assertEquals("input line 00\n" +
Expand Down
2 changes: 1 addition & 1 deletion udc/src/main/java/io/crate/udc/ping/PingTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void run() {
conn.setReadTimeout((int)HTTP_TIMEOUT.millis());

if (conn.getResponseCode() >= 300) {
throw new Exception(String.format("%s Responded with Code %d", url.getHost(), conn.getResponseCode()));
throw new Exception(String.format(Locale.ENGLISH, "%s Responded with Code %d", url.getHost(), conn.getResponseCode()));
}
if (logger.isDebugEnabled()) {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Expand Down

0 comments on commit 9b073a4

Please sign in to comment.