tmf: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / 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.assertTrue;
17 import static org.junit.Assert.fail;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.URL;
22
23 import org.eclipse.core.runtime.FileLocator;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Plugin;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
30 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
31 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
32 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
33 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
34 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
35 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
36 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
37 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
38 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
39 import org.junit.Test;
40
41 /**
42 * Test suite for the {@link TmfXmlTraceStub} class
43 *
44 * @author Geneviève Bastien
45 */
46 public class XmlStubTraceTest {
47
48 private static final Path VALID_FILE = new Path("testfiles/stub_xml_traces/valid/test.xml");
49 private static final Path VALID_PATH = new Path("testfiles/stub_xml_traces/valid");
50 private static final Path INVALID_PATH = new Path("testfiles/stub_xml_traces/invalid");
51
52 private static final String EVENT_A = "A";
53 private static final String EVENT_B = "B";
54 private static final String FIELD_A = "b";
55 private static final String FIELD_B = "f";
56
57 private static IPath getAbsolutePath(Path relativePath) {
58 Plugin plugin = TmfCoreTestPlugin.getDefault();
59 if (plugin == null) {
60 /*
61 * Shouldn't happen but at least throw something to get the test to
62 * fail early
63 */
64 throw new IllegalStateException();
65 }
66 URL location = FileLocator.find(plugin.getBundle(), relativePath, null);
67 try {
68 IPath path = new Path(FileLocator.toFileURL(location).getPath());
69 return path;
70 } catch (IOException e) {
71 throw new IllegalStateException();
72 }
73 }
74
75 /**
76 * Test the
77 * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
78 * method
79 */
80 @Test
81 public void testValidate() {
82 TmfXmlTraceStub trace = new TmfXmlTraceStub();
83 File[] invalidFiles = getAbsolutePath(INVALID_PATH).toFile().listFiles();
84 assertTrue(invalidFiles.length > 0);
85 for (File f : invalidFiles) {
86 assertTrue(!trace.validate(null, f.getAbsolutePath()).isOK());
87 }
88
89 File[] validFiles = getAbsolutePath(VALID_PATH).toFile().listFiles();
90 assertTrue(validFiles.length > 0);
91 for (File f : validFiles) {
92 assertTrue(trace.validate(null, f.getAbsolutePath()).isOK());
93 }
94 }
95
96 /**
97 * Test the reading and querying the XML trace and make sure fields are
98 * present
99 */
100 @Test
101 public void testDevelopmentTrace() {
102 TmfXmlTraceStub trace = new TmfXmlTraceStub();
103 IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString());
104 if (!status.isOK()) {
105 fail(status.getException().getMessage());
106 }
107
108 try {
109 trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class);
110 } catch (TmfTraceException e1) {
111 fail(e1.getMessage());
112 }
113
114 CustomEventRequest req = new CustomEventRequest(trace);
115 trace.sendRequest(req);
116 try {
117 req.waitForCompletion();
118 if (req.isCancelled()) {
119 fail(req.getStatus().getMessage());
120 }
121 } catch (InterruptedException e) {
122 fail(e.getMessage());
123 }
124 assertEquals(4, req.getCount());
125 }
126
127 private static IStatus testEvent(ITmfEvent event) {
128 switch (event.getType().getName()) {
129 case EVENT_A: {
130 ITmfEventField content = event.getContent();
131 if (!event.getSource().equals("1")) {
132 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Events of type A should have source 1 but was " + event.getSource());
133 }
134 if (content.getField(FIELD_A) == null) {
135 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_A, EVENT_A));
136 }
137 break;
138 }
139 case EVENT_B: {
140 ITmfEventField content = event.getContent();
141 if (!event.getSource().equals("2")) {
142 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Events of type B should have source 2 but was " + event.getSource());
143 }
144 if (content.getField(FIELD_B) == null) {
145 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_B, EVENT_B));
146 }
147 break;
148 }
149 default:
150 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Unexpected event " + event.getType().getName());
151 }
152 return Status.OK_STATUS;
153 }
154
155 private class CustomEventRequest extends TmfEventRequest {
156 private final ITmfTrace fTrace;
157 private IStatus fResult = Status.OK_STATUS;
158 private int fCount = 0;
159
160 public CustomEventRequest(ITmfTrace trace) {
161 super(trace.getEventType(),
162 TmfTimeRange.ETERNITY,
163 0,
164 ITmfEventRequest.ALL_DATA,
165 ITmfEventRequest.ExecutionType.BACKGROUND);
166 this.fTrace = trace;
167 }
168
169 @Override
170 public void handleData(final ITmfEvent event) {
171 super.handleData(event);
172 if (event.getTrace() == fTrace) {
173 fCount++;
174 IStatus result = testEvent(event);
175 if (!result.isOK()) {
176 fResult = result;
177 this.cancel();
178 }
179 }
180 }
181
182 public IStatus getStatus() {
183 return fResult;
184 }
185
186 public int getCount() {
187 return fCount;
188 }
189
190 }
191 }
This page took 0.035028 seconds and 5 git commands to generate.