Commit | Line | Data |
---|---|---|
0f19a90f PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.core.tests.synchronization; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | ||
17 | import java.io.ByteArrayInputStream; | |
18 | import java.io.File; | |
19 | import java.io.IOException; | |
20 | import java.io.InputStream; | |
21 | import java.net.URISyntaxException; | |
22 | import java.net.URL; | |
d291a715 | 23 | import java.util.concurrent.TimeUnit; |
0f19a90f PT |
24 | |
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; | |
47 | ||
48 | /** | |
49 | * Test suite for time offset of traces. | |
50 | */ | |
51 | @SuppressWarnings("javadoc") | |
52 | public class TimeOffsetTest { | |
53 | ||
54 | /** Time-out tests after 20 seconds */ | |
55 | @Rule | |
d291a715 | 56 | public TestRule globalTimeout= new Timeout(20, TimeUnit.SECONDS); |
0f19a90f PT |
57 | |
58 | // ------------------------------------------------------------------------ | |
59 | // Variables | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
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; | |
67 | ||
68 | // ------------------------------------------------------------------------ | |
69 | // Housekeeping | |
70 | // ------------------------------------------------------------------------ | |
71 | ||
72 | @Before | |
73 | public void setUp() throws CoreException { | |
74 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT); | |
75 | if (!project.exists()) { | |
76 | project.create(null); | |
77 | } | |
78 | project.open(null); | |
79 | fResource = project.getFile(RESOURCE); | |
80 | if (!fResource.exists()) { | |
81 | final InputStream source = new ByteArrayInputStream(new byte[0]); | |
82 | fResource.create(source, true, null); | |
83 | } | |
cdb43c16 | 84 | fResource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, fResource.getParent().getLocation().toOSString()); |
0f19a90f PT |
85 | } |
86 | ||
87 | @After | |
88 | public void tearDown() throws CoreException { | |
89 | if (fResource != null && fResource.exists()) { | |
90 | fResource.getProject().delete(true, null); | |
91 | } | |
92 | } | |
93 | ||
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); | |
99 | return trace; | |
100 | } | |
101 | ||
102 | // ------------------------------------------------------------------------ | |
103 | // Tests | |
104 | // ------------------------------------------------------------------------ | |
105 | ||
106 | @Test | |
107 | public void testNoOffset() throws URISyntaxException, IOException, TmfTraceException { | |
108 | ITmfTrace trace = createAndIndexTrace(); | |
109 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
110 | ||
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()); | |
115 | ||
116 | trace.dispose(); | |
117 | } | |
118 | ||
119 | @Test | |
120 | public void testPositiveOffset() throws URISyntaxException, IOException, TmfTraceException { | |
121 | ITmfTimestampTransform tt = TimestampTransformFactory.createWithOffset(ONE_MS); | |
122 | TimestampTransformFactory.setTimestampTransform(fResource, tt); | |
123 | ||
124 | ITmfTrace trace = createAndIndexTrace(); | |
125 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
126 | ||
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()); | |
131 | ||
132 | trace.dispose(); | |
133 | } | |
134 | ||
135 | @Test | |
136 | public void testNegativeOffset() throws URISyntaxException, IOException, TmfTraceException { | |
137 | ITmfTimestampTransform tt = TimestampTransformFactory.createWithOffset(-ONE_MS); | |
138 | TimestampTransformFactory.setTimestampTransform(fResource, tt); | |
139 | ||
140 | ITmfTrace trace = createAndIndexTrace(); | |
141 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
142 | ||
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()); | |
147 | ||
148 | trace.dispose(); | |
149 | } | |
150 | ||
151 | @Test | |
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); | |
156 | ||
157 | ITmfTrace trace = createAndIndexTrace(); | |
158 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
159 | ||
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()); | |
164 | ||
165 | trace.dispose(); | |
166 | } | |
167 | } |