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
.linuxtools
.tmf
.core
.tests
.trace
.stub
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertTrue
;
17 import static org
.junit
.Assert
.fail
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
32 import org
.eclipse
.linuxtools
.tmf
.tests
.stubs
.trace
.xml
.TmfXmlTraceStub
;
33 import org
.junit
.Test
;
36 * Test suite for the {@link TmfXmlTraceStub} class
38 * @author Geneviève Bastien
40 public class XmlStubTraceTest
{
42 private static final String VALID_FILE
= "../org.eclipse.linuxtools.tmf.core.tests/testfiles/stub_xml_traces/valid/test.xml";
43 private static final String VALID_PATH
= "../org.eclipse.linuxtools.tmf.core.tests/testfiles/stub_xml_traces/valid";
44 private static final String INVALID_PATH
= "../org.eclipse.linuxtools.tmf.core.tests/testfiles/stub_xml_traces/invalid";
46 private static final String EVENT_A
= "A";
47 private static final String EVENT_B
= "B";
48 private static final String FIELD_A
= "b";
49 private static final String FIELD_B
= "f";
53 * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
57 public void testValidate() {
58 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
59 File
[] invalidFiles
= (new File(INVALID_PATH
)).listFiles();
60 assertTrue(invalidFiles
.length
> 0);
61 for (File f
: invalidFiles
) {
62 assertTrue(!trace
.validate(null, f
.getAbsolutePath()).isOK());
65 File
[] validFiles
= (new File(VALID_PATH
)).listFiles();
66 assertTrue(validFiles
.length
> 0);
67 for (File f
: validFiles
) {
68 assertTrue(trace
.validate(null, f
.getAbsolutePath()).isOK());
73 * Test the reading and querying the XML trace and make sure fields are
77 public void testDevelopmentTrace() {
78 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
79 IStatus status
= trace
.validate(null, VALID_FILE
);
81 fail(status
.getException().getMessage());
85 trace
.initTrace(null, VALID_FILE
, TmfEvent
.class);
86 } catch (TmfTraceException e1
) {
87 fail(e1
.getMessage());
90 CustomEventRequest req
= new CustomEventRequest(trace
);
91 trace
.sendRequest(req
);
93 req
.waitForCompletion();
94 if (req
.isCancelled()) {
95 fail(req
.getStatus().getMessage());
97 } catch (InterruptedException e
) {
100 assertEquals(4, req
.getCount());
103 private static IStatus
testEvent(ITmfEvent event
) {
104 switch (event
.getType().getName()) {
106 ITmfEventField content
= event
.getContent();
107 if (!event
.getSource().equals("1")) {
108 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "Events of type A should have source 1 but was " + event
.getSource());
110 if (content
.getField(FIELD_A
) == null) {
111 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, String
.format("Field %s does not exist in event %s", FIELD_A
, EVENT_A
));
116 ITmfEventField content
= event
.getContent();
117 if (!event
.getSource().equals("2")) {
118 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "Events of type B should have source 2 but was " + event
.getSource());
120 if (content
.getField(FIELD_B
) == null) {
121 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, String
.format("Field %s does not exist in event %s", FIELD_B
, EVENT_B
));
126 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "Unexpected event " + event
.getType().getName());
128 return Status
.OK_STATUS
;
131 private class CustomEventRequest
extends TmfEventRequest
{
132 private final ITmfTrace fTrace
;
133 private IStatus fResult
= Status
.OK_STATUS
;
134 private int fCount
= 0;
136 public CustomEventRequest(ITmfTrace trace
) {
137 super(trace
.getEventType(),
138 TmfTimeRange
.ETERNITY
,
140 ITmfEventRequest
.ALL_DATA
,
141 ITmfEventRequest
.ExecutionType
.BACKGROUND
);
146 public void handleData(final ITmfEvent event
) {
147 super.handleData(event
);
148 if (event
.getTrace() == fTrace
) {
150 IStatus result
= testEvent(event
);
151 if (!result
.isOK()) {
158 public IStatus
getStatus() {
162 public int getCount() {
This page took 0.040652 seconds and 5 git commands to generate.