91dbb54991f63317750b8fbf648ec9bf330392e9
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
.assertTrue
;
17 import static org
.junit
.Assert
.fail
;
20 import java
.io
.IOException
;
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
;
42 * Test suite for the {@link TmfXmlTraceStub} class
44 * @author Geneviève Bastien
46 public class XmlStubTraceTest
{
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");
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";
57 private static IPath
getAbsolutePath(Path relativePath
) {
58 Plugin plugin
= TmfCoreTestPlugin
.getDefault();
61 * Shouldn't happen but at least throw something to get the test to
64 throw new IllegalStateException();
66 URL location
= FileLocator
.find(plugin
.getBundle(), relativePath
, null);
68 IPath path
= new Path(FileLocator
.toFileURL(location
).getPath());
70 } catch (IOException e
) {
71 throw new IllegalStateException();
77 * {@link TmfXmlTraceStub#validate(org.eclipse.core.resources.IProject, String)}
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());
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());
97 * Test the reading and querying the XML trace and make sure fields are
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());
109 trace
.initTrace(null, getAbsolutePath(VALID_FILE
).toOSString(), TmfEvent
.class);
110 } catch (TmfTraceException e1
) {
111 fail(e1
.getMessage());
114 CustomEventRequest req
= new CustomEventRequest(trace
);
115 trace
.sendRequest(req
);
117 req
.waitForCompletion();
118 if (req
.isCancelled()) {
119 fail(req
.getStatus().getMessage());
121 } catch (InterruptedException e
) {
122 fail(e
.getMessage());
124 assertEquals(4, req
.getCount());
127 private static IStatus
testEvent(ITmfEvent event
) {
128 switch (event
.getType().getName()) {
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
));
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
));
144 return new Status(IStatus
.ERROR
, TmfCoreTestPlugin
.PLUGIN_ID
, "Unexpected event " + event
.getType().getName());
146 return Status
.OK_STATUS
;
149 private class CustomEventRequest
extends TmfEventRequest
{
150 private final ITmfTrace fTrace
;
151 private IStatus fResult
= Status
.OK_STATUS
;
152 private int fCount
= 0;
154 public CustomEventRequest(ITmfTrace trace
) {
155 super(trace
.getEventType(),
156 TmfTimeRange
.ETERNITY
,
158 ITmfEventRequest
.ALL_DATA
,
159 ITmfEventRequest
.ExecutionType
.BACKGROUND
);
164 public void handleData(final ITmfEvent event
) {
165 super.handleData(event
);
166 if (event
.getTrace() == fTrace
) {
168 IStatus result
= testEvent(event
);
169 if (!result
.isOK()) {
176 public IStatus
getStatus() {
180 public int getCount() {
This page took 0.036783 seconds and 4 git commands to generate.