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,
This commit is contained in:
Jason Tedor 2015-11-03 11:40:37 -05:00
parent 43323c3541
commit 832267bcc1
2 changed files with 16 additions and 1 deletions

View File

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

View File

@ -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<ClusterBlockLevel> 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(",")));
}
}