releng: Transition to jdt.annotation 2.0
[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
8192209b 66 private static final @NonNull Collection<ITmfEventAspect> EVENT_ASPECTS;
35f39420
AM
67 static {
68 ImmutableList.Builder<ITmfEventAspect> builder = ImmutableList.builder();
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);
8192209b
AM
76 @SuppressWarnings("null")
77 @NonNull Collection<ITmfEventAspect> ret = builder.build();
78 EVENT_ASPECTS = ret;
35f39420
AM
79 }
80
81 public TmfTraceStubWithAspects(String path) throws TmfTraceException {
82 super(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false, null);
83 }
84
85 @Override
86 public Iterable<ITmfEventAspect> getEventAspects() {
87 return EVENT_ASPECTS;
88 }
89
90 }
91
b8585c7c
AM
92 // ------------------------------------------------------------------------
93 // Housekeeping
94 // ------------------------------------------------------------------------
95
96 /**
97 * Test setup
98 */
99 @Before
100 public void setUp() {
101 try {
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());
35f39420 104 fTrace = new TmfTraceStubWithAspects(test.toURI().getPath());
b8585c7c
AM
105 TmfSignalManager.deregister(fTrace);
106 fTrace.indexTrace(true);
107 } catch (final TmfTraceException | URISyntaxException | IOException e) {
108 fail(e.getMessage());
109 }
110 }
111
112 /**
113 * Test cleanup
114 */
115 @After
116 public void tearDown() {
117 fTrace.dispose();
118 fTrace = null;
119 }
120
121 // ------------------------------------------------------------------------
122 // Test methods
123 // ------------------------------------------------------------------------
124
125 /**
126 * Test the {@link TmfTraceUtils#getAnalysisModuleOfClass} method.
127 */
128 @Test
129 public void testGetModulesByClass() {
1d83ed07
AM
130 TmfTrace trace = fTrace;
131 assertNotNull(trace);
132
b8585c7c 133 /* Open the trace, the modules should be populated */
1d83ed07 134 trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
b8585c7c 135
1d83ed07 136 Iterable<TestAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(trace, TestAnalysis.class);
b8585c7c
AM
137 assertTrue(testModules.iterator().hasNext());
138
139 int count = 0;
140 for (TestAnalysis module : testModules) {
141 assertNotNull(module);
142 count++;
143 }
144 /*
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
147 */
148 assertTrue(count >= 2);
149
1d83ed07 150 TestAnalysis module = TmfTraceUtils.getAnalysisModuleOfClass(trace, TestAnalysis.class, AnalysisManagerTest.MODULE_PARAM);
b8585c7c 151 assertNotNull(module);
1d83ed07 152 IAnalysisModule traceModule = trace.getAnalysisModule(AnalysisManagerTest.MODULE_PARAM);
b8585c7c
AM
153 assertNotNull(traceModule);
154 assertEquals(module, traceModule);
155
156 }
35f39420
AM
157
158 /**
b1aad44e 159 * Test the {@link TmfTraceUtils#resolveEventAspectOfClassForEvent(ITmfTrace, Class, ITmfEvent)} method.
35f39420
AM
160 */
161 @Test
b1aad44e 162 public void testResolveEventAspectsOfClassForEvent() {
1d83ed07
AM
163 TmfTrace trace = fTrace;
164 assertNotNull(trace);
165
166 ITmfContext context = trace.seekEvent(0L);
167 ITmfEvent event = trace.getNext(context);
b1aad44e
GB
168 assertNotNull(event);
169
170 /* Make sure the CPU aspect returns the expected value */
1d83ed07 171 Object cpuObj = TmfTraceUtils.resolveEventAspectOfClassForEvent(trace, TmfCpuAspect.class, event);
b1aad44e
GB
172 assertNotNull(cpuObj);
173 assertEquals(1, cpuObj);
174
35f39420 175 }
b8585c7c 176}
This page took 0.051561 seconds and 5 git commands to generate.