From 832267bcc1db78d0a83659780e4baa1762ea6df2 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 3 Nov 2015 11:40:37 -0500 Subject: [PATCH] Fix dangling comma in ClusterBlock#toString This commit address some serious health issues that could arise from a dangerous combination of OCD, string concatentation, and dangling commas, --- .../elasticsearch/cluster/block/ClusterBlock.java | 4 +++- .../cluster/block/ClusterBlockTests.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java b/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java index 39e1068f605..5a7f8f7c0a9 100644 --- a/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java +++ b/core/src/main/java/org/elasticsearch/cluster/block/ClusterBlock.java @@ -155,8 +155,10 @@ public class ClusterBlock implements Streamable, ToXContent { public String toString() { StringBuilder sb = new StringBuilder(); sb.append(id).append(",").append(description).append(", blocks "); + String delimiter = ""; for (ClusterBlockLevel level : levels) { - sb.append(level.name()).append(","); + sb.append(delimiter).append(level.name()); + delimiter = ","; } return sb.toString(); } diff --git a/core/src/test/java/org/elasticsearch/cluster/block/ClusterBlockTests.java b/core/src/test/java/org/elasticsearch/cluster/block/ClusterBlockTests.java index 0de18e9e0bb..daf715f385e 100644 --- a/core/src/test/java/org/elasticsearch/cluster/block/ClusterBlockTests.java +++ b/core/src/test/java/org/elasticsearch/cluster/block/ClusterBlockTests.java @@ -28,7 +28,9 @@ import org.elasticsearch.test.ESTestCase; import java.util.EnumSet; import static org.elasticsearch.test.VersionUtils.randomVersion; +import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; public class ClusterBlockTests extends ESTestCase { public void testSerialization() throws Exception { @@ -63,4 +65,15 @@ public class ClusterBlockTests extends ESTestCase { assertArrayEquals(result.levels().toArray(), clusterBlock.levels().toArray()); } } + + public void testToStringDanglingComma() { + EnumSet levels = EnumSet.noneOf(ClusterBlockLevel.class); + int nbLevels = randomIntBetween(1, ClusterBlockLevel.values().length); + for (int j = 0; j < nbLevels; j++) { + levels.add(randomFrom(ClusterBlockLevel.values())); + } + ClusterBlock clusterBlock = new ClusterBlock(randomInt(), "cluster block #" + randomInt(), randomBoolean(), + randomBoolean(), randomFrom(RestStatus.values()), levels); + assertThat(clusterBlock.toString(), not(endsWith(","))); + } }