From 74da9e62fce9ee9b6bec6dbbdf696ea915814c10 Mon Sep 17 00:00:00 2001 From: Benjamin Amos Date: Sun, 3 Mar 2024 14:20:40 +0000 Subject: [PATCH] test: add private serialisation cases to type serialiser tests --- .../serializers/TypeSerializerTest.java | 4 +++ .../ObjectFieldMapTypeHandlerFactoryTest.java | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java index 87e6cba2d6c..d90edcd0ab2 100644 --- a/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java +++ b/engine-tests/src/test/java/org/terasology/engine/persistence/serializers/TypeSerializerTest.java @@ -110,15 +110,19 @@ void testJsonSerializeDeserialize() throws IOException { assertEquals(INSTANCE, deserializedInstance); } + @SuppressWarnings("PMD.UnusedPrivateField") public static final class SomeClass { @SerializedName("generic-t") public T data; public List list = Lists.newArrayList(); public Set> animals = Sets.newHashSet(); public Animal singleAnimal; + @SerializedName("private-generic-t") + private final T privateData; SomeClass(T data) { this.data = data; + this.privateData = data; } @Override 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 45f40b575de..d8d9df57b50 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 @@ -55,6 +55,16 @@ public void testMultiTypeClassTypeHandlerCreationViaFactory() { assertTrue(typeHandler.get() instanceof ObjectFieldMapTypeHandler); } + @Test + @DisplayName("Test that type handler is correctly created via type handler factory") + public void testMixedVisibilityTypeClassTypeHandlerCreationViaFactory() { + Optional>>> typeHandler = + typeHandlerFactory.create(new TypeInfo>>() { }, context); + + assertTrue(typeHandler.isPresent()); + assertTrue(typeHandler.get() instanceof ObjectFieldMapTypeHandler); + } + @Test @DisplayName("Test implicit type handler loading for plain old java objects (pojo)") public void testPojo() { @@ -91,6 +101,18 @@ public void testMultipleObjectsOfSameType() { verify(typeHandlerLibrary, never()).getTypeHandler((TypeInfo) any()); } + @Test + @DisplayName("Test implicit type handler loading for multiple objects of same type but differing visibility") + public void testMixedVisibilityMultipleObjectsOfSameType() { + 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 multiple objects of different type") public void testMultipleObjectsOfDifferentType() { @@ -113,6 +135,11 @@ private static class MultiTypeClass { public U u; } + private static class PublicPrivateMixedClass { + public T t; + private U u; + } + @edu.umd.cs.findbugs.annotations.SuppressFBWarnings( value = "UUF_UNUSED_FIELD", justification = "Direct member access is not expected. TypeHandler factory dynamically loads type handlers on type handler" +