-
Notifications
You must be signed in to change notification settings - Fork 10
/
XmlConfigurationSourceTest.java
305 lines (273 loc) · 11.8 KB
/
XmlConfigurationSourceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*******************************************************************************
* Copyright (c) 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.tmf.analysis.xml.core.tests.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.config.XmlConfigurationSource;
import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.XmlUtils;
import org.eclipse.tracecompass.tmf.analysis.xml.core.tests.Activator;
import org.eclipse.tracecompass.tmf.core.config.ITmfConfigParamDescriptor;
import org.eclipse.tracecompass.tmf.core.config.ITmfConfiguration;
import org.eclipse.tracecompass.tmf.core.config.ITmfConfigurationSource;
import org.eclipse.tracecompass.tmf.core.config.ITmfConfigurationSourceType;
import org.eclipse.tracecompass.tmf.core.config.TmfConfigurationSourceManager;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfConfigurationException;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import com.google.common.collect.ImmutableMap;
/**
* Tests for the {@link XmlConfigurationSource} class
*
* @author Bernd Hufmann
*/
@SuppressWarnings("null")
public class XmlConfigurationSourceTest {
private static final String UNKNOWN_TYPE = "test-test-test";
private static final String XML_ANALYSIS_TYPE_ID = "org.eclipse.tracecompass.tmf.core.config.xmlsourcetype"; //$NON-NLS-1$
private static final Path PATH_INVALID = new Path("test_xml_files/test_invalid");
private static final Path PATH_VALID = new Path("test_xml_files/test_valid");
private static String VALID_NAME = "kvm_exits";
private static String VALID_XML_FILE = VALID_NAME + ".xml";
private static String INVALID_XML_FILE = "invalid_condition_operators.xml";
private static String PATH_TO_INVALID_FILE = getPath(PATH_INVALID, INVALID_XML_FILE);
private static String PATH_TO_VALID_FILE = getPath(PATH_VALID, VALID_XML_FILE);
private static String PATH_TO_VALID_FILE2 = getPath(PATH_VALID, "state_provider_placement.xml"); //$NON-NLS-1$
private static final String EXPECTED_TYPE_NAME = "XML Data-driven analyses"; //$NON-NLS-1$
private static final String EXPECTED_TYPE_DESCRIPTION = "Data-driven analyses described in XML"; //$NON-NLS-1$
private static final String EXPECTED_KEY_NAME = "path";
private static final String EXPECTED_PARAM_DESCRIPTION = "URI to XML analysis file";
private static final String EXPECTED_DATA_TYPE = "STRING";
private static final String EXPECTED_CONFIG_NAME = VALID_NAME;
private static final String EXPECTED_CONFIG_ID = VALID_XML_FILE;
private static final String EXPECTED_CONFIG_DESCRIPTION = "XML Data-driven analysis: " + VALID_NAME;
private static Map<String, Object> VALID_PARAM = ImmutableMap.of(EXPECTED_KEY_NAME, PATH_TO_VALID_FILE);
private static Map<String, Object> VALID_PARAM2 = ImmutableMap.of(EXPECTED_KEY_NAME, PATH_TO_VALID_FILE2);
private static Map<String, Object> INVALID_PARAM = ImmutableMap.of(EXPECTED_KEY_NAME, PATH_TO_INVALID_FILE);
private static final String EXPECTED_MESSAGE_NO_PATH = "Missing path"; //$NON-NLS-1$
private static final String EXPECTED_MESSAGE_FILE_NOT_FOUND = "An error occurred while validating the XML file."; //$NON-NLS-1$
private static final String EXPECTED_MESSAGE_INVALID_CONFIG = "No such configuration with ID: " + UNKNOWN_TYPE; //$NON-NLS-1$
private static final String EXPECTED_MESSAGE_FILE_MISMATCH = "File mismatch"; //$NON-NLS-1$
private static ITmfConfigurationSource sfXmlConfigSource;
private static TmfConfigurationSourceManager sfInstance;
/**
* Test setup
*/
@BeforeClass
public static void init() {
sfInstance = TmfConfigurationSourceManager.getInstance();
assertNotNull(sfInstance);
sfXmlConfigSource = sfInstance.getConfigurationSource(XML_ANALYSIS_TYPE_ID);
assertNotNull(sfXmlConfigSource);
}
/**
* Empty the XML directory after the test
*/
@After
public void emptyXmlFolder() {
// Clean-up config source
Iterator<ITmfConfiguration> iter = sfXmlConfigSource.getConfigurations().iterator();
while (iter.hasNext()) {
sfXmlConfigSource.remove(iter.next().getId());
}
// Delete any remaining xml files, if any
File fFolder = XmlUtils.getXmlFilesPath().toFile();
if (!(fFolder.isDirectory() && fFolder.exists())) {
return;
}
for (File xmlFile : fFolder.listFiles()) {
xmlFile.delete();
}
}
/**
* Test the constructor of the XML configuration source
*
* @throws TmfConfigurationException
* If an error occurred
*/
@Test
public void testConstructor() throws TmfConfigurationException {
// Make sure that an xml file is loaded
ITmfConfiguration config = sfXmlConfigSource.create(VALID_PARAM);
XmlConfigurationSource instance = new XmlConfigurationSource();
List<ITmfConfiguration> configurations = instance.getConfigurations();
assertTrue(configurations.contains(config));
}
/**
* Test the ITmfConfigurationSourceType for this XML configuration source
*/
@Test
public void testSourceType() {
ITmfConfigurationSourceType type = sfXmlConfigSource.getConfigurationSourceType();
assertEquals(XML_ANALYSIS_TYPE_ID, type.getId());
assertEquals(EXPECTED_TYPE_NAME, type.getName());
assertEquals(EXPECTED_TYPE_DESCRIPTION, type.getDescription());
List<ITmfConfigParamDescriptor> descriptors = type.getConfigParamDescriptors();
assertNotNull(descriptors);
assertEquals(1, descriptors.size());
ITmfConfigParamDescriptor desc = descriptors.get(0);
assertEquals(EXPECTED_KEY_NAME, desc.getKeyName());
assertEquals(EXPECTED_DATA_TYPE, desc.getDataType());
assertEquals(EXPECTED_PARAM_DESCRIPTION, desc.getDescription());
assertTrue(desc.isRequired());
}
/**
* Test the creation of a configuration instance
*/
@Test
public void testCreation() {
// Test missing path
Map<String, Object> param = Collections.emptyMap();
try {
sfXmlConfigSource.create(param);
} catch (TmfConfigurationException e) {
// success
assertEquals(EXPECTED_MESSAGE_NO_PATH, e.getMessage());
}
// Test XML file doesn't exist
param = ImmutableMap.of(EXPECTED_KEY_NAME, XmlUtils.getXmlFilesPath().append(UNKNOWN_TYPE).toOSString());
try {
sfXmlConfigSource.create(param);
} catch (TmfConfigurationException e) {
// success
assertEquals(EXPECTED_MESSAGE_FILE_NOT_FOUND, e.getMessage());
}
// Test invalid XML file
try {
sfXmlConfigSource.create(INVALID_PARAM);
} catch (TmfConfigurationException e) {
// success
}
try {
ITmfConfiguration config = sfXmlConfigSource.create(VALID_PARAM);
validateConfig(PATH_TO_VALID_FILE, config);
} catch (TmfConfigurationException e) {
fail();
}
}
/**
* Test the update of a configuration instance
*/
@Test
public void testUpdate() {
ITmfConfiguration config = createConfig(PATH_TO_VALID_FILE);
assertNotNull(config);
// Test missing path
Map<String, Object> param = Collections.emptyMap();
try {
sfXmlConfigSource.update(config.getId(), param);
} catch (TmfConfigurationException e) {
// success
assertEquals(EXPECTED_MESSAGE_NO_PATH, e.getMessage());
}
// Test config doesn't exist
try {
sfXmlConfigSource.update(UNKNOWN_TYPE, param);
} catch (TmfConfigurationException e) {
// success
assertEquals(EXPECTED_MESSAGE_INVALID_CONFIG, e.getMessage());
}
// Test XML file doesn't exist
param = ImmutableMap.of(EXPECTED_KEY_NAME, XmlUtils.getXmlFilesPath().append(UNKNOWN_TYPE).toOSString());
try {
sfXmlConfigSource.update(config.getId(), param);
} catch (TmfConfigurationException e) {
// success
assertEquals(EXPECTED_MESSAGE_FILE_NOT_FOUND, e.getMessage());
}
// Test invalid XML file
try {
sfXmlConfigSource.update(EXPECTED_CONFIG_ID, INVALID_PARAM);
} catch (TmfConfigurationException e) {
// success
}
// Test file name mismatch... not allowed to use different xmlfile name
try {
sfXmlConfigSource.update(config.getId(), VALID_PARAM2);
} catch (TmfConfigurationException e) {
assertEquals(EXPECTED_MESSAGE_FILE_MISMATCH, e.getMessage());
}
try {
ITmfConfiguration configUpdated = sfXmlConfigSource.update(config.getId(), VALID_PARAM);
validateConfig(PATH_TO_VALID_FILE, configUpdated);
} catch (TmfConfigurationException e) {
fail();
}
}
/**
* Test contains
*/
@Test
public void testContains() {
createConfig(PATH_TO_VALID_FILE);
assertTrue(sfXmlConfigSource.contains(EXPECTED_CONFIG_ID));
assertFalse(sfXmlConfigSource.contains(UNKNOWN_TYPE));
}
/**
* Test the removal
*/
@Test
public void testRemove() {
createConfig(PATH_TO_VALID_FILE);
ITmfConfiguration config = sfXmlConfigSource.remove(UNKNOWN_TYPE);
assertNull(config);
config = sfXmlConfigSource.remove(EXPECTED_CONFIG_ID);
validateConfig(PATH_TO_VALID_FILE, config);
}
/**
* Test list configuration
*/
@Test
public void testGetConfiguration() {
List<ITmfConfiguration> configurations = sfXmlConfigSource.getConfigurations();
assertTrue(configurations.isEmpty());
ITmfConfiguration config = createConfig(PATH_TO_VALID_FILE);
assertNotNull(config);
configurations = sfXmlConfigSource.getConfigurations();
assertFalse(configurations.isEmpty());
assertTrue(configurations.contains(config));
}
private static ITmfConfiguration createConfig(String path) {
Map<String, Object> param = ImmutableMap.of("path", path);
try {
return sfXmlConfigSource.create(param);
} catch (TmfConfigurationException e) {
// Nothing to do
}
return null;
}
private static void validateConfig(String path, ITmfConfiguration config) {
assertNotNull(config);
assertEquals(EXPECTED_CONFIG_NAME, config.getName());
assertEquals(EXPECTED_CONFIG_ID, config.getId());
assertEquals(XML_ANALYSIS_TYPE_ID, config.getSourceTypeId());
assertEquals(EXPECTED_CONFIG_DESCRIPTION, config.getDescription());
assertTrue(config.getParameters().isEmpty());
}
private static String getPath(Path folder, String name) {
IPath path = Activator.getAbsolutePath(folder).append(name);
File file = path.toFile();
assertTrue(file.exists());
return Objects.requireNonNull(file.getAbsolutePath());
}
}