Search in sources :

Example 1 with PersistedDataSerializer

use of org.terasology.persistence.typeHandling.PersistedDataSerializer in project Terasology by MovingBlocks.

the class ColorTypeHandlerTest method testSerialize.

@Test
public void testSerialize() {
    PersistedDataSerializer persistedDataSerializer = Mockito.mock(PersistedDataSerializer.class);
    handler.serialize(new Color(0x010380FF), persistedDataSerializer);
    Mockito.verify(persistedDataSerializer).serialize(1, 3, 128, 255);
}
Also used : PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) Color(org.terasology.nui.Color) Test(org.junit.jupiter.api.Test)

Example 2 with PersistedDataSerializer

use of org.terasology.persistence.typeHandling.PersistedDataSerializer in project Terasology by MovingBlocks.

the class EnumTypeHandlerSerializerTest method testNullValue.

@Test
void testNullValue() throws Exception {
    PersistedData nullData = mock(PersistedData.class);
    when(nullData.isNull()).thenReturn(true);
    PersistedDataSerializer persistedDataSerializer = mock(PersistedDataSerializer.class);
    when(persistedDataSerializer.serializeNull()).thenReturn(nullData);
    EnumTypeHandler<TestEnum> handler = new EnumTypeHandler<>(TestEnum.class);
    PersistedData serializedNull = handler.serialize(null, persistedDataSerializer);
    assertEquals(nullData, serializedNull);
    assertFalse(handler.deserialize(nullData).isPresent());
}
Also used : PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) PersistedData(org.terasology.persistence.typeHandling.PersistedData) Test(org.junit.jupiter.api.Test)

Example 3 with PersistedDataSerializer

use of org.terasology.persistence.typeHandling.PersistedDataSerializer in project Terasology by MovingBlocks.

the class EnumTypeHandlerSerializerTest method testNonNullValue.

@Test
void testNonNullValue() throws Exception {
    PersistedData data = mock(PersistedData.class);
    when(data.getAsString()).thenReturn(TestEnum.NON_NULL.toString());
    when(data.isString()).thenReturn(true);
    PersistedDataSerializer persistedDataSerializer = mock(PersistedDataSerializer.class);
    when(persistedDataSerializer.serialize(TestEnum.NON_NULL.toString())).thenReturn(data);
    EnumTypeHandler<TestEnum> handler = new EnumTypeHandler<>(TestEnum.class);
    PersistedData serializedNonNull = handler.serialize(TestEnum.NON_NULL, persistedDataSerializer);
    assertEquals(data, serializedNonNull);
    TestEnum deserializedValue = handler.deserialize(data).get();
    assertEquals(TestEnum.NON_NULL, deserializedValue);
}
Also used : PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) PersistedData(org.terasology.persistence.typeHandling.PersistedData) Test(org.junit.jupiter.api.Test)

Example 4 with PersistedDataSerializer

use of org.terasology.persistence.typeHandling.PersistedDataSerializer in project Terasology by MovingBlocks.

the class ArrayTypeHandlerTest method testSerialize.

@Test
void testSerialize() {
    IntTypeHandler elementTypeHandler = mock(IntTypeHandler.class);
    ArrayTypeHandler<Integer> typeHandler = new ArrayTypeHandler<>(elementTypeHandler, TypeInfo.of(Integer.class));
    Integer[] array = new Integer[ARRAY_SIZE];
    final int[] c = { 0 };
    Collections.nCopies(array.length, -1).forEach(i -> array[c[0]++] = i);
    PersistedDataSerializer context = mock(PersistedDataSerializer.class);
    typeHandler.serialize(array, context);
    verify(elementTypeHandler, times(array.length)).serialize(any(), any());
    verify(context).serialize(argThat((ArgumentMatcher<Iterable<PersistedData>>) argument -> argument instanceof Collection && ((Collection) argument).size() == array.length));
}
Also used : PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) ArgumentMatcher(org.mockito.ArgumentMatcher) PersistedData(org.terasology.persistence.typeHandling.PersistedData) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 5 with PersistedDataSerializer

use of org.terasology.persistence.typeHandling.PersistedDataSerializer in project Terasology by MovingBlocks.

the class RuntimeDelegatingTypeHandlerTest method testSerializeBase.

@Test
void testSerializeBase() {
    PersistedDataSerializer serializer = mock(PersistedDataSerializer.class);
    Base base = new Base();
    runtimeDelegatingTypeHandler.serialize(base, serializer);
    verify(baseTypeHandler).serialize(any(), any());
    verify(subTypeHandler, never()).serialize(any(), any());
    verify(serializer, never()).serialize(argThat((ArgumentMatcher<Map<String, PersistedData>>) argument -> argument.containsKey(RuntimeDelegatingTypeHandler.TYPE_FIELD)));
}
Also used : PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) ArgumentMatcher(org.mockito.ArgumentMatcher) AbstractPersistedData(org.terasology.persistence.typeHandling.inMemory.AbstractPersistedData) PersistedData(org.terasology.persistence.typeHandling.PersistedData) PersistedString(org.terasology.persistence.typeHandling.inMemory.PersistedString) Test(org.junit.jupiter.api.Test)

Aggregations

PersistedDataSerializer (org.terasology.persistence.typeHandling.PersistedDataSerializer)10 Test (org.junit.jupiter.api.Test)8 PersistedData (org.terasology.persistence.typeHandling.PersistedData)8 ArgumentMatcher (org.mockito.ArgumentMatcher)4 Collection (java.util.Collection)2 AbstractPersistedData (org.terasology.persistence.typeHandling.inMemory.AbstractPersistedData)2 PersistedString (org.terasology.persistence.typeHandling.inMemory.PersistedString)2 Maps (com.google.common.collect.Maps)1 ByteString (com.google.protobuf.ByteString)1 Type (java.lang.reflect.Type)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Queue (java.util.Queue)1 Set (java.util.Set)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Event (org.terasology.engine.entitySystem.event.Event)1 ReplicatedFieldMetadata (org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata)1 ProtobufPersistedData (org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData)1 ProtobufPersistedDataSerializer (org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)1