tmf: Split "CTF adaptor" into separate plugins/feature
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core.tests / src / org / eclipse / linuxtools / tmf / analysis / xml / core / tests / stateprovider / StateProviderModuleTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013 É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.linuxtools.tmf.analysis.xml.core.tests.stateprovider;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17 import static org.junit.Assert.assertTrue;
18 import static org.junit.Assert.fail;
19 import static org.junit.Assume.assumeTrue;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.ParserConfigurationException;
27
28 import org.eclipse.core.runtime.NullProgressMonitor;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlHeadInfo;
31 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
32 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.XmlStateSystemModule;
33 import org.eclipse.linuxtools.tmf.analysis.xml.core.tests.common.TmfXmlTestFiles;
34 import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
36 import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
37 import org.junit.Test;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.NodeList;
41 import org.xml.sax.SAXException;
42
43 /**
44 * Test suite for the XmlStateSystemModule Test. It tests the reading of the
45 * file, the header and the module's proper functioning as a module, but not the
46 * state system building, which is covered by another test suite.
47 *
48 * @author Geneviève Bastien
49 */
50 public class StateProviderModuleTest {
51
52 private static String ANALYSIS_ID = "kernel.linux.sp";
53 private static String ANALYSIS_NAME = "Xml kernel State System";
54
55 private XmlStateSystemModule fModule;
56
57 private static Document getXmlDocumentFromFile(File file) {
58 /* Initialize the state provider module */
59 try {
60 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
61 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
62
63 Document doc = dBuilder.parse(file);
64
65 doc.getDocumentElement().normalize();
66 return doc;
67 } catch (ParserConfigurationException e) {
68 fail("Xml document parse exception");
69 } catch (SAXException e) {
70 fail("Exception parsing xml file");
71 } catch (IOException e) {
72 fail("File io exception");
73 }
74 return null;
75 }
76
77 /**
78 * Test the module construction
79 */
80 @Test
81 public void testModuleConstruction() {
82
83 Document doc = getXmlDocumentFromFile(TmfXmlTestFiles.VALID_FILE.getFile());
84 assertNotNull(doc);
85
86 /* get State Providers modules */
87 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
88 assertTrue(stateproviderNodes.getLength() > 0);
89
90 Element node = (Element) stateproviderNodes.item(0);
91 fModule = new XmlStateSystemModule();
92 String moduleId = node.getAttribute(TmfXmlStrings.ID);
93 fModule.setId(moduleId);
94 assertEquals(ANALYSIS_ID, fModule.getId());
95
96 fModule.setXmlFile(new Path(TmfXmlTestFiles.VALID_FILE.getFile().getAbsolutePath()));
97 NodeList head = node.getElementsByTagName(TmfXmlStrings.HEAD);
98 XmlHeadInfo headInfo = null;
99 if (head.getLength() == 1) {
100 headInfo = new XmlHeadInfo(head.item(0));
101 }
102 fModule.setHeadInfo(headInfo);
103
104 assertEquals(ANALYSIS_NAME, fModule.getName());
105 }
106
107 /**
108 * Test the module executes correctly
109 */
110 @Test
111 public void testModuleExecution() {
112 assumeTrue(CtfTmfTestTrace.KERNEL.exists());
113
114 Document doc = getXmlDocumentFromFile(TmfXmlTestFiles.VALID_FILE.getFile());
115 assertNotNull(doc);
116
117 /* get State Providers modules */
118 NodeList stateproviderNodes = doc.getElementsByTagName(TmfXmlStrings.STATE_PROVIDER);
119
120 Element node = (Element) stateproviderNodes.item(0);
121 fModule = new XmlStateSystemModule();
122 String moduleId = node.getAttribute(TmfXmlStrings.ID);
123 fModule.setId(moduleId);
124
125 fModule.setXmlFile(new Path(TmfXmlTestFiles.VALID_FILE.getFile().getAbsolutePath()));
126 NodeList head = node.getElementsByTagName(TmfXmlStrings.HEAD);
127 XmlHeadInfo headInfo = null;
128 if (head.getLength() == 1) {
129 headInfo = new XmlHeadInfo(head.item(0));
130 }
131 fModule.setHeadInfo(headInfo);
132
133 try {
134 ITmfTrace trace = CtfTmfTestTrace.KERNEL.getTrace();
135 fModule.setTrace(trace);
136 fModule.schedule();
137 assertTrue(fModule.waitForCompletion(new NullProgressMonitor()));
138 } catch (TmfAnalysisException e) {
139 fail("Cannot set trace " + e.getMessage());
140 } finally {
141 CtfTmfTestTrace.KERNEL.dispose();
142 }
143
144 }
145 }
This page took 0.041313 seconds and 5 git commands to generate.