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 testEqualsConsistency() throws Exception
{
147 assertTrue("equals", fBasicContent0
.equals(fBasicContent0
));
148 assertTrue("equals", fBasicContent0
.equals(fBasicContent0
));
150 assertTrue("equals", fStubContent
.equals(fStubContent
));
151 assertTrue("equals", fStubContent
.equals(fStubContent
));
154 public void testEqualsNull() throws Exception
{
155 assertTrue("equals", !fBasicContent0
.equals(null));
156 assertTrue("equals", !fStubContent
.equals(null));
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
163 public void testHashCode() throws Exception
{
164 assertTrue("hashCode", fBasicContent0
.hashCode() == fBasicContent2
.hashCode());
165 assertTrue("hashCode", fBasicContent0
.hashCode() != fStubContent
.hashCode());
168 public void testHashCode2() throws Exception
{
169 TmfEventContent basicContent0
= new TmfEventContent(null, fRawContent0
);
170 TmfEventContent basicContent1
= new TmfEventContent(fEvent
, null);
171 TmfEventContent basicContent2
= new TmfEventContent(null, null);
173 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent0
.hashCode());
174 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent1
.hashCode());
175 assertTrue("hashCode", fBasicContent0
.hashCode() != basicContent2
.hashCode());
177 assertTrue("hashCode", basicContent0
.hashCode() != basicContent1
.hashCode());
178 assertTrue("hashCode", basicContent0
.hashCode() != basicContent2
.hashCode());
180 assertTrue("hashCode", basicContent1
.hashCode() != basicContent2
.hashCode());
183 // ------------------------------------------------------------------------
185 // ------------------------------------------------------------------------
187 public void testToString() {
188 String expected
= "[TmfEventContent(" + fRawContent0
+ ")]";
189 TmfEventContent content
= new TmfEventContent(fEvent
, fRawContent0
);
190 assertEquals("toString", expected
, content
.toString());
193 public void testToString2() {
194 String expected
= "[TmfEventContent(1,-10,true,some string,[TmfTimestamp(1,2,3)])]";
195 TmfEventContentStub content
= new TmfEventContentStub(fEvent
, fRawContent0
);
196 assertEquals("toString", expected
, content
.toString());
199 // ------------------------------------------------------------------------
200 // Basic content parsing
201 // ------------------------------------------------------------------------
203 public void testGetFields() {
204 Object
[] fields
= fBasicContent0
.getFields();
205 assertEquals("getFields", 1, fields
.length
);
206 assertEquals("getFields", fRawContent0
, fields
[0].toString());
209 public void testGetFieldFromId() {
212 field
= fBasicContent0
.getField("Content");
213 assertEquals("getField", fRawContent0
, field
.toString());
214 } catch (TmfNoSuchFieldException e
) {
215 fail("Field not found");
219 public void testGetFieldFromIdFailed() {
221 fBasicContent0
.getField("Dummy");
222 fail("Found an inexisting field...");
223 } catch (TmfNoSuchFieldException e
) {
228 public void testGetFieldFromPos() {
229 Object field
= fBasicContent0
.getField(0);
230 assertEquals("getField", fRawContent0
, field
.toString());
233 public void testGetFieldFromPosFailed() {
234 Object field
= fBasicContent0
.getField(10);
235 assertEquals("getField", null, field
);
238 // ------------------------------------------------------------------------
239 // Standard content parsing
240 // ------------------------------------------------------------------------
242 public void testGetFields2() {
243 Object
[] fields
= fStubContent
.getFields();
244 assertEquals("getFields", 5, fields
.length
);
247 public void testGetFieldFromId2() {
250 field
= fStubContent
.getField("Field1");
251 assertEquals("getField", new Integer(1), field
);
253 field
= fStubContent
.getField("Field2");
254 assertEquals("getField", new Integer(-10), field
);
256 field
= fStubContent
.getField("Field3");
257 assertEquals("getField", new Boolean(true), field
);
259 field
= fStubContent
.getField("Field4");
260 assertEquals("getField", new String("some string"), field
);
262 field
= fStubContent
.getField("Field5");
263 assertEquals("getField", new TmfTimestamp(1, (byte) 2, 3), field
);
265 } catch (TmfNoSuchFieldException e
) {
266 fail("Field not found");
270 public void testGetFieldFromPos2() {
271 TmfEventContentStub content
= new TmfEventContentStub(fEvent
, fRawContent0
);
274 field
= content
.getField(0);
275 assertEquals("getField", new Integer(1), field
);
277 field
= content
.getField(1);
278 assertEquals("getField", new Integer(-10), field
);
280 field
= content
.getField(2);
281 assertEquals("getField", new Boolean(true), field
);
283 field
= content
.getField(3);
284 assertEquals("getField", new String("some string"), field
);
286 field
= content
.getField(4);
287 assertEquals("getField", new TmfTimestamp(1, (byte) 2, 3), field
);