69ee1098eb63a59fc80e49b5852081662380ea97
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / TmfTraceUtilsTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2017 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 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.tests.trace;
14
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;
19
20 import java.util.Collection;
21
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;
41
42 import com.google.common.collect.ImmutableList;
43
44 /**
45 * Test suite for {@link TmfTraceUtils}
46 */
47 public class TmfTraceUtilsTest {
48
49 private static final TmfTestTrace TEST_TRACE = TmfTestTrace.A_TEST_10K;
50
51 private TmfTrace fTrace;
52
53 // ------------------------------------------------------------------------
54 // Test trace class definition
55 // ------------------------------------------------------------------------
56
57 private static class TmfTraceStubWithAspects extends TmfTraceStub {
58
59 private static final @NonNull Collection<ITmfEventAspect<?>> EVENT_ASPECTS;
60 static {
61 ImmutableList.Builder<ITmfEventAspect<?>> builder = ImmutableList.builder();
62 builder.add(new TmfCpuAspect() {
63 @Override
64 public Integer resolve(ITmfEvent event) {
65 return 1;
66 }
67 });
68 builder.addAll(TmfTrace.BASE_ASPECTS);
69 EVENT_ASPECTS = builder.build();
70 }
71
72 public TmfTraceStubWithAspects(String path) throws TmfTraceException {
73 super(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false, null);
74 }
75
76 @Override
77 public Iterable<ITmfEventAspect<?>> getEventAspects() {
78 return EVENT_ASPECTS;
79 }
80
81 }
82
83 // ------------------------------------------------------------------------
84 // Housekeeping
85 // ------------------------------------------------------------------------
86
87 /**
88 * Test setup
89 */
90 @Before
91 public void setUp() {
92 try {
93 fTrace = new TmfTraceStubWithAspects(TEST_TRACE.getFullPath());
94 TmfSignalManager.deregister(fTrace);
95 fTrace.indexTrace(true);
96 } catch (final TmfTraceException e) {
97 fail(e.getMessage());
98 }
99 }
100
101 /**
102 * Test cleanup
103 */
104 @After
105 public void tearDown() {
106 fTrace.dispose();
107 fTrace = null;
108 }
109
110 // ------------------------------------------------------------------------
111 // Test methods
112 // ------------------------------------------------------------------------
113
114 /**
115 * Test the {@link TmfTraceUtils#getAnalysisModuleOfClass} method.
116 */
117 @Test
118 public void testGetModulesByClass() {
119 TmfTrace trace = fTrace;
120 assertNotNull(trace);
121
122 /* Open the trace, the modules should be populated */
123 trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
124
125 Iterable<TestAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(trace, TestAnalysis.class);
126 assertTrue(testModules.iterator().hasNext());
127
128 int count = 0;
129 for (TestAnalysis module : testModules) {
130 assertNotNull(module);
131 count++;
132 }
133 /*
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
136 */
137 assertTrue(count >= 2);
138
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);
144
145 }
146
147 /**
148 * Test the {@link TmfTraceUtils#resolveEventAspectOfClassForEvent(ITmfTrace, Class, ITmfEvent)} method.
149 */
150 @Test
151 public void testResolveEventAspectsOfClassForEvent() {
152 TmfTrace trace = fTrace;
153 assertNotNull(trace);
154
155 ITmfContext context = trace.seekEvent(0L);
156 ITmfEvent event = trace.getNext(context);
157 assertNotNull(event);
158
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);
163
164 }
165 }
This page took 0.035112 seconds and 5 git commands to generate.