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
.TmfProvider
;
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
.TmfTraceContext
;
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("Opened 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 TmfTraceContext 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 TmfTraceContext 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 TmfTraceContext 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 TmfTraceContext 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 TmfTraceContext 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 TmfProvider
<TmfEvent
>[] providers
= (TmfProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
230 providers
[0].processRequest(request
, true);
232 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
233 assertTrue("isCompleted", request
.isCompleted());
234 assertFalse("isCancelled", request
.isCancelled());
236 // Ensure that we have distinct events.
237 // Don't go overboard: we are not validating the stub!
238 for (int i
= 0; i
< NB_EVENTS
; i
++) {
239 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
243 @SuppressWarnings("unchecked")
244 public void testProcessRequestForAllEvents() throws Exception
{
245 final int BLOCK_SIZE
= 1;
246 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
247 // long nbExpectedEvents = NB_EVENTS;
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 TmfProvider
<TmfEvent
>[] providers
= (TmfProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
260 providers
[0].processRequest(request
, true);
262 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
263 assertTrue("isCompleted", request
.isCompleted());
264 assertFalse("isCancelled", request
.isCancelled());
266 // Ensure that we have distinct events.
267 // Don't go overboard: we are not validating the stub!
268 for (int i
= 0; i
< NB_EVENTS
; i
++) {
269 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
273 // ------------------------------------------------------------------------
275 // ------------------------------------------------------------------------
277 @SuppressWarnings("unchecked")
278 public void testCancel() throws Exception
{
279 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
281 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
282 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, NB_EVENTS
, NB_EVENTS
) {
284 public void handleData() {
285 TmfEvent
[] events
= getData();
286 for (TmfEvent e
: events
) {
287 requestedEvents
.add(e
);
289 // Cancel request after the first chunk is received
293 TmfProvider
<TmfEvent
>[] providers
= (TmfProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
294 providers
[0].processRequest(request
, true);
296 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
297 assertTrue("isCompleted", request
.isCompleted());
298 assertTrue("isCancelled", request
.isCancelled());
This page took 0.037953 seconds and 5 git commands to generate.