Skip to content

Commit

Permalink
typehandlerlibrary qa
Browse files Browse the repository at this point in the history
as a workaround, add dependency for spotbugs annotations, and create a
[ticket there so the spotbugs gradle plugin would include the dependency]
(spotbugs/spotbugs-gradle-plugin#1018).

see here a motivation for inner type last rule. if we leave the rule we
should respect it, otherwise disable the rule.
https://stackoverflow.com/questions/25158939/what-motivation-is-behind-checkstyle-inner-type-last-rule

pmd warnings not yet addressed, there are some log guard related, which
are false positive.

see #3859

typehandlerlibrary qa, checkstyle

squash to qa
  • Loading branch information
soloturn committed Oct 28, 2023
1 parent 07176f7 commit 74c41da
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .idea/jpa-buddy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions build-logic/src/main/kotlin/terasology-metrics.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ plugins {
}

dependencies {
// workaround to maake spotbugs @SuppressFBWarnings work.
implementation("com.github.spotbugs:spotbugs-annotations:4.8.0")

pmd("net.sourceforge.pmd:pmd-core:6.55.0")
pmd("net.sourceforge.pmd:pmd-java:6.55.0")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class Serializer {

private static final Logger logger = LoggerFactory.getLogger(Serializer.class);

private ClassMetadata<?, ?> classMetadata;
private Map<FieldMetadata<?, ?>, TypeHandler> fieldHandlers;
private final ClassMetadata<?, ?> classMetadata;
private final Map<FieldMetadata<?, ?>, TypeHandler> fieldHandlers;

public Serializer(ClassMetadata<?, ?> classMetadata, Map<FieldMetadata<?, ?>, TypeHandler> fieldHandlers) {
this.fieldHandlers = fieldHandlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ public class FutureTypeHandlerTest {
private final TypeHandlerLibrary typeHandlerLibrary =
Mockito.spy(new TypeHandlerLibrary(reflections));

private static final class RecursiveType<T> {
final T data;
final List<RecursiveType<T>> children;

@SafeVarargs
private RecursiveType(T data, RecursiveType<T>... children) {
this.data = data;
this.children = Lists.newArrayList(children);
}
}

private class ResultCaptor<T> implements Answer {
private T result = null;
public T getResult() {
return result;
}

@Override
public T answer(InvocationOnMock invocationOnMock) throws Throwable {
result = (T) invocationOnMock.callRealMethod();
return result;
}
}

@Test
public void testRecursiveType() {
ResultCaptor<Optional<TypeHandler<RecursiveType<Integer>>>> resultCaptor = new ResultCaptor<>();
Expand All @@ -77,4 +53,28 @@ public void testRecursiveType() {

assertEquals(typeHandler, future.typeHandler);
}

private static final class RecursiveType<T> {
final T data;
final List<RecursiveType<T>> children;

@SafeVarargs
private RecursiveType(T data, RecursiveType<T>... children) {
this.data = data;
this.children = Lists.newArrayList(children);
}
}

private class ResultCaptor<T> implements Answer {
private T result = null;
public T getResult() {
return result;
}

@Override
public T answer(InvocationOnMock invocationOnMock) throws Throwable {
result = (T) invocationOnMock.callRealMethod();
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@


class InMemorySerializerTest {
private InMemoryPersistedDataSerializer serializer = new InMemoryPersistedDataSerializer();
private final InMemoryPersistedDataSerializer serializer = new InMemoryPersistedDataSerializer();

public static Stream<Arguments> types() {
return Stream.of(
Expand Down Expand Up @@ -123,30 +123,6 @@ void serializeStrings() {
Assertions.assertThrows(ClassCastException.class, data::getAsDouble);
}

//TODO remove it
public void template(PersistedData data) {
Assertions.assertFalse(data.isString());
Assertions.assertFalse(data.isArray());
Assertions.assertFalse(data.isNull());
Assertions.assertFalse(data.isNumber());
Assertions.assertFalse(data.isBoolean());
Assertions.assertFalse(data.isBytes());
Assertions.assertFalse(data.isValueMap());

Assertions.assertThrows(IllegalStateException.class, data::getAsValueMap);
Assertions.assertThrows(IllegalStateException.class, data::getAsArray);

Assertions.assertThrows(DeserializationException.class, data::getAsByteBuffer);
Assertions.assertThrows(DeserializationException.class, data::getAsBytes);

Assertions.assertThrows(ClassCastException.class, data::getAsString);
Assertions.assertThrows(ClassCastException.class, data::getAsBoolean);
Assertions.assertThrows(ClassCastException.class, data::getAsInteger);
Assertions.assertThrows(ClassCastException.class, data::getAsLong);
Assertions.assertThrows(ClassCastException.class, data::getAsFloat);
Assertions.assertThrows(ClassCastException.class, data::getAsDouble);
}

@Test
void serializeOneAsStrings() {
PersistedData data = serializer.serialize(new String[]{"foo"});
Expand Down Expand Up @@ -306,7 +282,7 @@ void serializeBytes() {

Assertions.assertEquals(PersistedBytes.class, data.getClass());
Assertions.assertTrue(data.isBytes());
Assertions.assertEquals(value, data.getAsBytes());
Assertions.assertArrayEquals(value, data.getAsBytes());
Assertions.assertEquals(ByteBuffer.wrap(value), data.getAsByteBuffer());

Assertions.assertFalse(data.isString());
Expand Down Expand Up @@ -335,7 +311,7 @@ void serializeByteBuffer() {
Assertions.assertEquals(PersistedBytes.class, data.getClass());
Assertions.assertTrue(data.isBytes());

Assertions.assertEquals(value, data.getAsBytes());
Assertions.assertArrayEquals(value, data.getAsBytes());
Assertions.assertEquals(ByteBuffer.wrap(value), data.getAsByteBuffer());

Assertions.assertFalse(data.isString());
Expand Down Expand Up @@ -469,7 +445,7 @@ private void checkValueArray(PersistedData data, PersistedData entry, Set<TypeGe
Assertions.assertEquals(PersistedValueArray.class, data.getClass());

Assertions.assertEquals(entry, data.getAsArray().getArrayItem(0));
typeGetters.stream()
typeGetters
.forEach(typeGetter ->
Assertions.assertEquals(typeGetter.getGetter().apply(entry),
typeGetter.getGetter().apply(data))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

class TypeHandlerLibraryTest {
private static Reflections reflections;
private static TypeHandlerLibrary typeHandlerLibrary;

@BeforeAll
public static void setup() {
reflections = new Reflections(TypeHandlerLibraryTest.class.getClassLoader());
Reflections reflections = new Reflections(TypeHandlerLibraryTest.class.getClassLoader());
typeHandlerLibrary = new TypeHandlerLibrary(reflections);
TypeHandlerLibrary.populateBuiltInHandlers(typeHandlerLibrary);
}

@MappedContainer
private static class AMappedContainer { }

@Test
public void testMappedContainerHandler() {
TypeHandler<AMappedContainer> handler = typeHandlerLibrary.getTypeHandler(AMappedContainer.class).get();
Expand Down Expand Up @@ -93,4 +89,7 @@ void testGetBaseTypeHandler() {
}

private enum AnEnum { }

@MappedContainer
private static class AMappedContainer { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ void byteArraySerializeDeserialize() {
byte[] expectedObj = new byte[]{(byte) 0xFF};

PersistedBytes data = serialize(expectedObj, new ByteArrayTypeHandler());
Assertions.assertEquals(expectedObj, data.getAsBytes());
Assertions.assertArrayEquals(expectedObj, data.getAsBytes());

byte[] obj = deserialize(data, new ByteArrayTypeHandler());
Assertions.assertEquals(expectedObj, obj);
Assertions.assertArrayEquals(expectedObj, obj);
}

private <R extends PersistedData, T> R serialize(T obj, TypeHandler<T> typeHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package org.terasology.persistence.typeHandling.coreTypes.factories;

import com.google.common.collect.Maps;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.junit.jupiter.api.Test;
import org.terasology.persistence.typeHandling.TypeHandler;
import org.terasology.persistence.typeHandling.TypeHandlerContext;
Expand Down Expand Up @@ -30,11 +31,6 @@ public class ObjectFieldMapTypeHandlerFactoryTest {
private final TypeHandlerContext context =
new TypeHandlerContext(typeHandlerLibrary, mock(SerializationSandbox.class));

private static class SomeClass<T> {
private T t;
private List<T> list;
}

@Test
public void testObject() {
Optional<TypeHandler<SomeClass<Integer>>> typeHandler =
Expand All @@ -48,4 +44,11 @@ public void testObject() {

verify(typeHandlerLibrary).getTypeHandler(eq(new TypeInfo<List<Integer>>() { }.getType()));
}

private static class SomeClass<T> {
@SuppressFBWarnings
private T t;
@SuppressFBWarnings
private List<T> list;
}
}

0 comments on commit 74c41da

Please sign in to comment.