1 /*******************************************************************************
2 * Copyright (c) 2014 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
;
21 import java
.io
.IOException
;
22 import java
.net
.URISyntaxException
;
24 import java
.util
.Collection
;
26 import org
.eclipse
.core
.runtime
.FileLocator
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.TmfCoreTestPlugin
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.analysis
.AnalysisManagerTest
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestTrace
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
43 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.analysis
.TestAnalysis
;
44 import org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.TmfTraceStub
;
45 import org
.junit
.After
;
46 import org
.junit
.Before
;
47 import org
.junit
.Test
;
49 import com
.google
.common
.collect
.ImmutableList
;
52 * Test suite for {@link TmfTraceUtils}
54 public class TmfTraceUtilsTest
{
56 private static final TmfTestTrace TEST_TRACE
= TmfTestTrace
.A_TEST_10K
;
58 private TmfTrace fTrace
;
60 // ------------------------------------------------------------------------
61 // Test trace class definition
62 // ------------------------------------------------------------------------
64 private static class TmfTraceStubWithAspects
extends TmfTraceStub
{
66 private static final @NonNull Collection
<ITmfEventAspect
> EVENT_ASPECTS
;
68 ImmutableList
.Builder
<ITmfEventAspect
> builder
= ImmutableList
.builder();
69 builder
.add(new TmfCpuAspect() {
71 public Integer
resolve(ITmfEvent event
) {
75 builder
.addAll(TmfTrace
.BASE_ASPECTS
);
76 @SuppressWarnings("null")
77 @NonNull Collection
<ITmfEventAspect
> ret
= builder
.build();
81 public TmfTraceStubWithAspects(String path
) throws TmfTraceException
{
82 super(path
, ITmfTrace
.DEFAULT_TRACE_CACHE_SIZE
, false, null);
86 public Iterable
<ITmfEventAspect
> getEventAspects() {
92 // ------------------------------------------------------------------------
94 // ------------------------------------------------------------------------
100 public void setUp() {
102 final URL location
= FileLocator
.find(TmfCoreTestPlugin
.getDefault().getBundle(), new Path(TEST_TRACE
.getFullPath()), null);
103 final File test
= new File(FileLocator
.toFileURL(location
).toURI());
104 fTrace
= new TmfTraceStubWithAspects(test
.toURI().getPath());
105 TmfSignalManager
.deregister(fTrace
);
106 fTrace
.indexTrace(true);
107 } catch (final TmfTraceException
| URISyntaxException
| IOException e
) {
108 fail(e
.getMessage());
116 public void tearDown() {
121 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
126 * Test the {@link TmfTraceUtils#getAnalysisModuleOfClass} method.
129 public void testGetModulesByClass() {
130 TmfTrace trace
= fTrace
;
131 assertNotNull(trace
);
133 /* Open the trace, the modules should be populated */
134 trace
.traceOpened(new TmfTraceOpenedSignal(this, trace
, null));
136 Iterable
<TestAnalysis
> testModules
= TmfTraceUtils
.getAnalysisModulesOfClass(trace
, TestAnalysis
.class);
137 assertTrue(testModules
.iterator().hasNext());
140 for (TestAnalysis module
: testModules
) {
141 assertNotNull(module
);
145 * FIXME: The exact count depends on the context the test is run (full
146 * test suite or this file only), but there must be at least 2 modules
148 assertTrue(count
>= 2);
150 TestAnalysis module
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, TestAnalysis
.class, AnalysisManagerTest
.MODULE_PARAM
);
151 assertNotNull(module
);
152 IAnalysisModule traceModule
= trace
.getAnalysisModule(AnalysisManagerTest
.MODULE_PARAM
);
153 assertNotNull(traceModule
);
154 assertEquals(module
, traceModule
);
159 * Test the {@link TmfTraceUtils#resolveEventAspectOfClassForEvent(ITmfTrace, Class, ITmfEvent)} method.
162 public void testResolveEventAspectsOfClassForEvent() {
163 TmfTrace trace
= fTrace
;
164 assertNotNull(trace
);
166 ITmfContext context
= trace
.seekEvent(0L);
167 ITmfEvent event
= trace
.getNext(context
);
168 assertNotNull(event
);
170 /* Make sure the CPU aspect returns the expected value */
171 Object cpuObj
= TmfTraceUtils
.resolveEventAspectOfClassForEvent(trace
, TmfCpuAspect
.class, event
);
172 assertNotNull(cpuObj
);
173 assertEquals(1, cpuObj
);