Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add private serialisation cases to type serialiser tests #5217

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading