Merge branch 'master' into TmfEventModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfEventContentTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.event;
14
15 import junit.framework.TestCase;
16
17 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
18 import org.eclipse.linuxtools.tmf.core.event.TmfEventContent;
19 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
20 import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
21 import org.eclipse.linuxtools.tmf.core.event.TmfNoSuchFieldException;
22 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
23 import org.eclipse.linuxtools.tmf.stubs.event.TmfEventContentStub;
24 import org.eclipse.linuxtools.tmf.stubs.event.TmfEventTypeStub;
25
26 /**
27 * <b><u>TmfEventContentTest</u></b>
28 * <p>
29 * Test suite for the TmfEventContent class.
30 */
31 @SuppressWarnings("nls")
32 public class TmfEventContentTest extends TestCase {
33
34 // ------------------------------------------------------------------------
35 // Variables
36 // ------------------------------------------------------------------------
37
38 private final TmfTimestamp fTimestamp;
39 private final String fEventSource;
40 private final TmfEventType fEventType;
41 private final TmfEventTypeStub fEventTypeStub;
42 private final String fReference;
43 private final TmfEvent fEvent;
44 private final TmfEvent fEventStub;
45
46 private final String fRawContent0 = "Some content";
47 private final String fRawContent1 = "Some other content";
48
49 private final TmfEventContent fBasicContent0;
50 private final TmfEventContent fBasicContent1;
51 private final TmfEventContent fBasicContent2;
52 private final TmfEventContentStub fStubContent;
53
54 // ------------------------------------------------------------------------
55 // Housekeeping
56 // ------------------------------------------------------------------------
57
58 /**
59 * @param name the test name
60 */
61 public TmfEventContentTest(String name) {
62 super(name);
63 fTimestamp = new TmfTimestamp();
64 fEventSource = "";
65 fEventType = new TmfEventTypeStub();
66 fEventTypeStub = new TmfEventTypeStub();
67 fReference = "";
68
69 fEvent = new TmfEvent(fTimestamp, fEventSource, fEventType, fReference);
70 fBasicContent0 = new TmfEventContent(fEvent, fRawContent0);
71 fBasicContent1 = new TmfEventContent(fEvent, fRawContent0);
72 fBasicContent2 = new TmfEventContent(fEvent, fRawContent0);
73
74 fEventStub = new TmfEvent(fTimestamp, fEventSource, fEventTypeStub, fReference);
75 fStubContent = new TmfEventContentStub(fEventStub, fRawContent1);
76 }
77
78 @Override
79 protected void setUp() throws Exception {
80 super.setUp();
81 }
82
83 @Override
84 protected void tearDown() throws Exception {
85 super.tearDown();
86 }
87
88 // ------------------------------------------------------------------------
89 // Constructors
90 // ------------------------------------------------------------------------
91
92 public void testTmfEventContent() {
93 assertSame("getLabels", fEvent, fBasicContent0.getEvent());
94 assertEquals("getType", fEventType, fBasicContent0.getType());
95 assertEquals("getContent", fRawContent0, fBasicContent0.getRawContent());
96 }
97
98 public void testTmfEventContentCopy() {
99 TmfEventContent content = new TmfEventContent(fBasicContent0);
100 assertSame("getLabels", fEvent, content.getEvent());
101 assertEquals("getType", fEventType, content.getType());
102 assertEquals("getContent", fRawContent0, content.getRawContent());
103 }
104
105 public void testTmfEventContentCopy2() {
106 try {
107 new TmfEventContent(null);
108 fail("null copy");
109 }
110 catch (IllegalArgumentException e) {
111 // Success
112 }
113 }
114
115 // // ------------------------------------------------------------------------
116 // // setEvent
117 // // ------------------------------------------------------------------------
118 //
119 // public void testSetEvent() {
120 // TmfEvent event = new TmfEvent(fTimestamp, fEventSource, fEventType, fReference);
121 // TmfEventContent content1 = new TmfEventContent(event, fRawContent0);
122 // event.setContent(content1);
123 // TmfEventContent content2 = new TmfEventContent(null, fRawContent1);
124 //
125 // content2.setEvent(event);
126 //
127 // assertEquals("setEvent", event, content2.getEvent());
128 //// assertEquals("setEvent", content2, event.getContent());
129 //// assertEquals("setEvent", null, content1.getEvent());
130 // }
131
132 // ------------------------------------------------------------------------
133 // equals
134 // ------------------------------------------------------------------------
135
136 public void testEqualsReflexivity() throws Exception {
137 @SuppressWarnings("unused")
138 Object[] fields1 = fBasicContent0.getFields();
139 @SuppressWarnings("unused")
140 Object[] fields2 = fStubContent.getFields();
141
142 assertTrue("equals", fBasicContent0.equals(fBasicContent0));
143 assertTrue("equals", fStubContent.equals(fStubContent));
144
145 assertTrue("equals", !fBasicContent0.equals(fStubContent));
146 assertTrue("equals", !fStubContent.equals(fBasicContent0));
147 }
148
149 public void testEqualsSymmetry() throws Exception {
150 assertTrue("equals", fBasicContent0.equals(fBasicContent2));
151 assertTrue("equals", fBasicContent2.equals(fBasicContent0));
152
153 assertTrue("equals", !fBasicContent0.equals(fStubContent));
154 assertTrue("equals", !fStubContent.equals(fBasicContent0));
155 }
156
157 public void testEqualsTransivity() throws Exception {
158 assertTrue("equals", fBasicContent0.equals(fBasicContent1));
159 assertTrue("equals", fBasicContent1.equals(fBasicContent2));
160 assertTrue("equals", fBasicContent0.equals(fBasicContent2));
161 }
162
163 public void testEqualsNull() throws Exception {
164 assertTrue("equals", !fBasicContent0.equals(null));
165 assertTrue("equals", !fStubContent.equals(null));
166 }
167
168 // ------------------------------------------------------------------------
169 // hashCode
170 // ------------------------------------------------------------------------
171
172 public void testHashCode() throws Exception {
173 assertTrue("hashCode", fBasicContent0.hashCode() == fBasicContent2.hashCode());
174 assertTrue("hashCode", fBasicContent0.hashCode() != fStubContent.hashCode());
175 }
176
177 public void testHashCode2() throws Exception {
178 TmfEventContent basicContent0 = new TmfEventContent(null, fRawContent0);
179 TmfEventContent basicContent1 = new TmfEventContent(fEvent, null);
180 TmfEventContent basicContent2 = new TmfEventContent(null, null);
181
182 assertTrue("hashCode", fBasicContent0.hashCode() == basicContent0.hashCode());
183 assertTrue("hashCode", fBasicContent0.hashCode() != basicContent1.hashCode());
184 assertTrue("hashCode", fBasicContent0.hashCode() != basicContent2.hashCode());
185
186 assertTrue("hashCode", basicContent0.hashCode() != basicContent1.hashCode());
187 assertTrue("hashCode", basicContent0.hashCode() != basicContent2.hashCode());
188
189 assertTrue("hashCode", basicContent1.hashCode() == basicContent2.hashCode());
190 }
191
192 // ------------------------------------------------------------------------
193 // toString
194 // ------------------------------------------------------------------------
195
196 public void testToString() {
197 String expected = "TmfEventContent [fRawContent=" + fRawContent0 + ", fFields=null]";
198 TmfEventContent content = new TmfEventContent(fEvent, fRawContent0);
199 assertEquals("toString", expected, content.toString());
200 }
201
202 public void testToString2() {
203 String expected = "TmfEventContent [fRawContent=" + fRawContent0 + ", fFields=null]";
204 TmfEventContentStub content = new TmfEventContentStub(fEvent, fRawContent0);
205 assertEquals("toString", expected, content.toString());
206 }
207
208 // ------------------------------------------------------------------------
209 // Basic content parsing
210 // ------------------------------------------------------------------------
211
212 public void testGetFields() {
213 TmfEventField expected = new TmfEventField(fBasicContent0, TmfEventContent.FIELD_ID_CONTENT, fRawContent0);
214 Object[] fields = fBasicContent0.getFields();
215 assertEquals("getFields", 1, fields.length);
216 assertEquals("getFields", expected, fields[0]);
217 }
218
219 // public void testGetFieldFromId() {
220 // Object field;
221 // try {
222 // field = fStubContent.getField("Content");
223 // assertEquals("getField", fRawContent0, field.toString());
224 // } catch (TmfNoSuchFieldException e) {
225 // fail("Field not found");
226 // }
227 // }
228
229 public void testGetFieldFromIdFailed() {
230 try {
231 fBasicContent0.getField("Dummy");
232 fail("Found an inexisting field...");
233 } catch (TmfNoSuchFieldException e) {
234 // Success
235 }
236 }
237
238 public void testGetFieldFromPos() {
239 String expected = "TmfEventField [fFieldId=" +
240 TmfEventContent.FIELD_ID_CONTENT + ", fValue=" + fRawContent0 + "]";
241 Object field = fBasicContent0.getField(0);
242 assertEquals("getField", expected, field.toString());
243 }
244
245 public void testGetFieldFromPosFailed() {
246 Object field = fBasicContent0.getField(10);
247 assertEquals("getField", null, field);
248 }
249
250 // ------------------------------------------------------------------------
251 // Standard content parsing
252 // ------------------------------------------------------------------------
253
254 public void testGetFields2() {
255 Object[] fields = fStubContent.getFields();
256 assertEquals("getFields", 5, fields.length);
257 }
258
259 public void testGetFieldFromId2() {
260 Object field;
261 TmfEventField expected;
262 try {
263 field = fStubContent.getField("Field1");
264 expected = new TmfEventField(fStubContent, "field1", new Integer(1));
265 assertEquals("getField", expected, field);
266
267 field = fStubContent.getField("Field2");
268 expected = new TmfEventField(fStubContent, "field2", new Integer(-10));
269 assertEquals("getField", expected, field);
270
271 field = fStubContent.getField("Field3");
272 expected = new TmfEventField(fStubContent, "field3", new Boolean(true));
273 assertEquals("getField", expected, field);
274
275 field = fStubContent.getField("Field4");
276 expected = new TmfEventField(fStubContent, "field4", "some string");
277 assertEquals("getField", expected, field);
278
279 field = fStubContent.getField("Field5");
280 expected = new TmfEventField(fStubContent, "field5", new TmfTimestamp(1, 2, 3));
281 assertEquals("getField", expected, field);
282
283 } catch (TmfNoSuchFieldException e) {
284 fail("Field not found");
285 }
286 }
287
288 public void testGetFieldFromPos2() {
289 TmfEventContentStub content = new TmfEventContentStub(fEvent, fRawContent0);
290
291 Object field;
292 TmfEventField expected;
293
294 field = content.getField(0);
295 expected = new TmfEventField(content, "field1", new Integer(1));
296 assertEquals("getField", expected, field);
297
298 field = content.getField(1);
299 expected = new TmfEventField(content, "field2", new Integer(-10));
300 assertEquals("getField", expected, field);
301
302 field = content.getField(2);
303 expected = new TmfEventField(content, "field3", new Boolean(true));
304 assertEquals("getField", expected, field);
305
306 field = content.getField(3);
307 expected = new TmfEventField(content, "field4", "some string");
308 assertEquals("getField",expected, field);
309
310 field = content.getField(4);
311 expected = new TmfEventField(content, "field5", new TmfTimestamp(1, 2, 3));
312 assertEquals("getField", expected, field);
313 }
314
315 }
This page took 0.040353 seconds and 5 git commands to generate.