Search in sources :

Example 1 with TmfStateValue

use of org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue in project tracecompass by tracecompass.

the class CustomStateValueTest method testNoFactoryAvailable.

/**
 * Test serialization/unserialization if no factory is available
 */
@Test
public void testNoFactoryAvailable() {
    CustomStateValueStub.unregisterFactory();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    ISafeByteBufferWriter safeBufferWriter = SafeByteBufferFactory.wrapWriter(buffer, VALUE.getSerializedSize());
    VALUE.serialize(safeBufferWriter);
    // Reset buffer
    buffer.flip();
    ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize());
    TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader);
    assertEquals("Custom state value: no factory", TmfStateValue.nullValue(), read);
}
Also used : ITmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue) TmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue) ByteBuffer(java.nio.ByteBuffer) ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader) ISafeByteBufferWriter(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter) Test(org.junit.Test)

Example 2 with TmfStateValue

use of org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue in project tracecompass by tracecompass.

the class CustomStateValueTest method testSerialization.

/**
 * Test serialization/unserialization with the factory present
 */
@Test
public void testSerialization() {
    CustomStateValueStub.registerFactory();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    ISafeByteBufferWriter safeBufferWriter = SafeByteBufferFactory.wrapWriter(buffer, VALUE.getSerializedSize());
    VALUE.serialize(safeBufferWriter);
    // Reset buffer
    buffer.flip();
    ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize());
    TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader);
    assertEquals("Custom state value: no factory", VALUE, read);
}
Also used : ITmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue) TmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue) ByteBuffer(java.nio.ByteBuffer) ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader) ISafeByteBufferWriter(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter) Test(org.junit.Test)

Example 3 with TmfStateValue

use of org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue in project tracecompass by tracecompass.

the class TmfIntervalDeserializer method deserialize.

@Override
public ITmfStateInterval deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    JsonObject object = json.getAsJsonObject();
    long start = object.get(TmfIntervalStrings.START).getAsLong();
    long end = object.get(TmfIntervalStrings.END).getAsLong();
    int quark = object.get(TmfIntervalStrings.QUARK).getAsInt();
    String type = object.get(TmfIntervalStrings.TYPE).getAsString();
    if (type.equals(TmfIntervalStrings.NULL)) {
        return new TmfStateInterval(start, end, quark, (Object) null);
    }
    JsonElement value = object.get(TmfIntervalStrings.VALUE);
    try {
        Class<?> typeClass = Class.forName(type);
        if (typeClass.isAssignableFrom(CustomStateValue.class)) {
            String encoded = value.getAsString();
            byte[] serialized = Base64.getDecoder().decode(encoded);
            ByteBuffer buffer = ByteBuffer.wrap(serialized);
            ISafeByteBufferReader sbbr = SafeByteBufferFactory.wrapReader(buffer, serialized.length);
            TmfStateValue sv = CustomStateValue.readSerializedValue(sbbr);
            return new TmfStateInterval(start, end, quark, sv.unboxValue());
        }
        if (typeClass.isAssignableFrom(Integer.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsInt());
        } else if (typeClass.isAssignableFrom(Long.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsLong());
        } else if (typeClass.isAssignableFrom(Double.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsDouble());
        } else if (typeClass.isAssignableFrom(String.class)) {
            return new TmfStateInterval(start, end, quark, value.getAsString());
        }
    } catch (ClassNotFoundException e) {
    // Fall through
    }
    // last ditch attempt
    return new TmfStateInterval(start, end, quark, value.toString());
}
Also used : ITmfStateInterval(org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval) TmfStateInterval(org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval) JsonObject(com.google.gson.JsonObject) ByteBuffer(java.nio.ByteBuffer) ISafeByteBufferReader(org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader) JsonElement(com.google.gson.JsonElement) TmfStateValue(org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue)

Aggregations

ByteBuffer (java.nio.ByteBuffer)3 ISafeByteBufferReader (org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader)3 TmfStateValue (org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue)3 ISafeByteBufferWriter (org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter)2 ITmfStateValue (org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue)2 Test (org.junit.Test)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ITmfStateInterval (org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval)1 TmfStateInterval (org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval)1