1 /*******************************************************************************
2 * Copyright (c) 2013 É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
.linuxtools
.tmf
.analysis
.xml
.core
.tests
.stateprovider
;
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
;
22 import java
.io
.IOException
;
24 import javax
.xml
.parsers
.DocumentBuilder
;
25 import javax
.xml
.parsers
.DocumentBuilderFactory
;
26 import javax
.xml
.parsers
.ParserConfigurationException
;
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
.tests
.shared
.CtfTmfTestTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
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
;
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.
48 * @author Geneviève Bastien
50 public class StateProviderModuleTest
{
52 private static String ANALYSIS_ID
= "polymtl.kernel.sp";
53 private static String ANALYSIS_NAME
= "Xml kernel State System";
55 private XmlStateSystemModule fModule
;
57 private static Document
getXmlDocumentFromFile(File file
) {
58 /* Initialize the state provider module */
60 DocumentBuilderFactory dbFactory
= DocumentBuilderFactory
.newInstance();
61 DocumentBuilder dBuilder
= dbFactory
.newDocumentBuilder();
63 Document doc
= dBuilder
.parse(file
);
65 doc
.getDocumentElement().normalize();
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");
78 * Test the module construction
81 public void testModuleConstruction() {
83 Document doc
= getXmlDocumentFromFile(TmfXmlTestFiles
.VALID_FILE
.getFile());
86 /* get State Providers modules */
87 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
88 assertTrue(stateproviderNodes
.getLength() > 0);
90 Element node
= (Element
) stateproviderNodes
.item(0);
91 fModule
= new XmlStateSystemModule();
92 String moduleId
= node
.getAttribute(TmfXmlStrings
.ANALYSIS_ID
);
93 fModule
.setId(moduleId
);
94 assertEquals(ANALYSIS_ID
, fModule
.getId());
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));
102 fModule
.setHeadInfo(headInfo
);
104 assertEquals(ANALYSIS_NAME
, fModule
.getName());
108 * Test the module executes correctly
111 public void testModuleExecution() {
112 assumeTrue(CtfTmfTestTrace
.KERNEL
.exists());
114 Document doc
= getXmlDocumentFromFile(TmfXmlTestFiles
.VALID_FILE
.getFile());
117 /* get State Providers modules */
118 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
120 Element node
= (Element
) stateproviderNodes
.item(0);
121 fModule
= new XmlStateSystemModule();
122 String moduleId
= node
.getAttribute(TmfXmlStrings
.ANALYSIS_ID
);
123 fModule
.setId(moduleId
);
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));
131 fModule
.setHeadInfo(headInfo
);
134 ITmfTrace trace
= CtfTmfTestTrace
.KERNEL
.getTrace();
135 fModule
.setTrace(trace
);
137 assertTrue(fModule
.waitForCompletion(new NullProgressMonitor()));
138 } catch (TmfAnalysisException e
) {
139 fail("Cannot set trace " + e
.getMessage());
141 CtfTmfTestTrace
.KERNEL
.dispose();