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