1 /*******************************************************************************
2 * Copyright (c) 2009 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
.trace
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertTrue
;
20 import java
.util
.Vector
;
22 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
24 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
26 import org
.junit
.BeforeClass
;
27 import org
.junit
.Test
;
30 * <b><u>TmfEventLogTest</u></b>
32 * TODO: Implement me. Please.
34 public class TmfExperimentTest
{
36 private static final String DIRECTORY
= "testfiles";
37 private static final String TEST_STREAM
= "M-Test-10K";
38 private static final String EXPERIMENT
= "MyExperiment";
39 private static String testfile
;
40 private static int NB_EVENTS
= 10000;
41 private static int fDefaultBlockSize
= 1000;
43 // private static ITmfEventParser fParser;
44 private static ITmfTrace fStream
;
45 private static TmfExperiment fExperiment
;
47 // private static byte SCALE = (byte) -3;
50 public static void setUpBeforeClass() throws Exception
{
51 String directory
= new File(".").getCanonicalPath() + File
.separator
+ DIRECTORY
;
52 testfile
= directory
+ File
.separator
+ TEST_STREAM
;
54 fStream
= new TmfTraceStub(testfile
);
55 fExperiment
= new TmfExperiment(EXPERIMENT
, new ITmfTrace
[] { fStream
});
56 while (fStream
.getNbEvents() < NB_EVENTS
) {
61 // ========================================================================
63 // ========================================================================
66 public void testBasicTmfTrace() {
67 assertEquals("GetId", EXPERIMENT
, fExperiment
.getExperimentId());
68 assertEquals("GetEpoch", TmfTimestamp
.BigBang
, fExperiment
.getEpoch());
69 assertEquals("GetNbEvents", NB_EVENTS
, fExperiment
.getNbEvents());
71 TmfTimeRange timeRange
= fExperiment
.getTimeRange();
72 assertEquals("GetTimeRange-start", 1, timeRange
.getStartTime().getValue());
73 assertEquals("GetTimeRange-end", NB_EVENTS
, timeRange
.getEndTime().getValue());
76 // ========================================================================
78 // ========================================================================
81 public void testProcessRequestForNbEvents() throws Exception
{
82 final int BLOCK_SIZE
= 100;
83 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
85 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
86 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, 0, NB_EVENTS
, BLOCK_SIZE
) {
88 public void handleData() {
89 TmfEvent
[] events
= getData();
90 for (TmfEvent e
: events
) {
91 requestedEvents
.add(e
);
95 fExperiment
.processRequest(request
, true);
97 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
98 assertTrue("isCompleted", request
.isCompleted());
99 assertFalse("isCancelled", request
.isCancelled());
101 // Ensure that we have distinct events.
102 // Don't go overboard: we are not validating the stub!
103 for (int i
= 0; i
< NB_EVENTS
; i
++) {
104 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
109 public void testProcessRequestForAllEvents() throws Exception
{
110 final int NB_EVENTS
= -1;
111 final int BLOCK_SIZE
= 1;
112 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
113 int nbExpectedEvents
= fExperiment
.getNbEvents();
115 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
116 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, 0, NB_EVENTS
, BLOCK_SIZE
) {
118 public void handleData() {
119 TmfEvent
[] events
= getData();
120 for (TmfEvent e
: events
) {
121 requestedEvents
.add(e
);
125 fExperiment
.processRequest(request
, true);
127 assertEquals("nbEvents", nbExpectedEvents
, requestedEvents
.size());
128 assertTrue("isCompleted", request
.isCompleted());
129 assertFalse("isCancelled", request
.isCancelled());
131 // Ensure that we have distinct events.
132 // Don't go overboard: we are not validating the stub!
133 for (int i
= 0; i
< nbExpectedEvents
; i
++) {
134 assertEquals("Distinct events", i
+1, requestedEvents
.get(i
).getTimestamp().getValue());
138 // ========================================================================
140 // ========================================================================
143 public void testCancel() throws Exception
{
144 final int BLOCK_SIZE
= fDefaultBlockSize
;
145 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
147 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
148 final TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(range
, 0, NB_EVENTS
, BLOCK_SIZE
) {
150 public void handleData() {
151 TmfEvent
[] events
= getData();
152 for (TmfEvent e
: events
) {
153 requestedEvents
.add(e
);
155 // Cancel request after the first chunk is received
159 fExperiment
.processRequest(request
, true);
161 assertEquals("nbEvents", BLOCK_SIZE
, requestedEvents
.size());
162 assertTrue("isCompleted", request
.isCompleted());
163 assertTrue("isCancelled", request
.isCancelled());