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; | |
8192209b | 30 | import org.eclipse.jdt.annotation.NonNull; |
b8585c7c | 31 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
35f39420 AM |
32 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
33 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; | |
34 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; | |
b8585c7c AM |
35 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
36 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager; | |
37 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; | |
38 | import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; | |
39 | import org.eclipse.tracecompass.tmf.core.tests.analysis.AnalysisManagerTest; | |
40 | import org.eclipse.tracecompass.tmf.core.tests.shared.TmfTestTrace; | |
41 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
42 | import org.eclipse.tracecompass.tmf.core.trace.TmfTrace; | |
43 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; | |
44 | import org.eclipse.tracecompass.tmf.tests.stubs.analysis.TestAnalysis; | |
45 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; | |
46 | import org.junit.After; | |
47 | import org.junit.Before; | |
48 | import org.junit.Test; | |
49 | ||
35f39420 AM |
50 | import com.google.common.collect.ImmutableList; |
51 | ||
b8585c7c AM |
52 | /** |
53 | * Test suite for {@link TmfTraceUtils} | |
54 | */ | |
55 | public 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() { | |
35f39420 | 71 | @Override |
52293ccd GB |
72 | public Integer resolve(ITmfEvent event) { |
73 | return 1; | |
35f39420 AM |
74 | } |
75 | }); | |
76 | builder.addAll(TmfTrace.BASE_ASPECTS); | |
8192209b AM |
77 | @SuppressWarnings("null") |
78 | @NonNull Collection<ITmfEventAspect> ret = builder.build(); | |
79 | EVENT_ASPECTS = ret; | |
35f39420 AM |
80 | } |
81 | ||
82 | public TmfTraceStubWithAspects(String path) throws TmfTraceException { | |
83 | super(path, ITmfTrace.DEFAULT_TRACE_CACHE_SIZE, false, null); | |
84 | } | |
85 | ||
86 | @Override | |
87 | public Iterable<ITmfEventAspect> getEventAspects() { | |
88 | return EVENT_ASPECTS; | |
89 | } | |
90 | ||
91 | } | |
92 | ||
b8585c7c AM |
93 | // ------------------------------------------------------------------------ |
94 | // Housekeeping | |
95 | // ------------------------------------------------------------------------ | |
96 | ||
97 | /** | |
98 | * Test setup | |
99 | */ | |
100 | @Before | |
101 | public void setUp() { | |
102 | try { | |
103 | final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(TEST_TRACE.getFullPath()), null); | |
104 | final File test = new File(FileLocator.toFileURL(location).toURI()); | |
35f39420 | 105 | fTrace = new TmfTraceStubWithAspects(test.toURI().getPath()); |
b8585c7c AM |
106 | TmfSignalManager.deregister(fTrace); |
107 | fTrace.indexTrace(true); | |
108 | } catch (final TmfTraceException | URISyntaxException | IOException e) { | |
109 | fail(e.getMessage()); | |
110 | } | |
111 | } | |
112 | ||
113 | /** | |
114 | * Test cleanup | |
115 | */ | |
116 | @After | |
117 | public void tearDown() { | |
118 | fTrace.dispose(); | |
119 | fTrace = null; | |
120 | } | |
121 | ||
122 | // ------------------------------------------------------------------------ | |
123 | // Test methods | |
124 | // ------------------------------------------------------------------------ | |
125 | ||
126 | /** | |
127 | * Test the {@link TmfTraceUtils#getAnalysisModuleOfClass} method. | |
128 | */ | |
129 | @Test | |
130 | public void testGetModulesByClass() { | |
131 | /* Open the trace, the modules should be populated */ | |
132 | fTrace.traceOpened(new TmfTraceOpenedSignal(this, fTrace, null)); | |
133 | ||
134 | Iterable<TestAnalysis> testModules = TmfTraceUtils.getAnalysisModulesOfClass(fTrace, TestAnalysis.class); | |
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 | ||
148 | TestAnalysis module = TmfTraceUtils.getAnalysisModuleOfClass(fTrace, TestAnalysis.class, AnalysisManagerTest.MODULE_PARAM); | |
149 | assertNotNull(module); | |
150 | IAnalysisModule traceModule = fTrace.getAnalysisModule(AnalysisManagerTest.MODULE_PARAM); | |
151 | assertNotNull(traceModule); | |
152 | assertEquals(module, traceModule); | |
153 | ||
154 | } | |
35f39420 AM |
155 | |
156 | /** | |
157 | * Test the {@link TmfTraceUtils#getEventAspectsOfClass} method. | |
158 | */ | |
159 | @Test | |
160 | public void testGetEventAspectsOfClass() { | |
161 | /* Our custom trace type adds 1 aspect in addition to the base ones */ | |
162 | Iterable<ITmfEventAspect> aspects = TmfTraceUtils.getEventAspectsOfClass(fTrace, ITmfEventAspect.class); | |
163 | Collection<ITmfEventAspect> aspectColl = ImmutableList.copyOf(aspects); | |
164 | assertEquals(TmfTrace.BASE_ASPECTS.size() + 1, aspectColl.size()); | |
165 | ||
166 | /* Make sure there is one and only one CpuAspect */ | |
167 | Iterable<TmfCpuAspect> cpuAspects = TmfTraceUtils.getEventAspectsOfClass(fTrace, TmfCpuAspect.class); | |
168 | Iterator<TmfCpuAspect> iter = cpuAspects.iterator(); | |
169 | assertNotNull(iter.next()); | |
170 | assertFalse(iter.hasNext()); | |
171 | } | |
b8585c7c | 172 | } |