1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Comparator;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.junit.BeforeClass;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.JOSMFixture;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
22 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
24 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
25 | import org.openstreetmap.josm.io.OsmReader;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 |
|
---|
28 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit tests of {@link DefaultNameFormatter} class.
|
---|
32 | */
|
---|
33 | public class DefaultNameFormatterTest {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Setup tests
|
---|
37 | */
|
---|
38 | @BeforeClass
|
---|
39 | public static void setUpBeforeClass() {
|
---|
40 | JOSMFixture.createUnitTestFixture().init();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/9632">#9632</a>.
|
---|
45 | * @throws IllegalDataException if an error was found while parsing the data from the source
|
---|
46 | * @throws IOException if any I/O error occurs
|
---|
47 | * @throws SAXException if any XML error occurs
|
---|
48 | */
|
---|
49 | @Test
|
---|
50 | @SuppressFBWarnings(value = "ITA_INEFFICIENT_TO_ARRAY")
|
---|
51 | public void testTicket9632() throws IllegalDataException, IOException, SAXException {
|
---|
52 | String source = "http://josm.openstreetmap.de/josmfile?page=Presets/BicycleJunction&preset";
|
---|
53 | TaggingPresets.addTaggingPresets(TaggingPresetReader.readAll(source, true));
|
---|
54 |
|
---|
55 | Comparator<Relation> comparator = DefaultNameFormatter.getInstance().getRelationComparator();
|
---|
56 |
|
---|
57 | try (InputStream is = TestUtils.getRegressionDataStream(9632, "data.osm.zip")) {
|
---|
58 | DataSet ds = OsmReader.parseDataSet(is, null);
|
---|
59 |
|
---|
60 | // Test with 3 known primitives causing the problem. Correct order is p1, p3, p2 with this preset
|
---|
61 | Relation p1 = (Relation) ds.getPrimitiveById(2983382, OsmPrimitiveType.RELATION);
|
---|
62 | Relation p2 = (Relation) ds.getPrimitiveById(550315, OsmPrimitiveType.RELATION);
|
---|
63 | Relation p3 = (Relation) ds.getPrimitiveById(167042, OsmPrimitiveType.RELATION);
|
---|
64 |
|
---|
65 | // route_master ("Bus 453", 6 members)
|
---|
66 | System.out.println("p1: "+DefaultNameFormatter.getInstance().format(p1)+" - "+p1);
|
---|
67 | // TMC ("A 6 Kaiserslautern - Mannheim [negative]", 123 members)
|
---|
68 | System.out.println("p2: "+DefaultNameFormatter.getInstance().format(p2)+" - "+p2);
|
---|
69 | // route(lcn Sal Salier-Radweg(412 members)
|
---|
70 | System.out.println("p3: "+DefaultNameFormatter.getInstance().format(p3)+" - "+p3);
|
---|
71 |
|
---|
72 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
73 | assertEquals(comparator.compare(p1, p2), -1); // p1 < p2
|
---|
74 | assertEquals(comparator.compare(p2, p1), 1); // p2 > p1
|
---|
75 |
|
---|
76 | assertEquals(comparator.compare(p1, p3), -1); // p1 < p3
|
---|
77 | assertEquals(comparator.compare(p3, p1), 1); // p3 > p1
|
---|
78 | assertEquals(comparator.compare(p2, p3), 1); // p2 > p3
|
---|
79 | assertEquals(comparator.compare(p3, p2), -1); // p3 < p2
|
---|
80 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
81 |
|
---|
82 | Relation[] relations = new ArrayList<>(ds.getRelations()).toArray(new Relation[0]);
|
---|
83 |
|
---|
84 | TestUtils.checkComparableContract(comparator, relations);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Tests formatting of relation names.
|
---|
90 | */
|
---|
91 | @Test
|
---|
92 | public void testRelationName() {
|
---|
93 | assertEquals("relation (0, 0 members)",
|
---|
94 | getFormattedRelationName("X=Y"));
|
---|
95 | assertEquals("relation (\"Foo\", 0 members)",
|
---|
96 | getFormattedRelationName("name=Foo"));
|
---|
97 | assertEquals("route (\"123\", 0 members)",
|
---|
98 | getFormattedRelationName("type=route route=tram ref=123"));
|
---|
99 | assertEquals("multipolygon (\"building\", 0 members)",
|
---|
100 | getFormattedRelationName("type=multipolygon building=yes"));
|
---|
101 | assertEquals("multipolygon (\"123\", 0 members)",
|
---|
102 | getFormattedRelationName("type=multipolygon building=yes ref=123"));
|
---|
103 | assertEquals("multipolygon (\"building\", 0 members)",
|
---|
104 | getFormattedRelationName("type=multipolygon building=yes addr:housenumber=123"));
|
---|
105 | assertEquals("multipolygon (\"residential\", 0 members)",
|
---|
106 | getFormattedRelationName("type=multipolygon building=residential addr:housenumber=123"));
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Tests formatting of way names.
|
---|
111 | */
|
---|
112 | @Test
|
---|
113 | public void testWayName() {
|
---|
114 | assertEquals("building (0 nodes)", getFormattedWayName("building=yes"));
|
---|
115 | assertEquals("House number 123 (0 nodes)", getFormattedWayName("building=yes addr:housenumber=123"));
|
---|
116 | assertEquals("House number 123 at FooStreet (0 nodes)", getFormattedWayName("building=yes addr:housenumber=123 addr:street=FooStreet"));
|
---|
117 | assertEquals("House FooName (0 nodes)", getFormattedWayName("building=yes addr:housenumber=123 addr:housename=FooName"));
|
---|
118 | }
|
---|
119 |
|
---|
120 | static String getFormattedRelationName(String tagsString) {
|
---|
121 | return DefaultNameFormatter.getInstance().format((Relation) OsmUtils.createPrimitive("relation " + tagsString))
|
---|
122 | .replace("\u200E", "").replace("\u200F", "");
|
---|
123 | }
|
---|
124 |
|
---|
125 | static String getFormattedWayName(String tagsString) {
|
---|
126 | return DefaultNameFormatter.getInstance().format((Way) OsmUtils.createPrimitive("way " + tagsString))
|
---|
127 | .replace("\u200E", "").replace("\u200F", "");
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Test of {@link DefaultNameFormatter#formatAsHtmlUnorderedList} methods.
|
---|
132 | */
|
---|
133 | @Test
|
---|
134 | public void testFormatAsHtmlUnorderedList() {
|
---|
135 | assertEquals("<ul><li>incomplete</li></ul>",
|
---|
136 | DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(new Node(1)));
|
---|
137 |
|
---|
138 | List<Node> nodes = new ArrayList<>(10);
|
---|
139 | for (int i = 1; i <= 10; i++) {
|
---|
140 | nodes.add(new Node(i, 1));
|
---|
141 | }
|
---|
142 | assertEquals("<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>...</li></ul>",
|
---|
143 | DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(nodes, 5));
|
---|
144 | }
|
---|
145 | }
|
---|