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