| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 Ericsson |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Matthew Khouzam - Initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.tracecompass.ctf.core.tests.trace; |
| 13 | |
| 14 | import static org.junit.Assert.assertEquals; |
| 15 | import static org.junit.Assert.assertFalse; |
| 16 | import static org.junit.Assert.assertNotNull; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | |
| 19 | import org.eclipse.tracecompass.ctf.core.CTFException; |
| 20 | import org.eclipse.tracecompass.ctf.core.event.EventDefinition; |
| 21 | import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils; |
| 22 | import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; |
| 23 | import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader; |
| 24 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | |
| 28 | /** |
| 29 | * The class <code>CTFTraceReaderTest</code> contains tests for the class |
| 30 | * <code>{@link CTFTraceReader}</code>. |
| 31 | * |
| 32 | * @author ematkho |
| 33 | * @version $Revision: 1.0 $ |
| 34 | */ |
| 35 | @SuppressWarnings("javadoc") |
| 36 | public class CTFTraceReaderTest { |
| 37 | |
| 38 | private static final CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
| 39 | |
| 40 | private CTFTraceReader fixture; |
| 41 | |
| 42 | /** |
| 43 | * Perform pre-test initialization. |
| 44 | * |
| 45 | * @throws CTFException |
| 46 | */ |
| 47 | @Before |
| 48 | public void setUp() throws CTFException { |
| 49 | fixture = new CTFTraceReader(CtfTestTraceUtils.getTrace(testTrace)); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Run the CTFTraceReader(CTFTrace) constructor test. Open a known good |
| 54 | * trace. |
| 55 | * |
| 56 | * @throws CTFException |
| 57 | */ |
| 58 | @Test |
| 59 | public void testOpen_existing() throws CTFException { |
| 60 | CTFTrace trace = CtfTestTraceUtils.getTrace(testTrace); |
| 61 | try (CTFTraceReader result = new CTFTraceReader(trace);) { |
| 62 | assertNotNull(result); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Run the CTFTraceReader(CTFTrace) constructor test. Open a non-existing |
| 68 | * trace, expect the exception. |
| 69 | * |
| 70 | * @throws CTFException |
| 71 | */ |
| 72 | @Test(expected = org.eclipse.tracecompass.ctf.core.CTFException.class) |
| 73 | public void testOpen_nonexisting() throws CTFException { |
| 74 | CTFTrace trace = new CTFTrace("badfile.bad"); |
| 75 | try (CTFTraceReader result = new CTFTraceReader(trace);) { |
| 76 | assertNotNull(result); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Run the CTFTraceReader(CTFTrace) constructor test. Try to pen an invalid |
| 82 | * path, expect exception. |
| 83 | * |
| 84 | * @throws CTFException |
| 85 | */ |
| 86 | @Test(expected = org.eclipse.tracecompass.ctf.core.CTFException.class) |
| 87 | public void testOpen_invalid() throws CTFException { |
| 88 | CTFTrace trace = new CTFTrace(""); |
| 89 | try (CTFTraceReader result = new CTFTraceReader(trace);) { |
| 90 | assertNotNull(result); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Run the boolean advance() method test. Test advancing normally. |
| 96 | * |
| 97 | * @throws CTFException |
| 98 | * error |
| 99 | */ |
| 100 | @Test |
| 101 | public void testAdvance_normal() throws CTFException { |
| 102 | boolean result = fixture.advance(); |
| 103 | assertTrue(result); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Run the boolean advance() method test. Test advancing when we're at the |
| 108 | * end, so we expect that there is no more events. |
| 109 | * |
| 110 | * @throws CTFException |
| 111 | * error |
| 112 | */ |
| 113 | @Test |
| 114 | public void testAdvance_end() throws CTFException { |
| 115 | int i = 0; |
| 116 | boolean result = fixture.advance(); |
| 117 | while (result) { |
| 118 | result = fixture.advance(); |
| 119 | i++; |
| 120 | } |
| 121 | fixture.seek(0); |
| 122 | fixture.advance(); |
| 123 | fixture.goToLastEvent(); |
| 124 | i = 1; |
| 125 | result = fixture.advance(); |
| 126 | while (result) { |
| 127 | result = fixture.advance(); |
| 128 | i++; |
| 129 | } |
| 130 | assertFalse(result); |
| 131 | assertEquals(i, 1); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Run the CTFTraceReader copy constructor test. |
| 136 | * |
| 137 | * @throws CTFException |
| 138 | * error |
| 139 | */ |
| 140 | @Test |
| 141 | public void testCopyFrom() throws CTFException { |
| 142 | try (CTFTraceReader result = fixture.copyFrom();) { |
| 143 | assertNotNull(result); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Run the getCurrentEventDef() method test. Get the first event's |
| 149 | * definition. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testGetCurrentEventDef_first() { |
| 153 | EventDefinition result = fixture.getCurrentEventDef(); |
| 154 | assertNotNull(result); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Run the getCurrentEventDef() method test. Get the last event's |
| 159 | * definition. |
| 160 | * |
| 161 | * @throws CTFException |
| 162 | * error |
| 163 | */ |
| 164 | @Test |
| 165 | public void testGetCurrentEventDef_last() throws CTFException { |
| 166 | fixture.goToLastEvent(); |
| 167 | EventDefinition result = fixture.getCurrentEventDef(); |
| 168 | assertNotNull(result); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Run the long getEndTime() method test. |
| 173 | */ |
| 174 | @Test |
| 175 | public void testGetEndTime() { |
| 176 | long result = fixture.getEndTime(); |
| 177 | assertTrue(0L < result); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Run the long getStartTime() method test. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testGetStartTime() { |
| 185 | long result = fixture.getStartTime(); |
| 186 | assertTrue(0L < result); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Run the void goToLastEvent() method test. |
| 191 | * |
| 192 | * @throws CTFException |
| 193 | * error |
| 194 | */ |
| 195 | @Test |
| 196 | public void testGoToLastEvent() throws CTFException { |
| 197 | fixture.goToLastEvent(); |
| 198 | long ts1 = getTimestamp(); |
| 199 | long ts2 = fixture.getEndTime(); |
| 200 | assertEquals(ts1, ts2); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Run the boolean hasMoreEvents() method test. |
| 205 | * |
| 206 | * @throws CTFException |
| 207 | */ |
| 208 | @Test |
| 209 | public void testHasMoreEvents() { |
| 210 | boolean result = fixture.hasMoreEvents(); |
| 211 | assertTrue(result); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Run the void printStats() method test with no 'width' parameter. |
| 216 | * |
| 217 | * @throws CTFException |
| 218 | * error |
| 219 | */ |
| 220 | @Test |
| 221 | public void testPrintStats_noparam() throws CTFException { |
| 222 | fixture.advance(); |
| 223 | fixture.printStats(); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Run the void printStats(int) method test with width = 0. |
| 228 | * |
| 229 | * @throws CTFException |
| 230 | * error |
| 231 | */ |
| 232 | @Test |
| 233 | public void testPrintStats_width0() throws CTFException { |
| 234 | fixture.advance(); |
| 235 | fixture.printStats(0); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Run the void printStats(int) method test with width = 1. |
| 240 | * |
| 241 | * @throws CTFException |
| 242 | * error |
| 243 | */ |
| 244 | @Test |
| 245 | public void testPrintStats_width1() throws CTFException { |
| 246 | fixture.advance(); |
| 247 | fixture.printStats(1); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Run the void printStats(int) method test with width = 2. |
| 252 | * |
| 253 | * @throws CTFException |
| 254 | * error |
| 255 | */ |
| 256 | @Test |
| 257 | public void testPrintStats_width2() throws CTFException { |
| 258 | fixture.advance(); |
| 259 | fixture.printStats(2); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Run the void printStats(int) method test with width = 10. |
| 264 | * |
| 265 | * @throws CTFException |
| 266 | * error |
| 267 | */ |
| 268 | @Test |
| 269 | public void testPrintStats_width10() throws CTFException { |
| 270 | fixture.advance(); |
| 271 | fixture.printStats(10); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Run the void printStats(int) method test with width = 100. |
| 276 | * |
| 277 | * @throws CTFException |
| 278 | * error |
| 279 | */ |
| 280 | @Test |
| 281 | public void testPrintStats_100() throws CTFException { |
| 282 | for (int i = 0; i < 1000; i++) { |
| 283 | fixture.advance(); |
| 284 | } |
| 285 | fixture.printStats(100); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Run the boolean seek(long) method test. |
| 290 | * |
| 291 | * @throws CTFException |
| 292 | * error |
| 293 | */ |
| 294 | @Test |
| 295 | public void testSeek() throws CTFException { |
| 296 | long timestamp = 1L; |
| 297 | boolean result = fixture.seek(timestamp); |
| 298 | assertTrue(result); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * @return |
| 303 | */ |
| 304 | private long getTimestamp() { |
| 305 | if (fixture.getCurrentEventDef() != null) { |
| 306 | return fixture.getTrace().timestampCyclesToNanos(fixture.getCurrentEventDef().getTimestamp()); |
| 307 | } |
| 308 | return -1; |
| 309 | } |
| 310 | } |