### Question:
ProtobufSerializationProvider implements SerializationProvider { @Override @SuppressWarnings("unchecked") public <T> StreamingDeserializer<T> getDeserializer(final Class<T> classToDeSerialize) { requireMessageLite(classToDeSerialize); Parser<T> parser = parsers.computeIfAbsent(classToDeSerialize, parserForClass::apply); return new ProtobufDeserializer<>(parser); } ProtobufSerializationProvider();  ProtobufSerializationProvider(final Function<Class<?>, Parser<?>> parserForClass); @Override StreamingSerializer getSerializer(final Class<T> classToSerialize); @Override StreamingSerializer getSerializer(final TypeHolder<T> typeToSerialize); @Override @SuppressWarnings("unchecked") StreamingDeserializer<T> getDeserializer(final Class<T> classToDeSerialize); @Override StreamingDeserializer<T> getDeserializer(final TypeHolder<T> typeToDeserialize);  }### Answer:
@Test public void deserializeMessageByClass() { Buffer buffer = wrap(testMessageBytes); DummyMessage message = firstOf(provider.getDeserializer(DummyMessage.class).deserialize(buffer)); assertThat(message, equalTo(testMessage)); }
@Test public void deserializeMessageByType() { Buffer buffer = wrap(testMessageBytes); DummyMessage message = firstOf(provider.getDeserializer(typeHolder).deserialize(buffer)); assertThat(message, equalTo(testMessage)); }
@Test public void invalidDeserializerClassException() { expectedException.expect(SerializationException.class); provider.getDeserializer(String.class); }
@Test public void deserializeExceptionTruncatedBytes() { Buffer badBuffer = newBuffer().writeBytes(testMessageBytes, 0, testMessageBytes.length / 2); expectedException.expect(SerializationException.class); provider.getDeserializer(DummyMessage.class).deserialize(badBuffer); }
@Test public void deserializeDoubledBytes() { DummyMessage overwrite = DummyMessage.newBuilder().setMessage("supplanted").build(); Buffer buffer = newBuffer().writeBytes(testMessageBytes).writeBytes(overwrite.toByteArray()); List<DummyMessage> messages = toList(provider.getDeserializer(DummyMessage.class).deserialize(buffer)); assertThat(messages, hasSize(1)); assertThat(messages.get(0), equalTo(overwrite)); }
@Test public void deserializeEmptyBuffer() { DummyMessage emptyMessage = DummyMessage.newBuilder().build(); DummyMessage message = firstOf(provider.getDeserializer(DummyMessage.class).deserialize(newBuffer())); assertThat(message, equalTo(emptyMessage)); }
@Test public void cacheParsers() { AtomicInteger providerCounter = new AtomicInteger(0); ProtobufSerializationProvider p = new ProtobufSerializationProvider(aClass -> { providerCounter.getAndIncrement(); return DummyMessage.parser(); }); assertThat(providerCounter.get(), equalTo(0)); p.getDeserializer(DummyMessage.class); assertThat(providerCounter.get(), equalTo(1)); p.getDeserializer(DummyMessage.class); assertThat(providerCounter.get(), equalTo(1)); }