1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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
.request
;
15 import junit
.framework
.TestCase
;
17 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
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
.TmfTimeRange
;
22 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
23 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
24 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequestStub
;
28 * <b><u>TmfEventRequestTest</u></b>
30 * Test suite for the TmfEventRequest class.
32 public class TmfEventRequestTest
extends TestCase
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private static TmfTimeRange range1
= new TmfTimeRange(TmfTimeRange
.Eternity
);
39 private static TmfTimeRange range2
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
41 private static TmfEventRequest
<TmfEvent
> fRequest1
;
42 private static TmfEventRequest
<TmfEvent
> fRequest1b
;
43 private static TmfEventRequest
<TmfEvent
> fRequest1c
;
44 private static TmfEventRequest
<TmfEvent
> fRequest2
;
45 private static TmfEventRequest
<TmfEvent
> fRequest3
;
46 private static TmfEventRequest
<TmfEvent
> fRequest4
;
48 private static int fRequestCount
;
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 public TmfEventRequestTest(String name
) {
59 protected void setUp() throws Exception
{
61 TmfDataRequest
.reset();
62 fRequest1
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
63 fRequest2
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range2
, 100, 200);
64 fRequest3
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range2
, 200, 200);
65 fRequest4
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range2
, 200, 300);
66 fRequest1b
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
67 fRequest1c
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range1
, 100, 200);
68 fRequestCount
= fRequest1c
.getRequestId() + 1;
72 protected void tearDown() throws Exception
{
76 private TmfEventRequest
<TmfEvent
> setupTestRequest(final boolean[] flags
) {
78 TmfEventRequest
<TmfEvent
> request
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, new TmfTimeRange(TmfTimeRange
.Eternity
), 100, 200) {
80 public void handleCompleted() {
81 super.handleCompleted();
85 public void handleSuccess() {
86 super.handleSuccess();
90 public void handleFailure() {
91 super.handleFailure();
95 public void handleCancel() {
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
107 public void testTmfEventRequest() {
108 TmfEventRequest
<TmfEvent
> request
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class);
110 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
111 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
113 assertEquals("StartTime", TmfTimestamp
.BigBang
, request
.getRange().getStartTime());
114 assertEquals("EndTime", TmfTimestamp
.BigCrunch
, request
.getRange().getEndTime());
116 assertEquals("getIndex", 0, request
.getIndex());
117 assertEquals("getNbRequestedEvents", TmfDataRequest
.ALL_DATA
, request
.getNbRequested());
118 assertEquals("getBlockize", TmfDataRequest
.DEFAULT_BLOCK_SIZE
, request
.getBlockize());
120 assertFalse("isCompleted", request
.isCompleted());
121 assertFalse("isFailed", request
.isFailed());
122 assertFalse("isCancelled", request
.isCancelled());
124 assertEquals("getNbRead", 0, request
.getNbRead());
127 public void testTmfEventRequestTimeRange() {
128 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
129 TmfEventRequest
<TmfEvent
> request
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range
);
131 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
132 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
134 assertEquals("StartTime", new TmfTimestamp(), request
.getRange().getStartTime());
135 assertEquals("EndTime", TmfTimestamp
.BigCrunch
, request
.getRange().getEndTime());
137 assertEquals("getIndex", 0, request
.getIndex());
138 assertEquals("getNbRequestedEvents", TmfDataRequest
.ALL_DATA
, request
.getNbRequested());
139 assertEquals("getBlockize", TmfDataRequest
.DEFAULT_BLOCK_SIZE
, request
.getBlockize());
141 assertFalse("isCompleted", request
.isCompleted());
142 assertFalse("isFailed", request
.isFailed());
143 assertFalse("isCancelled", request
.isCancelled());
145 assertEquals("getNbRead", 0, request
.getNbRead());
148 public void testTmfEventRequestTimeRangeNbRequested() {
149 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
150 TmfEventRequest
<TmfEvent
> request
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range
, 100);
152 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
153 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
155 assertEquals("StartTime", new TmfTimestamp(), request
.getRange().getStartTime());
156 assertEquals("EndTime", TmfTimestamp
.BigCrunch
, request
.getRange().getEndTime());
158 assertEquals("getIndex", 0, request
.getIndex());
159 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
160 assertEquals("getBlockize", TmfDataRequest
.DEFAULT_BLOCK_SIZE
, request
.getBlockize());
162 assertFalse("isCompleted", request
.isCompleted());
163 assertFalse("isFailed", request
.isFailed());
164 assertFalse("isCancelled", request
.isCancelled());
166 assertEquals("getNbRead", 0, request
.getNbRead());
169 public void testTmfEventRequestTimeRangeNbRequestedBlocksize() {
170 TmfTimeRange range
= new TmfTimeRange(new TmfTimestamp(), TmfTimestamp
.BigCrunch
);
171 TmfEventRequest
<TmfEvent
> request
= new TmfEventRequestStub
<TmfEvent
>(TmfEvent
.class, range
, 100, 200);
173 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
174 assertEquals("getDataType", TmfEvent
.class, request
.getDataType());
176 assertEquals("StartTime", new TmfTimestamp(), request
.getRange().getStartTime());
177 assertEquals("EndTime", TmfTimestamp
.BigCrunch
, request
.getRange().getEndTime());
179 assertEquals("getIndex", 0, request
.getIndex());
180 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
181 assertEquals("getBlockize", 200, request
.getBlockize());
183 assertFalse("isCompleted", request
.isCompleted());
184 assertFalse("isFailed", request
.isFailed());
185 assertFalse("isCancelled", request
.isCancelled());
187 assertEquals("getNbRead", 0, request
.getNbRead());
190 // ------------------------------------------------------------------------
192 // ------------------------------------------------------------------------
194 public void testEqualsReflexivity() throws Exception
{
195 assertTrue("equals", fRequest1
.equals(fRequest1
));
196 assertTrue("equals", fRequest2
.equals(fRequest2
));
198 assertFalse("equals", fRequest1
.equals(fRequest2
));
199 assertFalse("equals", fRequest2
.equals(fRequest1
));
202 public void testEqualsSymmetry() throws Exception
{
203 assertTrue("equals", fRequest1
.equals(fRequest1b
));
204 assertTrue("equals", fRequest1b
.equals(fRequest1
));
206 assertFalse("equals", fRequest1
.equals(fRequest3
));
207 assertFalse("equals", fRequest2
.equals(fRequest3
));
208 assertFalse("equals", fRequest3
.equals(fRequest1
));
209 assertFalse("equals", fRequest3
.equals(fRequest2
));
212 public void testEqualsTransivity() throws Exception
{
213 assertTrue("equals", fRequest1
.equals(fRequest1b
));
214 assertTrue("equals", fRequest1b
.equals(fRequest1c
));
215 assertTrue("equals", fRequest1
.equals(fRequest1c
));
218 public void testEqualsNull() throws Exception
{
219 assertFalse("equals", fRequest1
.equals(null));
220 assertFalse("equals", fRequest2
.equals(null));
223 // ------------------------------------------------------------------------
225 // ------------------------------------------------------------------------
227 public void testHashCode() throws Exception
{
228 assertTrue("hashCode", fRequest1
.hashCode() == fRequest1
.hashCode());
229 assertTrue("hashCode", fRequest2
.hashCode() == fRequest2
.hashCode());
230 assertTrue("hashCode", fRequest1
.hashCode() != fRequest2
.hashCode());
233 // ------------------------------------------------------------------------
235 // ------------------------------------------------------------------------
237 public void testToString() {
238 String expected1
= "[TmfEventRequest(0,TmfEvent," + range1
+ ",100,200)]";
239 String expected2
= "[TmfEventRequest(1,TmfEvent," + range2
+ ",100,200)]";
240 String expected3
= "[TmfEventRequest(2,TmfEvent," + range2
+ ",200,200)]";
241 String expected4
= "[TmfEventRequest(3,TmfEvent," + range2
+ ",200,300)]";
243 assertEquals("toString", expected1
, fRequest1
.toString());
244 assertEquals("toString", expected2
, fRequest2
.toString());
245 assertEquals("toString", expected3
, fRequest3
.toString());
246 assertEquals("toString", expected4
, fRequest4
.toString());
249 // ------------------------------------------------------------------------
251 // ------------------------------------------------------------------------
253 public void testSetData() {
254 // Initialize the data
256 TmfEvent
[] events
= new TmfEvent
[nbEvents
];
257 for (int i
= 0; i
< nbEvents
; i
++) {
258 events
[i
] = new TmfEvent(new TmfTimestamp(i
), new TmfEventSource(),
259 new TmfEventType(), new TmfEventReference());
262 fRequest1
.setData(events
);
263 assertEquals("setData", nbEvents
, fRequest1
.getNbRead());
265 TmfEvent
[] eventsRead
= fRequest1
.getData();
266 assertEquals("getData", nbEvents
, eventsRead
.length
);
268 for (int i
= 0; i
< nbEvents
; i
++) {
269 assertEquals("getData", i
, eventsRead
[i
].getTimestamp().getValue());
273 // ------------------------------------------------------------------------
275 // ------------------------------------------------------------------------
277 public void testDone() {
279 final boolean[] flags
= new boolean[4];
280 TmfEventRequest
<TmfEvent
> request
= setupTestRequest(flags
);
283 assertTrue ("isCompleted", request
.isCompleted());
284 assertFalse("isFailed", request
.isFailed());
285 assertFalse("isCancelled", request
.isCancelled());
287 assertTrue ("handleCompleted", flags
[0]);
288 assertTrue ("handleSuccess", flags
[1]);
289 assertFalse("handleFailure", flags
[2]);
290 assertFalse("handleCancel", flags
[3]);
293 // ------------------------------------------------------------------------
295 // ------------------------------------------------------------------------
297 public void testFail() {
299 final boolean[] flags
= new boolean[4];
300 TmfEventRequest
<TmfEvent
> request
= setupTestRequest(flags
);
303 assertTrue ("isCompleted", request
.isCompleted());
304 assertTrue ("isFailed", request
.isFailed());
305 assertFalse("isCancelled", request
.isCancelled());
307 assertTrue ("handleCompleted", flags
[0]);
308 assertFalse("handleSuccess", flags
[1]);
309 assertTrue ("handleFailure", flags
[2]);
310 assertFalse("handleCancel", flags
[3]);
313 // ------------------------------------------------------------------------
315 // ------------------------------------------------------------------------
317 public void testCancel() {
319 final boolean[] flags
= new boolean[4];
320 TmfEventRequest
<TmfEvent
> request
= setupTestRequest(flags
);
323 assertTrue ("isCompleted", request
.isCompleted());
324 assertFalse("isFailed", request
.isFailed());
325 assertTrue ("isCancelled", request
.isCancelled());
327 assertTrue ("handleCompleted", flags
[0]);
328 assertFalse("handleSuccess", flags
[1]);
329 assertFalse("handleFailure", flags
[2]);
330 assertTrue ("handleCancel", flags
[3]);
333 // ------------------------------------------------------------------------
335 // ------------------------------------------------------------------------
This page took 0.039202 seconds and 6 git commands to generate.