Add RuntimeDelegatingTypeHandlerAdapter.testDeserialize
*/
package org.terasology.persistence.typeHandling.coreTypes;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;
import org.mockito.ArgumentMatcher;
import org.terasology.persistence.typeHandling.PersistedData;
+import org.terasology.persistence.typeHandling.PersistedDataArray;
import org.terasology.persistence.typeHandling.PersistedDataSerializer;
import org.terasology.persistence.typeHandling.TypeHandler;
import org.terasology.persistence.typeHandling.TypeSerializationLibrary;
import org.terasology.persistence.typeHandling.coreTypes.factories.CollectionTypeHandlerFactory;
+import org.terasology.persistence.typeHandling.inMemory.PersistedMap;
import org.terasology.persistence.typeHandling.inMemory.PersistedString;
import org.terasology.reflection.TypeInfo;
import org.terasology.reflection.reflect.ConstructorLibrary;
@@ -90,5 +93,45 @@ public void testSerialize() {
@Test
public void testDeserialize() {
+ Class<Sub> subType = Sub.class;
+ Type baseType = TypeInfo.of(Base.class).getType();
+
+ abstract class SubHandler extends TypeHandler<Sub> {}
+
+ TypeHandler baseTypeHandler = mock(TypeHandler.class);
+ TypeHandler<Sub> subTypeHandler = mock(SubHandler.class);
+
+ when(typeSerializationLibrary.getTypeHandler(eq(baseType))).thenReturn(baseTypeHandler);
+ when(typeSerializationLibrary.getTypeHandler(eq(subType))).thenReturn(subTypeHandler);
+
+ TypeHandler<List<Base>> listTypeHandler = collectionHandlerFactory.create(
+ new TypeInfo<List<Base>>() {}, typeSerializationLibrary
+ ).get();
+
+ PersistedData persistedBase = new PersistedMap(ImmutableMap.of());
+
+ PersistedData persistedSub = new PersistedMap(
+ ImmutableMap.of(
+ RuntimeDelegatingTypeHandler.TYPE_FIELD,
+ new PersistedString(subType.getName()),
+ RuntimeDelegatingTypeHandler.VALUE_FIELD,
+ new PersistedMap(ImmutableMap.of())
+ )
+ );
+
+ PersistedDataArray persistedBases = mock(PersistedDataArray.class);
+ when(persistedBases.isArray()).thenReturn(true);
+ when(persistedBases.getAsArray()).thenReturn(persistedBases);
+ when(persistedBases.iterator()).thenReturn(
+ Lists.newArrayList(persistedSub, persistedBase, persistedSub, persistedBase, persistedSub).iterator()
+ );
+
+ listTypeHandler.deserialize(persistedBases);
+
+ verify(typeSerializationLibrary).getTypeHandler(baseType);
+ verify(typeSerializationLibrary, times(3)).getTypeHandler(subType);
+
+ verify(baseTypeHandler, times(2)).deserialize(any());
+ verify(subTypeHandler, times(3)).deserialize(any());
}
}
