1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.stateprovider
;
11 import static org
.junit
.Assert
.assertEquals
;
12 import static org
.junit
.Assert
.assertNotNull
;
13 import static org
.junit
.Assert
.assertTrue
;
14 import static org
.junit
.Assert
.fail
;
16 import java
.util
.List
;
18 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.Path
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.pattern
.stateprovider
.XmlPatternAnalysis
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.XmlStateSystemModule
;
25 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
26 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.common
.TmfXmlTestFiles
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAbstractAnalysisModule
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
29 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
30 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
31 import org
.junit
.Test
;
32 import org
.w3c
.dom
.Document
;
33 import org
.w3c
.dom
.Element
;
34 import org
.w3c
.dom
.NodeList
;
37 * Base test for XML analysis module
39 * @author Jean-Christian Kouame
41 public abstract class XmlModuleTestBase
{
43 private TmfAbstractAnalysisModule fModule
;
46 * Test the module construction
49 public void testModuleConstruction() {
51 Document doc
= getXmlFile().getXmlDocument();
54 /* get analysis modules */
55 NodeList analysisNodes
= doc
.getElementsByTagName(getAnalysisNodeName());
56 assertTrue(analysisNodes
.getLength() > 0);
58 final Element node
= (Element
) analysisNodes
.item(0);
63 String moduleId
= node
.getAttribute(TmfXmlStrings
.ID
);
64 assertNotNull(moduleId
);
65 fModule
.setId(moduleId
);
66 assertEquals(getAnalysisId(), fModule
.getId());
68 assertEquals(getAnalysisName(), fModule
.getName());
71 private void createModule(@NonNull Element element
) {
72 switch (getAnalysisNodeName()) {
73 case TmfXmlStrings
.PATTERN
:
74 fModule
= new XmlPatternAnalysis();
75 ((XmlPatternAnalysis
) fModule
).setXmlFile(new Path(getXmlFile().getFile().getAbsolutePath()));
76 fModule
.setName(getName(element
));
78 case TmfXmlStrings
.STATE_PROVIDER
:
79 fModule
= new XmlStateSystemModule();
80 ((XmlStateSystemModule
) fModule
).setXmlFile(new Path(getXmlFile().getFile().getAbsolutePath()));
89 * Get the analysis XML node name
93 protected abstract @NonNull String
getAnalysisNodeName();
96 * Get the XML file this test use
100 protected abstract TmfXmlTestFiles
getXmlFile();
103 * Get the analysis name
107 protected abstract String
getAnalysisName();
110 * Get the id of the analysis
114 protected abstract String
getAnalysisId();
117 * Get the trace this test use
121 protected abstract @NonNull CtfTestTrace
getTrace();
124 * Get the name of the analysis
127 * The analysis element
130 public static @NonNull String
getName(Element element
) {
132 List
<Element
> head
= XmlUtils
.getChildElements(element
, TmfXmlStrings
.HEAD
);
133 if (head
.size() == 1) {
134 List
<Element
> labels
= XmlUtils
.getChildElements(head
.get(0), TmfXmlStrings
.LABEL
);
135 if (!labels
.isEmpty()) {
136 name
= labels
.get(0).getAttribute(TmfXmlStrings
.VALUE
);
139 assertNotNull("analysis name", name
);
144 * Test the module executes correctly
147 public void testModuleExecution() {
148 Document doc
= getXmlFile().getXmlDocument();
151 /* get State Providers modules */
152 NodeList stateproviderNodes
= doc
.getElementsByTagName(getAnalysisNodeName());
154 Element node
= (Element
) stateproviderNodes
.item(0);
159 String moduleId
= node
.getAttribute(TmfXmlStrings
.ID
);
160 assertNotNull(moduleId
);
161 fModule
.setId(moduleId
);
163 CtfTmfTrace trace
= CtfTmfTestTraceUtils
.getTrace(getTrace());
165 fModule
.setTrace(trace
);
167 assertTrue(fModule
.waitForCompletion(new NullProgressMonitor()));
169 } catch (TmfAnalysisException e
) {
170 fail("Cannot set trace " + e
.getMessage());