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
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
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.io.File;
21 import java.io.IOException;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.util.Collection;
25
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;
48
49 import com.google.common.collect.ImmutableList;
50
51 /**
52 * Test suite for {@link TmfTraceUtils}
53 */
54 public class TmfTraceUtilsTest {
55
56 private static final TmfTestTrace TEST_TRACE = TmfTestTrace.A_TEST_10K;
57
58 private TmfTrace fTrace;
59
60 // ------------------------------------------------------------------------
61 // Test trace class definition
62 // ------------------------------------------------------------------------
63
64 private static class TmfTraceStubWithAspects extends TmfTraceStub {
65
66 private static final @NonNull Collection<ITmfEventAspect> EVENT_ASPECTS;
67 static {
68 ImmutableList.Builder<ITmfEventAspect> builder = ImmutableList.builder();
69 builder.add(new TmfCpuAspect() {
70 @Override
71 public Integer resolve(ITmfEvent event) {
72 return 1;
73 }
74 });
75 builder.addAll(TmfTrace.BASE_ASPECTS);
76 @SuppressWarnings("null")
77 @NonNull Collection<ITmfEventAspect> ret = builder.build();
78 EVENT_ASPECTS = ret;
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
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());
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());
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() {
130 TmfTrace trace = fTrace;
131 assertNotNull(trace);
132
133 /* Open the trace, the modules should be populated */
134 trace.traceOpened(new TmfTraceOpenedSignal(this, trace, null));
135
136 Iterable<TestAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(trace, TestAnalysis.class);
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
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);
155
156 }
157
158 /**
159 * Test the {@link TmfTraceUtils#resolveEventAspectOfClassForEvent(ITmfTrace, Class, ITmfEvent)} method.
160 */
161 @Test
162 public void testResolveEventAspectsOfClassForEvent() {
163 TmfTrace trace = fTrace;
164 assertNotNull(trace);
165
166 ITmfContext context = trace.seekEvent(0L);
167 ITmfEvent event = trace.getNext(context);
168 assertNotNull(event);
169
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);
174
175 }
176 }
This page took 0.059154 seconds and 5 git commands to generate.