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
.tracecompass
.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
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
31 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.XmlStateSystemModule
;
32 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.common
.TmfXmlTestFiles
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
34 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfTmfTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
36 import org
.junit
.Test
;
37 import org
.w3c
.dom
.Document
;
38 import org
.w3c
.dom
.Element
;
39 import org
.w3c
.dom
.NodeList
;
40 import org
.xml
.sax
.SAXException
;
43 * Test suite for the XmlStateSystemModule Test. It tests the reading of the
44 * file, the header and the module's proper functioning as a module, but not the
45 * state system building, which is covered by another test suite.
47 * @author Geneviève Bastien
49 public class StateProviderModuleTest
{
51 private static String ANALYSIS_ID
= "kernel.linux.sp";
52 private static String ANALYSIS_NAME
= "Xml kernel State System";
54 private XmlStateSystemModule fModule
;
56 private static Document
getXmlDocumentFromFile(File file
) {
57 /* Initialize the state provider module */
59 DocumentBuilderFactory dbFactory
= DocumentBuilderFactory
.newInstance();
60 DocumentBuilder dBuilder
= dbFactory
.newDocumentBuilder();
62 Document doc
= dBuilder
.parse(file
);
64 doc
.getDocumentElement().normalize();
66 } catch (ParserConfigurationException e
) {
67 fail("Xml document parse exception");
68 } catch (SAXException e
) {
69 fail("Exception parsing xml file");
70 } catch (IOException e
) {
71 fail("File io exception");
77 * Test the module construction
80 public void testModuleConstruction() {
82 Document doc
= getXmlDocumentFromFile(TmfXmlTestFiles
.VALID_FILE
.getFile());
85 /* get State Providers modules */
86 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
87 assertTrue(stateproviderNodes
.getLength() > 0);
89 Element node
= (Element
) stateproviderNodes
.item(0);
90 fModule
= new XmlStateSystemModule();
91 String moduleId
= node
.getAttribute(TmfXmlStrings
.ID
);
92 fModule
.setId(moduleId
);
93 assertEquals(ANALYSIS_ID
, fModule
.getId());
95 fModule
.setXmlFile(new Path(TmfXmlTestFiles
.VALID_FILE
.getFile().getAbsolutePath()));
97 assertEquals(ANALYSIS_NAME
, fModule
.getName());
101 * Test the module executes correctly
104 public void testModuleExecution() {
105 assumeTrue(CtfTmfTestTrace
.KERNEL
.exists());
107 Document doc
= getXmlDocumentFromFile(TmfXmlTestFiles
.VALID_FILE
.getFile());
110 /* get State Providers modules */
111 NodeList stateproviderNodes
= doc
.getElementsByTagName(TmfXmlStrings
.STATE_PROVIDER
);
113 Element node
= (Element
) stateproviderNodes
.item(0);
114 fModule
= new XmlStateSystemModule();
115 String moduleId
= node
.getAttribute(TmfXmlStrings
.ID
);
116 fModule
.setId(moduleId
);
118 fModule
.setXmlFile(new Path(TmfXmlTestFiles
.VALID_FILE
.getFile().getAbsolutePath()));
120 try (CtfTmfTrace trace
= CtfTmfTestTrace
.KERNEL
.getTrace();) {
121 fModule
.setTrace(trace
);
124 assertTrue(fModule
.waitForCompletion(new NullProgressMonitor()));
125 } catch (TmfAnalysisException e
) {
126 fail("Cannot set trace " + e
.getMessage());