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