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
.trace
;
16 import java
.io
.IOException
;
17 import java
.net
.URISyntaxException
;
19 import java
.util
.Vector
;
21 import junit
.framework
.TestCase
;
23 import org
.eclipse
.core
.runtime
.FileLocator
;
24 import org
.eclipse
.core
.runtime
.Path
;
25 import org
.eclipse
.linuxtools
.tmf
.component
.ITmfDataProvider
;
26 import org
.eclipse
.linuxtools
.tmf
.component
.TmfProviderManager
;
27 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
29 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.tests
.TmfCoreTestPlugin
;
32 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTraceStub
;
36 * <b><u>TmfTraceTest</u></b>
38 * TODO: Implement me. Please.
40 public class TmfTraceTest
extends TestCase
{
42 private static final String DIRECTORY
= "testfiles";
43 private static final String TEST_STREAM
= "M-Test-10K";
44 private static final int NB_EVENTS
= 10000;
45 private static TmfTraceStub fTrace
= null;
47 private static byte SCALE
= (byte) -3;
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private TmfTraceStub
setupTrace(String path
) {
56 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getPlugin().getBundle(), new Path(path
), null);
57 File test
= new File(FileLocator
.toFileURL(location
).toURI());
58 TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), 500, true);
60 } catch (URISyntaxException e
) {
62 } catch (IOException e
) {
69 public TmfTraceTest(String name
) throws Exception
{
74 protected void setUp() throws Exception
{
76 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
80 protected void tearDown() throws Exception
{
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
88 public void testTmfTraceDefault() throws Exception
{
89 TmfTraceStub trace
= null;
91 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getPlugin().getBundle(), new Path(DIRECTORY
+ File
.separator
+ TEST_STREAM
), null);
92 File test
= new File(FileLocator
.toFileURL(location
).toURI());
93 trace
= new TmfTraceStub(test
.getPath(), true);
94 } catch (URISyntaxException e
) {
96 } catch (IOException e
) {
99 assertTrue("Open trace", trace
!= null);
100 assertEquals("getCacheSize", TmfTraceStub
.DEFAULT_CACHE_SIZE
, trace
.getCacheSize());
101 assertEquals("getTraceSize", NB_EVENTS
, trace
.getNbEvents());
104 public void testTmfTrace() throws Exception
{
105 assertEquals("getCacheSize", 500, fTrace
.getCacheSize());
106 assertEquals("getTraceSize", NB_EVENTS
, fTrace
.getNbEvents());
107 assertEquals("getRange-start", 1, fTrace
.getTimeRange().getStartTime().getValue());
108 assertEquals("getRange-end", NB_EVENTS
, fTrace
.getTimeRange().getEndTime().getValue());
111 // ------------------------------------------------------------------------
113 // ------------------------------------------------------------------------
115 public void testSeekOnCacheBoundary() throws Exception
{
116 TmfContext context
= fTrace
.seekLocation(null);
118 context
= fTrace
.seekEvent(new TmfTimestamp(1, SCALE
, 0));
119 TmfEvent event
= fTrace
.getNextEvent(context
);
120 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
121 assertEquals("Event rank", 1, context
.getRank());
123 context
= fTrace
.seekEvent(new TmfTimestamp(1001, SCALE
, 0));
124 event
= fTrace
.getNextEvent(context
);
125 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
126 assertEquals("Event rank", 1001, context
.getRank());
128 context
= fTrace
.seekEvent(new TmfTimestamp(4001, SCALE
, 0));
129 event
= fTrace
.getNextEvent(context
);
130 assertEquals("Event timestamp", 4001, event
.getTimestamp().getValue());
131 assertEquals("Event rank", 4001, context
.getRank());
134 public void testSeekNotOnCacheBoundary() throws Exception
{
135 TmfContext context
= fTrace
.seekLocation(null);
137 context
= fTrace
.seekEvent(new TmfTimestamp(10, SCALE
, 0));
138 TmfEvent event
= fTrace
.getNextEvent(context
);
139 assertEquals("Event timestamp", 10, event
.getTimestamp().getValue());
140 assertEquals("Event rank", 10, context
.getRank());
142 context
= fTrace
.seekEvent(new TmfTimestamp(999, SCALE
, 0));
143 event
= fTrace
.getNextEvent(context
);
144 assertEquals("Event timestamp", 999, event
.getTimestamp().getValue());
145 assertEquals("Event rank", 999, context
.getRank());
147 context
= fTrace
.seekEvent(new TmfTimestamp(1001, SCALE
, 0));
148 event
= fTrace
.getNextEvent(context
);
149 assertEquals("Event timestamp", 1001, event
.getTimestamp().getValue());
150 assertEquals("Event rank", 1001, context
.getRank());
152 context
= fTrace
.seekEvent(new TmfTimestamp(4499, SCALE
, 0));
153 event
= fTrace
.getNextEvent(context
);
154 assertEquals("Event timestamp", 4499, event
.getTimestamp().getValue());
155 assertEquals("Event rank", 4499, context
.getRank());
158 public void testSeekForEventOutOfBounds() throws Exception
{
159 TmfContext context
= fTrace
.seekLocation(null);
161 // On lower bound, returns the first event (ts = 1)
162 context
= fTrace
.seekEvent(new TmfTimestamp(-1, SCALE
, 0));
163 TmfEvent event
= fTrace
.getNextEvent(context
);
164 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
166 // On higher bound, returns null (no event)
167 context
= fTrace
.seekEvent(new TmfTimestamp(NB_EVENTS
+ 1, SCALE
, 0));
168 event
= fTrace
.getNextEvent(context
);
169 assertEquals("Event timestamp", null, event
);
172 public void testSeekOnIndex() throws Exception
{
173 TmfContext context
= fTrace
.seekLocation(null);
175 // On lower bound, returns the first event (ts = 1)
176 context
= fTrace
.seekEvent(0);
177 TmfEvent event
= fTrace
.getNextEvent(context
);
178 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
181 context
= fTrace
.seekEvent(NB_EVENTS
- 1);
182 event
= fTrace
.getNextEvent(context
);
183 assertEquals("Event timestamp", NB_EVENTS
, event
.getTimestamp().getValue());
186 context
= fTrace
.seekEvent(NB_EVENTS
);
187 event
= fTrace
.getNextEvent(context
);
188 assertEquals("Event", null, event
);
191 // ------------------------------------------------------------------------
193 // ------------------------------------------------------------------------
195 public void testGetNextEvent() throws Exception
{
196 TmfContext context
= fTrace
.seekLocation(null);
198 // On lower bound, returns the first event (ts = 0)
199 context
= fTrace
.seekEvent(new TmfTimestamp(0, SCALE
, 0));
200 TmfEvent event
= fTrace
.getNextEvent(context
);
201 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
203 for (int i
= 2; i
< 20; i
++) {
204 event
= fTrace
.getNextEvent(context
);
205 assertEquals("Event timestamp", i
, event
.getTimestamp().getValue());
209 // ------------------------------------------------------------------------
211 // ------------------------------------------------------------------------
213 @SuppressWarnings("unchecked")
214 public void testProcessRequestForNbEvents() throws Exception
{
215 final int BLOCK_SIZE
= 100;
216 final int NB_EVENTS
= 1000;
217 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
219 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
220 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
222 public void handleData() {
223 TmfEvent
[] events
= getData();
224 for (TmfEvent e
: events
) {
225 requestedEvents
.add(e
);
229 ITmfDataProvider
<TmfEvent
>[] providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
230 providers
[0].sendRequest(request
);
231 request
.waitForCompletion();
233 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
234 assertTrue("isCompleted", request
.isCompleted());
235 assertFalse("isCancelled", request
.isCancelled());
237 // Ensure that we have distinct events.
238 // Don't go overboard: we are not validating the stub!
239 for (int i
= 0; i
< NB_EVENTS
; i
++) {
240 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
244 @SuppressWarnings("unchecked")
245 public void testProcessRequestForAllEvents() throws Exception
{
246 final int BLOCK_SIZE
= 1;
247 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
249 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
250 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, BLOCK_SIZE
) {
252 public void handleData() {
253 TmfEvent
[] events
= getData();
254 for (TmfEvent e
: events
) {
255 requestedEvents
.add(e
);
259 ITmfDataProvider
<TmfEvent
>[] providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
260 providers
[0].sendRequest(request
);
261 request
.waitForCompletion();
263 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
264 assertTrue("isCompleted", request
.isCompleted());
265 assertFalse("isCancelled", request
.isCancelled());
267 // Ensure that we have distinct events.
268 // Don't go overboard: we are not validating the stub!
269 for (int i
= 0; i
< NB_EVENTS
; i
++) {
270 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
274 // ------------------------------------------------------------------------
276 // ------------------------------------------------------------------------
278 @SuppressWarnings("unchecked")
279 public void testCancel() throws Exception
{
280 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
282 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
283 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, NB_EVENTS
) {
285 public void handleData() {
286 TmfEvent
[] events
= getData();
287 for (TmfEvent e
: events
) {
288 requestedEvents
.add(e
);
290 // Cancel request after the first chunk is received
294 ITmfDataProvider
<TmfEvent
>[] providers
= (ITmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
295 providers
[0].sendRequest(request
);
296 request
.waitForCompletion();
298 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
299 assertTrue("isCompleted", request
.isCompleted());
300 assertTrue("isCancelled", request
.isCancelled());
This page took 0.037036 seconds and 6 git commands to generate.