Commit | Line | Data |
---|---|---|
1d8ab692 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 École Polytechnique de Montréal | |
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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.tests.trace.stub; |
1d8ab692 GB |
14 | |
15 | import static org.junit.Assert.assertEquals; | |
69ff4f26 | 16 | import static org.junit.Assert.assertNotNull; |
1d8ab692 GB |
17 | import static org.junit.Assert.assertTrue; |
18 | import static org.junit.Assert.fail; | |
19 | ||
20 | import java.io.File; | |
56e9f830 GB |
21 | import java.io.IOException; |
22 | import java.net.URL; | |
1d8ab692 | 23 | |
56e9f830 GB |
24 | import org.eclipse.core.runtime.FileLocator; |
25 | import org.eclipse.core.runtime.IPath; | |
1d8ab692 | 26 | import org.eclipse.core.runtime.IStatus; |
56e9f830 GB |
27 | import org.eclipse.core.runtime.Path; |
28 | import org.eclipse.core.runtime.Plugin; | |
1d8ab692 | 29 | import org.eclipse.core.runtime.Status; |
2bdf0193 AM |
30 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
31 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
32 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; | |
69ff4f26 GB |
33 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
34 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; | |
2bdf0193 AM |
35 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
36 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest; | |
37 | import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest; | |
38 | import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin; | |
39 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; | |
69ff4f26 | 40 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
2bdf0193 AM |
41 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
42 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub; | |
1d8ab692 GB |
43 | import org.junit.Test; |
44 | ||
45 | /** | |
46 | * Test suite for the {@link TmfXmlTraceStub} class | |
47 | * | |
48 | * @author Geneviève Bastien | |
49 | */ | |
50 | public class XmlStubTraceTest { | |
51 | ||
56e9f830 GB |
52 | private static final Path VALID_FILE = new Path("testfiles/stub_xml_traces/valid/test.xml"); |
53 | private static final Path VALID_PATH = new Path("testfiles/stub_xml_traces/valid"); | |
54 | private static final Path INVALID_PATH = new Path("testfiles/stub_xml_traces/invalid"); | |
1d8ab692 GB |
55 | |
56 | private static final String EVENT_A = "A"; | |
57 | private static final String EVENT_B = "B"; | |
58 | private static final String FIELD_A = "b"; | |
59 | private static final String FIELD_B = "f"; | |
60 | ||
56e9f830 GB |
61 | private static IPath getAbsolutePath(Path relativePath) { |
62 | Plugin plugin = TmfCoreTestPlugin.getDefault(); | |
63 | if (plugin == null) { | |
64 | /* | |
65 | * Shouldn't happen but at least throw something to get the test to | |
66 | * fail early | |
67 | */ | |
68 | throw new IllegalStateException(); | |
69 | } | |
70 | URL location = FileLocator.find(plugin.getBundle(), relativePath, null); | |
71 | try { | |
72 | IPath path = new Path(FileLocator.toFileURL(location).getPath()); | |
73 | return path; | |
74 | } catch (IOException e) { | |
75 | throw new IllegalStateException(); | |
76 | } | |
77 | } | |
78 | ||
1d8ab692 GB |
79 | /** |
80 | * Test the | |
81 | * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)} | |
82 | * method | |
83 | */ | |
84 | @Test | |
85 | public void testValidate() { | |
86 | TmfXmlTraceStub trace = new TmfXmlTraceStub(); | |
56e9f830 | 87 | File[] invalidFiles = getAbsolutePath(INVALID_PATH).toFile().listFiles(); |
1d8ab692 GB |
88 | assertTrue(invalidFiles.length > 0); |
89 | for (File f : invalidFiles) { | |
90 | assertTrue(!trace.validate(null, f.getAbsolutePath()).isOK()); | |
91 | } | |
92 | ||
56e9f830 | 93 | File[] validFiles = getAbsolutePath(VALID_PATH).toFile().listFiles(); |
1d8ab692 GB |
94 | assertTrue(validFiles.length > 0); |
95 | for (File f : validFiles) { | |
96 | assertTrue(trace.validate(null, f.getAbsolutePath()).isOK()); | |
97 | } | |
98 | } | |
99 | ||
100 | /** | |
101 | * Test the reading and querying the XML trace and make sure fields are | |
102 | * present | |
103 | */ | |
104 | @Test | |
105 | public void testDevelopmentTrace() { | |
106 | TmfXmlTraceStub trace = new TmfXmlTraceStub(); | |
56e9f830 | 107 | IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString()); |
1d8ab692 GB |
108 | if (!status.isOK()) { |
109 | fail(status.getException().getMessage()); | |
110 | } | |
111 | ||
112 | try { | |
56e9f830 | 113 | trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class); |
1d8ab692 GB |
114 | } catch (TmfTraceException e1) { |
115 | fail(e1.getMessage()); | |
116 | } | |
117 | ||
118 | CustomEventRequest req = new CustomEventRequest(trace); | |
119 | trace.sendRequest(req); | |
120 | try { | |
121 | req.waitForCompletion(); | |
122 | if (req.isCancelled()) { | |
123 | fail(req.getStatus().getMessage()); | |
124 | } | |
125 | } catch (InterruptedException e) { | |
126 | fail(e.getMessage()); | |
127 | } | |
128 | assertEquals(4, req.getCount()); | |
129 | } | |
130 | ||
69ff4f26 GB |
131 | /** |
132 | * Test the presence and resolve of the aspects for this trace | |
133 | */ | |
134 | @Test | |
135 | public void testAspects() { | |
136 | TmfXmlTraceStub trace = new TmfXmlTraceStub(); | |
137 | IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString()); | |
138 | if (!status.isOK()) { | |
139 | fail(status.getException().getMessage()); | |
140 | } | |
141 | ||
142 | try { | |
143 | trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class); | |
144 | } catch (TmfTraceException e1) { | |
145 | fail(e1.getMessage()); | |
146 | } | |
147 | ||
148 | ITmfEventAspect cpuAspect = null; | |
149 | ITmfEventAspect testAspect = null; | |
150 | int aspectCount = 0; | |
151 | for (ITmfEventAspect aspect : trace.getEventAspects()) { | |
152 | aspectCount++; | |
153 | if (aspect instanceof TmfCpuAspect) { | |
154 | cpuAspect = aspect; | |
155 | } else if (aspect.getName().equals("test")) { | |
156 | testAspect = aspect; | |
157 | } | |
158 | } | |
159 | /* Check the presence of the cpu and test aspects */ | |
160 | assertEquals("Number of aspects", 5, aspectCount); | |
161 | assertNotNull(cpuAspect); | |
162 | assertNotNull(testAspect); | |
163 | ||
164 | ITmfContext ctx; | |
165 | ctx = trace.seekEvent(0L); | |
166 | assertNotNull(ctx); | |
167 | ITmfEvent event = trace.getNext(ctx); | |
168 | assertNotNull(event); | |
169 | assertEquals("Cpu aspect of event 1", 1, cpuAspect.resolve(event)); | |
170 | assertEquals("Test aspect of event 1", "abc", testAspect.resolve(event)); | |
171 | event = trace.getNext(ctx); | |
172 | assertNotNull(event); | |
173 | assertEquals("Cpu aspect of event 2", 1, cpuAspect.resolve(event)); | |
174 | assertEquals("Test aspect of event 2", "abc", testAspect.resolve(event)); | |
175 | event = trace.getNext(ctx); | |
176 | assertNotNull(event); | |
177 | assertEquals("Cpu aspect of event 3", 2, cpuAspect.resolve(event)); | |
178 | assertEquals("Test aspect of event 3", "def", testAspect.resolve(event)); | |
179 | event = trace.getNext(ctx); | |
180 | assertNotNull(event); | |
181 | assertEquals("Cpu aspect of event 4", 1, cpuAspect.resolve(event)); | |
182 | assertEquals("Test aspect of event 4", "def", testAspect.resolve(event)); | |
183 | } | |
184 | ||
1d8ab692 | 185 | private static IStatus testEvent(ITmfEvent event) { |
578716e6 | 186 | switch (event.getName()) { |
1d8ab692 GB |
187 | case EVENT_A: { |
188 | ITmfEventField content = event.getContent(); | |
1d8ab692 | 189 | if (content.getField(FIELD_A) == null) { |
56e9f830 | 190 | return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_A, EVENT_A)); |
1d8ab692 GB |
191 | } |
192 | break; | |
193 | } | |
194 | case EVENT_B: { | |
195 | ITmfEventField content = event.getContent(); | |
1d8ab692 | 196 | if (content.getField(FIELD_B) == null) { |
56e9f830 | 197 | return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_B, EVENT_B)); |
1d8ab692 GB |
198 | } |
199 | break; | |
200 | } | |
201 | default: | |
56e9f830 | 202 | return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Unexpected event " + event.getType().getName()); |
1d8ab692 GB |
203 | } |
204 | return Status.OK_STATUS; | |
205 | } | |
206 | ||
207 | private class CustomEventRequest extends TmfEventRequest { | |
208 | private final ITmfTrace fTrace; | |
209 | private IStatus fResult = Status.OK_STATUS; | |
210 | private int fCount = 0; | |
211 | ||
212 | public CustomEventRequest(ITmfTrace trace) { | |
213 | super(trace.getEventType(), | |
214 | TmfTimeRange.ETERNITY, | |
215 | 0, | |
216 | ITmfEventRequest.ALL_DATA, | |
217 | ITmfEventRequest.ExecutionType.BACKGROUND); | |
218 | this.fTrace = trace; | |
219 | } | |
220 | ||
221 | @Override | |
222 | public void handleData(final ITmfEvent event) { | |
223 | super.handleData(event); | |
224 | if (event.getTrace() == fTrace) { | |
225 | fCount++; | |
226 | IStatus result = testEvent(event); | |
227 | if (!result.isOK()) { | |
228 | fResult = result; | |
229 | this.cancel(); | |
230 | } | |
231 | } | |
232 | } | |
233 | ||
234 | public IStatus getStatus() { | |
235 | return fResult; | |
236 | } | |
237 | ||
238 | public int getCount() { | |
239 | return fCount; | |
240 | } | |
241 | ||
242 | } | |
243 | } |