source: josm/trunk/test/unit/org/openstreetmap/josm/gui/DefaultNameFormatterTest.java @ 12557

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

see #15102 - first batch of HTTP unit tests mocking, using WireMock 2.7.1

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