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

Last change on this file was 18958, checked in by taylor.smock, 3 months ago

Fix #23418: Improve unit test consistency when run with non-English locales

This also fixes some tests that fail when run individually.

  • Property svn:eol-style set to native
File size: 3.5 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.assertInstanceOf;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9import static org.junit.jupiter.api.Assertions.fail;
10
11import java.io.IOException;
12import java.util.Collection;
13import java.util.List;
14import java.util.stream.Collectors;
15
16import org.junit.jupiter.api.Test;
17import org.openstreetmap.josm.TestUtils;
18import org.openstreetmap.josm.gui.tagging.presets.items.Check;
19import org.openstreetmap.josm.gui.tagging.presets.items.Key;
20import org.openstreetmap.josm.testutils.annotations.Territories;
21import org.xml.sax.SAXException;
22
23/**
24 * Unit tests of {@link TaggingPresetReader} class.
25 */
26class 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}
Note: See TracBrowser for help on using the repository browser.