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
.analysis
.xml
.core
.tests
.stateprovider
;
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
;
20 import java
.util
.List
;
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
;
46 * Test the XML model for state providers
48 * @author Geneviève Bastien
50 public class StateProviderModelTest
{
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;
56 private static @NonNull ITmfTrace
initializeTrace(String traceFile
) {
57 /* Initialize the trace */
58 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
60 trace
.initTrace(null, Activator
.getAbsolutePath(new Path(traceFile
)).toOSString(), TmfEvent
.class);
61 } catch (TmfTraceException e1
) {
62 fail(e1
.getMessage());
67 private static @NonNull XmlStateSystemModule
initializeModule(TmfXmlTestFiles xmlAnalysisFile
) {
69 /* Initialize the state provider module */
70 Document doc
= xmlAnalysisFile
.getXmlDocument();
73 /* get State Providers modules */
74 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
75 assertFalse(stateproviderNodes
.getLength() == 0);
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
);
83 module
.setXmlFile(xmlAnalysisFile
.getPath());
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());
102 * Test an increment of one, for an event name attribute
105 public void testEventName() {
106 ITmfTrace trace
= initializeTrace(testTrace1
);
107 XmlStateSystemModule module
= initializeModule(TmfXmlTestFiles
.ATTRIBUTE_FILE
);
110 module
.setTrace(trace
);
113 module
.waitForCompletion();
115 ITmfStateSystem ss
= module
.getStateSystem();
118 List
<Integer
> quarks
= ss
.getQuarks("*");
119 assertEquals(2, quarks
.size());
121 for (Integer quark
: quarks
) {
122 String name
= ss
.getAttributeName(quark
);
126 final int[] expectedStarts
= { 1, 5, 7 };
127 ITmfStateValue
[] expectedValues
= { TmfStateValue
.newValueInt(1), TmfStateValue
.newValueInt(2) };
128 verifyStateIntervals("test", ss
, quark
, expectedStarts
, expectedValues
);
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
);
139 fail("Wrong attribute name " + name
);
144 } catch (TmfAnalysisException
| AttributeNotFoundException
| StateSystemDisposedException e
) {
145 fail(e
.getMessage());