1 /*******************************************************************************
2 * Copyright (c) 2014, 2017 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertNotNull
;
17 import static org
.junit
.Assert
.assertTrue
;
18 import static org
.junit
.Assert
.fail
;
20 import java
.util
.Collection
;
22 import org
.eclipse
.jdt
.annotation
.NonNull
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.analysis
.AnalysisManagerTest
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
36 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
.TestAnalysis
;
37 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
38 import org
.junit
.After
;
39 import org
.junit
.Before
;
40 import org
.junit
.Test
;
42 import com
.google
.common
.collect
.ImmutableList
;
45 * Test suite for {@link TmfTraceUtils}
47 public class TmfTraceUtilsTest
{
49 private static final TmfTestTrace TEST_TRACE
= TmfTestTrace
.A_TEST_10K
;
51 private TmfTrace fTrace
;
53 // ------------------------------------------------------------------------
54 // Test trace class definition
55 // ------------------------------------------------------------------------
57 private static class TmfTraceStubWithAspects
extends TmfTraceStub
{
59 private static final @NonNull Collection
<ITmfEventAspect
<?
>> EVENT_ASPECTS
;
61 ImmutableList
.Builder
<ITmfEventAspect
<?
>> builder
= ImmutableList
.builder();
62 builder
.add(new TmfCpuAspect() {
64 public Integer
resolve(ITmfEvent event
) {
68 builder
.addAll(TmfTrace
.BASE_ASPECTS
);
69 EVENT_ASPECTS
= builder
.build();
72 public TmfTraceStubWithAspects(String path
) throws TmfTraceException
{
73 super(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false, null);
77 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
93 fTrace
= new TmfTraceStubWithAspects(TEST_TRACE
.getFullPath());
94 TmfSignalManager
.deregister(fTrace
);
95 fTrace
.indexTrace(true);
96 } catch (final TmfTraceException e
) {
105 public void tearDown() {
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
115 * Test the {@link TmfTraceUtils#getAnalysisModuleOfClass} method.
118 public void testGetModulesByClass() {
119 TmfTrace trace
= fTrace
;
120 assertNotNull(trace
);
122 /* Open the trace, the modules should be populated */
123 trace
.traceOpened(new TmfTraceOpenedSignal(this, trace
, null));
125 Iterable
<TestAnalysis
> testModules
= TmfTraceUtils
.getAnalysisModulesOfClass(trace
, TestAnalysis
.class);
126 assertTrue(testModules
.iterator().hasNext());
129 for (TestAnalysis module
: testModules
) {
130 assertNotNull(module
);
134 * FIXME: The exact count depends on the context the test is run (full
135 * test suite or this file only), but there must be at least 2 modules
137 assertTrue(count
>= 2);
139 TestAnalysis module
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, TestAnalysis
.class, AnalysisManagerTest
.MODULE_PARAM
);
140 assertNotNull(module
);
141 IAnalysisModule traceModule
= trace
.getAnalysisModule(AnalysisManagerTest
.MODULE_PARAM
);
142 assertNotNull(traceModule
);
143 assertEquals(module
, traceModule
);
148 * Test the {@link TmfTraceUtils#resolveEventAspectOfClassForEvent(ITmfTrace, Class, ITmfEvent)} method.
151 public void testResolveEventAspectsOfClassForEvent() {
152 TmfTrace trace
= fTrace
;
153 assertNotNull(trace
);
155 ITmfContext context
= trace
.seekEvent(0L);
156 ITmfEvent event
= trace
.getNext(context
);
157 assertNotNull(event
);
159 /* Make sure the CPU aspect returns the expected value */
160 Object cpuObj
= TmfTraceUtils
.resolveEventAspectOfClassForEvent(trace
, TmfCpuAspect
.class, event
);
161 assertNotNull(cpuObj
);
162 assertEquals(1, cpuObj
);