source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/DefaultNameFormatterTest.java

Last change on this file was 18977, checked in by taylor.smock, 2 months ago

See #23468: Improve performance in the validator tree window

This fixes an issue where there was a difference in sorting algorithms between
the faster ASCII only sorting method and the sorting method used by non-ASCII
strings.

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5import static com.github.tomakehurst.wiremock.client.WireMock.get;
6import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
7import static org.junit.jupiter.api.Assertions.assertEquals;
8
9import java.io.IOException;
10import java.io.InputStream;
11import java.util.ArrayList;
12import java.util.Comparator;
13import java.util.List;
14import java.util.stream.Collectors;
15import java.util.stream.IntStream;
16
17import org.junit.jupiter.api.Test;
18import org.openstreetmap.josm.TestUtils;
19import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
20import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
21import org.openstreetmap.josm.io.IllegalDataException;
22import org.openstreetmap.josm.io.OsmReader;
23import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
24import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
25import org.openstreetmap.josm.testutils.annotations.HTTP;
26import org.xml.sax.SAXException;
27
28import com.github.tomakehurst.wiremock.WireMockServer;
29
30import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
31
32/**
33 * Unit tests of {@link DefaultNameFormatter} class.
34 */
35// Preferences are needed for OSM primitives
36@BasicPreferences
37@BasicWiremock
38@HTTP
39class DefaultNameFormatterTest {
40 /**
41 * HTTP mock.
42 */
43 @BasicWiremock
44 WireMockServer wireMockServer;
45
46 /**
47 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/9632">#9632</a>.
48 * @throws IllegalDataException if an error was found while parsing the data from the source
49 * @throws IOException if any I/O error occurs
50 * @throws SAXException if any XML error occurs
51 */
52 @Test
53 @SuppressFBWarnings(value = "ITA_INEFFICIENT_TO_ARRAY")
54 void testTicket9632() throws IllegalDataException, IOException, SAXException {
55 String source = "presets/Presets_BicycleJunction-preset.xml";
56 wireMockServer.stubFor(get(urlEqualTo("/" + source))
57 .willReturn(aResponse()
58 .withStatus(200)
59 .withHeader("Content-Type", "text/xml")
60 .withBodyFile(source)));
61 TaggingPresets.addTaggingPresets(TaggingPresetReader.readAll(wireMockServer.url(source), true));
62
63 Comparator<IRelation<?>> comparator = DefaultNameFormatter.getInstance().getRelationComparator();
64
65 try (InputStream is = TestUtils.getRegressionDataStream(9632, "data.osm.zip")) {
66 DataSet ds = OsmReader.parseDataSet(is, null);
67
68 // Test with 3 known primitives causing the problem. Correct order is p1, p3, p2 with this preset
69 Relation p1 = (Relation) ds.getPrimitiveById(2983382, OsmPrimitiveType.RELATION);
70 Relation p2 = (Relation) ds.getPrimitiveById(550315, OsmPrimitiveType.RELATION);
71 Relation p3 = (Relation) ds.getPrimitiveById(167042, OsmPrimitiveType.RELATION);
72
73 // route_master ("Bus 453", 6 members)
74 System.out.println("p1: "+DefaultNameFormatter.getInstance().format(p1)+" - "+p1);
75 // TMC ("A 6 Kaiserslautern - Mannheim [negative]", 123 members)
76 System.out.println("p2: "+DefaultNameFormatter.getInstance().format(p2)+" - "+p2);
77 // route(lcn Sal Salier-Radweg(412 members)
78 System.out.println("p3: "+DefaultNameFormatter.getInstance().format(p3)+" - "+p3);
79
80 // CHECKSTYLE.OFF: SingleSpaceSeparator
81 assertEquals(-1, comparator.compare(p1, p2)); // p1 < p2
82 assertEquals(1, comparator.compare(p2, p1)); // p2 > p1
83
84 assertEquals(-1, comparator.compare(p1, p3)); // p1 < p3
85 assertEquals(1, comparator.compare(p3, p1)); // p3 > p1
86 assertEquals(1, comparator.compare(p2, p3)); // p2 > p3
87 assertEquals(-1, comparator.compare(p3, p2)); // p3 < p2
88 // CHECKSTYLE.ON: SingleSpaceSeparator
89
90 Relation[] relations = new ArrayList<>(ds.getRelations()).toArray(new Relation[0]);
91
92 TestUtils.checkComparableContract(comparator, relations);
93 }
94 }
95
96 /**
97 * Tests formatting of relation names.
98 */
99 @Test
100 void testRelationName() {
101 assertEquals("relation (0, 0 members)",
102 getFormattedRelationName("X=Y"));
103 assertEquals("relation (\"Foo\", 0 members)",
104 getFormattedRelationName("name=Foo"));
105 assertEquals("route (\"123\", 0 members)",
106 getFormattedRelationName("type=route route=tram ref=123"));
107 assertEquals("multipolygon (\"building\", 0 members)",
108 getFormattedRelationName("type=multipolygon building=yes"));
109 assertEquals("multipolygon (\"123\", 0 members)",
110 getFormattedRelationName("type=multipolygon building=yes ref=123"));
111 assertEquals("multipolygon (\"building\", 0 members)",
112 getFormattedRelationName("type=multipolygon building=yes addr:housenumber=123"));
113 assertEquals("multipolygon (\"residential\", 0 members)",
114 getFormattedRelationName("type=multipolygon building=residential addr:housenumber=123"));
115 }
116
117 /**
118 * Tests formatting of way names.
119 */
120 @Test
121 void testWayName() {
122 assertEquals("\u200Ebuilding\u200E (0 nodes)\u200C", getFormattedWayName("building=yes"));
123 assertEquals("\u200EHouse number 123\u200E (0 nodes)\u200C",
124 getFormattedWayName("building=yes addr:housenumber=123"));
125 assertEquals("\u200EHouse number 123 at FooStreet\u200E (0 nodes)\u200C",
126 getFormattedWayName("building=yes addr:housenumber=123 addr:street=FooStreet"));
127 assertEquals("\u200EHouse FooName\u200E (0 nodes)\u200C",
128 getFormattedWayName("building=yes addr:housenumber=123 addr:housename=FooName"));
129 }
130
131 static String getFormattedRelationName(String tagsString) {
132 return DefaultNameFormatter.getInstance().format(OsmUtils.createPrimitive("relation " + tagsString));
133 }
134
135 static String getFormattedWayName(String tagsString) {
136 return DefaultNameFormatter.getInstance().format(OsmUtils.createPrimitive("way " + tagsString));
137 }
138
139 /**
140 * Test of {@link DefaultNameFormatter#formatAsHtmlUnorderedList} methods.
141 */
142 @Test
143 void testFormatAsHtmlUnorderedList() {
144 assertEquals("<ul><li>incomplete</li></ul>",
145 DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(new Node(1)));
146
147 List<Node> nodes = IntStream.rangeClosed(1, 10).mapToObj(i -> new Node(i, 1))
148 .collect(Collectors.toList());
149 assertEquals("<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>...</li></ul>",
150 DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(nodes, 5));
151 }
152
153 /**
154 * Test of {@link DefaultNameFormatter#buildDefaultToolTip(IPrimitive)}.
155 */
156 @Test
157 void testBuildDefaultToolTip() {
158 assertEquals("<html><strong>id</strong>=0<br>"+
159 "<strong>name:en</strong>=foo<br>"+
160 "<strong>tourism</strong>=hotel<br>"+
161 "<strong>url</strong>=http://foo.bar<br>"+
162 "<strong>xml</strong>=&lt;tag/&gt;</html>",
163 DefaultNameFormatter.getInstance().buildDefaultToolTip(
164 TestUtils.newNode("tourism=hotel name:en=foo url=http://foo.bar xml=<tag/>")));
165 }
166
167 /**
168 * Test of {@link DefaultNameFormatter#removeBiDiCharacters(String)}.
169 */
170 @Test
171 void testRemoveBiDiCharacters() {
172 assertEquals("building (0 nodes)", DefaultNameFormatter.removeBiDiCharacters("\u200Ebuilding\u200E (0 nodes)\u200C"));
173 }
174}
Note: See TracBrowser for help on using the repository browser.