1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
15 import junit
.framework
.TestCase
;
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
;
27 * <b><u>TmfEventContentTest</u></b>
29 * Test suite for the TmfEventContent class.
31 @SuppressWarnings("nls")
32 public class TmfEventContentTest
extends TestCase
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
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
;
46 private final String fRawContent0
= "Some content";
47 private final String fRawContent1
= "Some other content";
49 private final TmfEventContent fBasicContent0
;
50 private final TmfEventContent fBasicContent1
;
51 private final TmfEventContent fBasicContent2
;
52 private final TmfEventContentStub fStubContent
;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * @param name the test name
61 public TmfEventContentTest(String name
) {
63 fTimestamp
= new TmfTimestamp();
65 fEventType
= new TmfEventTypeStub();
66 fEventTypeStub
= new TmfEventTypeStub();
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
);
74 fEventStub
= new TmfEvent(fTimestamp
, fEventSource
, fEventTypeStub
, fReference
);
75 fStubContent
= new TmfEventContentStub(fEventStub
, fRawContent1
);
79 protected void setUp() throws Exception
{
84 protected void tearDown() throws Exception
{
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
92 public void testTmfEventContent() {
93 assertSame("getLabels", fEvent
, fBasicContent0
.getEvent());
94 assertEquals("getType", fEventType
, fBasicContent0
.getType());
95 assertEquals("getContent", fRawContent0
, fBasicContent0
.getRawContent());
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());
105 public void testTmfEventContentCopy2() {
107 new TmfEventContent(null);
110 catch (IllegalArgumentException e
) {
115 // // ------------------------------------------------------------------------
117 // // ------------------------------------------------------------------------
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);
125 // content2.setEvent(event);
127 // assertEquals("setEvent", event, content2.getEvent());
128 //// assertEquals("setEvent", content2, event.getContent());
129 //// assertEquals("setEvent", null, content1.getEvent());
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
136 public void testEqualsReflexivity() throws Exception
{
137 @SuppressWarnings("unused")
138 Object
[] fields1
= fBasicContent0
.getFields();
139 @SuppressWarnings("unused")
140 Object
[] fields2
= fStubContent
.getFields();
142 assertTrue("equals", fBasicContent0
.equals(fBasicContent0
));
143 assertTrue("equals", fStubContent
.equals(fStubContent
));
145 assertTrue("equals", !fBasicContent0
.equals(fStubContent
));
146 assertTrue("equals", !fStubContent
.equals(fBasicContent0
));
149 public void testEqualsSymmetry() throws Exception
{
150 assertTrue("equals", fBasicContent0
.equals(fBasicContent2
));
151 assertTrue("equals", fBasicContent2
.equals(fBasicContent0
));
153 assertTrue("equals", !fBasicContent0
.equals(fStubContent
));
154 assertTrue("equals", !fStubContent
.equals(fBasicContent0
));
157 public void testEqualsTransivity() throws Exception
{
158 assertTrue("equals", fBasicContent0
.equals(fBasicContent1
));
159 assertTrue("equals", fBasicContent1
.equals(fBasicContent2
));
160 assertTrue("equals", fBasicContent0
.equals(fBasicContent2
));
163 public void testEqualsNull() throws Exception
{
164 assertTrue("equals", !fBasicContent0
.equals(null));
165 assertTrue("equals", !fStubContent
.equals(null));
168 // ------------------------------------------------------------------------
170 // ------------------------------------------------------------------------
172 public void testHashCode() throws Exception
{
173 assertTrue("hashCode", fBasicContent0
.hashCode() == fBasicContent2
.hashCode());
174 assertTrue("hashCode", fBasicContent0
.hashCode() != fStubContent
.hashCode());
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);
182 assertTrue("hashCode", fBasicContent0
.hashCode() == basicContent0
.hashCode());
183 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent1
.hashCode());
184 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent2
.hashCode());
186 assertTrue("hashCode", basicContent0
.hashCode() != basicContent1
.hashCode());
187 assertTrue("hashCode", basicContent0
.hashCode() != basicContent2
.hashCode());
189 assertTrue("hashCode", basicContent1
.hashCode() == basicContent2
.hashCode());
192 // ------------------------------------------------------------------------
194 // ------------------------------------------------------------------------
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());
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());
208 // ------------------------------------------------------------------------
209 // Basic content parsing
210 // ------------------------------------------------------------------------
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]);
219 // public void testGetFieldFromId() {
222 // field = fStubContent.getField("Content");
223 // assertEquals("getField", fRawContent0, field.toString());
224 // } catch (TmfNoSuchFieldException e) {
225 // fail("Field not found");
229 public void testGetFieldFromIdFailed() {
231 fBasicContent0
.getField("Dummy");
232 fail("Found an inexisting field...");
233 } catch (TmfNoSuchFieldException e
) {
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());
245 public void testGetFieldFromPosFailed() {
246 Object field
= fBasicContent0
.getField(10);
247 assertEquals("getField", null, field
);
250 // ------------------------------------------------------------------------
251 // Standard content parsing
252 // ------------------------------------------------------------------------
254 public void testGetFields2() {
255 Object
[] fields
= fStubContent
.getFields();
256 assertEquals("getFields", 5, fields
.length
);
259 public void testGetFieldFromId2() {
261 TmfEventField expected
;
263 field
= fStubContent
.getField("Field1");
264 expected
= new TmfEventField(fStubContent
, "field1", new Integer(1));
265 assertEquals("getField", expected
, field
);
267 field
= fStubContent
.getField("Field2");
268 expected
= new TmfEventField(fStubContent
, "field2", new Integer(-10));
269 assertEquals("getField", expected
, field
);
271 field
= fStubContent
.getField("Field3");
272 expected
= new TmfEventField(fStubContent
, "field3", new Boolean(true));
273 assertEquals("getField", expected
, field
);
275 field
= fStubContent
.getField("Field4");
276 expected
= new TmfEventField(fStubContent
, "field4", "some string");
277 assertEquals("getField", expected
, field
);
279 field
= fStubContent
.getField("Field5");
280 expected
= new TmfEventField(fStubContent
, "field5", new TmfTimestamp(1, 2, 3));
281 assertEquals("getField", expected
, field
);
283 } catch (TmfNoSuchFieldException e
) {
284 fail("Field not found");
288 public void testGetFieldFromPos2() {
289 TmfEventContentStub content
= new TmfEventContentStub(fEvent
, fRawContent0
);
292 TmfEventField expected
;
294 field
= content
.getField(0);
295 expected
= new TmfEventField(content
, "field1", new Integer(1));
296 assertEquals("getField", expected
, field
);
298 field
= content
.getField(1);
299 expected
= new TmfEventField(content
, "field2", new Integer(-10));
300 assertEquals("getField", expected
, field
);
302 field
= content
.getField(2);
303 expected
= new TmfEventField(content
, "field3", new Boolean(true));
304 assertEquals("getField", expected
, field
);
306 field
= content
.getField(3);
307 expected
= new TmfEventField(content
, "field4", "some string");
308 assertEquals("getField",expected
, field
);
310 field
= content
.getField(4);
311 expected
= new TmfEventField(content
, "field5", new TmfTimestamp(1, 2, 3));
312 assertEquals("getField", expected
, field
);