Commit | Line | Data |
---|---|---|
d69a6555 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 École Polytechnique de Montréal | |
3 | * | |
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 | *******************************************************************************/ | |
9 | ||
10 | package org.eclipse.tracecompass.statesystem.core.tests.statevalue; | |
11 | ||
12 | import static org.junit.Assert.assertEquals; | |
13 | ||
14 | import java.nio.ByteBuffer; | |
15 | ||
16 | import org.eclipse.jdt.annotation.NonNull; | |
88177c18 GB |
17 | import org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferReader; |
18 | import org.eclipse.tracecompass.datastore.core.serialization.ISafeByteBufferWriter; | |
19 | import org.eclipse.tracecompass.datastore.core.serialization.SafeByteBufferFactory; | |
d69a6555 | 20 | import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.CustomStateValue; |
d69a6555 GB |
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; | |
26 | ||
27 | /** | |
28 | * Test the {@link CustomStateValue} class | |
29 | * | |
30 | * @author Geneviève Bastien | |
31 | */ | |
32 | public class CustomStateValueTest extends StateValueTestBase { | |
33 | ||
34 | private static final CustomStateValue VALUE = new CustomStateValueStub(3, "MyString"); | |
35 | ||
36 | @Override | |
37 | protected @NonNull ITmfStateValue getStateValueFixture() { | |
38 | return VALUE; | |
39 | } | |
40 | ||
41 | @Override | |
42 | protected @NonNull Type getStateValueType() { | |
43 | return Type.CUSTOM; | |
44 | } | |
45 | ||
46 | /** | |
47 | * Test serialization/unserialization with the factory present | |
48 | */ | |
49 | @Test | |
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); | |
55 | ||
56 | // Reset buffer | |
57 | buffer.flip(); | |
58 | ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize()); | |
59 | TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader); | |
60 | assertEquals("Custom state value: no factory", VALUE, read); | |
61 | } | |
62 | ||
63 | /** | |
64 | * Test serialization/unserialization if no factory is available | |
65 | */ | |
66 | @Test | |
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); | |
72 | ||
73 | // Reset buffer | |
74 | buffer.flip(); | |
75 | ISafeByteBufferReader safeBufferReader = SafeByteBufferFactory.wrapReader(buffer, VALUE.getSerializedSize()); | |
76 | TmfStateValue read = CustomStateValue.readSerializedValue(safeBufferReader); | |
77 | assertEquals("Custom state value: no factory", TmfStateValue.nullValue(), read); | |
78 | } | |
79 | ||
80 | /** | |
81 | * Test the exception when asking a size too large | |
82 | */ | |
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); | |
89 | } | |
90 | CustomStateValue sv = new CustomStateValueStub(3, builder.toString()); | |
91 | sv.getSerializedSize(); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Test the illegal argument exception with custom forbidden custom IDs | |
96 | */ | |
97 | @Test(expected = IllegalArgumentException.class) | |
98 | public void testWrongCustomId() { | |
99 | CustomStateValue.registerCustomFactory((byte) 0, CustomStateValueStub.FACTORY); | |
100 | } | |
101 | ||
102 | /** | |
103 | * Test the illegal argument exception with custom forbidden custom IDs | |
104 | */ | |
105 | @Test(expected = IllegalArgumentException.class) | |
106 | public void testWrongCustomId2() { | |
107 | CustomStateValue.registerCustomFactory((byte) 20, CustomStateValueStub.FACTORY); | |
108 | } | |
109 | ||
110 | } |