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