1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.stub
;
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
;
21 import java
.io
.IOException
;
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
;
46 * Test suite for the {@link TmfXmlTraceStub} class
48 * @author Geneviève Bastien
50 public class XmlStubTraceTest
{
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");
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";
61 private static IPath
getAbsolutePath(Path relativePath
) {
62 Plugin plugin
= TmfCoreTestPlugin
.getDefault();
65 * Shouldn't happen but at least throw something to get the test to
68 throw new IllegalStateException();
70 URL location
= FileLocator
.find(plugin
.getBundle(), relativePath
, null);
72 IPath path
= new Path(FileLocator
.toFileURL(location
).getPath());
74 } catch (IOException e
) {
75 throw new IllegalStateException();
81 * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
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());
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());
101 * Test the reading and querying the XML trace and make sure fields are
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());
113 trace
.initTrace(null, getAbsolutePath(VALID_FILE
).toOSString(), TmfEvent
.class);
114 } catch (TmfTraceException e1
) {
115 fail(e1
.getMessage());
118 CustomEventRequest req
= new CustomEventRequest(trace
);
119 trace
.sendRequest(req
);
121 req
.waitForCompletion();
122 if (req
.isCancelled()) {
123 fail(req
.getStatus().getMessage());
125 } catch (InterruptedException e
) {
126 fail(e
.getMessage());
128 assertEquals(4, req
.getCount());
132 * Test the presence and resolve of the aspects for this trace
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());
143 trace
.initTrace(null, getAbsolutePath(VALID_FILE
).toOSString(), TmfEvent
.class);
144 } catch (TmfTraceException e1
) {
145 fail(e1
.getMessage());
148 ITmfEventAspect
<?
> cpuAspect
= null;
149 ITmfEventAspect
<?
> testAspect
= null;
151 for (ITmfEventAspect
<?
> aspect
: trace
.getEventAspects()) {
153 if (aspect
instanceof TmfCpuAspect
) {
155 } else if (aspect
.getName().equals("test")) {
159 /* Check the presence of the cpu and test aspects */
160 assertEquals("Number of aspects", 5, aspectCount
);
161 assertNotNull(cpuAspect
);
162 assertNotNull(testAspect
);
165 ctx
= trace
.seekEvent(0L);
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
));
185 private static IStatus
testEvent(ITmfEvent event
) {
186 switch (event
.getName()) {
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
));
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
));
202 return new Status(IStatus
.ERROR
, TmfCoreTestPlugin
.PLUGIN_ID
, "Unexpected event " + event
.getType().getName());
204 return Status
.OK_STATUS
;
207 private class CustomEventRequest
extends TmfEventRequest
{
208 private final ITmfTrace fTrace
;
209 private IStatus fResult
= Status
.OK_STATUS
;
210 private int fCount
= 0;
212 public CustomEventRequest(ITmfTrace trace
) {
213 super(trace
.getEventType(),
214 TmfTimeRange
.ETERNITY
,
216 ITmfEventRequest
.ALL_DATA
,
217 ITmfEventRequest
.ExecutionType
.BACKGROUND
);
222 public void handleData(final ITmfEvent event
) {
223 super.handleData(event
);
224 if (event
.getTrace() == fTrace
) {
226 IStatus result
= testEvent(event
);
227 if (!result
.isOK()) {
234 public IStatus
getStatus() {
238 public int getCount() {
This page took 0.037554 seconds and 6 git commands to generate.