### Question:
ImmutableMapSerializerExtension implements SerializerExtension { @Override public Optional<Serializer> getSerializer( TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv) { if (!isImmutableMap(typeMirror)) { return Optional.empty(); } TypeMirror keyType = getKeyType(typeMirror); TypeMirror valueType = getValueType(typeMirror); Serializer keyTypeSerializer = factory.getSerializer(keyType); Serializer valueTypeSerializer = factory.getSerializer(valueType); if (keyTypeSerializer.isIdentity() && valueTypeSerializer.isIdentity()) { return Optional.empty(); } return Optional.of( new ImmutableMapSerializer( keyType, valueType, keyTypeSerializer, valueTypeSerializer, processingEnv)); } ImmutableMapSerializerExtension(); @Override Optional<Serializer> getSerializer(
      TypeMirror typeMirror, SerializerFactory factory, ProcessingEnvironment processingEnv);  }### Answer:
@Test public void getSerializer_nonImmutableMap_emptyReturned() { TypeMirror typeMirror = typeMirrorOf(String.class); Optional<Serializer> actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment); assertThat(actualSerializer).isEmpty(); }
@Test public void getSerializer_immutableMapWithSerializableContainedTypes_emptyReturned() { fakeSerializerFactory.setReturnIdentitySerializer(true); TypeMirror typeMirror = typeMirrorOf(ImmutableMap.class); Optional<Serializer> actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment); assertThat(actualSerializer).isEmpty(); }
@Test public void getSerializer_immutableMap_serializerReturned() { TypeMirror typeMirror = typeMirrorOf(ImmutableMap.class); Serializer actualSerializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); assertThat(actualSerializer.getClass().getName()) .contains("ImmutableMapSerializerExtension$ImmutableMapSerializer"); }
@Test public void proxyFieldType() { TypeMirror typeMirror = declaredTypeOf(ImmutableMap.class, Integer.class, String.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); TypeMirror actualTypeMirror = serializer.proxyFieldType(); assertThat(typeUtils.isSameType(actualTypeMirror, typeMirror)).isTrue(); }
@Test public void toProxy() { TypeMirror typeMirror = declaredTypeOf(ImmutableMap.class, Integer.class, String.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); CodeBlock actualCodeBlock = serializer.toProxy(CodeBlock.of("x")); assertThat(actualCodeBlock.toString()) .isEqualTo( String.format( "x.entrySet().stream().collect(%s.toImmutableMap(value$ -> %s.<%s," + " %s>wrapper(element$ -> element$).apply(value$.getKey()), value$ -> %s.<%s," + " %s>wrapper(element$ -> element$).apply(value$.getValue())))", IMMUTABLE_MAP, FUNCTION_WITH_EXCEPTIONS, INTEGER, INTEGER, FUNCTION_WITH_EXCEPTIONS, STRING, STRING)); }
@Test public void fromProxy() { TypeMirror typeMirror = declaredTypeOf(ImmutableMap.class, Integer.class, String.class); Serializer serializer = extension.getSerializer(typeMirror, fakeSerializerFactory, mockProcessingEnvironment).get(); CodeBlock actualCodeBlock = serializer.fromProxy(CodeBlock.of("x")); assertThat(actualCodeBlock.toString()) .isEqualTo( String.format( "x.entrySet().stream().collect(%s.toImmutableMap(value$ -> %s.<%s," + " %s>wrapper(element$ -> element$).apply(value$.getKey()), value$ -> %s.<%s," + " %s>wrapper(element$ -> element$).apply(value$.getValue())))", IMMUTABLE_MAP, FUNCTION_WITH_EXCEPTIONS, INTEGER, INTEGER, FUNCTION_WITH_EXCEPTIONS, STRING, STRING)); }