ctf: Make CtfStreamInputReader NonNullByDefault
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / CTFTraceWriterTest.java
CommitLineData
51173fa1
BH
1/*******************************************************************************
2 * Copyright (c) 2015 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.ctf.core.tests.trace;
14
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16import static org.junit.Assert.assertEquals;
17import static org.junit.Assert.assertFalse;
18import static org.junit.Assert.assertTrue;
19import static org.junit.Assert.fail;
20import static org.junit.Assume.assumeTrue;
21
22import java.io.File;
23import java.net.URISyntaxException;
24import java.util.LinkedList;
25import java.util.List;
26
27import org.eclipse.core.runtime.URIUtil;
28import org.eclipse.tracecompass.ctf.core.CTFException;
29import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
30import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTrace;
31import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
32import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
33import org.eclipse.tracecompass.ctf.core.trace.CTFTraceWriter;
1b638236 34import org.eclipse.tracecompass.internal.ctf.core.trace.Utils;
51173fa1
BH
35import org.junit.BeforeClass;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.junit.runners.Parameterized;
39import org.junit.runners.Parameterized.Parameters;
40
41/**
42 * CTFTraceWriter test cases
43 *
44 * @author Bernd Hufmann
45 *
46 */
47@SuppressWarnings("javadoc")
48@RunWith(Parameterized.class)
49public class CTFTraceWriterTest {
50
51 private static File fTempDir;
52
53 // Trace details
54 private static final long CLOCK_OFFSET = 1332166405241713987L;
55 private static final int TOTAL_NB_EVENTS = 695319;
56 private static final long LAST_EVENT_TIME = 1332170692664579801L;
57
58 // Stream 0 values
59 private static final long STREAM0_FIRST_PACKET_TIME = CLOCK_OFFSET + 4277170993912L;
60 private static final long STREAM0_FIRST_EVENT_TIME = 1332170682440316151L;
61 private static final long STREAM0_LAST_EVENT_TIME = 1332170682702066969L;
62 private static final int STREAM0_FIRST_PACKET_NB_EVENTS = 14219;
63
64 // Stream 1 values
65 private static final long STREAM1_FIRST_PACKET_TIME = CLOCK_OFFSET + 4277171555436L;
66 private static final int STREAM1_FIRST_PACKET_NB_EVENTS = 8213;
67 private static final long STREAM1_FIRST_EVENT_TIME = 1332170682440133097L;
68 private static final long STREAM1_FIFTH_PACKET_TIME = CLOCK_OFFSET + 4277970712221L;
69 private static final long STREAM1_TENTH_PACKET_TIME = CLOCK_OFFSET + 4279440048309L;
70 private static final long STREAM1_FIFTH_PACKET_FIRST_EVENT_TIME = 1332170683212426208L;
71 private static final long STREAM1_TENTH_PACKET_LAST_EVENT_TIME = 1332170685256508077L;
72
73 // Miscellaneous
74 private static final int NB_EVENTS_SEVERAL_PACKETS = 138894;
75
76 // Test parameters
77 private String fName;
78 private long fStartTime;
79 private long fEndTime;
80 private int fNbEvents;
81 private long fFirstEventTime;
82 private long fLastEventTime;
83
84 /**
85 * Gets a list of test case parameters.
86 *
87 * @return The list of test parameters
88 */
89 @Parameters(name = "{index}: {0}")
90 public static Iterable<Object[]> getTestParams() {
91 final List<Object[]> params = new LinkedList<>();
92
93 addParams(params, "WHOLE_TRACE",
94 0,
95 Long.MAX_VALUE,
96 TOTAL_NB_EVENTS,
97 STREAM1_FIRST_EVENT_TIME,
98 LAST_EVENT_TIME);
99
100 addParams(params, "NO_EVENTS_USING_INVERTED_TIME",
101 Long.MAX_VALUE, Long.MIN_VALUE,
102 0,
103 -1,
104 -1);
105
106 addParams(params, "NO_EVENTS_USING_FIRST_PACKET",
107 STREAM0_FIRST_PACKET_TIME + 1,
108 STREAM0_FIRST_PACKET_TIME + 1,
109 0,
110 -1,
111 -1);
112
113 addParams(params, "STREAM0_FIRST_PACKET_TIME",
114 STREAM0_FIRST_PACKET_TIME,
115 STREAM0_FIRST_PACKET_TIME,
116 STREAM0_FIRST_PACKET_NB_EVENTS,
117 STREAM0_FIRST_EVENT_TIME,
118 STREAM0_LAST_EVENT_TIME);
119
120 addParams(params, "BOTH_STREAMS_FIRST_PACKET_ONLY",
121 STREAM0_FIRST_PACKET_TIME,
122 STREAM1_FIRST_PACKET_TIME,
123 STREAM0_FIRST_PACKET_NB_EVENTS + STREAM1_FIRST_PACKET_NB_EVENTS,
124 STREAM1_FIRST_EVENT_TIME,
125 STREAM0_LAST_EVENT_TIME);
126
127 addParams(params, "BOTH_STREAMS_SEVERAL_PACKETS",
128 STREAM1_FIFTH_PACKET_TIME,
129 STREAM1_TENTH_PACKET_TIME,
130 NB_EVENTS_SEVERAL_PACKETS,
131 STREAM1_FIFTH_PACKET_FIRST_EVENT_TIME,
132 STREAM1_TENTH_PACKET_LAST_EVENT_TIME);
133
134 return params;
135 }
136
137 private static void addParams(List<Object[]> params, String name, long startTime, long endTime, int nbEvents, long firstEventTime, long lastEventTime) {
138 Object array[] = new Object[] { name, startTime, endTime, nbEvents, firstEventTime, lastEventTime };
139 params.add(array);
140 }
141
142 @BeforeClass
143 public static void beforeClass() {
144 String property = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
145 File dir = null;
146 if (property != null) {
147 try {
148 dir = URIUtil.toFile(URIUtil.fromString(property));
149 dir = new File(dir.getAbsolutePath() + File.separator);
150 if (!dir.exists()) {
151 dir.mkdirs();
152 }
153 } catch (URISyntaxException e) {
154 }
155 }
156 if (dir == null) {
157 dir = new File(System.getProperty("java.io.tmpdir")); //$NON-NLS-1$)
158 }
159 String tempDir = dir.getAbsolutePath() + File.separator + "testcases" + File.separator;
160 fTempDir = new File(tempDir);
161 if (!fTempDir.exists()) {
162 fTempDir.mkdirs();
163 }
164 }
165
166 public CTFTraceWriterTest (String name, long startTime, long endTime, int nbEvents, long firstEventTime, long lastEventTime) {
167 fName = name;
168 fStartTime = startTime;
169 fEndTime = endTime;
170 fNbEvents = nbEvents;
171 fFirstEventTime = firstEventTime;
172 fLastEventTime = lastEventTime;
173 }
174
175 /**
176 * Test various time ranges
177 */
178 @Test
179 public void testKernelTrace() {
180 assumeTrue(CtfTestTrace.KERNEL.exists());
181 try {
182 CTFTrace trace = CtfTestTrace.KERNEL.getTrace();
183 CTFTraceWriter ctfWriter = new CTFTraceWriter(checkNotNull(trace));
184 String traceName = createTraceName(fName);
185 ctfWriter.copyPackets(fStartTime, fEndTime, traceName);
186
187 File metadata = new File(traceName + Utils.SEPARATOR + "metadata");
188 assertTrue("metadata", metadata.exists());
189
190 CTFTrace outTrace = new CTFTrace(traceName);
191 int count = 0;
192 Long start = null;
193 long end = 0;
194 try (CTFTraceReader reader = new CTFTraceReader(outTrace)) {
195 while(reader.hasMoreEvents()) {
196 count++;
197 EventDefinition def = reader.getCurrentEventDef();
198 end = def.getTimestamp();
199 if (start == null) {
200 start = outTrace.getClock().getClockOffset() + reader.getStartTime();
201 }
202 reader.advance();
203 }
204 end = outTrace.getClock().getClockOffset() + end;
205 }
206
207 if (fFirstEventTime >= 0) {
208 assertEquals("first event time", Long.valueOf(fFirstEventTime), start);
209 }
210 if (fLastEventTime >= 0) {
211 assertEquals("last event time", fLastEventTime, end);
212 }
213 assertEquals(toString(), fNbEvents, count);
214
215 if (fNbEvents == 0) {
216 assertFalse("channel0", getChannelFile(traceName, 0).exists());
217 assertFalse("channel1", getChannelFile(traceName, 1).exists());
218 }
219
220 } catch (CTFException e) {
221 fail();
222 }
223 }
224
225 private static File getChannelFile(String path, int id) {
226 File channel = new File(path + Utils.SEPARATOR + "channel_" + String.valueOf(id));
227 return channel;
228 }
229
230 private static String createTraceName(String testCase) {
231 return fTempDir.getAbsolutePath() + File.separator + testCase.toString();
232 }
233
234}
This page took 0.052164 seconds and 5 git commands to generate.