91dbb54991f63317750b8fbf648ec9bf330392e9
[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 (content.getField(FIELD_A) == null) {
132 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_A, EVENT_A));
133 }
134 break;
135 }
136 case EVENT_B: {
137 ITmfEventField content = event.getContent();
138 if (content.getField(FIELD_B) == null) {
139 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, String.format("Field %s does not exist in event %s", FIELD_B, EVENT_B));
140 }
141 break;
142 }
143 default:
144 return new Status(IStatus.ERROR, TmfCoreTestPlugin.PLUGIN_ID, "Unexpected event " + event.getType().getName());
145 }
146 return Status.OK_STATUS;
147 }
148
149 private class CustomEventRequest extends TmfEventRequest {
150 private final ITmfTrace fTrace;
151 private IStatus fResult = Status.OK_STATUS;
152 private int fCount = 0;
153
154 public CustomEventRequest(ITmfTrace trace) {
155 super(trace.getEventType(),
156 TmfTimeRange.ETERNITY,
157 0,
158 ITmfEventRequest.ALL_DATA,
159 ITmfEventRequest.ExecutionType.BACKGROUND);
160 this.fTrace = trace;
161 }
162
163 @Override
164 public void handleData(final ITmfEvent event) {
165 super.handleData(event);
166 if (event.getTrace() == fTrace) {
167 fCount++;
168 IStatus result = testEvent(event);
169 if (!result.isOK()) {
170 fResult = result;
171 this.cancel();
172 }
173 }
174 }
175
176 public IStatus getStatus() {
177 return fResult;
178 }
179
180 public int getCount() {
181 return fCount;
182 }
183
184 }
185 }
This page took 0.036783 seconds and 4 git commands to generate.