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
60 @SuppressWarnings("null")
62 public void setUp() throws CTFReaderException
{
63 this.trace
= TestParams
.createTrace();
65 CTFTraceReader tr
= new CTFTraceReader(this.trace
);
66 String name
= ""; //$NON-NLS-1$
67 StructDefinition structDef
= null;
68 boolean foundArray
= false;
70 while (tr
.hasMoreEvents() && !foundArray
) {
72 EventDefinition ed
= tr
.getCurrentEventDef();
73 for (String key
: ed
.fields
.getDefinitions().keySet()) {
74 structDef
= ed
.fields
;
75 Definition d
= structDef
.lookupDefinition(key
);
76 if (d
instanceof ArrayDefinition
) {
83 fixture
= structDef
.lookupArray(name
);
87 * Perform post-test clean-up.
90 public void tearDown() {
91 // Add additional tear down code here
94 private static StringDefinition
[] createDefs() {
96 StringDefinition
[] defs
= new StringDefinition
[size
];
97 for (int i
= 0; i
< size
; i
++) {
99 String content
= "test" + i
; //$NON-NLS-1$
100 defs
[i
] = new StringDefinition(
101 new StringDeclaration(Encoding
.UTF8
), null, content
);
102 defs
[i
].setString(new StringBuilder(content
));
108 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
112 public void testArrayDefinition_baseDeclaration() {
113 ArrayDeclaration declaration
= fixture
.getDeclaration();
114 String fieldName
= ""; //$NON-NLS-1$
116 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
,
119 assertNotNull(result
);
123 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
127 public void testArrayDefinition_newDeclaration() {
128 ArrayDeclaration declaration
= new ArrayDeclaration(0,
129 new StringDeclaration());
130 IDefinitionScope definitionScope
= null;
131 String fieldName
= ""; //$NON-NLS-1$
133 ArrayDefinition result
= new ArrayDefinition(declaration
,
134 definitionScope
, fieldName
);
136 assertNotNull(result
);
140 * Run the ArrayDeclaration getDeclaration() method test.
143 public void testGetDeclaration() {
144 fixture
.setDefinitions(new Definition
[] {});
145 ArrayDeclaration result
= fixture
.getDeclaration();
147 assertNotNull(result
);
151 * Run the Definition getElem(int) method test.
154 public void testGetElem_noDefs() {
156 Definition result
= fixture
.getElem(i
);
158 assertNotNull(result
);
162 * Run the Definition getElem(int) method test.
165 public void testGetElem_withDefs() {
166 Definition defs
[] = createDefs();
167 fixture
.setDefinitions(defs
);
170 Definition result
= fixture
.getElem(j
);
172 assertNotNull(result
);
176 * Run the boolean isString() method test.
179 public void testIsString_ownDefs() {
180 StringDefinition
[] defs
= createDefs();
181 fixture
.setDefinitions(defs
);
183 boolean result
= fixture
.isString();
189 * Run the boolean isString() method test.
192 public void testIsString_complex() {
193 final IntegerDeclaration id
= new IntegerDeclaration(8, false, 16,
194 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, null);
195 ArrayDeclaration ad
= new ArrayDeclaration(0, id
);
196 ArrayDefinition ownFixture
= new ArrayDefinition(ad
, this.trace
,
197 "Testx"); //$NON-NLS-1$
200 IntegerDefinition
[] defs
= new IntegerDefinition
[size
];
201 for (int i
= 0; i
< size
; i
++) {
203 String content
= "test" + i
; //$NON-NLS-1$
204 defs
[i
] = new IntegerDefinition(new IntegerDeclaration(8, false,
205 16, ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
, content
), null, content
);
209 ownFixture
.setDefinitions(defs
);
210 boolean result
= ownFixture
.isString();
216 * Run the boolean isString() method test.
219 public void testIsString_emptyDef() {
220 fixture
.setDefinitions(new Definition
[] {});
221 boolean result
= fixture
.isString();
227 * Run the void read(BitBuffer) method test.
230 public void testRead_noDefs() {
231 BitBuffer input
= new BitBuffer(ByteBuffer
.allocateDirect(128));
237 * Run the void read(BitBuffer) method test.
240 public void testRead_withDefs() {
241 fixture
.setDefinitions(new Definition
[] {});
242 BitBuffer input
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
248 * Run the String toString() method test.
251 public void testToString_base() {
252 String result
= fixture
.toString();
254 assertNotNull(result
);
258 * Run the String toString() method test.
261 public void testToString_withDefs() {
263 StringDefinition
[] defs
= new StringDefinition
[size
];
264 for (int i
= 0; i
< size
; i
++) {
265 defs
[i
] = new StringDefinition(null, null, ("test" + i
)); //$NON-NLS-1$
267 fixture
.setDefinitions(defs
);
268 String result
= fixture
.toString();
270 assertNotNull(result
);