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