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
;
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
;
38 * Test suite for the {@link TmfXmlTraceStub} class
40 * @author Geneviève Bastien
42 public class XmlStubTraceTest
{
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";
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";
55 * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
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());
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());
75 * Test the reading and querying the XML trace and make sure fields are
79 public void testDevelopmentTrace() {
80 TmfXmlTraceStub trace
= TmfXmlTraceStub
.setupTrace(TmfCoreTestPlugin
.getAbsoluteFilePath(VALID_FILE
));
82 CustomEventRequest req
= new CustomEventRequest(trace
);
83 trace
.sendRequest(req
);
85 req
.waitForCompletion();
86 if (req
.isCancelled()) {
87 fail(req
.getStatus().getMessage());
89 } catch (InterruptedException e
) {
92 assertEquals(4, req
.getCount());
96 * Test the presence and resolve of the aspects for this trace
99 public void testAspects() {
100 TmfXmlTraceStub trace
= TmfXmlTraceStub
.setupTrace(TmfCoreTestPlugin
.getAbsoluteFilePath(VALID_FILE
));
102 ITmfEventAspect
<?
> cpuAspect
= null;
103 ITmfEventAspect
<?
> testAspect
= null;
105 for (ITmfEventAspect
<?
> aspect
: trace
.getEventAspects()) {
107 if (aspect
instanceof TmfCpuAspect
) {
109 } else if (aspect
.getName().equals("test")) {
113 /* Check the presence of the cpu and test aspects */
114 assertEquals("Number of aspects", 5, aspectCount
);
115 assertNotNull(cpuAspect
);
116 assertNotNull(testAspect
);
119 ctx
= trace
.seekEvent(0L);
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
));
139 private static IStatus
testEvent(ITmfEvent event
) {
140 switch (event
.getName()) {
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
));
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
));
156 return new Status(IStatus
.ERROR
, TmfCoreTestPlugin
.PLUGIN_ID
, "Unexpected event " + event
.getType().getName());
158 return Status
.OK_STATUS
;
161 private class CustomEventRequest
extends TmfEventRequest
{
162 private final ITmfTrace fTrace
;
163 private IStatus fResult
= Status
.OK_STATUS
;
164 private int fCount
= 0;
166 public CustomEventRequest(ITmfTrace trace
) {
167 super(trace
.getEventType(),
168 TmfTimeRange
.ETERNITY
,
170 ITmfEventRequest
.ALL_DATA
,
171 ITmfEventRequest
.ExecutionType
.BACKGROUND
);
176 public void handleData(final ITmfEvent event
) {
177 super.handleData(event
);
178 if (event
.getTrace() == fTrace
) {
180 IStatus result
= testEvent(event
);
181 if (!result
.isOK()) {
188 public IStatus
getStatus() {
192 public int getCount() {
This page took 0.034918 seconds and 5 git commands to generate.