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; | |
23 | ||
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; | |
46 | ||
47 | /** | |
48 | * Test suite for time offset of traces. | |
49 | */ | |
50 | @SuppressWarnings("javadoc") | |
51 | public class TimeOffsetTest { | |
52 | ||
53 | /** Time-out tests after 20 seconds */ | |
54 | @Rule | |
55 | public TestRule globalTimeout= new Timeout(20000); | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Variables | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
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; | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // Housekeeping | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
71 | @Before | |
72 | public void setUp() throws CoreException { | |
73 | IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(PROJECT); | |
74 | if (!project.exists()) { | |
75 | project.create(null); | |
76 | } | |
77 | project.open(null); | |
78 | fResource = project.getFile(RESOURCE); | |
79 | if (!fResource.exists()) { | |
80 | final InputStream source = new ByteArrayInputStream(new byte[0]); | |
81 | fResource.create(source, true, null); | |
82 | } | |
83 | fResource.setPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER, fResource.getParent().getLocationURI().getPath()); | |
84 | } | |
85 | ||
86 | @After | |
87 | public void tearDown() throws CoreException { | |
88 | if (fResource != null && fResource.exists()) { | |
89 | fResource.getProject().delete(true, null); | |
90 | } | |
91 | } | |
92 | ||
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); | |
98 | return trace; | |
99 | } | |
100 | ||
101 | // ------------------------------------------------------------------------ | |
102 | // Tests | |
103 | // ------------------------------------------------------------------------ | |
104 | ||
105 | @Test | |
106 | public void testNoOffset() throws URISyntaxException, IOException, TmfTraceException { | |
107 | ITmfTrace trace = createAndIndexTrace(); | |
108 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
109 | ||
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()); | |
114 | ||
115 | trace.dispose(); | |
116 | } | |
117 | ||
118 | @Test | |
119 | public void testPositiveOffset() throws URISyntaxException, IOException, TmfTraceException { | |
120 | ITmfTimestampTransform tt = TimestampTransformFactory.createWithOffset(ONE_MS); | |
121 | TimestampTransformFactory.setTimestampTransform(fResource, tt); | |
122 | ||
123 | ITmfTrace trace = createAndIndexTrace(); | |
124 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
125 | ||
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()); | |
130 | ||
131 | trace.dispose(); | |
132 | } | |
133 | ||
134 | @Test | |
135 | public void testNegativeOffset() throws URISyntaxException, IOException, TmfTraceException { | |
136 | ITmfTimestampTransform tt = TimestampTransformFactory.createWithOffset(-ONE_MS); | |
137 | TimestampTransformFactory.setTimestampTransform(fResource, tt); | |
138 | ||
139 | ITmfTrace trace = createAndIndexTrace(); | |
140 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
141 | ||
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()); | |
146 | ||
147 | trace.dispose(); | |
148 | } | |
149 | ||
150 | @Test | |
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); | |
155 | ||
156 | ITmfTrace trace = createAndIndexTrace(); | |
157 | final TmfContext context = (TmfContext) trace.seekEvent(0); | |
158 | ||
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()); | |
163 | ||
164 | trace.dispose(); | |
165 | } | |
166 | } |