1 /*******************************************************************************
2 * Copyright (c) 2014 É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 implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.module
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assert
.assertFalse
;
17 import static org
.junit
.Assert
.assertNotNull
;
18 import static org
.junit
.Assert
.assertTrue
;
19 import static org
.junit
.Assert
.fail
;
23 import java
.util
.stream
.StreamSupport
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlAnalysisModuleSource
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.TmfAnalysisModuleHelperXml
.XmlAnalysisModuleType
;
28 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.tests
.common
.TmfXmlTestFiles
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModuleHelper
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisManager
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.ITmfPropertiesProvider
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.Test
;
36 import com
.google
.common
.collect
.Multimap
;
39 * Test suite for the {@link XmlAnalysisModuleSource} class
41 * @author Geneviève Bastien
43 public class XmlAnalysisModuleSourceTest
{
45 private static final String SS_MODULE
= "kernel.linux.sp";
46 private static final String BUILTIN_MODULE
= "test.builtin.sp";
48 private static void emptyXmlFolder() {
49 File fFolder
= XmlUtils
.getXmlFilesPath().toFile();
50 if (!(fFolder
.isDirectory() && fFolder
.exists())) {
53 for (File xmlFile
: fFolder
.listFiles()) {
56 XmlAnalysisModuleSource
.notifyModuleChange();
60 * Empty the XML directory before the test, just in case
68 * Empty the XML directory after the test
71 public void cleanUp() {
76 * Test the {@link XmlAnalysisModuleSource#getAnalysisModules()} method
79 public void testPopulateModules() {
80 XmlAnalysisModuleSource module
= new XmlAnalysisModuleSource();
82 Iterable
<IAnalysisModuleHelper
> modules
= module
.getAnalysisModules();
83 assertFalse("Module present", findModule(modules
, SS_MODULE
));
85 /* Test that the builtin module is present */
86 assertTrue("builtin module present", findModule(modules
, BUILTIN_MODULE
));
88 /* use the valid XML test file */
89 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
90 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
91 fail("XML test file does not exist");
94 XmlUtils
.addXmlFile(testXmlFile
);
95 XmlAnalysisModuleSource
.notifyModuleChange();
96 modules
= module
.getAnalysisModules();
98 assertTrue("Modules available from source", modules
.iterator().hasNext());
99 assertTrue("Module present after add file", findModule(modules
, SS_MODULE
));
100 assertTrue("Builtin module present after add file", findModule(modules
, BUILTIN_MODULE
));
103 private static boolean findModule(Iterable
<IAnalysisModuleHelper
> modules
, String moduleName
) {
104 for (IAnalysisModuleHelper helper
: modules
) {
105 if (moduleName
.equals(helper
.getId())) {
113 * Test that XML modules are available through the analysis manager
116 public void testPopulateModulesWithAnalysisManager() {
119 * Make sure module sources are initialized. When run as unit test, the
120 * XML module source is sometimes missing
122 TmfAnalysisManager
.initialize();
124 Multimap
<String
, IAnalysisModuleHelper
> modules
= TmfAnalysisManager
.getAnalysisModules();
125 assertFalse(findModule(modules
.values(), SS_MODULE
));
126 /* Test that the builtin module is present */
127 assertTrue(findModule(modules
.values(), BUILTIN_MODULE
));
129 /* use the valid XML test file */
130 File testXmlFile
= TmfXmlTestFiles
.VALID_FILE
.getFile();
131 if ((testXmlFile
== null) || !testXmlFile
.exists()) {
132 fail("XML test file does not exist");
135 XmlUtils
.addXmlFile(testXmlFile
);
136 XmlAnalysisModuleSource
.notifyModuleChange();
137 modules
= TmfAnalysisManager
.getAnalysisModules();
138 assertTrue(findModule(modules
.values(), SS_MODULE
));
139 assertTrue(findModule(modules
.values(), BUILTIN_MODULE
));
143 * Test that helper returns the right properties
146 public void testProperties() {
148 /* Get the helper for the builtin module */
149 XmlAnalysisModuleSource module
= new XmlAnalysisModuleSource();
151 Iterable
<IAnalysisModuleHelper
> modules
= module
.getAnalysisModules();
153 IAnalysisModuleHelper helper
= StreamSupport
.stream(modules
.spliterator(), false)
154 .filter(h
-> h
.getId().equals(BUILTIN_MODULE
))
157 assertNotNull(helper
);
159 /* Verify the helper is a properties provider */
160 assertTrue(helper
instanceof ITmfPropertiesProvider
);
161 ITmfPropertiesProvider provider
= (ITmfPropertiesProvider
) helper
;
162 Map
<String
, String
> properties
= provider
.getProperties();
163 assertEquals(2, properties
.size());
166 * The text is externalized, so we're not sure about the property key,
167 * but we know the values that should be present
169 assertTrue(properties
.containsValue(XmlAnalysisModuleType
.STATE_SYSTEM
.name()));
170 assertTrue(properties
.containsValue("test_builtin.xml"));