From 1b286a6881297c6ac37717d79c7d14cb22aa262a Mon Sep 17 00:00:00 2001 From: jdrueckert Date: Mon, 13 Nov 2023 12:33:01 +0100 Subject: [PATCH] test: split and improve ObjectFieldMapTypeHandlerFactoryTests (#5168) --- .../ObjectFieldMapTypeHandlerFactoryTest.java | 82 +++++++++++++++++-- 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java b/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java index 3451b19d965..c6e73e3bd7a 100644 --- a/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java +++ b/subsystems/TypeHandlerLibrary/src/test/java/org/terasology/persistence/typeHandling/coreTypes/factories/ObjectFieldMapTypeHandlerFactoryTest.java @@ -3,6 +3,7 @@ package org.terasology.persistence.typeHandling.coreTypes.factories; import com.google.common.collect.Maps; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.terasology.persistence.typeHandling.TypeHandler; import org.terasology.persistence.typeHandling.TypeHandlerContext; @@ -12,12 +13,16 @@ import org.terasology.reflection.TypeInfo; import org.terasology.reflection.reflect.ConstructorLibrary; +import java.lang.reflect.Type; import java.util.List; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; public class ObjectFieldMapTypeHandlerFactoryTest { @@ -30,22 +35,81 @@ public class ObjectFieldMapTypeHandlerFactoryTest { private final TypeHandlerContext context = new TypeHandlerContext(typeHandlerLibrary, mock(SerializationSandbox.class)); - private static class SomeClass { - private T t; - private List list; + @Test + @DisplayName("Test that type handler is correctly created via type handler factory") + public void testSingleTypeClassTypeHandlerCreationViaFactory() { + Optional>> typeHandler = + typeHandlerFactory.create(new TypeInfo>() { }, context); + + assertTrue(typeHandler.isPresent()); + assertTrue(typeHandler.get() instanceof ObjectFieldMapTypeHandler); } @Test - public void testObject() { - Optional>> typeHandler = - typeHandlerFactory.create(new TypeInfo>() { }, context); + @DisplayName("Test that type handler is correctly created via type handler factory") + public void testMultiTypeClassTypeHandlerCreationViaFactory() { + Optional>>> typeHandler = + typeHandlerFactory.create(new TypeInfo>>() { }, context); assertTrue(typeHandler.isPresent()); assertTrue(typeHandler.get() instanceof ObjectFieldMapTypeHandler); + } - // Verify that the Integer and List TypeHandlers were loaded from the TypeHandlerLibrary - verify(typeHandlerLibrary).getTypeHandler(eq(TypeInfo.of(Integer.class).getType())); + @Test + @DisplayName("Test implicit type handler loading for plain old java objects (pojo)") + public void testPojo() { + typeHandlerFactory.create(new TypeInfo>() { }, context); + + // Verify that the Integer TypeHandler loading was called on the TypeHandlerLibrary + verify(typeHandlerLibrary, times(1)).getTypeHandler((Type) any()); + verify(typeHandlerLibrary, times(1)).getTypeHandler(eq(TypeInfo.of(Integer.class).getType())); + verify(typeHandlerLibrary, never()).getTypeHandler((Class) any()); + verify(typeHandlerLibrary, never()).getTypeHandler((TypeInfo) any()); + } + + @Test + @DisplayName("Test implicit type handler loading for list collection objects") + public void testListCollection() { + typeHandlerFactory.create(new TypeInfo>>() { }, context); + + // Verify that the List TypeHandler was loaded from the TypeHandlerLibrary + verify(typeHandlerLibrary, times(1)).getTypeHandler((Type) any()); + verify(typeHandlerLibrary, times(1)).getTypeHandler(eq(new TypeInfo>() { }.getType())); + verify(typeHandlerLibrary, never()).getTypeHandler((Class) any()); + verify(typeHandlerLibrary, never()).getTypeHandler((TypeInfo) any()); + } - verify(typeHandlerLibrary).getTypeHandler(eq(new TypeInfo>() { }.getType())); + @Test + @DisplayName("Test implicit type handler loading for multiple objects of same type") + public void testMultipleObjectsOfSameType() { + typeHandlerFactory.create(new TypeInfo>() { }, context); + + // Verify that the Integer TypeHandler loading was called on the TypeHandlerLibrary + verify(typeHandlerLibrary, times(2)).getTypeHandler((Type) any()); + verify(typeHandlerLibrary, times(2)).getTypeHandler(eq(TypeInfo.of(Integer.class).getType())); + verify(typeHandlerLibrary, never()).getTypeHandler((Class) any()); + verify(typeHandlerLibrary, never()).getTypeHandler((TypeInfo) any()); + } + + @Test + @DisplayName("Test implicit type handler loading for multiple objects of different type") + public void testMultipleObjectsOfDifferentType() { + typeHandlerFactory.create(new TypeInfo>>() { }, context); + + // Verify that the Integer TypeHandler loading was called on the TypeHandlerLibrary + verify(typeHandlerLibrary, times(2)).getTypeHandler((Type) any()); + verify(typeHandlerLibrary, times(1)).getTypeHandler(eq(TypeInfo.of(Integer.class).getType())); + verify(typeHandlerLibrary, times(1)).getTypeHandler(eq(new TypeInfo>() { }.getType())); + verify(typeHandlerLibrary, never()).getTypeHandler((Class) any()); + verify(typeHandlerLibrary, never()).getTypeHandler((TypeInfo) any()); + } + + private static class SingleTypeClass { + private T t; + } + + private static class MultiTypeClass { + private T t; + private U u; } }