Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
e31e01e8 | 2 | * Copyright (c) 2009, 2010 Ericsson |
d18dd09b ASL |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.tests.trace; | |
14 | ||
15 | import java.io.File; | |
e31e01e8 FC |
16 | import java.io.IOException; |
17 | import java.net.URISyntaxException; | |
18 | import java.net.URL; | |
d18dd09b ASL |
19 | import java.util.Vector; |
20 | ||
e31e01e8 FC |
21 | import junit.framework.TestCase; |
22 | ||
23 | import org.eclipse.core.runtime.FileLocator; | |
24 | import org.eclipse.core.runtime.Path; | |
d18dd09b ASL |
25 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
26 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
27 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
e31e01e8 FC |
28 | import org.eclipse.linuxtools.tmf.experiment.TmfExperiment; |
29 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; | |
30 | import org.eclipse.linuxtools.tmf.tests.TmfCoreTestPlugin; | |
d18dd09b | 31 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace; |
d18dd09b ASL |
32 | import org.eclipse.linuxtools.tmf.trace.TmfTraceStub; |
33 | ||
34 | /** | |
35 | * <b><u>TmfExperimentTest</u></b> | |
36 | * <p> | |
37 | * TODO: Implement me. Please. | |
38 | */ | |
39 | public class TmfExperimentTest extends TestCase { | |
40 | ||
41 | private static final String DIRECTORY = "testfiles"; | |
75828b1a | 42 | private static final String TEST_STREAM = "M-Test-10K"; |
d18dd09b | 43 | private static final String EXPERIMENT = "MyExperiment"; |
e31e01e8 FC |
44 | private static int NB_EVENTS = 10000; |
45 | private static int fDefaultBlockSize = 1000; | |
d18dd09b | 46 | |
e31e01e8 FC |
47 | private static ITmfTrace fTrace; |
48 | private static TmfExperiment<TmfEvent> fExperiment; | |
d18dd09b | 49 | |
e31e01e8 | 50 | // ------------------------------------------------------------------------ |
d18dd09b | 51 | // Housekeeping |
e31e01e8 FC |
52 | // ------------------------------------------------------------------------ |
53 | ||
54 | private ITmfTrace setupTrace(String path) { | |
55 | if (fTrace == null) { | |
56 | try { | |
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); | |
60 | fTrace = trace; | |
61 | } catch (URISyntaxException e) { | |
62 | e.printStackTrace(); | |
63 | } catch (IOException e) { | |
64 | e.printStackTrace(); | |
65 | } | |
66 | } | |
67 | return fTrace; | |
68 | } | |
69 | ||
70 | private void setupExperiment() { | |
71 | if (fExperiment == null) { | |
72 | fExperiment = new TmfExperiment<TmfEvent>(TmfEvent.class, EXPERIMENT, new ITmfTrace[] { fTrace }); | |
73 | fExperiment.indexExperiment(true); | |
74 | } | |
75 | } | |
d18dd09b ASL |
76 | |
77 | public TmfExperimentTest(String name) throws Exception { | |
78 | super(name); | |
79 | } | |
80 | ||
81 | @Override | |
82 | protected void setUp() throws Exception { | |
83 | super.setUp(); | |
e31e01e8 FC |
84 | setupTrace(DIRECTORY + File.separator + TEST_STREAM); |
85 | setupExperiment(); | |
d18dd09b ASL |
86 | } |
87 | ||
88 | @Override | |
89 | protected void tearDown() throws Exception { | |
90 | super.tearDown(); | |
91 | } | |
92 | ||
e31e01e8 | 93 | // ------------------------------------------------------------------------ |
d18dd09b | 94 | // Constructor |
e31e01e8 | 95 | // ------------------------------------------------------------------------ |
d18dd09b | 96 | |
75828b1a FC |
97 | public void testBasicTmfTrace() { |
98 | assertEquals("GetId", EXPERIMENT, fExperiment.getExperimentId()); | |
e31e01e8 | 99 | assertEquals("GetEpoch", TmfTimestamp.Zero, fExperiment.getEpoch()); |
d18dd09b ASL |
100 | assertEquals("GetNbEvents", NB_EVENTS, fExperiment.getNbEvents()); |
101 | ||
102 | TmfTimeRange timeRange = fExperiment.getTimeRange(); | |
e31e01e8 FC |
103 | assertEquals("getStartTime", 1, timeRange.getStartTime().getValue()); |
104 | assertEquals("getEndTime", NB_EVENTS, timeRange.getEndTime().getValue()); | |
d18dd09b ASL |
105 | } |
106 | ||
e31e01e8 | 107 | // ------------------------------------------------------------------------ |
d18dd09b | 108 | // processRequest |
e31e01e8 | 109 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
110 | |
111 | public void testProcessRequestForNbEvents() throws Exception { | |
112 | final int blockSize = 100; | |
e31e01e8 | 113 | final int nbEvents = 1000; |
d18dd09b ASL |
114 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); |
115 | ||
116 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
e31e01e8 | 117 | final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) { |
d18dd09b ASL |
118 | @Override |
119 | public void handleData() { | |
120 | TmfEvent[] events = getData(); | |
121 | for (TmfEvent e : events) { | |
122 | requestedEvents.add(e); | |
123 | } | |
124 | } | |
125 | }; | |
75828b1a | 126 | fExperiment.processRequest(request, true); |
d18dd09b ASL |
127 | |
128 | assertEquals("nbEvents", nbEvents, requestedEvents.size()); | |
129 | assertTrue("isCompleted", request.isCompleted()); | |
130 | assertFalse("isCancelled", request.isCancelled()); | |
131 | ||
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()); | |
136 | } | |
137 | } | |
138 | ||
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>(); | |
143 | ||
144 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
e31e01e8 | 145 | final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) { |
d18dd09b ASL |
146 | @Override |
147 | public void handleData() { | |
148 | TmfEvent[] events = getData(); | |
149 | for (TmfEvent e : events) { | |
150 | requestedEvents.add(e); | |
151 | } | |
152 | } | |
153 | }; | |
75828b1a | 154 | fExperiment.processRequest(request, true); |
d18dd09b ASL |
155 | |
156 | assertEquals("nbEvents", nbEvents, requestedEvents.size()); | |
157 | assertTrue("isCompleted", request.isCompleted()); | |
158 | assertFalse("isCancelled", request.isCancelled()); | |
159 | ||
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()); | |
164 | } | |
165 | } | |
166 | ||
167 | public void testProcessRequestForAllEvents() throws Exception { | |
e31e01e8 | 168 | final int nbEvents = TmfEventRequest.ALL_DATA; |
d18dd09b ASL |
169 | final int blockSize = 1; |
170 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); | |
75828b1a | 171 | int nbExpectedEvents = fExperiment.getNbEvents(); |
d18dd09b ASL |
172 | |
173 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
e31e01e8 | 174 | final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) { |
d18dd09b ASL |
175 | @Override |
176 | public void handleData() { | |
177 | TmfEvent[] events = getData(); | |
178 | for (TmfEvent e : events) { | |
179 | requestedEvents.add(e); | |
180 | } | |
181 | } | |
182 | }; | |
75828b1a | 183 | fExperiment.processRequest(request, true); |
d18dd09b ASL |
184 | |
185 | assertEquals("nbEvents", nbExpectedEvents, requestedEvents.size()); | |
186 | assertTrue("isCompleted", request.isCompleted()); | |
187 | assertFalse("isCancelled", request.isCancelled()); | |
188 | ||
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()); | |
193 | } | |
194 | } | |
195 | ||
e31e01e8 | 196 | // ------------------------------------------------------------------------ |
d18dd09b | 197 | // cancel |
e31e01e8 | 198 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
199 | |
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>(); | |
204 | ||
205 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
e31e01e8 | 206 | final TmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, range, nbEvents, blockSize) { |
d18dd09b ASL |
207 | @Override |
208 | public void handleData() { | |
209 | TmfEvent[] events = getData(); | |
210 | for (TmfEvent e : events) { | |
211 | requestedEvents.add(e); | |
212 | } | |
213 | // Cancel request after the first chunk is received | |
214 | cancel(); | |
215 | } | |
216 | }; | |
75828b1a | 217 | fExperiment.processRequest(request, true); |
d18dd09b ASL |
218 | |
219 | assertEquals("nbEvents", blockSize, requestedEvents.size()); | |
220 | assertTrue("isCompleted", request.isCompleted()); | |
221 | assertTrue("isCancelled", request.isCancelled()); | |
222 | } | |
e31e01e8 FC |
223 | |
224 | // ------------------------------------------------------------------------ | |
225 | // getRank | |
226 | // ------------------------------------------------------------------------ | |
227 | ||
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))); | |
234 | } | |
235 | ||
236 | } |