| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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 | * Matthew Khouzam - Initial generation with CodePro tools |
| 11 | * Alexandre Montplaisir - Clean up, consolidate redundant tests |
| 12 | *******************************************************************************/ |
| 13 | |
| 14 | package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; |
| 15 | |
| 16 | import static org.junit.Assert.assertEquals; |
| 17 | import static org.junit.Assert.assertFalse; |
| 18 | import static org.junit.Assert.assertNotNull; |
| 19 | import static org.junit.Assert.assertNull; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | import static org.junit.Assume.assumeTrue; |
| 22 | |
| 23 | import org.eclipse.core.resources.IProject; |
| 24 | import org.eclipse.core.resources.IResource; |
| 25 | import org.eclipse.core.runtime.IStatus; |
| 26 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocation; |
| 27 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocationInfo; |
| 28 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; |
| 29 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp; |
| 30 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; |
| 31 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 32 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
| 33 | import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal; |
| 34 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignal; |
| 35 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTraces; |
| 36 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 37 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 38 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 39 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 40 | import org.junit.After; |
| 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | |
| 44 | /** |
| 45 | * The class <code>CtfTmfTraceTest</code> contains tests for the class |
| 46 | * <code>{@link CtfTmfTrace}</code>. |
| 47 | * |
| 48 | * @author ematkho |
| 49 | * @version 1.0 |
| 50 | */ |
| 51 | public class CtfTmfTraceTest { |
| 52 | |
| 53 | private static final int TRACE_INDEX = 0; |
| 54 | private static final String PATH = CtfTmfTestTraces.getTestTracePath(TRACE_INDEX); |
| 55 | |
| 56 | private CtfTmfTrace fixture; |
| 57 | |
| 58 | /** |
| 59 | * Perform pre-test initialization. |
| 60 | * |
| 61 | * @throws TmfTraceException |
| 62 | * If the test trace is not found |
| 63 | */ |
| 64 | @Before |
| 65 | public void setUp() throws TmfTraceException { |
| 66 | assumeTrue(CtfTmfTestTraces.tracesExist()); |
| 67 | fixture = new CtfTmfTrace(); |
| 68 | fixture.initTrace((IResource) null, PATH, CtfTmfEvent.class); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Perform post-test clean-up. |
| 73 | */ |
| 74 | @After |
| 75 | public void tearDown() { |
| 76 | if (fixture != null) { |
| 77 | fixture.dispose(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Run the CtfTmfTrace() constructor test. |
| 83 | */ |
| 84 | @Test |
| 85 | public void testCtfTmfTrace() { |
| 86 | CtfTmfTrace result = new CtfTmfTrace(); |
| 87 | |
| 88 | assertNotNull(result); |
| 89 | assertNull(result.getEventType()); |
| 90 | assertEquals(1000, result.getCacheSize()); |
| 91 | assertEquals(0L, result.getNbEvents()); |
| 92 | assertEquals(0L, result.getStreamingInterval()); |
| 93 | assertNull(result.getResource()); |
| 94 | assertEquals(1000, result.getQueueSize()); |
| 95 | assertNull(result.getType()); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Test the parseEvent() method |
| 100 | */ |
| 101 | @Test |
| 102 | public void testParseEvent() { |
| 103 | ITmfContext ctx = fixture.seekEvent(0); |
| 104 | fixture.getNext(ctx); |
| 105 | CtfTmfEvent event = fixture.parseEvent(ctx); |
| 106 | assertNotNull(event); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Run the void broadcast(TmfSignal) method test. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testBroadcast() { |
| 114 | TmfSignal signal = new TmfEndSynchSignal(1); |
| 115 | fixture.broadcast(signal); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /** |
| 120 | * Run the void dispose() method test. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testDispose() { |
| 124 | CtfTmfTrace emptyFixture = new CtfTmfTrace(); |
| 125 | emptyFixture.dispose(); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Run the int getCacheSize() method test. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testGetCacheSize() { |
| 134 | CtfTmfTrace emptyFixture = new CtfTmfTrace(); |
| 135 | int result = emptyFixture.getCacheSize(); |
| 136 | assertEquals(1000, result); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Run the ITmfLocation<Comparable> getCurrentLocation() method test. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testGetCurrentLocation() { |
| 144 | CtfLocation result = (CtfLocation) fixture.getCurrentLocation(); |
| 145 | assertNull(result); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Test the seekEvent() method with a null location. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testSeekEventLoc_null() { |
| 153 | CtfLocation loc = null; |
| 154 | fixture.seekEvent(loc); |
| 155 | assertNotNull(fixture); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Test the seekEvent() method with a location from a timestamp. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testSeekEventLoc_timetamp(){ |
| 163 | CtfLocation loc = new CtfLocation(new CtfTmfTimestamp(0L)); |
| 164 | fixture.seekEvent(loc); |
| 165 | assertNotNull(fixture); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Run the ITmfTimestamp getEndTime() method test. |
| 171 | */ |
| 172 | @Test |
| 173 | public void testGetEndTime() { |
| 174 | ITmfTimestamp result = fixture.getEndTime(); |
| 175 | assertNotNull(result); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Run the String getEnvironment method test. |
| 180 | */ |
| 181 | @Test |
| 182 | public void testGetEnvValue() { |
| 183 | String key = "tracer_name"; |
| 184 | String result = fixture.getTraceProperties().get(key); |
| 185 | assertEquals("\"lttng-modules\"",result); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Run the Class<CtfTmfEvent> getEventType() method test. |
| 190 | */ |
| 191 | @Test |
| 192 | public void testGetEventType() { |
| 193 | Class<ITmfEvent> result = fixture.getEventType(); |
| 194 | assertNotNull(result); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Run the double getLocationRatio(ITmfLocation<?>) method test. |
| 199 | */ |
| 200 | @Test |
| 201 | public void testGetLocationRatio() { |
| 202 | final CtfLocationInfo location2 = new CtfLocationInfo(1, 0); |
| 203 | CtfLocation location = new CtfLocation(location2); |
| 204 | double result = fixture.getLocationRatio(location); |
| 205 | |
| 206 | assertEquals(Double.NEGATIVE_INFINITY, result, 0.1); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Run the String getName() method test. |
| 211 | */ |
| 212 | @Test |
| 213 | public void testGetName() { |
| 214 | String result = fixture.getName(); |
| 215 | assertNotNull(result); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Run the int getNbEnvVars() method test. |
| 220 | */ |
| 221 | @Test |
| 222 | public void testGetNbEnvVars() { |
| 223 | int result = fixture.getTraceProperties().size(); |
| 224 | assertEquals(8, result); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Run the long getNbEvents() method test. |
| 229 | */ |
| 230 | @Test |
| 231 | public void testGetNbEvents() { |
| 232 | long result = fixture.getNbEvents(); |
| 233 | assertEquals(1L, result); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Run the CtfTmfEvent getNext(ITmfContext) method test. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testGetNext() { |
| 241 | ITmfContext context = fixture.seekEvent(0); |
| 242 | CtfTmfEvent result = fixture.getNext(context); |
| 243 | assertNotNull(result); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Run the String getPath() method test. |
| 248 | */ |
| 249 | @Test |
| 250 | public void testGetPath() { |
| 251 | String result = fixture.getPath(); |
| 252 | assertNotNull(result); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Run the IResource getResource() method test. |
| 257 | */ |
| 258 | @Test |
| 259 | public void testGetResource() { |
| 260 | IResource result = fixture.getResource(); |
| 261 | assertNull(result); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Run the ITmfTimestamp getStartTime() method test. |
| 266 | */ |
| 267 | @Test |
| 268 | public void testGetStartTime() { |
| 269 | ITmfTimestamp result = fixture.getStartTime(); |
| 270 | assertNotNull(result); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Run the long getStreamingInterval() method test. |
| 275 | */ |
| 276 | @Test |
| 277 | public void testGetStreamingInterval() { |
| 278 | long result = fixture.getStreamingInterval(); |
| 279 | assertEquals(0L, result); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Run the TmfTimeRange getTimeRange() method test. |
| 284 | */ |
| 285 | @Test |
| 286 | public void testGetTimeRange() { |
| 287 | TmfTimeRange result = fixture.getTimeRange(); |
| 288 | assertNotNull(result); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Run the CtfTmfEvent readNextEvent(ITmfContext) method test. |
| 293 | */ |
| 294 | @Test |
| 295 | public void testReadNextEvent() { |
| 296 | ITmfContext context = fixture.seekEvent(0); |
| 297 | CtfTmfEvent result = fixture.getNext(context); |
| 298 | assertNotNull(result); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Run the ITmfContext seekEvent(double) method test. |
| 303 | */ |
| 304 | @Test |
| 305 | public void testSeekEvent_ratio() { |
| 306 | double ratio = 0.99; |
| 307 | ITmfContext result = fixture.seekEvent(ratio); |
| 308 | assertNotNull(result); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Run the ITmfContext seekEvent(long) method test. |
| 313 | */ |
| 314 | @Test |
| 315 | public void testSeekEvent_rank() { |
| 316 | long rank = 1L; |
| 317 | ITmfContext result = fixture.seekEvent(rank); |
| 318 | assertNotNull(result); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Run the ITmfContext seekEvent(ITmfTimestamp) method test. |
| 323 | */ |
| 324 | @Test |
| 325 | public void testSeekEvent_timestamp() { |
| 326 | ITmfTimestamp timestamp = new TmfTimestamp(); |
| 327 | ITmfContext result = fixture.seekEvent(timestamp); |
| 328 | assertNotNull(result); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Run the ITmfContext seekEvent(ITmfLocation<?>) method test. |
| 333 | */ |
| 334 | @Test |
| 335 | public void testSeekEvent_location() { |
| 336 | final CtfLocationInfo location2 = new CtfLocationInfo(1L, 0L); |
| 337 | CtfLocation ctfLocation = new CtfLocation(location2); |
| 338 | ITmfContext result = fixture.seekEvent(ctfLocation); |
| 339 | assertNotNull(result); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Run the boolean validate(IProject,String) method test. |
| 344 | */ |
| 345 | @Test |
| 346 | public void testValidate() { |
| 347 | IProject project = null; |
| 348 | String path = PATH; |
| 349 | IStatus result = fixture.validate(project, path); |
| 350 | assertTrue(result.isOK()); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Run the boolean hasEvent(final String) method test |
| 355 | */ |
| 356 | @Test |
| 357 | public void testEventLookup() { |
| 358 | assertTrue(fixture.hasEvent("sched_switch")); |
| 359 | assertFalse(fixture.hasEvent("Sched_switch")); |
| 360 | String[] events = { "sched_switch", "sched_wakeup", "timer_init" }; |
| 361 | assertTrue(fixture.hasAllEvents(events)); |
| 362 | assertTrue(fixture.hasAtLeastOneOfEvents(events)); |
| 363 | String[] names = { "inexistent", "sched_switch", "SomeThing" }; |
| 364 | assertTrue(fixture.hasAtLeastOneOfEvents(names)); |
| 365 | assertFalse(fixture.hasAllEvents(names)); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Run the String getHostId() method test |
| 370 | */ |
| 371 | @Test |
| 372 | public void testCtfHostId() { |
| 373 | String a = fixture.getHostId(); |
| 374 | assertEquals("\"84db105b-b3f4-4821-b662-efc51455106a\"", a); |
| 375 | } |
| 376 | |
| 377 | } |