source: josm/trunk/test/unit/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetReaderTest.java@ 17275

Last change on this file since 17275 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

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