TMF: Add trace stub for TMF unit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / 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.linuxtools.tmf.core.tests.trace.stub;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18
19 import java.io.File;
20
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;
34
35 /**
36 * Test suite for the {@link TmfXmlTraceStub} class
37 *
38 * @author Geneviève Bastien
39 */
40 public class XmlStubTraceTest {
41
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";
45
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";
50
51 /**
52 * Test the
53 * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
54 * method
55 */
56 @Test
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());
63 }
64
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());
69 }
70 }
71
72 /**
73 * Test the reading and querying the XML trace and make sure fields are
74 * present
75 */
76 @Test
77 public void testDevelopmentTrace() {
78 TmfXmlTraceStub trace = new TmfXmlTraceStub();
79 IStatus status = trace.validate(null, VALID_FILE);
80 if (!status.isOK()) {
81 fail(status.getException().getMessage());
82 }
83
84 try {
85 trace.initTrace(null, VALID_FILE, TmfEvent.class);
86 } catch (TmfTraceException e1) {
87 fail(e1.getMessage());
88 }
89
90 CustomEventRequest req = new CustomEventRequest(trace);
91 trace.sendRequest(req);
92 try {
93 req.waitForCompletion();
94 if (req.isCancelled()) {
95 fail(req.getStatus().getMessage());
96 }
97 } catch (InterruptedException e) {
98 fail(e.getMessage());
99 }
100 assertEquals(4, req.getCount());
101 }
102
103 private static IStatus testEvent(ITmfEvent event) {
104 switch (event.getType().getName()) {
105 case EVENT_A: {
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());
109 }
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));
112 }
113 break;
114 }
115 case EVENT_B: {
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());
119 }
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));
122 }
123 break;
124 }
125 default:
126 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unexpected event " + event.getType().getName());
127 }
128 return Status.OK_STATUS;
129 }
130
131 private class CustomEventRequest extends TmfEventRequest {
132 private final ITmfTrace fTrace;
133 private IStatus fResult = Status.OK_STATUS;
134 private int fCount = 0;
135
136 public CustomEventRequest(ITmfTrace trace) {
137 super(trace.getEventType(),
138 TmfTimeRange.ETERNITY,
139 0,
140 ITmfEventRequest.ALL_DATA,
141 ITmfEventRequest.ExecutionType.BACKGROUND);
142 this.fTrace = trace;
143 }
144
145 @Override
146 public void handleData(final ITmfEvent event) {
147 super.handleData(event);
148 if (event.getTrace() == fTrace) {
149 fCount++;
150 IStatus result = testEvent(event);
151 if (!result.isOK()) {
152 fResult = result;
153 this.cancel();
154 }
155 }
156 }
157
158 public IStatus getStatus() {
159 return fResult;
160 }
161
162 public int getCount() {
163 return fCount;
164 }
165
166 }
167 }
This page took 0.040652 seconds and 5 git commands to generate.