1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.is;
|
---|
5 | import static org.junit.Assert.assertSame;
|
---|
6 | import static org.junit.Assert.assertThat;
|
---|
7 |
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Comparator;
|
---|
13 |
|
---|
14 | import org.junit.BeforeClass;
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.openstreetmap.josm.JOSMFixture;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
20 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
21 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.gui.tagging.TaggingPresetReader;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.TaggingPresets;
|
---|
25 | import org.openstreetmap.josm.io.Compression;
|
---|
26 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
27 | import org.openstreetmap.josm.io.OsmReader;
|
---|
28 | import org.xml.sax.SAXException;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Unit tests of {@link DefaultNameFormatter} class.
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class DefaultNameFormatterTest {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Setup tests
|
---|
38 | */
|
---|
39 | @BeforeClass
|
---|
40 | public static void setUpBeforeClass() {
|
---|
41 | JOSMFixture.createUnitTestFixture().init();
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/9632">#9632</a>.
|
---|
46 | * @throws IllegalDataException if an error was found while parsing the data from the source
|
---|
47 | * @throws IOException if any I/O error occurs
|
---|
48 | * @throws SAXException if any XML error occurs
|
---|
49 | */
|
---|
50 | @Test
|
---|
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 = new FileInputStream(TestUtils.getRegressionDataFile(9632, "data.osm.zip"))) {
|
---|
58 | DataSet ds = OsmReader.parseDataSet(Compression.ZIP.getUncompressedInputStream(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 | assertSame(comparator.compare(p1, p2), -1); // p1 < p2
|
---|
73 | assertSame(comparator.compare(p2, p1), 1); // p2 > p1
|
---|
74 |
|
---|
75 | assertSame(comparator.compare(p1, p3), -1); // p1 < p3
|
---|
76 | assertSame(comparator.compare(p3, p1), 1); // p3 > p1
|
---|
77 | assertSame(comparator.compare(p2, p3), 1); // p2 > p3
|
---|
78 | assertSame(comparator.compare(p3, p2), -1); // p3 < p2
|
---|
79 |
|
---|
80 | Relation[] relations = new ArrayList<>(ds.getRelations()).toArray(new Relation[0]);
|
---|
81 |
|
---|
82 | TestUtils.checkComparableContract(comparator, relations);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Tests formatting of relation names.
|
---|
88 | */
|
---|
89 | @Test
|
---|
90 | public void testRelationName() {
|
---|
91 | assertThat(getFormattedRelationName("X=Y"), is("relation (0, 0 members)"));
|
---|
92 | assertThat(getFormattedRelationName("name=Foo"), is("relation (\"Foo\", 0 members)"));
|
---|
93 | assertThat(getFormattedRelationName("type=route route=tram ref=123"), is("route (\"123\", 0 members)"));
|
---|
94 | assertThat(getFormattedRelationName("type=multipolygon building=yes"), is("multipolygon (\"building\", 0 members)"));
|
---|
95 | assertThat(getFormattedRelationName("type=multipolygon building=yes ref=123"), is("multipolygon (\"123\", 0 members)"));
|
---|
96 | assertThat(getFormattedRelationName("type=multipolygon building=yes addr:housenumber=123"),
|
---|
97 | is("multipolygon (\"building\", 0 members)"));
|
---|
98 | assertThat(getFormattedRelationName("type=multipolygon building=residential addr:housenumber=123"),
|
---|
99 | is("multipolygon (\"residential\", 0 members)"));
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Tests formatting of way names.
|
---|
104 | */
|
---|
105 | @Test
|
---|
106 | public void testWayName() {
|
---|
107 | assertThat(getFormattedWayName("building=yes"), is("building (0 nodes)"));
|
---|
108 | assertThat(getFormattedWayName("building=yes addr:housenumber=123"), is("House number 123 (0 nodes)"));
|
---|
109 | assertThat(getFormattedWayName("building=yes addr:housenumber=123 addr:street=FooStreet"),
|
---|
110 | is("House number 123 at FooStreet (0 nodes)"));
|
---|
111 | assertThat(getFormattedWayName("building=yes addr:housenumber=123 addr:housename=FooName"), is("House FooName (0 nodes)"));
|
---|
112 | }
|
---|
113 |
|
---|
114 | static String getFormattedRelationName(String tagsString) {
|
---|
115 | return DefaultNameFormatter.getInstance().format((Relation) OsmUtils.createPrimitive("relation " + tagsString))
|
---|
116 | .replace("\u200E", "").replace("\u200F", "");
|
---|
117 | }
|
---|
118 |
|
---|
119 | static String getFormattedWayName(String tagsString) {
|
---|
120 | return DefaultNameFormatter.getInstance().format((Way) OsmUtils.createPrimitive("way " + tagsString))
|
---|
121 | .replace("\u200E", "").replace("\u200F", "");
|
---|
122 | }
|
---|
123 | }
|
---|