TMF: Add some XML basic test cases and fix a bug
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / src / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / stateprovider / StateProviderModelTest.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.analysis.xml.core.tests.stateprovider;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertFalse;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.fail;
19
20 import java.util.List;
21
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
25 import org.eclipse.tracecompass.statesystem.core.StateSystemUtils;
26 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
27 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
28 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
29 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
30 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
31 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
32 import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
33 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
34 import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
35 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
36 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
37 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
38 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
39 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
40 import org.junit.Test;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Element;
43 import org.w3c.dom.NodeList;
44
45 /**
46 * Test the XML model for state providers
47 *
48 * @author Geneviève Bastien
49 */
50 public class StateProviderModelTest {
51
52 private static final @NonNull String testTrace1 = "test_traces/testTrace1.xml";
53 /* Factor to convert seconds to nanoseconds */
54 private static final long TO_NS = 1000000000L;
55
56 private static @NonNull ITmfTrace initializeTrace(String traceFile) {
57 /* Initialize the trace */
58 TmfXmlTraceStub trace = new TmfXmlTraceStub();
59 try {
60 trace.initTrace(null, Activator.getAbsolutePath(new Path(traceFile)).toOSString(), TmfEvent.class);
61 } catch (TmfTraceException e1) {
62 fail(e1.getMessage());
63 }
64 return trace;
65 }
66
67 private static @NonNull XmlStateSystemModule initializeModule(TmfXmlTestFiles xmlAnalysisFile) {
68
69 /* Initialize the state provider module */
70 Document doc = xmlAnalysisFile.getXmlDocument();
71 assertNotNull(doc);
72
73 /* get State Providers modules */
74 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
75 assertFalse(stateproviderNodes.getLength() == 0);
76
77 Element node = (Element) stateproviderNodes.item(0);
78 XmlStateSystemModule module = new XmlStateSystemModule();
79 String moduleId = node.getAttribute(TmfXmlStrings.ID);
80 assertNotNull(moduleId);
81 module.setId(moduleId);
82
83 module.setXmlFile(xmlAnalysisFile.getPath());
84
85 return module;
86 }
87
88 private static void verifyStateIntervals(String testId, @NonNull ITmfStateSystem ss, Integer quark, int[] expectedStarts, ITmfStateValue[] expectedValues) throws AttributeNotFoundException, StateSystemDisposedException {
89 int expectedCount = expectedStarts.length - 1;
90 List<ITmfStateInterval> intervals = StateSystemUtils.queryHistoryRange(ss, quark, expectedStarts[0] * TO_NS, expectedStarts[expectedCount] * TO_NS);
91 assertEquals(testId + ": Interval count", expectedCount, intervals.size());
92 for (int i = 0; i < expectedCount; i++) {
93 ITmfStateInterval interval = intervals.get(i);
94 assertEquals(testId + ": Start time of interval " + i, expectedStarts[i] * TO_NS, interval.getStartTime());
95 long actualEnd = (i == expectedCount - 1) ? (expectedStarts[i + 1] * TO_NS) : (expectedStarts[i + 1] * TO_NS) - 1;
96 assertEquals(testId + ": End time of interval " + i, actualEnd, interval.getEndTime());
97 assertEquals(testId + ": Expected value of interval " + i, expectedValues[i], interval.getStateValue());
98 }
99 }
100
101 /**
102 * Test an increment of one, for an event name attribute
103 */
104 @Test
105 public void testEventName() {
106 ITmfTrace trace = initializeTrace(testTrace1);
107 XmlStateSystemModule module = initializeModule(TmfXmlTestFiles.ATTRIBUTE_FILE);
108 try {
109
110 module.setTrace(trace);
111
112 module.schedule();
113 module.waitForCompletion();
114
115 ITmfStateSystem ss = module.getStateSystem();
116 assertNotNull(ss);
117
118 List<Integer> quarks = ss.getQuarks("*");
119 assertEquals(2, quarks.size());
120
121 for (Integer quark : quarks) {
122 String name = ss.getAttributeName(quark);
123 switch (name) {
124 case "test":
125 {
126 final int[] expectedStarts = { 1, 5, 7 };
127 ITmfStateValue[] expectedValues = { TmfStateValue.newValueInt(1), TmfStateValue.newValueInt(2) };
128 verifyStateIntervals("test", ss, quark, expectedStarts, expectedValues);
129 }
130 break;
131 case "test1":
132 {
133 final int[] expectedStarts = { 1, 3, 7, 7 };
134 ITmfStateValue[] expectedValues = { TmfStateValue.nullValue(), TmfStateValue.newValueInt(1), TmfStateValue.newValueInt(2) };
135 verifyStateIntervals("test1", ss, quark, expectedStarts, expectedValues);
136 }
137 break;
138 default:
139 fail("Wrong attribute name " + name);
140 break;
141 }
142 }
143
144 } catch (TmfAnalysisException | AttributeNotFoundException | StateSystemDisposedException e) {
145 fail(e.getMessage());
146 } finally {
147 module.dispose();
148 trace.dispose();
149 }
150
151 }
152
153 }
This page took 0.033097 seconds and 5 git commands to generate.