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
.CTFTrace
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
25 import org
.junit
.After
;
26 import org
.junit
.Before
;
27 import org
.junit
.Test
;
30 * The class <code>ArrayDefinitionTest</code> contains tests for the class
31 * <code>{@link ArrayDefinition}</code>.
34 * @version $Revision: 1.0 $
36 public class ArrayDefinitionTest
{
38 private CTFTrace trace
;
39 private ArrayDefinition fixture
;
45 * the command line arguments
47 public static void main(String
[] args
) {
48 new org
.junit
.runner
.JUnitCore().run(ArrayDefinitionTest
.class);
52 * Perform pre-test initialization.
54 * structDef shouldn't be null after parsing the CTFTraceReader object, so
55 * we can ignore the warning.
60 this.trace
= TestParams
.createTrace();
62 CTFTraceReader tr
= new CTFTraceReader(this.trace
);
63 String name
= ""; //$NON-NLS-1$
64 StructDefinition structDef
= null;
65 boolean foundArray
= false;
67 while (tr
.hasMoreEvents() && !foundArray
) {
69 EventDefinition ed
= tr
.getCurrentEventDef();
70 for (String key
: ed
.fields
.getDefinitions().keySet()) {
71 structDef
= ed
.fields
;
72 Definition d
= structDef
.lookupDefinition(key
);
73 if (d
instanceof ArrayDefinition
) {
80 fixture
= structDef
.lookupArray(name
);
84 * Perform post-test clean-up.
87 public void tearDown() {
88 // Add additional tear down code here
91 private static StringDefinition
[] createDefs() {
93 StringDefinition
[] defs
= new StringDefinition
[size
];
94 for (int i
= 0; i
< size
; i
++) {
96 String content
= "test" + i
; //$NON-NLS-1$
97 defs
[i
] = new StringDefinition(
98 new StringDeclaration(Encoding
.UTF8
), null, content
);
99 defs
[i
].setString(new StringBuilder(content
));
105 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
109 public void testArrayDefinition_baseDeclaration() {
110 ArrayDeclaration declaration
= fixture
.getDeclaration();
111 String fieldName
= ""; //$NON-NLS-1$
113 ArrayDefinition result
= new ArrayDefinition(declaration
, this.trace
,
116 assertNotNull(result
);
120 * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String)
124 public void testArrayDefinition_newDeclaration() {
125 ArrayDeclaration declaration
= new ArrayDeclaration(0,
126 new StringDeclaration());
127 IDefinitionScope definitionScope
= null;
128 String fieldName
= ""; //$NON-NLS-1$
130 ArrayDefinition result
= new ArrayDefinition(declaration
,
131 definitionScope
, fieldName
);
133 assertNotNull(result
);
137 * Run the ArrayDeclaration getDeclaration() method test.
140 public void testGetDeclaration() {
141 fixture
.setDefinitions(new Definition
[] {});
142 ArrayDeclaration result
= fixture
.getDeclaration();
144 assertNotNull(result
);
148 * Run the Definition getElem(int) method test.
151 public void testGetElem_noDefs() {
153 Definition result
= fixture
.getElem(i
);
155 assertNotNull(result
);
159 * Run the Definition getElem(int) method test.
162 public void testGetElem_withDefs() {
163 Definition defs
[] = createDefs();
164 fixture
.setDefinitions(defs
);
167 Definition result
= fixture
.getElem(j
);
169 assertNotNull(result
);
173 * Run the boolean isString() method test.
176 public void testIsString_ownDefs() {
177 StringDefinition
[] defs
= createDefs();
178 fixture
.setDefinitions(defs
);
180 boolean result
= fixture
.isString();
186 * Run the boolean isString() method test.
189 public void testIsString_complex() {
190 final IntegerDeclaration id
= new IntegerDeclaration(8, false, 16,
191 ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
);
192 ArrayDeclaration ad
= new ArrayDeclaration(0, id
);
193 ArrayDefinition ownFixture
= new ArrayDefinition(ad
, this.trace
,
194 "Testx"); //$NON-NLS-1$
197 IntegerDefinition
[] defs
= new IntegerDefinition
[size
];
198 for (int i
= 0; i
< size
; i
++) {
200 String content
= "test" + i
; //$NON-NLS-1$
201 defs
[i
] = new IntegerDefinition(new IntegerDeclaration(8, false,
202 16, ByteOrder
.LITTLE_ENDIAN
, Encoding
.UTF8
), null, content
);
206 ownFixture
.setDefinitions(defs
);
207 boolean result
= ownFixture
.isString();
213 * Run the boolean isString() method test.
216 public void testIsString_emptyDef() {
217 fixture
.setDefinitions(new Definition
[] {});
218 boolean result
= fixture
.isString();
224 * Run the void read(BitBuffer) method test.
227 public void testRead_noDefs() {
228 BitBuffer input
= new BitBuffer(ByteBuffer
.allocateDirect(128));
234 * Run the void read(BitBuffer) method test.
237 public void testRead_withDefs() {
238 fixture
.setDefinitions(new Definition
[] {});
239 BitBuffer input
= new BitBuffer(java
.nio
.ByteBuffer
.allocateDirect(128));
245 * Run the String toString() method test.
248 public void testToString_base() {
249 String result
= fixture
.toString();
251 assertNotNull(result
);
255 * Run the String toString() method test.
258 public void testToString_withDefs() {
260 StringDefinition
[] defs
= new StringDefinition
[size
];
261 for (int i
= 0; i
< size
; i
++) {
262 defs
[i
] = new StringDefinition(null, null, ("test" + i
)); //$NON-NLS-1$
264 fixture
.setDefinitions(defs
);
265 String result
= fixture
.toString();
267 assertNotNull(result
);