tmf: Decouple tmf.core unit tests from TmfEvent
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / tests / stubs / trace / TmfTraceStub.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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.stubs.trace;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.RandomAccessFile;
18 import java.util.concurrent.locks.ReentrantLock;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
24 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
25 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer;
31 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
32 import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation;
33 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
34
35 /**
36 * <b><u>TmfTraceStub</u></b>
37 * <p>
38 * Dummy test trace. Use in conjunction with TmfEventParserStub.
39 */
40 @SuppressWarnings({"nls","javadoc"})
41 public class TmfTraceStub extends TmfTrace implements ITmfEventParser {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 // The actual stream
48 private RandomAccessFile fTrace;
49
50 // // The associated event parser
51 // private ITmfEventParser<TmfEvent> fParser;
52
53 // The synchronization lock
54 private final ReentrantLock fLock = new ReentrantLock();
55
56 private ITmfTimestamp fInitialRangeOffset = null;
57
58 // ------------------------------------------------------------------------
59 // Constructors
60 // ------------------------------------------------------------------------
61
62 public TmfTraceStub() {
63 super();
64 setParser(new TmfEventParserStub(this));
65 }
66
67 /**
68 * @param path
69 * @throws FileNotFoundException
70 */
71 public TmfTraceStub(final String path) throws TmfTraceException {
72 this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false);
73 }
74
75 /**
76 * @param path
77 * @param cacheSize
78 * @throws FileNotFoundException
79 */
80 public TmfTraceStub(final String path, final int cacheSize) throws TmfTraceException {
81 this(path, cacheSize, false);
82 }
83
84 /**
85 * @param path
86 * @param cacheSize
87 * @throws FileNotFoundException
88 */
89 public TmfTraceStub(final String path, final int cacheSize, final long interval) throws TmfTraceException {
90 super(null, ITmfEvent.class, path, cacheSize, interval);
91 try {
92 fTrace = new RandomAccessFile(path, "r");
93 } catch (FileNotFoundException e) {
94 throw new TmfTraceException(e.getMessage());
95 }
96 setParser(new TmfEventParserStub(this));
97 }
98
99 /**
100 * @param path
101 * @param cacheSize
102 * @throws FileNotFoundException
103 */
104 public TmfTraceStub(final String path, final int cacheSize, final ITmfTraceIndexer indexer) throws TmfTraceException {
105 this(path, cacheSize, false, null, indexer);
106 }
107
108 /**
109 * @param path
110 * @param waitForCompletion
111 * @throws FileNotFoundException
112 */
113 public TmfTraceStub(final String path, final boolean waitForCompletion) throws TmfTraceException {
114 this(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, waitForCompletion);
115 }
116
117 /**
118 * @param path
119 * @param cacheSize
120 * @param waitForCompletion
121 * @throws FileNotFoundException
122 */
123 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
124 super(null, ITmfEvent.class, path, cacheSize);
125 try {
126 fTrace = new RandomAccessFile(path, "r");
127 } catch (FileNotFoundException e) {
128 throw new TmfTraceException(e.getMessage());
129 }
130 setParser(new TmfEventParserStub(this));
131 if (waitForCompletion) {
132 indexTrace();
133 }
134 }
135
136 /**
137 * @param path
138 * @param cacheSize
139 * @param waitForCompletion
140 * @throws FileNotFoundException
141 */
142 public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws TmfTraceException {
143 super(resource, ITmfEvent.class, path, cacheSize);
144 try {
145 fTrace = new RandomAccessFile(path, "r");
146 } catch (FileNotFoundException e) {
147 throw new TmfTraceException(e.getMessage());
148 }
149 setParser(new TmfEventParserStub(this));
150 }
151
152 /**
153 * @param path
154 * @param cacheSize
155 * @param waitForCompletion
156 * @param parser
157 * @throws FileNotFoundException
158 */
159 public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion,
160 final ITmfEventParser parser, final ITmfTraceIndexer indexer) throws TmfTraceException {
161 super(null, ITmfEvent.class, path, cacheSize, 0, indexer);
162 try {
163 fTrace = new RandomAccessFile(path, "r");
164 } catch (FileNotFoundException e) {
165 throw new TmfTraceException(e.getMessage());
166 }
167 setParser((parser != null) ? parser : new TmfEventParserStub(this));
168 }
169
170 /**
171 * Copy constructor
172 */
173 public TmfTraceStub(final TmfTraceStub trace) throws TmfTraceException {
174 super(trace);
175 try {
176 fTrace = new RandomAccessFile(getPath(), "r");
177 } catch (FileNotFoundException e) {
178 throw new TmfTraceException(e.getMessage());
179 }
180 setParser(new TmfEventParserStub(this));
181 }
182
183 public void indexTrace() {
184 indexTrace(true);
185 }
186
187 @Override
188 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
189 try {
190 fTrace = new RandomAccessFile(path, "r");
191 } catch (FileNotFoundException e) {
192 throw new TmfTraceException(e.getMessage());
193 }
194 setParser(new TmfEventParserStub(this));
195 super.initTrace(resource, path, type);
196 }
197
198 @Override
199 public void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
200 super.initialize(resource, path, type);
201 }
202
203 // ------------------------------------------------------------------------
204 // Accessors
205 // ------------------------------------------------------------------------
206
207 public RandomAccessFile getStream() {
208 return fTrace;
209 }
210
211 public void setInitialRangeOffset(ITmfTimestamp initOffset) {
212 fInitialRangeOffset = initOffset;
213 }
214
215 @Override
216 public ITmfTimestamp getInitialRangeOffset() {
217 if (fInitialRangeOffset != null) {
218 return fInitialRangeOffset;
219 }
220 return super.getInitialRangeOffset();
221 }
222
223 // ------------------------------------------------------------------------
224 // Operators
225 // ------------------------------------------------------------------------
226
227 @Override
228 public TmfContext seekEvent(final ITmfLocation location) {
229 try {
230 fLock.lock();
231 try {
232 if (fTrace != null) {
233 // Position the trace at the requested location and
234 // returns the corresponding context
235 long loc = 0;
236 long rank = 0;
237 if (location != null) {
238 loc = (Long) location.getLocationInfo();
239 rank = ITmfContext.UNKNOWN_RANK;
240 }
241 if (loc != fTrace.getFilePointer()) {
242 fTrace.seek(loc);
243 }
244 final TmfContext context = new TmfContext(getCurrentLocation(), rank);
245 return context;
246 }
247 } catch (final IOException e) {
248 e.printStackTrace();
249 } catch (final NullPointerException e) {
250 e.printStackTrace();
251 }
252 finally{
253 fLock.unlock();
254 }
255 } catch (final NullPointerException e) {
256 e.printStackTrace();
257 }
258 return null;
259 }
260
261
262 @Override
263 public TmfContext seekEvent(final double ratio) {
264 fLock.lock();
265 try {
266 if (fTrace != null) {
267 final ITmfLocation location = new TmfLongLocation(Long.valueOf(Math.round(ratio * fTrace.length())));
268 final TmfContext context = seekEvent(location);
269 context.setRank(ITmfContext.UNKNOWN_RANK);
270 return context;
271 }
272 } catch (final IOException e) {
273 e.printStackTrace();
274 } finally {
275 fLock.unlock();
276 }
277
278 return null;
279 }
280
281 @Override
282 public double getLocationRatio(ITmfLocation location) {
283 fLock.lock();
284 try {
285 if (fTrace != null) {
286 if (location.getLocationInfo() instanceof Long) {
287 return (double) ((Long) location.getLocationInfo()) / fTrace.length();
288 }
289 }
290 } catch (final IOException e) {
291 e.printStackTrace();
292 } finally {
293 fLock.unlock();
294 }
295 return 0;
296 }
297
298 @Override
299 public ITmfLocation getCurrentLocation() {
300 fLock.lock();
301 try {
302 if (fTrace != null) {
303 return new TmfLongLocation(fTrace.getFilePointer());
304 }
305 } catch (final IOException e) {
306 e.printStackTrace();
307 } finally {
308 fLock.unlock();
309 }
310 return null;
311 }
312
313 @Override
314 public ITmfEvent parseEvent(final ITmfContext context) {
315 fLock.lock();
316 try {
317 // parseNextEvent will update the context
318 if (fTrace != null && getParser() != null && context != null) {
319 final ITmfEvent event = getParser().parseEvent(context.clone());
320 return event;
321 }
322 } finally {
323 fLock.unlock();
324 }
325 return null;
326 }
327
328 @Override
329 public synchronized void setNbEvents(final long nbEvents) {
330 super.setNbEvents(nbEvents);
331 }
332
333 @Override
334 public void setTimeRange(final TmfTimeRange range) {
335 super.setTimeRange(range);
336 }
337
338 @Override
339 public void setStartTime(final ITmfTimestamp startTime) {
340 super.setStartTime(startTime);
341 }
342
343 @Override
344 public void setEndTime(final ITmfTimestamp endTime) {
345 super.setEndTime(endTime);
346 }
347
348 @Override
349 public void setStreamingInterval(final long interval) {
350 super.setStreamingInterval(interval);
351 }
352
353 @Override
354 public synchronized void dispose() {
355 fLock.lock();
356 try {
357 if (fTrace != null) {
358 fTrace.close();
359 fTrace = null;
360 }
361 } catch (final IOException e) {
362 // Ignore
363 } finally {
364 fLock.unlock();
365 }
366 super.dispose();
367 }
368
369 /* (non-Javadoc)
370 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
371 */
372 @Override
373 public boolean validate(IProject project, String path) {
374 return fileExists(path);
375 }
376
377 }
This page took 0.05622 seconds and 6 git commands to generate.