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
.event
.TmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
27 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
29 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.tests
.TmfCoreTestPlugin
;
31 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfTraceStub
;
35 * <b><u>TmfExperimentTest</u></b>
37 * TODO: Implement me. Please.
39 public class TmfExperimentTest
extends TestCase
{
41 private static final String DIRECTORY
= "testfiles";
42 private static final String TEST_STREAM
= "M-Test-10K";
43 private static final String EXPERIMENT
= "MyExperiment";
44 private static int NB_EVENTS
= 10000;
45 private static int fDefaultBlockSize
= 1000;
47 private static ITmfTrace fTrace
;
48 private static TmfExperiment
<TmfEvent
> fExperiment
;
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 private ITmfTrace
setupTrace(String path
) {
57 URL location
= FileLocator
.find(TmfCoreTestPlugin
.getPlugin().getBundle(), new Path(path
), null);
58 File test
= new File(FileLocator
.toFileURL(location
).toURI());
59 TmfTraceStub trace
= new TmfTraceStub(test
.getPath(), true);
61 } catch (URISyntaxException e
) {
63 } catch (IOException e
) {
70 private void setupExperiment() {
71 if (fExperiment
== null) {
72 fExperiment
= new TmfExperiment
<TmfEvent
>(TmfEvent
.class, EXPERIMENT
, new ITmfTrace
[] { fTrace
});
73 fExperiment
.indexExperiment(true);
77 public TmfExperimentTest(String name
) throws Exception
{
82 protected void setUp() throws Exception
{
84 setupTrace(DIRECTORY
+ File
.separator
+ TEST_STREAM
);
89 protected void tearDown() throws Exception
{
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
97 public void testBasicTmfTrace() {
98 assertEquals("GetId", EXPERIMENT
, fExperiment
.getExperimentId());
99 assertEquals("GetEpoch", TmfTimestamp
.Zero
, fExperiment
.getEpoch());
100 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
102 TmfTimeRange timeRange
= fExperiment
.getTimeRange();
103 assertEquals("getStartTime", 1, timeRange
.getStartTime().getValue());
104 assertEquals("getEndTime", NB_EVENTS
, timeRange
.getEndTime().getValue());
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
111 public void testProcessRequestForNbEvents() throws Exception
{
112 final int blockSize
= 100;
113 final int nbEvents
= 1000;
114 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
116 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
117 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
119 public void handleData() {
120 TmfEvent
[] events
= getData();
121 for (TmfEvent e
: events
) {
122 requestedEvents
.add(e
);
126 fExperiment
.processRequest(request
, true);
128 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
129 assertTrue("isCompleted", request
.isCompleted());
130 assertFalse("isCancelled", request
.isCancelled());
132 // Ensure that we have distinct events.
133 // Don't go overboard: we are not validating the stub!
134 for (int i
= 0; i
< nbEvents
; i
++) {
135 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
139 public void testProcessRequestForNbEvents2() throws Exception
{
140 final int blockSize
= 2 * NB_EVENTS
;
141 final int nbEvents
= 1000;
142 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
144 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
145 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
147 public void handleData() {
148 TmfEvent
[] events
= getData();
149 for (TmfEvent e
: events
) {
150 requestedEvents
.add(e
);
154 fExperiment
.processRequest(request
, true);
156 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
157 assertTrue("isCompleted", request
.isCompleted());
158 assertFalse("isCancelled", request
.isCancelled());
160 // Ensure that we have distinct events.
161 // Don't go overboard: we are not validating the stub!
162 for (int i
= 0; i
< nbEvents
; i
++) {
163 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
167 public void testProcessRequestForAllEvents() throws Exception
{
168 final int nbEvents
= TmfEventRequest
.ALL_DATA
;
169 final int blockSize
= 1;
170 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
171 int nbExpectedEvents
= fExperiment
.getNbEvents();
173 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
174 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
176 public void handleData() {
177 TmfEvent
[] events
= getData();
178 for (TmfEvent e
: events
) {
179 requestedEvents
.add(e
);
183 fExperiment
.processRequest(request
, true);
185 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
186 assertTrue("isCompleted", request
.isCompleted());
187 assertFalse("isCancelled", request
.isCancelled());
189 // Ensure that we have distinct events.
190 // Don't go overboard: we are not validating the stub!
191 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
192 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
196 // ------------------------------------------------------------------------
198 // ------------------------------------------------------------------------
200 public void testCancel() throws Exception
{
201 final int nbEvents
= NB_EVENTS
;
202 final int blockSize
= fDefaultBlockSize
;
203 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
205 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
206 final TmfEventRequest
<TmfEvent
> request
= new TmfEventRequest
<TmfEvent
>(TmfEvent
.class, range
, nbEvents
, blockSize
) {
208 public void handleData() {
209 TmfEvent
[] events
= getData();
210 for (TmfEvent e
: events
) {
211 requestedEvents
.add(e
);
213 // Cancel request after the first chunk is received
217 fExperiment
.processRequest(request
, true);
219 assertEquals("nbEvents", blockSize
, requestedEvents
.size());
220 assertTrue("isCompleted", request
.isCompleted());
221 assertTrue("isCancelled", request
.isCancelled());
224 // ------------------------------------------------------------------------
226 // ------------------------------------------------------------------------
228 public void testGetRank() throws Exception
{
229 assertEquals("getRank", 0, fExperiment
.getRank(new TmfTimestamp()));
230 assertEquals("getRank", 0, fExperiment
.getRank(new TmfTimestamp( 1, (byte) -3)));
231 assertEquals("getRank", 10, fExperiment
.getRank(new TmfTimestamp( 11, (byte) -3)));
232 assertEquals("getRank", 100, fExperiment
.getRank(new TmfTimestamp( 101, (byte) -3)));
233 assertEquals("getRank", 1000, fExperiment
.getRank(new TmfTimestamp(1001, (byte) -3)));
This page took 0.03469 seconds and 5 git commands to generate.