FallbackNamespace implements Namespace { public <T> T deserialize(final byte[] bytes) { try { return namespace.deserialize(bytes); } catch (final Exception compatEx) { try { return fallback.deserialize(bytes); } catch (final Exception legacyEx) { LOG.warn(String.format(DESERIALIZE_ERROR, legacyEx)); throw compatEx; } } } FallbackNamespace(final Namespace fallback, final Namespace namespace); byte[] serialize(final Object obj); byte[] serialize(final Object obj, final int bufferSize); void serialize(final Object obj, final ByteBuffer buffer); T deserialize(final byte[] bytes); T deserialize(final ByteBuffer buffer);  }
@Test public void shouldDeserializeBytesWithCompatibleFirst() { final Object object = fallback.deserialize(compatibleBytes); assertThat(object).isInstanceOf(TestClass.class); assertThat(((TestClass) object).testField).isEqualTo(COMPAT_FIELD); Mockito.verify(compatible, times(1)).deserialize(compatibleBytes); Mockito.verifyNoInteractions(legacy); }
@Test public void shouldDeserializeBufferWithCompatibleFirst() { final Object object = fallback.deserialize(compatibleBuffer); assertThat(object).isInstanceOf(TestClass.class); assertThat(((TestClass) object).testField).isEqualTo(COMPAT_FIELD); Mockito.verify(compatible, times(1)).deserialize(compatibleBuffer); Mockito.verifyNoInteractions(legacy); }
@Test public void shouldDeserializeBytesWithLegacyIfCompatibleFails() { final TestClass object = fallback.deserialize(legacyBytes); assertThat(object.testField).isEqualTo(LEGACY_FIELD); Mockito.verify(compatible, times(1)).deserialize(legacyBytes); Mockito.verify(legacy, times(1)).deserialize(legacyBytes); }
@Test public void shouldDeserializeBufferWithLegacyIfCompatibleFails() { final TestClass object = fallback.deserialize(legacyBuffer); assertThat(object.testField).isEqualTo(LEGACY_FIELD); Mockito.verify(compatible, times(1)).deserialize(legacyBuffer); Mockito.verify(legacy, times(1)).deserialize(legacyBuffer); }