tmf.core.tests: Add method to simplify XML Trace stub setup
[deliverable/tracecompass.git] / tmf / 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
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
25 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
26 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
27 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
28 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
29 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
30 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
31 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
32 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
33 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
34 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
35 import org.junit.Test;
36
37 /**
38 * Test suite for the {@link TmfXmlTraceStub} class
39 *
40 * @author Geneviève Bastien
41 */
42 public class XmlStubTraceTest {
43
44 private static final String VALID_FILE = "testfiles/stub_xml_traces/valid/test.xml";
45 private static final String VALID_PATH = "testfiles/stub_xml_traces/valid";
46 private static final String INVALID_PATH = "testfiles/stub_xml_traces/invalid";
47
48 private static final String EVENT_A = "A";
49 private static final String EVENT_B = "B";
50 private static final String FIELD_A = "b";
51 private static final String FIELD_B = "f";
52
53 /**
54 * Test the
55 * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
56 * method
57 */
58 @Test
59 public void testValidate() {
60 TmfXmlTraceStub trace = new TmfXmlTraceStub();
61 File[] invalidFiles = TmfCoreTestPlugin.getAbsoluteFilePath(INVALID_PATH).toFile().listFiles();
62 assertTrue(invalidFiles.length > 0);
63 for (File f : invalidFiles) {
64 assertTrue(!trace.validate(null, f.getAbsolutePath()).isOK());
65 }
66
67 File[] validFiles = TmfCoreTestPlugin.getAbsoluteFilePath(VALID_PATH).toFile().listFiles();
68 assertTrue(validFiles.length > 0);
69 for (File f : validFiles) {
70 assertTrue(trace.validate(null, f.getAbsolutePath()).isOK());
71 }
72 }
73
74 /**
75 * Test the reading and querying the XML trace and make sure fields are
76 * present
77 */
78 @Test
79 public void testDevelopmentTrace() {
80 TmfXmlTraceStub trace = TmfXmlTraceStub.setupTrace(TmfCoreTestPlugin.getAbsoluteFilePath(VALID_FILE));
81
82 CustomEventRequest req = new CustomEventRequest(trace);
83 trace.sendRequest(req);
84 try {
85 req.waitForCompletion();
86 if (req.isCancelled()) {
87 fail(req.getStatus().getMessage());
88 }
89 } catch (InterruptedException e) {
90 fail(e.getMessage());
91 }
92 assertEquals(4, req.getCount());
93 }
94
95 /**
96 * Test the presence and resolve of the aspects for this trace
97 */
98 @Test
99 public void testAspects() {
100 TmfXmlTraceStub trace = TmfXmlTraceStub.setupTrace(TmfCoreTestPlugin.getAbsoluteFilePath(VALID_FILE));
101
102 ITmfEventAspect<?> cpuAspect = null;
103 ITmfEventAspect<?> testAspect = null;
104 int aspectCount = 0;
105 for (ITmfEventAspect<?> aspect : trace.getEventAspects()) {
106 aspectCount++;
107 if (aspect instanceof TmfCpuAspect) {
108 cpuAspect = aspect;
109 } else if (aspect.getName().equals("test")) {
110 testAspect = aspect;
111 }
112 }
113 /* Check the presence of the cpu and test aspects */
114 assertEquals("Number of aspects", 5, aspectCount);
115 assertNotNull(cpuAspect);
116 assertNotNull(testAspect);
117
118 ITmfContext ctx;
119 ctx = trace.seekEvent(0L);
120 assertNotNull(ctx);
121 ITmfEvent event = trace.getNext(ctx);
122 assertNotNull(event);
123 assertEquals("Cpu aspect of event 1", 1, cpuAspect.resolve(event));
124 assertEquals("Test aspect of event 1", "abc", testAspect.resolve(event));
125 event = trace.getNext(ctx);
126 assertNotNull(event);
127 assertEquals("Cpu aspect of event 2", 1, cpuAspect.resolve(event));
128 assertEquals("Test aspect of event 2", "abc", testAspect.resolve(event));
129 event = trace.getNext(ctx);
130 assertNotNull(event);
131 assertEquals("Cpu aspect of event 3", 2, cpuAspect.resolve(event));
132 assertEquals("Test aspect of event 3", "def", testAspect.resolve(event));
133 event = trace.getNext(ctx);
134 assertNotNull(event);
135 assertEquals("Cpu aspect of event 4", 1, cpuAspect.resolve(event));
136 assertEquals("Test aspect of event 4", "def", testAspect.resolve(event));
137 }
138
139 private static IStatus testEvent(ITmfEvent event) {
140 switch (event.getName()) {
141 case EVENT_A: {
142 ITmfEventField content = event.getContent();
143 if (content.getField(FIELD_A) == null) {
144 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_A, EVENT_A));
145 }
146 break;
147 }
148 case EVENT_B: {
149 ITmfEventField content = event.getContent();
150 if (content.getField(FIELD_B) == null) {
151 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_B, EVENT_B));
152 }
153 break;
154 }
155 default:
156 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Unexpected event " + event.getType().getName());
157 }
158 return Status.OK_STATUS;
159 }
160
161 private class CustomEventRequest extends TmfEventRequest {
162 private final ITmfTrace fTrace;
163 private IStatus fResult = Status.OK_STATUS;
164 private int fCount = 0;
165
166 public CustomEventRequest(ITmfTrace trace) {
167 super(trace.getEventType(),
168 TmfTimeRange.ETERNITY,
169 0,
170 ITmfEventRequest.ALL_DATA,
171 ITmfEventRequest.ExecutionType.BACKGROUND);
172 this.fTrace = trace;
173 }
174
175 @Override
176 public void handleData(final ITmfEvent event) {
177 super.handleData(event);
178 if (event.getTrace() == fTrace) {
179 fCount++;
180 IStatus result = testEvent(event);
181 if (!result.isOK()) {
182 fResult = result;
183 this.cancel();
184 }
185 }
186 }
187
188 public IStatus getStatus() {
189 return fResult;
190 }
191
192 public int getCount() {
193 return fCount;
194 }
195
196 }
197 }
This page took 0.034918 seconds and 5 git commands to generate.