tmf: Add possibility to add global aspects
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / TmfTraceUtilsTest.java
CommitLineData
b8585c7c 1/*******************************************************************************
2cf3cc72 2 * Copyright (c) 2014, 2017 Ericsson
b8585c7c
AM
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 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.tmf.core.tests.trace;
14
15import static org.junit.Assert.assertEquals;
16import static org.junit.Assert.assertNotNull;
cf392b73 17import static org.junit.Assert.assertNull;
b8585c7c
AM
18import static org.junit.Assert.assertTrue;
19import static org.junit.Assert.fail;
20
35f39420 21import java.util.Collection;
b8585c7c 22
8192209b 23import org.eclipse.jdt.annotation.NonNull;
cf392b73 24import org.eclipse.jdt.annotation.Nullable;
b8585c7c 25import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
35f39420
AM
26import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
27import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
28import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
b8585c7c
AM
29import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
30import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
31import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
b8585c7c
AM
32import org.eclipse.tracecompass.tmf.core.tests.analysis.AnalysisManagerTest;
33import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace;
b1aad44e 34import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
b8585c7c
AM
35import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
36import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
37import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
38import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis;
39import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub;
40import org.junit.After;
41import org.junit.Before;
42import org.junit.Test;
43
35f39420
AM
44import com.google.common.collect.ImmutableList;
45
b8585c7c
AM
46/**
47 * Test suite for {@link TmfTraceUtils}
48 */
49public class TmfTraceUtilsTest {
50
51 private static final TmfTestTrace TEST_TRACE = TmfTestTrace.A_TEST_10K;
52
53 private TmfTrace fTrace;
54
35f39420
AM
55 // ------------------------------------------------------------------------
56 // Test trace class definition
57 // ------------------------------------------------------------------------
58
59 private static class TmfTraceStubWithAspects extends TmfTraceStub {
60
ec48d248 61 private static final @NonNull Collection<ITmfEventAspect<?>> EVENT_ASPECTS;
35f39420 62 static {
ec48d248 63 ImmutableList.Builder<ITmfEventAspect<?>> builder = ImmutableList.builder();
35f39420 64 builder.add(new TmfCpuAspect() {
35f39420 65 @Override
52293ccd
GB
66 public Integer resolve(ITmfEvent event) {
67 return 1;
35f39420
AM
68 }
69 });
70 builder.addAll(TmfTrace.BASE_ASPECTS);
872ec368 71 EVENT_ASPECTS = builder.build();
35f39420
AM
72 }
73
74 public TmfTraceStubWithAspects(String path) throws TmfTraceException {
75 super(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false, null);
76 }
77
78 @Override
ec48d248 79 public Iterable<ITmfEventAspect<?>> getEventAspects() {
35f39420
AM
80 return EVENT_ASPECTS;
81 }
82
83 }
84
cf392b73
GB
85 private static class TestEventAspect implements ITmfEventAspect<Integer> {
86
87 public static final Integer RESOLVED_VALUE = 2;
88
89 @Override
90 public @NonNull String getName() {
91 return "test";
92 }
93
94 @Override
95 public @NonNull String getHelpText() {
96 return "test";
97 }
98
99 @Override
100 public @Nullable Integer resolve(@NonNull ITmfEvent event) {
101 return RESOLVED_VALUE;
102 }
103
104 }
105
b8585c7c
AM
106 // ------------------------------------------------------------------------
107 // Housekeeping
108 // ------------------------------------------------------------------------
109
110 /**
111 * Test setup
112 */
113 @Before
114 public void setUp() {
115 try {
2cf3cc72 116 fTrace = new TmfTraceStubWithAspects(TEST_TRACE.getFullPath());
b8585c7c
AM
117 TmfSignalManager.deregister(fTrace);
118 fTrace.indexTrace(true);
2cf3cc72 119 } catch (final TmfTraceException e) {
b8585c7c
AM
120 fail(e.getMessage());
121 }
122 }
123
124 /**
125 * Test cleanup
126 */
127 @After
128 public void tearDown() {
129 fTrace.dispose();
130 fTrace = null;
131 }
132
133 // ------------------------------------------------------------------------
134 // Test methods
135 // ------------------------------------------------------------------------
136
137 /**
138 * Test the {@link TmfTraceUtils#getAnalysisModuleOfClass} method.
139 */
140 @Test
141 public void testGetModulesByClass() {
1d83ed07
AM
142 TmfTrace trace = fTrace;
143 assertNotNull(trace);
144
b8585c7c 145 /* Open the trace, the modules should be populated */
1d83ed07 146 trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
b8585c7c 147
1d83ed07 148 Iterable<TestAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(trace, TestAnalysis.class);
b8585c7c
AM
149 assertTrue(testModules.iterator().hasNext());
150
151 int count = 0;
152 for (TestAnalysis module : testModules) {
153 assertNotNull(module);
154 count++;
155 }
156 /*
157 * FIXME: The exact count depends on the context the test is run (full
158 * test suite or this file only), but there must be at least 2 modules
159 */
160 assertTrue(count >= 2);
161
1d83ed07 162 TestAnalysis module = TmfTraceUtils.getAnalysisModuleOfClass(trace, TestAnalysis.class, AnalysisManagerTest.MODULE_PARAM);
b8585c7c 163 assertNotNull(module);
1d83ed07 164 IAnalysisModule traceModule = trace.getAnalysisModule(AnalysisManagerTest.MODULE_PARAM);
b8585c7c
AM
165 assertNotNull(traceModule);
166 assertEquals(module, traceModule);
167
168 }
35f39420
AM
169
170 /**
b1aad44e 171 * Test the {@link TmfTraceUtils#resolveEventAspectOfClassForEvent(ITmfTrace, Class, ITmfEvent)} method.
35f39420
AM
172 */
173 @Test
b1aad44e 174 public void testResolveEventAspectsOfClassForEvent() {
1d83ed07
AM
175 TmfTrace trace = fTrace;
176 assertNotNull(trace);
177
178 ITmfContext context = trace.seekEvent(0L);
179 ITmfEvent event = trace.getNext(context);
b1aad44e
GB
180 assertNotNull(event);
181
182 /* Make sure the CPU aspect returns the expected value */
cf392b73 183 Object cpuObj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TmfCpuAspect.class, event);
b1aad44e
GB
184 assertNotNull(cpuObj);
185 assertEquals(1, cpuObj);
186
35f39420 187 }
cf392b73
GB
188
189 /**
190 * Test the {@link TmfTraceUtils#registerEventAspect(ITmfEventAspect)} method
191 */
192 @Test
193 public void testAdditionalAspects() {
194 TmfTrace trace = fTrace;
195
196 assertNotNull(trace);
197
198 ITmfContext context = trace.seekEvent(0L);
199 ITmfEvent event = trace.getNext(context);
200 assertNotNull(event);
201
202 Object obj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TestEventAspect.class, event);
203 assertNull(obj);
204
205 // Register the aspect
206 TmfTraceUtils.registerEventAspect(new TestEventAspect());
207 // See that the aspect is resolved now
208 obj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TestEventAspect.class, event);
209 assertNotNull(obj);
210 assertEquals(TestEventAspect.RESOLVED_VALUE, obj);
211 }
b8585c7c 212}
This page took 0.069702 seconds and 5 git commands to generate.