org.elasticsearch.test.AbstractBWCSerializationTestCase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of framework Show documentation
Show all versions of framework Show documentation
Elasticsearch subproject :test:framework
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
package org.elasticsearch.test;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.xcontent.ToXContent;
import java.io.IOException;
import java.util.List;
import static org.elasticsearch.test.BWCVersions.DEFAULT_BWC_VERSIONS;
public abstract class AbstractBWCSerializationTestCase extends AbstractXContentSerializingTestCase {
/**
* Returns the expected instance if serialized from the given version.
*/
protected abstract T mutateInstanceForVersion(T instance, TransportVersion version);
/**
* The bwc versions to test serialization against
*/
protected List bwcVersions() {
return DEFAULT_BWC_VERSIONS;
}
/**
* Test serialization and deserialization of the test instance across versions
*/
public final void testBwcSerialization() throws IOException {
for (int runs = 0; runs < NUMBER_OF_TEST_RUNS; runs++) {
T testInstance = createTestInstance();
for (TransportVersion bwcVersion : bwcVersions()) {
assertBwcSerialization(testInstance, bwcVersion);
}
}
}
/**
* Assert that instances copied at a particular version are equal. The version is useful
* for sanity checking the backwards compatibility of the wire. It isn't a substitute for
* real backwards compatibility tests but it is *so* much faster.
*/
protected final void assertBwcSerialization(T testInstance, TransportVersion version) throws IOException {
T deserializedInstance = copyWriteable(testInstance, getNamedWriteableRegistry(), instanceReader(), version);
assertOnBWCObject(deserializedInstance, mutateInstanceForVersion(testInstance, version), version);
}
/**
* @param bwcSerializedObject The object deserialized from the previous version
* @param testInstance The original test instance
* @param version The version which serialized
*/
protected void assertOnBWCObject(T bwcSerializedObject, T testInstance, TransportVersion version) {
assertNotSame(version.toString(), bwcSerializedObject, testInstance);
assertEquals(version.toString(), bwcSerializedObject, testInstance);
assertEquals(version.toString(), bwcSerializedObject.hashCode(), testInstance.hashCode());
}
}