Skip to content

Commit

Permalink
test: add private serialisation cases to type serialiser tests (#5217)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminAmos authored Mar 8, 2024
1 parent 23b83d6 commit ed7a3d6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,19 @@ void testJsonSerializeDeserialize() throws IOException {
assertEquals(INSTANCE, deserializedInstance);
}

@SuppressWarnings("PMD.UnusedPrivateField")
public static final class SomeClass<T> {
@SerializedName("generic-t")
public T data;
public List<T> list = Lists.newArrayList();
public Set<Animal<?>> animals = Sets.newHashSet();
public Animal<T> singleAnimal;
@SerializedName("private-generic-t")
private final T privateData;

SomeClass(T data) {
this.data = data;
this.privateData = data;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicPrivateMixedClass<Integer, List<Integer>>>> typeHandler =
typeHandlerFactory.create(new TypeInfo<PublicPrivateMixedClass<Integer, List<Integer>>>() { }, 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() {
Expand Down Expand Up @@ -91,6 +101,18 @@ public void testMultipleObjectsOfSameType() {
verify(typeHandlerLibrary, never()).getTypeHandler((TypeInfo<Object>) any());
}

@Test
@DisplayName("Test implicit type handler loading for multiple objects of same type but differing visibility")
public void testMixedVisibilityMultipleObjectsOfSameType() {
typeHandlerFactory.create(new TypeInfo<PublicPrivateMixedClass<Integer, Integer>>() { }, 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<Object>) any());
verify(typeHandlerLibrary, never()).getTypeHandler((TypeInfo<Object>) any());
}

@Test
@DisplayName("Test implicit type handler loading for multiple objects of different type")
public void testMultipleObjectsOfDifferentType() {
Expand All @@ -113,6 +135,11 @@ private static class MultiTypeClass<T, U> {
public U u;
}

private static class PublicPrivateMixedClass<T, U> {
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" +
Expand Down

0 comments on commit ed7a3d6

Please sign in to comment.