Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | package org.eclipse.linuxtools.ctf.core.tests.types; |
2 | ||
3 | import static org.junit.Assert.assertFalse; | |
4 | import static org.junit.Assert.assertNotNull; | |
5 | import static org.junit.Assert.assertTrue; | |
6 | ||
7 | import java.nio.ByteBuffer; | |
8 | import java.nio.ByteOrder; | |
9 | ||
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; | |
13be1a8f | 23 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
866e5b51 FC |
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; | |
29 | ||
30 | /** | |
31 | * The class <code>ArrayDefinitionTest</code> contains tests for the class | |
32 | * <code>{@link ArrayDefinition}</code>. | |
33 | * | |
34 | * @author ematkho | |
35 | * @version $Revision: 1.0 $ | |
36 | */ | |
37 | public class ArrayDefinitionTest { | |
38 | ||
39 | private CTFTrace trace; | |
40 | private ArrayDefinition fixture; | |
41 | ||
42 | /** | |
43 | * Launch the test. | |
44 | * | |
45 | * @param args | |
46 | * the command line arguments | |
47 | */ | |
48 | public static void main(String[] args) { | |
49 | new org.junit.runner.JUnitCore().run(ArrayDefinitionTest.class); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Perform pre-test initialization. | |
54 | * | |
55 | * structDef shouldn't be null after parsing the CTFTraceReader object, so | |
56 | * we can ignore the warning. | |
13be1a8f AM |
57 | * |
58 | * @throws CTFReaderException | |
866e5b51 | 59 | */ |
13be1a8f | 60 | @SuppressWarnings("null") |
866e5b51 | 61 | @Before |
13be1a8f | 62 | public void setUp() throws CTFReaderException { |
866e5b51 FC |
63 | this.trace = TestParams.createTrace(); |
64 | ||
65 | CTFTraceReader tr = new CTFTraceReader(this.trace); | |
66 | String name = ""; //$NON-NLS-1$ | |
67 | StructDefinition structDef = null; | |
68 | boolean foundArray = false; | |
69 | ||
70 | while (tr.hasMoreEvents() && !foundArray) { | |
71 | tr.advance(); | |
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) { | |
77 | foundArray = true; | |
78 | name = key; | |
79 | break; | |
80 | } | |
81 | } | |
82 | } | |
83 | fixture = structDef.lookupArray(name); | |
84 | } | |
85 | ||
86 | /** | |
87 | * Perform post-test clean-up. | |
88 | */ | |
89 | @After | |
90 | public void tearDown() { | |
91 | // Add additional tear down code here | |
92 | } | |
93 | ||
94 | private static StringDefinition[] createDefs() { | |
95 | int size = 4; | |
96 | StringDefinition[] defs = new StringDefinition[size]; | |
97 | for (int i = 0; i < size; i++) { | |
98 | ||
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)); | |
103 | } | |
104 | return defs; | |
105 | } | |
106 | ||
107 | /** | |
108 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) | |
109 | * constructor test. | |
110 | */ | |
111 | @Test | |
112 | public void testArrayDefinition_baseDeclaration() { | |
113 | ArrayDeclaration declaration = fixture.getDeclaration(); | |
114 | String fieldName = ""; //$NON-NLS-1$ | |
115 | ||
116 | ArrayDefinition result = new ArrayDefinition(declaration, this.trace, | |
117 | fieldName); | |
118 | ||
119 | assertNotNull(result); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) | |
124 | * constructor test. | |
125 | */ | |
126 | @Test | |
127 | public void testArrayDefinition_newDeclaration() { | |
128 | ArrayDeclaration declaration = new ArrayDeclaration(0, | |
129 | new StringDeclaration()); | |
130 | IDefinitionScope definitionScope = null; | |
131 | String fieldName = ""; //$NON-NLS-1$ | |
132 | ||
133 | ArrayDefinition result = new ArrayDefinition(declaration, | |
134 | definitionScope, fieldName); | |
135 | ||
136 | assertNotNull(result); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Run the ArrayDeclaration getDeclaration() method test. | |
141 | */ | |
142 | @Test | |
143 | public void testGetDeclaration() { | |
144 | fixture.setDefinitions(new Definition[] {}); | |
145 | ArrayDeclaration result = fixture.getDeclaration(); | |
146 | ||
147 | assertNotNull(result); | |
148 | } | |
149 | ||
150 | /** | |
151 | * Run the Definition getElem(int) method test. | |
152 | */ | |
153 | @Test | |
154 | public void testGetElem_noDefs() { | |
155 | int i = 0; | |
156 | Definition result = fixture.getElem(i); | |
157 | ||
158 | assertNotNull(result); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Run the Definition getElem(int) method test. | |
163 | */ | |
164 | @Test | |
165 | public void testGetElem_withDefs() { | |
166 | Definition defs[] = createDefs(); | |
167 | fixture.setDefinitions(defs); | |
168 | int j = 1; | |
169 | ||
170 | Definition result = fixture.getElem(j); | |
171 | ||
172 | assertNotNull(result); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Run the boolean isString() method test. | |
177 | */ | |
178 | @Test | |
179 | public void testIsString_ownDefs() { | |
180 | StringDefinition[] defs = createDefs(); | |
181 | fixture.setDefinitions(defs); | |
182 | ||
183 | boolean result = fixture.isString(); | |
184 | ||
185 | assertFalse(result); | |
186 | } | |
187 | ||
188 | /** | |
189 | * Run the boolean isString() method test. | |
190 | */ | |
191 | @Test | |
192 | public void testIsString_complex() { | |
193 | final IntegerDeclaration id = new IntegerDeclaration(8, false, 16, | |
194 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8); | |
195 | ArrayDeclaration ad = new ArrayDeclaration(0, id); | |
196 | ArrayDefinition ownFixture = new ArrayDefinition(ad, this.trace, | |
197 | "Testx"); //$NON-NLS-1$ | |
198 | ||
199 | int size = 4; | |
200 | IntegerDefinition[] defs = new IntegerDefinition[size]; | |
201 | for (int i = 0; i < size; i++) { | |
202 | ||
203 | String content = "test" + i; //$NON-NLS-1$ | |
204 | defs[i] = new IntegerDefinition(new IntegerDeclaration(8, false, | |
205 | 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8), null, content); | |
206 | defs[i].setValue(i); | |
207 | } | |
208 | ||
209 | ownFixture.setDefinitions(defs); | |
210 | boolean result = ownFixture.isString(); | |
211 | ||
212 | assertTrue(result); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Run the boolean isString() method test. | |
217 | */ | |
218 | @Test | |
219 | public void testIsString_emptyDef() { | |
220 | fixture.setDefinitions(new Definition[] {}); | |
221 | boolean result = fixture.isString(); | |
222 | ||
223 | assertFalse(result); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Run the void read(BitBuffer) method test. | |
228 | */ | |
229 | @Test | |
230 | public void testRead_noDefs() { | |
231 | BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); | |
232 | ||
233 | fixture.read(input); | |
234 | } | |
235 | ||
236 | /** | |
237 | * Run the void read(BitBuffer) method test. | |
238 | */ | |
239 | @Test | |
240 | public void testRead_withDefs() { | |
241 | fixture.setDefinitions(new Definition[] {}); | |
242 | BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); | |
243 | ||
244 | fixture.read(input); | |
245 | } | |
246 | ||
247 | /** | |
248 | * Run the String toString() method test. | |
249 | */ | |
250 | @Test | |
251 | public void testToString_base() { | |
252 | String result = fixture.toString(); | |
253 | ||
254 | assertNotNull(result); | |
255 | } | |
256 | ||
257 | /** | |
258 | * Run the String toString() method test. | |
259 | */ | |
260 | @Test | |
261 | public void testToString_withDefs() { | |
262 | int size = 2; | |
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$ | |
266 | } | |
267 | fixture.setDefinitions(defs); | |
268 | String result = fixture.toString(); | |
269 | ||
270 | assertNotNull(result); | |
271 | } | |
272 | } |