1 package org
.eclipse
.linuxtools
.ctf
.core
.tests
.types
;
3 import static org
.junit
.Assert
.assertFalse
;
4 import static org
.junit
.Assert
.assertNotNull
;
5 import static org
.junit
.Assert
.assertTrue
;
7 import java
.nio
.ByteBuffer
;
8 import java
.nio
.ByteOrder
;
10 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
11 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
12 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
13 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
14 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Encoding
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDeclaration
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.tests
.TestParams
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
26 import org
.junit
.After
;
27 import org
.junit
.Before
;
28 import org
.junit
.Test
;
31 * The class <code>ArrayDefinitionTest</code> contains tests for the class
32 * <code>{@link ArrayDefinition}</code>.
35 * @version $Revision: 1.0 $
37 public class ArrayDefinitionTest
{
39 private CTFTrace trace
;
40 private ArrayDefinition fixture
;
46 * the command line arguments
48 public static void main(String
[] args
) {
49 new org
.junit
.runner
.JUnitCore().run(ArrayDefinitionTest
.class);
53 * Perform pre-test initialization.
55 * structDef shouldn't be null after parsing the CTFTraceReader object, so
56 * we can ignore the warning.
58 * @throws CTFReaderException
61 public void setUp() throws CTFReaderException
{
62 this.trace
= TestParams
.createTrace();
64 CTFTraceReader tr
= new CTFTraceReader(this.trace
);
65 String name
= ""; //$NON-NLS-1$
66 StructDefinition structDef
= null;
67 boolean foundArray
= false;
69 while (tr
.hasMoreEvents() && !foundArray
) {
71 EventDefinition ed
= tr
.getCurrentEventDef();
72 for (String key
: ed
.fields
.getDefinitions().keySet()) {
73 structDef
= ed
.fields
;
74 Definition d
= structDef
.lookupDefinition(key
);
75 if (d
instanceof ArrayDefinition
) {
82 fixture
= structDef
.lookupArray(name
);
86 * Perform post-test clean-up.
89 public void tearDown() {
90 // Add additional tear down code here
93 private static StringDefinition
[] createDefs() {
95 StringDefinition
[] defs
= new StringDefinition
[size
];
96 for (int i
= 0; i
< size
; i
++) {
98 String content
= "test" + i
; //$NON-NLS-1$
99 defs
[i
] = new StringDefinition(
100 new StringDeclaration(Encoding
.UTF8
), null, content
);
101 defs
[i
].setString(new StringBuilder(content
));
107 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
111 public void testArrayDefinition_baseDeclaration() {
112 ArrayDeclaration declaration
= fixture
.getDeclaration();
113 String fieldName
= ""; //$NON-NLS-1$
115 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
,
118 assertNotNull(result
);
122 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
126 public void testArrayDefinition_newDeclaration() {
127 ArrayDeclaration declaration
= new ArrayDeclaration(0,
128 new StringDeclaration());
129 IDefinitionScope definitionScope
= null;
130 String fieldName
= ""; //$NON-NLS-1$
132 ArrayDefinition result
= new ArrayDefinition(declaration
,
133 definitionScope
, fieldName
);
135 assertNotNull(result
);
139 * Run the ArrayDeclaration getDeclaration() method test.
142 public void testGetDeclaration() {
143 fixture
.setDefinitions(new Definition
[] {});
144 ArrayDeclaration result
= fixture
.getDeclaration();
146 assertNotNull(result
);
150 * Run the Definition getElem(int) method test.
153 public void testGetElem_noDefs() {
155 Definition result
= fixture
.getElem(i
);
157 assertNotNull(result
);
161 * Run the Definition getElem(int) method test.
164 public void testGetElem_withDefs() {
165 Definition defs
[] = createDefs();
166 fixture
.setDefinitions(defs
);
169 Definition result
= fixture
.getElem(j
);
171 assertNotNull(result
);
175 * Run the boolean isString() method test.
178 public void testIsString_ownDefs() {
179 StringDefinition
[] defs
= createDefs();
180 fixture
.setDefinitions(defs
);
182 boolean result
= fixture
.isString();
188 * Run the boolean isString() method test.
191 public void testIsString_complex() {
192 final IntegerDeclaration id
= new IntegerDeclaration(8, false, 16,
193 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, null);
194 ArrayDeclaration ad
= new ArrayDeclaration(0, id
);
195 ArrayDefinition ownFixture
= new ArrayDefinition(ad
, this.trace
,
196 "Testx"); //$NON-NLS-1$
199 IntegerDefinition
[] defs
= new IntegerDefinition
[size
];
200 for (int i
= 0; i
< size
; i
++) {
202 String content
= "test" + i
; //$NON-NLS-1$
203 defs
[i
] = new IntegerDefinition(new IntegerDeclaration(8, false,
204 16, ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, content
), null, content
);
208 ownFixture
.setDefinitions(defs
);
209 boolean result
= ownFixture
.isString();
215 * Run the boolean isString() method test.
218 public void testIsString_emptyDef() {
219 fixture
.setDefinitions(new Definition
[] {});
220 boolean result
= fixture
.isString();
226 * Run the void read(BitBuffer) method test.
229 public void testRead_noDefs() {
230 BitBuffer input
= new BitBuffer(ByteBuffer
.allocateDirect(128));
236 * Run the void read(BitBuffer) method test.
239 public void testRead_withDefs() {
240 fixture
.setDefinitions(new Definition
[] {});
241 BitBuffer input
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
247 * Run the String toString() method test.
250 public void testToString_base() {
251 String result
= fixture
.toString();
253 assertNotNull(result
);
257 * Run the String toString() method test.
260 public void testToString_withDefs() {
262 StringDefinition
[] defs
= new StringDefinition
[size
];
263 for (int i
= 0; i
< size
; i
++) {
264 defs
[i
] = new StringDefinition(null, null, ("test" + i
)); //$NON-NLS-1$
266 fixture
.setDefinitions(defs
);
267 String result
= fixture
.toString();
269 assertNotNull(result
);