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
;
24 import org
.eclipse
.core
.resources
.IFile
;
25 import org
.eclipse
.core
.resources
.IProject
;
26 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
27 import org
.eclipse
.core
.runtime
.CoreException
;
28 import org
.eclipse
.core
.runtime
.FileLocator
;
29 import org
.eclipse
.core
.runtime
.Path
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.TmfCommonConstants
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.ITmfTimestampTransform
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.synchronization
.TimestampTransformFactory
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
39 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
40 import org
.junit
.After
;
41 import org
.junit
.Before
;
42 import org
.junit
.Rule
;
43 import org
.junit
.Test
;
44 import org
.junit
.rules
.TestRule
;
45 import org
.junit
.rules
.Timeout
;
48 * Test suite for time offset of traces.
50 @SuppressWarnings("javadoc")
51 public class TimeOffsetTest
{
53 /** Time-out tests after 20 seconds */
55 public TestRule globalTimeout
= new Timeout(20000);
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
61 private static final TmfTestTrace TEST_TRACE
= TmfTestTrace
.A_TEST_10K
;
62 private static final String PROJECT
= "Test Project";
63 private static final String RESOURCE
= "Test Resource";
64 private static final long ONE_MS
= 1000000L;
65 private IFile fResource
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 public void setUp() throws CoreException
{
73 IProject project
= ResourcesPlugin
.getWorkspace().getRoot().getProject(PROJECT
);
74 if (!project
.exists()) {
78 fResource
= project
.getFile(RESOURCE
);
79 if (!fResource
.exists()) {
80 final InputStream source
= new ByteArrayInputStream(new byte[0]);
81 fResource
.create(source
, true, null);
83 fResource
.setPersistentProperty(TmfCommonConstants
.TRACE_SUPPLEMENTARY_FOLDER
, fResource
.getParent().getLocation().toOSString());
87 public void tearDown() throws CoreException
{
88 if (fResource
!= null && fResource
.exists()) {
89 fResource
.getProject().delete(true, null);
93 private ITmfTrace
createAndIndexTrace() throws URISyntaxException
, IOException
, TmfTraceException
{
94 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TEST_TRACE
.getFullPath()), null);
95 File testfile
= new File(FileLocator
.toFileURL(location
).toURI());
96 TmfTraceStub trace
= new TmfTraceStub(fResource
, testfile
.toURI().getPath(), ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false, null);
97 trace
.indexTrace(true);
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 public void testNoOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
107 ITmfTrace trace
= createAndIndexTrace();
108 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
110 ITmfEvent event
= trace
.getNext(context
);
111 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
112 event
= trace
.getNext(context
);
113 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
119 public void testPositiveOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
120 ITmfTimestampTransform tt
= TimestampTransformFactory
.createWithOffset(ONE_MS
);
121 TimestampTransformFactory
.setTimestampTransform(fResource
, tt
);
123 ITmfTrace trace
= createAndIndexTrace();
124 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
126 ITmfEvent event
= trace
.getNext(context
);
127 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());
128 event
= trace
.getNext(context
);
129 assertEquals("Event timestamp", 3, event
.getTimestamp().getValue());
135 public void testNegativeOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
136 ITmfTimestampTransform tt
= TimestampTransformFactory
.createWithOffset(-ONE_MS
);
137 TimestampTransformFactory
.setTimestampTransform(fResource
, tt
);
139 ITmfTrace trace
= createAndIndexTrace();
140 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
142 ITmfEvent event
= trace
.getNext(context
);
143 assertEquals("Event timestamp", 0, event
.getTimestamp().getValue());
144 event
= trace
.getNext(context
);
145 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
151 public void testClearOffset() throws URISyntaxException
, IOException
, TmfTraceException
{
152 ITmfTimestampTransform tt
= TimestampTransformFactory
.createWithOffset(ONE_MS
);
153 TimestampTransformFactory
.setTimestampTransform(fResource
, tt
);
154 TimestampTransformFactory
.setTimestampTransform(fResource
, null);
156 ITmfTrace trace
= createAndIndexTrace();
157 final TmfContext context
= (TmfContext
) trace
.seekEvent(0);
159 ITmfEvent event
= trace
.getNext(context
);
160 assertEquals("Event timestamp", 1, event
.getTimestamp().getValue());
161 event
= trace
.getNext(context
);
162 assertEquals("Event timestamp", 2, event
.getTimestamp().getValue());