Fix mutate function to always actually modify the failure object.

This commit is contained in:
Adrien Grand 2016-12-26 10:34:50 +01:00
parent 1cb5dc42ff
commit d89757b848
2 changed files with 20 additions and 6 deletions

View File

@ -34,8 +34,11 @@ import org.elasticsearch.test.EqualsHashCodeTestUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.Supplier;
import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode;
@ -81,11 +84,15 @@ public class ReplicationResponseTests extends ESTestCase {
List<Supplier<ReplicationResponse.ShardInfo.Failure>> mutations = new ArrayList<>();
final Index index = failure.fullShardId().getIndex();
final ShardId randomIndex = new ShardId(randomUnicodeOfCodepointLength(5), index.getUUID(), failure.shardId());
final Set<String> indexNamePool = new HashSet<>(Arrays.asList(randomUnicodeOfCodepointLength(5), randomUnicodeOfCodepointLength(6)));
indexNamePool.remove(index.getName());
final ShardId randomIndex = new ShardId(randomFrom(indexNamePool), index.getUUID(), failure.shardId());
mutations.add(() -> new ReplicationResponse.ShardInfo.Failure(randomIndex, failure.nodeId(), (Exception) failure.getCause(),
failure.status(), failure.primary()));
final ShardId randomUUID = new ShardId(index.getName(), randomUnicodeOfCodepointLength(5), failure.shardId());
final Set<String> uuidPool = new HashSet<>(Arrays.asList(randomUnicodeOfCodepointLength(5), randomUnicodeOfCodepointLength(6)));
uuidPool.remove(index.getUUID());
final ShardId randomUUID = new ShardId(index.getName(), randomFrom(uuidPool), failure.shardId());
mutations.add(() -> new ReplicationResponse.ShardInfo.Failure(randomUUID, failure.nodeId(), (Exception) failure.getCause(),
failure.status(), failure.primary()));
@ -93,15 +100,21 @@ public class ReplicationResponseTests extends ESTestCase {
mutations.add(() -> new ReplicationResponse.ShardInfo.Failure(randomShardId, failure.nodeId(), (Exception) failure.getCause(),
failure.status(), failure.primary()));
final String randomNode = randomUnicodeOfLength(3);
final Set<String> nodeIdPool = new HashSet<>(Arrays.asList(randomUnicodeOfLength(3), randomUnicodeOfLength(4)));
nodeIdPool.remove(failure.nodeId());
final String randomNode = randomFrom(nodeIdPool);
mutations.add(() -> new ReplicationResponse.ShardInfo.Failure(failure.fullShardId(), randomNode, (Exception) failure.getCause(),
failure.status(), failure.primary()));
final Exception randomException = randomFrom(new IllegalStateException("a"), new IllegalArgumentException("b"));
final Set<Exception> exceptionPool = new HashSet<>(Arrays.asList(new IllegalStateException("a"), new IllegalArgumentException("b")));
exceptionPool.remove(failure.getCause());
final Exception randomException = randomFrom(exceptionPool);
mutations.add(() -> new ReplicationResponse.ShardInfo.Failure(failure.fullShardId(), failure.nodeId(), randomException,
failure.status(), failure.primary()));
final RestStatus randomStatus = randomFrom(RestStatus.values());
final Set<RestStatus> otherStatuses = new HashSet<>(Arrays.asList(RestStatus.values()));
otherStatuses.remove(failure.status());
final RestStatus randomStatus = randomFrom(otherStatuses);
mutations.add(() -> new ReplicationResponse.ShardInfo.Failure(failure.fullShardId(), failure.nodeId(),
(Exception) failure.getCause(), randomStatus, failure.primary()));

View File

@ -78,7 +78,8 @@ public class EqualsHashCodeTestUtils {
assertThat(objectName + " hashcode returns different values if called multiple times", original.hashCode(),
equalTo(original.hashCode()));
if (mutationFunction != null) {
assertThat(objectName + " mutation should not be equal to original", mutationFunction.mutate(original),
T mutation = mutationFunction.mutate(original);
assertThat(objectName + " mutation should not be equal to original", mutation,
not(equalTo(original)));
}