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