15361b6c3a3c4e85715d789759c54f2cc976fcf6
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / stub / XmlStubTraceTest.java
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
13 package org.eclipse.tracecompass.tmf.core.tests.trace.stub;
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.URL;
23
24 import org.eclipse.core.runtime.FileLocator;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.core.runtime.Plugin;
29 import org.eclipse.core.runtime.Status;
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;
33 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
34 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
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;
40 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
41 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
42 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
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
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");
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
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
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();
87 File[] invalidFiles = getAbsolutePath(INVALID_PATH).toFile().listFiles();
88 assertTrue(invalidFiles.length > 0);
89 for (File f : invalidFiles) {
90 assertTrue(!trace.validate(null, f.getAbsolutePath()).isOK());
91 }
92
93 File[] validFiles = getAbsolutePath(VALID_PATH).toFile().listFiles();
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();
107 IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString());
108 if (!status.isOK()) {
109 fail(status.getException().getMessage());
110 }
111
112 try {
113 trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class);
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
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
185 private static IStatus testEvent(ITmfEvent event) {
186 switch (event.getType().getName()) {
187 case EVENT_A: {
188 ITmfEventField content = event.getContent();
189 if (content.getField(FIELD_A) == null) {
190 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_A, EVENT_A));
191 }
192 break;
193 }
194 case EVENT_B: {
195 ITmfEventField content = event.getContent();
196 if (content.getField(FIELD_B) == null) {
197 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_B, EVENT_B));
198 }
199 break;
200 }
201 default:
202 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Unexpected event " + event.getType().getName());
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 }
This page took 0.037788 seconds and 4 git commands to generate.