tmf.xml: Bug 500195. Modify initial state behavior
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / src / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / stateprovider / XmlModuleTestBase.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.stateprovider;
10
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;
15
16 import java.util.List;
17
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;
35
36 /**
37 * Base test for XML analysis module
38 *
39 * @author Jean-Christian Kouame
40 */
41 public abstract class XmlModuleTestBase {
42
43 private TmfAbstractAnalysisModule fModule;
44
45 /**
46 * Test the module construction
47 */
48 @Test
49 public void testModuleConstruction() {
50
51 Document doc = getXmlFile().getXmlDocument();
52 assertNotNull(doc);
53
54 /* get analysis modules */
55 NodeList analysisNodes = doc.getElementsByTagName(getAnalysisNodeName());
56 assertTrue(analysisNodes.getLength() > 0);
57
58 final Element node = (Element) analysisNodes.item(0);
59 assertNotNull(node);
60
61 createModule(node);
62
63 String moduleId = node.getAttribute(TmfXmlStrings.ID);
64 assertNotNull(moduleId);
65 fModule.setId(moduleId);
66 assertEquals(getAnalysisId(), fModule.getId());
67
68 assertEquals(getAnalysisName(), fModule.getName());
69 }
70
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));
77 break;
78 case TmfXmlStrings.STATE_PROVIDER:
79 fModule = new XmlStateSystemModule();
80 ((XmlStateSystemModule) fModule).setXmlFile(new Path(getXmlFile().getFile().getAbsolutePath()));
81 break;
82
83 default:
84 fail();
85 }
86 }
87
88 /**
89 * Get the analysis XML node name
90 *
91 * @return The name
92 */
93 protected abstract @NonNull String getAnalysisNodeName();
94
95 /**
96 * Get the XML file this test use
97 *
98 * @return The file
99 */
100 protected abstract TmfXmlTestFiles getXmlFile();
101
102 /**
103 * Get the analysis name
104 *
105 * @return The name
106 */
107 protected abstract String getAnalysisName();
108
109 /**
110 * Get the id of the analysis
111 *
112 * @return The id
113 */
114 protected abstract String getAnalysisId();
115
116 /**
117 * Get the trace this test use
118 *
119 * @return The trace
120 */
121 protected abstract @NonNull CtfTestTrace getTrace();
122
123 /**
124 * Get the name of the analysis
125 *
126 * @param element
127 * The analysis element
128 * @return The name
129 */
130 public static @NonNull String getName(Element element) {
131 String name = null;
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);
137 }
138 }
139 assertNotNull("analysis name", name);
140 return name;
141 }
142
143 /**
144 * Test the module executes correctly
145 */
146 @Test
147 public void testModuleExecution() {
148 Document doc = getXmlFile().getXmlDocument();
149 assertNotNull(doc);
150
151 /* get State Providers modules */
152 NodeList stateproviderNodes = doc.getElementsByTagName(getAnalysisNodeName());
153
154 Element node = (Element) stateproviderNodes.item(0);
155 assertNotNull(node);
156
157 createModule(node);
158
159 String moduleId = node.getAttribute(TmfXmlStrings.ID);
160 assertNotNull(moduleId);
161 fModule.setId(moduleId);
162
163 CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(getTrace());
164 try {
165 fModule.setTrace(trace);
166 fModule.schedule();
167 assertTrue(fModule.waitForCompletion(new NullProgressMonitor()));
168
169 } catch (TmfAnalysisException e) {
170 fail("Cannot set trace " + e.getMessage());
171 } finally {
172 trace.dispose();
173 }
174
175 }
176 }
This page took 0.035216 seconds and 5 git commands to generate.