tmf: Add XML analysis helper properties
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core.tests / src / org / eclipse / tracecompass / tmf / analysis / xml / core / tests / module / XmlAnalysisModuleSourceTest.java
1 /*******************************************************************************
2 * Copyright (c) 2014 É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 implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.module;
14
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;
20
21 import java.io.File;
22 import java.util.Map;
23 import java.util.stream.StreamSupport;
24
25 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.TmfAnalysisModuleHelperXml.XmlAnalysisModuleType;
26 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlAnalysisModuleSource;
27 import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
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;
35
36 import com.google.common.collect.Multimap;
37
38 /**
39 * Test suite for the {@link XmlAnalysisModuleSource} class
40 *
41 * @author Geneviève Bastien
42 */
43 public class XmlAnalysisModuleSourceTest {
44
45 private static final String SS_MODULE = "kernel.linux.sp";
46 private static final String BUILTIN_MODULE = "test.builtin.sp";
47
48 private static void emptyXmlFolder() {
49 File fFolder = XmlUtils.getXmlFilesPath().toFile();
50 if (!(fFolder.isDirectory() && fFolder.exists())) {
51 return;
52 }
53 for (File xmlFile : fFolder.listFiles()) {
54 xmlFile.delete();
55 }
56 XmlAnalysisModuleSource.notifyModuleChange();
57 }
58
59 /**
60 * Empty the XML directory before the test, just in case
61 */
62 @Before
63 public void setUp() {
64 emptyXmlFolder();
65 }
66
67 /**
68 * Empty the XML directory after the test
69 */
70 @After
71 public void cleanUp() {
72 emptyXmlFolder();
73 }
74
75 /**
76 * Test the {@link XmlAnalysisModuleSource#getAnalysisModules()} method
77 */
78 @Test
79 public void testPopulateModules() {
80 XmlAnalysisModuleSource module = new XmlAnalysisModuleSource();
81
82 Iterable<IAnalysisModuleHelper> modules = module.getAnalysisModules();
83 assertFalse("Module present", findModule(modules, SS_MODULE));
84
85 /* Test that the builtin module is present */
86 assertTrue("builtin module present", findModule(modules, BUILTIN_MODULE));
87
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");
92 }
93
94 XmlUtils.addXmlFile(testXmlFile);
95 XmlAnalysisModuleSource.notifyModuleChange();
96 modules = module.getAnalysisModules();
97
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));
101 }
102
103 private static boolean findModule(Iterable<IAnalysisModuleHelper> modules, String moduleName) {
104 for (IAnalysisModuleHelper helper : modules) {
105 if (moduleName.equals(helper.getId())) {
106 return true;
107 }
108 }
109 return false;
110 }
111
112 /**
113 * Test that XML modules are available through the analysis manager
114 */
115 @Test
116 public void testPopulateModulesWithAnalysisManager() {
117
118 /*
119 * Make sure module sources are initialized. When run as unit test, the
120 * XML module source is sometimes missing
121 */
122 TmfAnalysisManager.initialize();
123
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));
128
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");
133 }
134
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));
140 }
141
142 /**
143 * Test that helper returns the right properties
144 */
145 @Test
146 public void testProperties() {
147
148 /* Get the helper for the builtin module */
149 XmlAnalysisModuleSource module = new XmlAnalysisModuleSource();
150
151 Iterable<IAnalysisModuleHelper> modules = module.getAnalysisModules();
152
153 IAnalysisModuleHelper helper = StreamSupport.stream(modules.spliterator(), false)
154 .filter(h -> h.getId().equals(BUILTIN_MODULE))
155 .findFirst()
156 .get();
157 assertNotNull(helper);
158
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());
164
165 /*
166 * The text is externalized, so we're not sure about the property key,
167 * but we know the values that should be present
168 */
169 assertTrue(properties.containsValue(XmlAnalysisModuleType.STATE_SYSTEM.name()));
170 assertTrue(properties.containsValue("test_builtin.xml"));
171 }
172
173 }
This page took 0.034316 seconds and 5 git commands to generate.