1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.statesystem
.core
.tests
.statevalue
;
12 import static org
.junit
.Assert
.assertEquals
;
14 import java
.nio
.ByteBuffer
;
16 import org
.eclipse
.jdt
.annotation
.NonNull
;
17 import org
.eclipse
.tracecompass
.internal
.provisional
.statesystem
.core
.statevalue
.CustomStateValue
;
18 import org
.eclipse
.tracecompass
.internal
.provisional
.statesystem
.core
.statevalue
.ISafeByteBufferReader
;
19 import org
.eclipse
.tracecompass
.internal
.provisional
.statesystem
.core
.statevalue
.ISafeByteBufferWriter
;
20 import org
.eclipse
.tracecompass
.internal
.provisional
.statesystem
.core
.statevalue
.SafeByteBufferFactory
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
.Type
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.tests
.stubs
.statevalues
.CustomStateValueStub
;
25 import org
.junit
.Test
;
28 * Test the {@link CustomStateValue} class
30 * @author Geneviève Bastien
32 public class CustomStateValueTest
extends StateValueTestBase
{
34 private static final CustomStateValue VALUE
= new CustomStateValueStub(3, "MyString");
37 protected @NonNull ITmfStateValue
getStateValueFixture() {
42 protected @NonNull Type
getStateValueType() {
47 * Test serialization/unserialization with the factory present
50 public void testSerialization() {
51 CustomStateValueStub
.registerFactory();
52 ByteBuffer buffer
= ByteBuffer
.allocate(1024);
53 ISafeByteBufferWriter safeBufferWriter
= SafeByteBufferFactory
.wrapWriter(buffer
, VALUE
.getSerializedSize());
54 VALUE
.serialize(safeBufferWriter
);
58 ISafeByteBufferReader safeBufferReader
= SafeByteBufferFactory
.wrapReader(buffer
, VALUE
.getSerializedSize());
59 TmfStateValue read
= CustomStateValue
.readSerializedValue(safeBufferReader
);
60 assertEquals("Custom state value: no factory", VALUE
, read
);
64 * Test serialization/unserialization if no factory is available
67 public void testNoFactoryAvailable() {
68 CustomStateValueStub
.unregisterFactory();
69 ByteBuffer buffer
= ByteBuffer
.allocate(1024);
70 ISafeByteBufferWriter safeBufferWriter
= SafeByteBufferFactory
.wrapWriter(buffer
, VALUE
.getSerializedSize());
71 VALUE
.serialize(safeBufferWriter
);
75 ISafeByteBufferReader safeBufferReader
= SafeByteBufferFactory
.wrapReader(buffer
, VALUE
.getSerializedSize());
76 TmfStateValue read
= CustomStateValue
.readSerializedValue(safeBufferReader
);
77 assertEquals("Custom state value: no factory", TmfStateValue
.nullValue(), read
);
81 * Test the exception when asking a size too large
83 @Test(expected
= ArrayIndexOutOfBoundsException
.class)
84 public void testSerializedTooLarge() {
85 String alphabet
= "abcdefghijklmnopqrstuvwxyz";
86 StringBuilder builder
= new StringBuilder();
87 while (builder
.length() < Short
.MAX_VALUE
) {
88 builder
.append(alphabet
);
90 CustomStateValue sv
= new CustomStateValueStub(3, builder
.toString());
91 sv
.getSerializedSize();
95 * Test the illegal argument exception with custom forbidden custom IDs
97 @Test(expected
= IllegalArgumentException
.class)
98 public void testWrongCustomId() {
99 CustomStateValue
.registerCustomFactory((byte) 0, CustomStateValueStub
.FACTORY
);
103 * Test the illegal argument exception with custom forbidden custom IDs
105 @Test(expected
= IllegalArgumentException
.class)
106 public void testWrongCustomId2() {
107 CustomStateValue
.registerCustomFactory((byte) 20, CustomStateValueStub
.FACTORY
);