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

Last change on this file since 15950 was 14138, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.platform and related methods - new class PlatformManager

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