Class EnumSerializationTestUtils

java.lang.Object
org.elasticsearch.test.EnumSerializationTestUtils

public class EnumSerializationTestUtils extends Object
Enum serialization via StreamOutput.writeEnum(E) and StreamInput.readEnum(Class) uses the enum value's ordinal on the wire. Reordering the values in an enum, or adding a new value, will change the ordinals and is therefore a wire protocol change, but it's easy to miss this fact in the context of a larger commit. To protect against this trap, any enums that we send over the wire should have a test that uses assertEnumSerialization(java.lang.Class<E>, E, E...) to assert a fixed mapping between ordinals and values. That way, a change to the ordinals will require a test change, and thus some thought about BwC.
  • Method Details

    • assertEnumSerialization

      @SafeVarargs public static <E extends Enum<E>> void assertEnumSerialization(Class<E> clazz, E value0, E... values)
      Assert that the enum constants of the given class are exactly the ones passed in explicitly as arguments, which fixes its wire protocol when using StreamOutput.writeEnum(E) and StreamInput.readEnum(Class).
      Parameters:
      value0 - The enum constant with ordinal 0, passed as a separate argument to avoid prevent callers just lazily using EnumClass.values() to pass the values of the enum, which would negate the point of this test.
      values - The remaining enum constants, in ordinal order.