1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2013 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 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.request
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertTrue
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
22 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.request
.TmfDataRequestStub
;
23 import org
.junit
.Before
;
24 import org
.junit
.Test
;
27 * Test suite for the TmfDataRequest class.
29 @SuppressWarnings({ "nls", "javadoc" })
30 public class TmfDataRequestTest
{
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 private static TmfDataRequest fRequest1
;
37 private static TmfDataRequest fRequest1b
;
38 private static TmfDataRequest fRequest1c
;
39 private static TmfDataRequest fRequest2
;
40 private static TmfDataRequest fRequest3
;
41 private static TmfDataRequest fRequest4
;
43 private static int fRequestCount
;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
51 TmfDataRequest
.reset();
52 fRequest1
= new TmfDataRequestStub(ITmfEvent
.class, 10, 100, 200);
53 fRequest2
= new TmfDataRequestStub(ITmfEvent
.class, 20, 100, 200);
54 fRequest3
= new TmfDataRequestStub(ITmfEvent
.class, 20, 200, 200);
55 fRequest4
= new TmfDataRequestStub(ITmfEvent
.class, 20, 200, 300);
56 fRequest1b
= new TmfDataRequestStub(ITmfEvent
.class, 10, 100, 200);
57 fRequest1c
= new TmfDataRequestStub(ITmfEvent
.class, 10, 100, 200);
58 fRequestCount
= fRequest1c
.getRequestId() + 1;
61 private static TmfDataRequest
setupTestRequest(final boolean[] flags
) {
63 TmfDataRequest request
= new TmfDataRequestStub(ITmfEvent
.class, 10, 100, 200) {
65 public void handleCompleted() {
66 super.handleCompleted();
71 public void handleSuccess() {
72 super.handleSuccess();
77 public void handleFailure() {
78 super.handleFailure();
83 public void handleCancel() {
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
96 public void testTmfDataRequest() {
97 TmfDataRequest request
= new TmfDataRequestStub(ITmfEvent
.class);
99 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
100 assertEquals("getDataType", ITmfEvent
.class, request
.getDataType());
102 assertEquals("getIndex", 0, request
.getIndex());
103 assertEquals("getNbRequestedEvents", TmfDataRequest
.ALL_DATA
, request
.getNbRequested());
105 assertFalse("isCompleted", request
.isCompleted());
106 assertFalse("isFailed", request
.isFailed());
107 assertFalse("isCancelled", request
.isCancelled());
109 assertEquals("getNbRead", 0, request
.getNbRead());
113 public void testTmfDataRequestIndex() {
114 TmfDataRequest request
= new TmfDataRequestStub(ITmfEvent
.class, 10);
116 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
117 assertEquals("getDataType", ITmfEvent
.class, request
.getDataType());
119 assertEquals("getIndex", 10, request
.getIndex());
120 assertEquals("getNbRequestedEvents", TmfDataRequest
.ALL_DATA
, request
.getNbRequested());
122 assertFalse("isCompleted", request
.isCompleted());
123 assertFalse("isFailed", request
.isFailed());
124 assertFalse("isCancelled", request
.isCancelled());
126 assertEquals("getNbRead", 0, request
.getNbRead());
130 public void testTmfDataRequestIndexNbRequested() {
131 TmfDataRequest request
= new TmfDataRequestStub(ITmfEvent
.class, 10, 100);
133 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
134 assertEquals("getDataType", ITmfEvent
.class, request
.getDataType());
136 assertEquals("getIndex", 10, request
.getIndex());
137 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
139 assertFalse("isCompleted", request
.isCompleted());
140 assertFalse("isFailed", request
.isFailed());
141 assertFalse("isCancelled", request
.isCancelled());
143 assertEquals("getNbRead", 0, request
.getNbRead());
147 public void testTmfDataRequestIndexNbEventsBlocksize() {
148 TmfDataRequest request
= new TmfDataRequestStub(ITmfEvent
.class, 10, 100, 200);
150 assertEquals("getRequestId", fRequestCount
++, request
.getRequestId());
151 assertEquals("getDataType", ITmfEvent
.class, request
.getDataType());
153 assertEquals("getIndex", 10, request
.getIndex());
154 assertEquals("getNbRequestedEvents", 100, request
.getNbRequested());
156 assertFalse("isCompleted", request
.isCompleted());
157 assertFalse("isFailed", request
.isFailed());
158 assertFalse("isCancelled", request
.isCancelled());
160 assertEquals("getNbRead", 0, request
.getNbRead());
163 // ------------------------------------------------------------------------
165 // ------------------------------------------------------------------------
168 public void testEqualsReflexivity() {
169 assertTrue("equals", fRequest1
.equals(fRequest1
));
170 assertTrue("equals", fRequest2
.equals(fRequest2
));
172 assertFalse("equals", fRequest1
.equals(fRequest2
));
173 assertFalse("equals", fRequest2
.equals(fRequest1
));
177 public void testEqualsSymmetry() {
178 assertTrue("equals", fRequest1
.equals(fRequest1b
));
179 assertTrue("equals", fRequest1b
.equals(fRequest1
));
181 assertFalse("equals", fRequest1
.equals(fRequest3
));
182 assertFalse("equals", fRequest2
.equals(fRequest3
));
183 assertFalse("equals", fRequest3
.equals(fRequest1
));
184 assertFalse("equals", fRequest3
.equals(fRequest2
));
188 public void testEqualsTransivity() {
189 assertTrue("equals", fRequest1
.equals(fRequest1b
));
190 assertTrue("equals", fRequest1b
.equals(fRequest1c
));
191 assertTrue("equals", fRequest1
.equals(fRequest1c
));
195 public void testEqualsNull() {
196 assertFalse("equals", fRequest1
.equals(null));
197 assertFalse("equals", fRequest2
.equals(null));
200 // ------------------------------------------------------------------------
202 // ------------------------------------------------------------------------
205 public void testHashCode() {
206 assertTrue("hashCode", fRequest1
.hashCode() == fRequest1
.hashCode());
207 assertTrue("hashCode", fRequest2
.hashCode() == fRequest2
.hashCode());
208 assertTrue("hashCode", fRequest1
.hashCode() != fRequest2
.hashCode());
211 // ------------------------------------------------------------------------
213 // ------------------------------------------------------------------------
216 public void testToString() {
217 String expected1
= "[TmfDataRequest(0,ITmfEvent,10,100,200)]";
218 String expected2
= "[TmfDataRequest(1,ITmfEvent,20,100,200)]";
219 String expected3
= "[TmfDataRequest(2,ITmfEvent,20,200,200)]";
220 String expected4
= "[TmfDataRequest(3,ITmfEvent,20,200,300)]";
222 assertEquals("toString", expected1
, fRequest1
.toString());
223 assertEquals("toString", expected2
, fRequest2
.toString());
224 assertEquals("toString", expected3
, fRequest3
.toString());
225 assertEquals("toString", expected4
, fRequest4
.toString());
228 // ------------------------------------------------------------------------
230 // ------------------------------------------------------------------------
233 public void testDone() {
234 final boolean[] flags
= new boolean[4];
235 TmfDataRequest request
= setupTestRequest(flags
);
238 assertTrue("isCompleted", request
.isCompleted());
239 assertFalse("isFailed", request
.isFailed());
240 assertFalse("isCancelled", request
.isCancelled());
242 assertTrue("handleCompleted", flags
[0]);
243 assertTrue("handleSuccess", flags
[1]);
244 assertFalse("handleFailure", flags
[2]);
245 assertFalse("handleCancel", flags
[3]);
248 // ------------------------------------------------------------------------
250 // ------------------------------------------------------------------------
253 public void testFail() {
254 final boolean[] flags
= new boolean[4];
255 TmfDataRequest request
= setupTestRequest(flags
);
258 assertTrue("isCompleted", request
.isCompleted());
259 assertTrue("isFailed", request
.isFailed());
260 assertFalse("isCancelled", request
.isCancelled());
262 assertTrue("handleCompleted", flags
[0]);
263 assertFalse("handleSuccess", flags
[1]);
264 assertTrue("handleFailure", flags
[2]);
265 assertFalse("handleCancel", flags
[3]);
268 // ------------------------------------------------------------------------
270 // ------------------------------------------------------------------------
273 public void testCancel() {
274 final boolean[] flags
= new boolean[4];
275 TmfDataRequest request
= setupTestRequest(flags
);
278 assertTrue("isCompleted", request
.isCompleted());
279 assertFalse("isFailed", request
.isFailed());
280 assertTrue("isCancelled", request
.isCancelled());
282 assertTrue("handleCompleted", flags
[0]);
283 assertFalse("handleSuccess", flags
[1]);
284 assertFalse("handleFailure", flags
[2]);
285 assertTrue("handleCancel", flags
[3]);