1 /*******************************************************************************
2 * Copyright (c) 2014 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.synchronization
;
15 import static org
.junit
.Assert
.assertEquals
;
17 import java
.io
.ByteArrayInputStream
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
21 import java
.net
.URISyntaxException
;
23 import java
.util
.concurrent
.TimeUnit
;
25 import org
.eclipse
.core
.resources
.IFile
;
26 import org
.eclipse
.core
.resources
.IProject
;
27 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
28 import org
.eclipse
.core
.runtime
.CoreException
;
29 import org
.eclipse
.core
.runtime
.FileLocator
;
30 import org
.eclipse
.core
.runtime
.Path
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
40 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
41 import org
.junit
.After
;
42 import org
.junit
.Before
;
43 import org
.junit
.Rule
;
44 import org
.junit
.Test
;
45 import org
.junit
.rules
.TestRule
;
46 import org
.junit
.rules
.Timeout
;
49 * Test suite for time offset of traces.
51 @SuppressWarnings("javadoc")
52 public class TimeOffsetTest
{
54 /** Time-out tests after 20 seconds */
56 public TestRule globalTimeout
= new Timeout(20, TimeUnit
.SECONDS
);
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 private static final TmfTestTrace TEST_TRACE
= TmfTestTrace
.A_TEST_10K
;
63 private static final String PROJECT
= "Test Project";
64 private static final String RESOURCE
= "Test Resource";
65 private static final long ONE_MS
= 1000000L;
66 private IFile fResource
;
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
73 public void setUp() throws CoreException
{
74 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(PROJECT
);
75 if (!project
.exists()) {
79 fResource
= project
.getFile(RESOURCE
);
80 if (!fResource
.exists()) {
81 final InputStream source
= new ByteArrayInputStream(new byte[0]);
82 fResource
.create(source
, true, null);
84 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, fResource
.getParent().getLocation().toOSString());
88 public void tearDown() throws CoreException
{
89 if (fResource
!= null && fResource
.exists()) {
90 fResource
.getProject().delete(true, null);
94 private ITmfTrace
createAndIndexTrace() throws URISyntaxException
, IOException
, TmfTraceException
{
95 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TEST_TRACE
.getFullPath()), null);
96 File testfile
= new File(FileLocator
.toFileURL(location
).toURI());
97 TmfTraceStub trace
= new TmfTraceStub(fResource
, testfile
.toURI().getPath(), ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false, null);
98 trace
.indexTrace(true);
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 public void testNoOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
108 ITmfTrace trace
= createAndIndexTrace();
109 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
111 ITmfEvent event
= trace
.getNext(context
);
112 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
113 event
= trace
.getNext(context
);
114 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
120 public void testPositiveOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
121 ITmfTimestampTransform tt
= TimestampTransformFactory
.createWithOffset(ONE_MS
);
122 TimestampTransformFactory
.setTimestampTransform(fResource
, tt
);
124 ITmfTrace trace
= createAndIndexTrace();
125 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
127 ITmfEvent event
= trace
.getNext(context
);
128 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
129 event
= trace
.getNext(context
);
130 assertEquals("Event timestamp", 3, event
.getTimestamp().getValue());
136 public void testNegativeOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
137 ITmfTimestampTransform tt
= TimestampTransformFactory
.createWithOffset(-ONE_MS
);
138 TimestampTransformFactory
.setTimestampTransform(fResource
, tt
);
140 ITmfTrace trace
= createAndIndexTrace();
141 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
143 ITmfEvent event
= trace
.getNext(context
);
144 assertEquals("Event timestamp", 0, event
.getTimestamp().getValue());
145 event
= trace
.getNext(context
);
146 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
152 public void testClearOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
153 ITmfTimestampTransform tt
= TimestampTransformFactory
.createWithOffset(ONE_MS
);
154 TimestampTransformFactory
.setTimestampTransform(fResource
, tt
);
155 TimestampTransformFactory
.setTimestampTransform(fResource
, null);
157 ITmfTrace trace
= createAndIndexTrace();
158 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
160 ITmfEvent event
= trace
.getNext(context
);
161 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
162 event
= trace
.getNext(context
);
163 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());