Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2009, 2010, 2013 Ericsson |
9b749023 | 3 | * |
d18dd09b ASL |
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 | |
9b749023 | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
d18dd09b ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.tests.request; |
d18dd09b | 15 | |
6e1886bc AM |
16 | import static org.junit.Assert.assertEquals; |
17 | import static org.junit.Assert.assertFalse; | |
18 | import static org.junit.Assert.assertTrue; | |
d18dd09b | 19 | |
2771b032 | 20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
6c13869b FC |
21 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
22 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
23 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; | |
24 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; | |
4918b8f2 | 25 | import org.eclipse.linuxtools.tmf.tests.stubs.request.TmfEventRequestStub; |
6e1886bc AM |
26 | import org.junit.Before; |
27 | import org.junit.Test; | |
d18dd09b ASL |
28 | |
29 | /** | |
2fb2eb37 | 30 | * Test suite for the TmfEventRequest class. |
d18dd09b | 31 | */ |
6e1886bc AM |
32 | @SuppressWarnings({ "nls", "javadoc" }) |
33 | public class TmfEventRequestTest { | |
34 | ||
35 | // ------------------------------------------------------------------------ | |
36 | // Variables | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
39 | private static TmfTimeRange range1 = new TmfTimeRange(TmfTimeRange.ETERNITY); | |
40 | private static TmfTimeRange range2 = new TmfTimeRange(new TmfTimestamp(), TmfTimestamp.BIG_CRUNCH); | |
41 | ||
42 | private static TmfEventRequest fRequest1; | |
43 | private static TmfEventRequest fRequest1b; | |
44 | private static TmfEventRequest fRequest1c; | |
45 | private static TmfEventRequest fRequest2; | |
46 | private static TmfEventRequest fRequest3; | |
47 | private static TmfEventRequest fRequest4; | |
48 | ||
49 | private static int fRequestCount; | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Housekeeping | |
53 | // ------------------------------------------------------------------------ | |
54 | ||
55 | @Before | |
56 | public void setUp() { | |
57 | TmfDataRequest.reset(); | |
58 | fRequest1 = new TmfEventRequestStub(ITmfEvent.class, range1, 100, 200); | |
59 | fRequest2 = new TmfEventRequestStub(ITmfEvent.class, range2, 100, 200); | |
60 | fRequest3 = new TmfEventRequestStub(ITmfEvent.class, range2, 200, 200); | |
61 | fRequest4 = new TmfEventRequestStub(ITmfEvent.class, range2, 200, 300); | |
62 | fRequest1b = new TmfEventRequestStub(ITmfEvent.class, range1, 100, 200); | |
63 | fRequest1c = new TmfEventRequestStub(ITmfEvent.class, range1, 100, 200); | |
64 | fRequestCount = fRequest1c.getRequestId() + 1; | |
65 | } | |
66 | ||
67 | private static TmfEventRequest setupTestRequest(final boolean[] flags) { | |
68 | ||
69 | TmfEventRequest request = new TmfEventRequestStub(ITmfEvent.class, new TmfTimeRange(TmfTimeRange.ETERNITY), 100, 200) { | |
70 | @Override | |
71 | public void handleCompleted() { | |
72 | super.handleCompleted(); | |
73 | flags[0] = true; | |
74 | } | |
75 | ||
76 | @Override | |
77 | public void handleSuccess() { | |
78 | super.handleSuccess(); | |
79 | flags[1] = true; | |
80 | } | |
81 | ||
82 | @Override | |
83 | public void handleFailure() { | |
84 | super.handleFailure(); | |
85 | flags[2] = true; | |
86 | } | |
87 | ||
88 | @Override | |
89 | public void handleCancel() { | |
90 | super.handleCancel(); | |
91 | flags[3] = true; | |
92 | } | |
93 | }; | |
94 | return request; | |
95 | } | |
96 | ||
97 | // ------------------------------------------------------------------------ | |
98 | // Constructors | |
99 | // ------------------------------------------------------------------------ | |
100 | ||
101 | @Test | |
102 | public void testTmfEventRequest() { | |
2771b032 | 103 | TmfEventRequest request = new TmfEventRequestStub(ITmfEvent.class); |
d18dd09b | 104 | |
2fb2eb37 | 105 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); |
6e1886bc | 106 | assertEquals("getDataType", ITmfEvent.class, request.getDataType()); |
2fb2eb37 | 107 | |
6e1886bc AM |
108 | assertEquals("StartTime", TmfTimestamp.BIG_BANG, request.getRange().getStartTime()); |
109 | assertEquals("EndTime", TmfTimestamp.BIG_CRUNCH, request.getRange().getEndTime()); | |
d18dd09b | 110 | |
2fb2eb37 | 111 | assertEquals("getIndex", 0, request.getIndex()); |
d18dd09b | 112 | assertEquals("getNbRequestedEvents", TmfDataRequest.ALL_DATA, request.getNbRequested()); |
2fb2eb37 FC |
113 | |
114 | assertFalse("isCompleted", request.isCompleted()); | |
115 | assertFalse("isFailed", request.isFailed()); | |
116 | assertFalse("isCancelled", request.isCancelled()); | |
117 | ||
118 | assertEquals("getNbRead", 0, request.getNbRead()); | |
6e1886bc | 119 | } |
d18dd09b | 120 | |
6e1886bc AM |
121 | @Test |
122 | public void testTmfEventRequestTimeRange() { | |
a4115405 | 123 | TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(), TmfTimestamp.BIG_CRUNCH); |
2771b032 | 124 | TmfEventRequest request = new TmfEventRequestStub(ITmfEvent.class, range); |
d18dd09b | 125 | |
2fb2eb37 | 126 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); |
2771b032 | 127 | assertEquals("getDataType", ITmfEvent.class, request.getDataType()); |
2fb2eb37 FC |
128 | |
129 | assertEquals("StartTime", new TmfTimestamp(), request.getRange().getStartTime()); | |
6e1886bc | 130 | assertEquals("EndTime", TmfTimestamp.BIG_CRUNCH, request.getRange().getEndTime()); |
d18dd09b | 131 | |
2fb2eb37 | 132 | assertEquals("getIndex", 0, request.getIndex()); |
d18dd09b | 133 | assertEquals("getNbRequestedEvents", TmfDataRequest.ALL_DATA, request.getNbRequested()); |
2fb2eb37 FC |
134 | |
135 | assertFalse("isCompleted", request.isCompleted()); | |
136 | assertFalse("isFailed", request.isFailed()); | |
137 | assertFalse("isCancelled", request.isCancelled()); | |
138 | ||
139 | assertEquals("getNbRead", 0, request.getNbRead()); | |
6e1886bc | 140 | } |
d18dd09b | 141 | |
6e1886bc AM |
142 | @Test |
143 | public void testTmfEventRequestTimeRangeNbRequested() { | |
a4115405 | 144 | TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(), TmfTimestamp.BIG_CRUNCH); |
2771b032 | 145 | TmfEventRequest request = new TmfEventRequestStub(ITmfEvent.class, range, 100); |
d18dd09b | 146 | |
2fb2eb37 | 147 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); |
2771b032 | 148 | assertEquals("getDataType", ITmfEvent.class, request.getDataType()); |
2fb2eb37 FC |
149 | |
150 | assertEquals("StartTime", new TmfTimestamp(), request.getRange().getStartTime()); | |
6e1886bc | 151 | assertEquals("EndTime", TmfTimestamp.BIG_CRUNCH, request.getRange().getEndTime()); |
d18dd09b | 152 | |
2fb2eb37 | 153 | assertEquals("getIndex", 0, request.getIndex()); |
d18dd09b | 154 | assertEquals("getNbRequestedEvents", 100, request.getNbRequested()); |
2fb2eb37 FC |
155 | |
156 | assertFalse("isCompleted", request.isCompleted()); | |
157 | assertFalse("isFailed", request.isFailed()); | |
158 | assertFalse("isCancelled", request.isCancelled()); | |
159 | ||
160 | assertEquals("getNbRead", 0, request.getNbRead()); | |
6e1886bc | 161 | } |
d18dd09b | 162 | |
6e1886bc AM |
163 | @Test |
164 | public void testTmfEventRequestTimeRangeNbRequestedBlocksize() { | |
a4115405 | 165 | TmfTimeRange range = new TmfTimeRange(new TmfTimestamp(), TmfTimestamp.BIG_CRUNCH); |
2771b032 | 166 | TmfEventRequest request = new TmfEventRequestStub(ITmfEvent.class, range, 100, 200); |
d18dd09b | 167 | |
2fb2eb37 | 168 | assertEquals("getRequestId", fRequestCount++, request.getRequestId()); |
2771b032 | 169 | assertEquals("getDataType", ITmfEvent.class, request.getDataType()); |
d18dd09b | 170 | |
2fb2eb37 | 171 | assertEquals("StartTime", new TmfTimestamp(), request.getRange().getStartTime()); |
6e1886bc | 172 | assertEquals("EndTime", TmfTimestamp.BIG_CRUNCH, request.getRange().getEndTime()); |
2fb2eb37 FC |
173 | |
174 | assertEquals("getIndex", 0, request.getIndex()); | |
d18dd09b | 175 | assertEquals("getNbRequestedEvents", 100, request.getNbRequested()); |
2fb2eb37 FC |
176 | |
177 | assertFalse("isCompleted", request.isCompleted()); | |
178 | assertFalse("isFailed", request.isFailed()); | |
179 | assertFalse("isCancelled", request.isCancelled()); | |
180 | ||
181 | assertEquals("getNbRead", 0, request.getNbRead()); | |
6e1886bc | 182 | } |
d18dd09b | 183 | |
6e1886bc AM |
184 | // ------------------------------------------------------------------------ |
185 | // equals | |
186 | // ------------------------------------------------------------------------ | |
951d134a | 187 | |
6e1886bc AM |
188 | @Test |
189 | public void testEqualsReflexivity() { | |
2fb2eb37 FC |
190 | assertTrue("equals", fRequest1.equals(fRequest1)); |
191 | assertTrue("equals", fRequest2.equals(fRequest2)); | |
192 | ||
193 | assertFalse("equals", fRequest1.equals(fRequest2)); | |
194 | assertFalse("equals", fRequest2.equals(fRequest1)); | |
6e1886bc | 195 | } |
2fb2eb37 | 196 | |
6e1886bc AM |
197 | @Test |
198 | public void testEqualsSymmetry() { | |
2fb2eb37 FC |
199 | assertTrue("equals", fRequest1.equals(fRequest1b)); |
200 | assertTrue("equals", fRequest1b.equals(fRequest1)); | |
201 | ||
202 | assertFalse("equals", fRequest1.equals(fRequest3)); | |
203 | assertFalse("equals", fRequest2.equals(fRequest3)); | |
204 | assertFalse("equals", fRequest3.equals(fRequest1)); | |
205 | assertFalse("equals", fRequest3.equals(fRequest2)); | |
6e1886bc | 206 | } |
9b749023 | 207 | |
6e1886bc AM |
208 | @Test |
209 | public void testEqualsTransivity() { | |
2fb2eb37 FC |
210 | assertTrue("equals", fRequest1.equals(fRequest1b)); |
211 | assertTrue("equals", fRequest1b.equals(fRequest1c)); | |
212 | assertTrue("equals", fRequest1.equals(fRequest1c)); | |
6e1886bc | 213 | } |
9b749023 | 214 | |
6e1886bc AM |
215 | @Test |
216 | public void testEqualsNull() { | |
2fb2eb37 FC |
217 | assertFalse("equals", fRequest1.equals(null)); |
218 | assertFalse("equals", fRequest2.equals(null)); | |
6e1886bc | 219 | } |
2fb2eb37 | 220 | |
6e1886bc AM |
221 | // ------------------------------------------------------------------------ |
222 | // hashCode | |
223 | // ------------------------------------------------------------------------ | |
2fb2eb37 | 224 | |
6e1886bc AM |
225 | @Test |
226 | public void testHashCode() { | |
2fb2eb37 FC |
227 | assertTrue("hashCode", fRequest1.hashCode() == fRequest1.hashCode()); |
228 | assertTrue("hashCode", fRequest2.hashCode() == fRequest2.hashCode()); | |
6e1886bc AM |
229 | assertTrue("hashCode", fRequest1.hashCode() != fRequest2.hashCode()); |
230 | } | |
9b749023 | 231 | |
6e1886bc AM |
232 | // ------------------------------------------------------------------------ |
233 | // toString | |
234 | // ------------------------------------------------------------------------ | |
951d134a | 235 | |
6e1886bc AM |
236 | @Test |
237 | public void testToString() { | |
2771b032 AM |
238 | String expected1 = "[TmfEventRequest(0,ITmfEvent," + range1 + ",0,100,200)]"; |
239 | String expected2 = "[TmfEventRequest(1,ITmfEvent," + range2 + ",0,100,200)]"; | |
240 | String expected3 = "[TmfEventRequest(2,ITmfEvent," + range2 + ",0,200,200)]"; | |
241 | String expected4 = "[TmfEventRequest(3,ITmfEvent," + range2 + ",0,200,300)]"; | |
2fb2eb37 FC |
242 | |
243 | assertEquals("toString", expected1, fRequest1.toString()); | |
244 | assertEquals("toString", expected2, fRequest2.toString()); | |
245 | assertEquals("toString", expected3, fRequest3.toString()); | |
246 | assertEquals("toString", expected4, fRequest4.toString()); | |
6e1886bc | 247 | } |
951d134a | 248 | |
6e1886bc AM |
249 | // ------------------------------------------------------------------------ |
250 | // done | |
251 | // ------------------------------------------------------------------------ | |
951d134a | 252 | |
6e1886bc AM |
253 | @Test |
254 | public void testDone() { | |
255 | final boolean[] flags = new boolean[4]; | |
256 | TmfEventRequest request = setupTestRequest(flags); | |
257 | request.done(); | |
2fb2eb37 | 258 | |
6e1886bc AM |
259 | assertTrue("isCompleted", request.isCompleted()); |
260 | assertFalse("isFailed", request.isFailed()); | |
261 | assertFalse("isCancelled", request.isCancelled()); | |
9b749023 | 262 | |
6e1886bc AM |
263 | assertTrue("handleCompleted", flags[0]); |
264 | assertTrue("handleSuccess", flags[1]); | |
265 | assertFalse("handleFailure", flags[2]); | |
266 | assertFalse("handleCancel", flags[3]); | |
267 | } | |
2fb2eb37 | 268 | |
6e1886bc AM |
269 | // ------------------------------------------------------------------------ |
270 | // fail | |
271 | // ------------------------------------------------------------------------ | |
2fb2eb37 | 272 | |
6e1886bc AM |
273 | @Test |
274 | public void testFail() { | |
275 | final boolean[] flags = new boolean[4]; | |
276 | TmfEventRequest request = setupTestRequest(flags); | |
277 | request.fail(); | |
2fb2eb37 | 278 | |
6e1886bc AM |
279 | assertTrue("isCompleted", request.isCompleted()); |
280 | assertTrue("isFailed", request.isFailed()); | |
281 | assertFalse("isCancelled", request.isCancelled()); | |
2fb2eb37 | 282 | |
6e1886bc AM |
283 | assertTrue("handleCompleted", flags[0]); |
284 | assertFalse("handleSuccess", flags[1]); | |
285 | assertTrue("handleFailure", flags[2]); | |
286 | assertFalse("handleCancel", flags[3]); | |
287 | } | |
2fb2eb37 | 288 | |
6e1886bc AM |
289 | // ------------------------------------------------------------------------ |
290 | // cancel | |
291 | // ------------------------------------------------------------------------ | |
2fb2eb37 | 292 | |
6e1886bc AM |
293 | @Test |
294 | public void testCancel() { | |
295 | final boolean[] flags = new boolean[4]; | |
296 | TmfEventRequest request = setupTestRequest(flags); | |
297 | request.cancel(); | |
2fb2eb37 | 298 | |
6e1886bc AM |
299 | assertTrue("isCompleted", request.isCompleted()); |
300 | assertFalse("isFailed", request.isFailed()); | |
301 | assertTrue("isCancelled", request.isCancelled()); | |
2fb2eb37 | 302 | |
6e1886bc AM |
303 | assertTrue("handleCompleted", flags[0]); |
304 | assertFalse("handleSuccess", flags[1]); | |
305 | assertFalse("handleFailure", flags[2]); | |
306 | assertTrue("handleCancel", flags[3]); | |
307 | } | |
2fb2eb37 | 308 | |
9b749023 | 309 | } |