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
.tests
.event
;
15 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
16 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventContent
;
17 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventContentStub
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventReference
;
19 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventSource
;
20 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventType
;
21 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEventTypeStub
;
22 import org
.eclipse
.linuxtools
.tmf
.event
.TmfNoSuchFieldException
;
23 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
25 import junit
.framework
.TestCase
;
28 * <b><u>TmfEventContentTest</u></b>
30 * Test suite for the TmfEventContent class.
32 public class TmfEventContentTest
extends TestCase
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private final TmfTimestamp fTimestamp
;
39 private final TmfEventSource fEventSource
;
40 private final TmfEventType fEventType
;
41 private final TmfEventTypeStub fEventTypeStub
;
42 private final TmfEventReference fReference
;
43 private final TmfEvent fEvent
;
44 private final TmfEvent fEventStub
;
46 private final Object fRawContent0
= new String("Some content");
47 private final Object fRawContent1
= new String("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();
64 fEventSource
= new TmfEventSource();
65 fEventType
= new TmfEventType();
66 fEventTypeStub
= new TmfEventTypeStub();
67 fReference
= new TmfEventReference();
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
.getContent());
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
.getContent());
105 public void testTmfEventContentCopy2() {
107 new TmfEventContent(null);
110 catch (IllegalArgumentException e
) {
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
119 public void testEqualsReflexivity() throws Exception
{
120 @SuppressWarnings("unused")
121 Object
[] fields1
= fBasicContent0
.getFields();
122 @SuppressWarnings("unused")
123 Object
[] fields2
= fStubContent
.getFields();
125 assertTrue("equals", fBasicContent0
.equals(fBasicContent0
));
126 assertTrue("equals", fStubContent
.equals(fStubContent
));
128 assertTrue("equals", !fBasicContent0
.equals(fStubContent
));
129 assertTrue("equals", !fStubContent
.equals(fBasicContent0
));
132 public void testEqualsSymmetry() throws Exception
{
133 assertTrue("equals", fBasicContent0
.equals(fBasicContent2
));
134 assertTrue("equals", fBasicContent2
.equals(fBasicContent0
));
136 assertTrue("equals", !fBasicContent0
.equals(fStubContent
));
137 assertTrue("equals", !fStubContent
.equals(fBasicContent0
));
140 public void testEqualsTransivity() throws Exception
{
141 assertTrue("equals", fBasicContent0
.equals(fBasicContent1
));
142 assertTrue("equals", fBasicContent1
.equals(fBasicContent2
));
143 assertTrue("equals", fBasicContent0
.equals(fBasicContent2
));
146 public void testEqualsNull() throws Exception
{
147 assertTrue("equals", !fBasicContent0
.equals(null));
148 assertTrue("equals", !fStubContent
.equals(null));
151 // ------------------------------------------------------------------------
153 // ------------------------------------------------------------------------
155 public void testHashCode() throws Exception
{
156 assertTrue("hashCode", fBasicContent0
.hashCode() == fBasicContent2
.hashCode());
157 assertTrue("hashCode", fBasicContent0
.hashCode() != fStubContent
.hashCode());
160 public void testHashCode2() throws Exception
{
161 TmfEventContent basicContent0
= new TmfEventContent(null, fRawContent0
);
162 TmfEventContent basicContent1
= new TmfEventContent(fEvent
, null);
163 TmfEventContent basicContent2
= new TmfEventContent(null, null);
165 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent0
.hashCode());
166 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent1
.hashCode());
167 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent2
.hashCode());
169 assertTrue("hashCode", basicContent0
.hashCode() != basicContent1
.hashCode());
170 assertTrue("hashCode", basicContent0
.hashCode() != basicContent2
.hashCode());
172 assertTrue("hashCode", basicContent1
.hashCode() != basicContent2
.hashCode());
175 // ------------------------------------------------------------------------
177 // ------------------------------------------------------------------------
179 public void testToString() {
180 String expected
= "[TmfEventContent(" + fRawContent0
+ ")]";
181 TmfEventContent content
= new TmfEventContent(fEvent
, fRawContent0
);
182 assertEquals("toString", expected
, content
.toString());
185 public void testToString2() {
186 String expected
= "[TmfEventContent(1,-10,true,some string,[TmfTimestamp(1,2,3)])]";
187 TmfEventContentStub content
= new TmfEventContentStub(fEvent
, fRawContent0
);
188 assertEquals("toString", expected
, content
.toString());
191 // ------------------------------------------------------------------------
192 // Basic content parsing
193 // ------------------------------------------------------------------------
195 public void testGetFields() {
196 Object
[] fields
= fBasicContent0
.getFields();
197 assertEquals("getFields", 1, fields
.length
);
198 assertEquals("getFields", fRawContent0
, fields
[0].toString());
201 public void testGetFieldFromId() {
204 field
= fBasicContent0
.getField("Content");
205 assertEquals("getField", fRawContent0
, field
.toString());
206 } catch (TmfNoSuchFieldException e
) {
207 fail("Field not found");
211 public void testGetFieldFromIdFailed() {
213 fBasicContent0
.getField("Dummy");
214 fail("Found an inexisting field...");
215 } catch (TmfNoSuchFieldException e
) {
220 public void testGetFieldFromPos() {
221 Object field
= fBasicContent0
.getField(0);
222 assertEquals("getField", fRawContent0
, field
.toString());
225 public void testGetFieldFromPosFailed() {
226 Object field
= fBasicContent0
.getField(10);
227 assertEquals("getField", null, field
);
230 // ------------------------------------------------------------------------
231 // Standard content parsing
232 // ------------------------------------------------------------------------
234 public void testGetFields2() {
235 Object
[] fields
= fStubContent
.getFields();
236 assertEquals("getFields", 5, fields
.length
);
239 public void testGetFieldFromId2() {
242 field
= fStubContent
.getField("Field1");
243 assertEquals("getField", new Integer(1), field
);
245 field
= fStubContent
.getField("Field2");
246 assertEquals("getField", new Integer(-10), field
);
248 field
= fStubContent
.getField("Field3");
249 assertEquals("getField", new Boolean(true), field
);
251 field
= fStubContent
.getField("Field4");
252 assertEquals("getField", new String("some string"), field
);
254 field
= fStubContent
.getField("Field5");
255 assertEquals("getField", new TmfTimestamp(1, (byte) 2, 3), field
);
257 } catch (TmfNoSuchFieldException e
) {
258 fail("Field not found");
262 public void testGetFieldFromPos2() {
263 TmfEventContentStub content
= new TmfEventContentStub(fEvent
, fRawContent0
);
266 field
= content
.getField(0);
267 assertEquals("getField", new Integer(1), field
);
269 field
= content
.getField(1);
270 assertEquals("getField", new Integer(-10), field
);
272 field
= content
.getField(2);
273 assertEquals("getField", new Boolean(true), field
);
275 field
= content
.getField(3);
276 assertEquals("getField", new String("some string"), field
);
278 field
= content
.getField(4);
279 assertEquals("getField", new TmfTimestamp(1, (byte) 2, 3), field
);