1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets;
|
---|
3 |
|
---|
4 | import static org.CustomMatchers.hasSize;
|
---|
5 | import static org.hamcrest.MatcherAssert.assertThat;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
---|
8 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
9 | import static org.junit.jupiter.api.Assertions.fail;
|
---|
10 |
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.stream.Collectors;
|
---|
15 |
|
---|
16 | import org.junit.jupiter.api.Test;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.gui.tagging.presets.items.Check;
|
---|
19 | import org.openstreetmap.josm.gui.tagging.presets.items.Key;
|
---|
20 | import org.openstreetmap.josm.testutils.annotations.Territories;
|
---|
21 | import org.xml.sax.SAXException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Unit tests of {@link TaggingPresetReader} class.
|
---|
25 | */
|
---|
26 | class TaggingPresetReaderTest {
|
---|
27 | /**
|
---|
28 | * #8954 - last checkbox in the preset is not added
|
---|
29 | * @throws SAXException if any XML error occurs
|
---|
30 | * @throws IOException if any I/O error occurs
|
---|
31 | */
|
---|
32 | @Test
|
---|
33 | void testTicket8954() throws SAXException, IOException {
|
---|
34 | String presetfile = TestUtils.getRegressionDataFile(8954, "preset.xml");
|
---|
35 | final Collection<TaggingPreset> presets = TaggingPresetReader.readAll(presetfile, false);
|
---|
36 | assertEquals(1, presets.size(), "Number of preset items");
|
---|
37 | final TaggingPreset preset = presets.iterator().next();
|
---|
38 | assertEquals(1, preset.data.size(), "Number of entries");
|
---|
39 | final TaggingPresetItem item = preset.data.get(0);
|
---|
40 | assertInstanceOf(Check.class, item, "Entry is not checkbox");
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Test nested chunks
|
---|
45 | * @throws SAXException if any XML error occurs
|
---|
46 | * @throws IOException if any I/O error occurs
|
---|
47 | */
|
---|
48 | @Test
|
---|
49 | void testNestedChunks() throws SAXException, IOException {
|
---|
50 | final Collection<TaggingPreset> presets = TaggingPresetReader.readAll(TestUtils.getTestDataRoot() + "preset_chunk.xml", true);
|
---|
51 | assertThat(presets, hasSize(1));
|
---|
52 | final TaggingPreset abc = presets.iterator().next();
|
---|
53 | assertTrue(abc.data.stream().allMatch(Key.class::isInstance));
|
---|
54 | final List<String> keys = abc.data.stream().map(x -> ((Key) x).key).collect(Collectors.toList());
|
---|
55 | assertEquals("[A1, A2, A3, B1, B2, B3, C1, C2, C3]", keys.toString());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Test external entity resolving.
|
---|
60 | * See #19286
|
---|
61 | * @throws IOException in case of I/O error
|
---|
62 | */
|
---|
63 | @Test
|
---|
64 | void testExternalEntityResolving() throws IOException {
|
---|
65 | try {
|
---|
66 | TaggingPresetReader.readAll(TestUtils.getTestDataRoot() + "preset_external_entity.xml", true);
|
---|
67 | fail("Reading a file with external entities should throw an SAXParseException!");
|
---|
68 | } catch (SAXException e) {
|
---|
69 | String expected = "DOCTYPE is disallowed when the feature \"http://apache.org/xml/features/disallow-doctype-decl\" set to true.";
|
---|
70 | assertEquals(expected, e.getMessage());
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Validate internal presets
|
---|
76 | * See #9027
|
---|
77 | * @throws SAXException if any XML error occurs
|
---|
78 | * @throws IOException if any I/O error occurs
|
---|
79 | */
|
---|
80 | @Territories
|
---|
81 | @Test
|
---|
82 | void testReadDefaultPresets() throws SAXException, IOException {
|
---|
83 | String presetfile = "resource://data/defaultpresets.xml";
|
---|
84 | final Collection<TaggingPreset> presets = TaggingPresetReader.readAll(presetfile, true);
|
---|
85 | assertTrue(presets.size() > 0, "Default presets are empty");
|
---|
86 | }
|
---|
87 | }
|
---|