1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.fail;
|
---|
5 |
|
---|
6 | import java.io.FileInputStream;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
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.Relation;
|
---|
19 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
20 | import org.openstreetmap.josm.gui.tagging.TaggingPresetReader;
|
---|
21 | import org.openstreetmap.josm.io.Compression;
|
---|
22 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
23 | import org.openstreetmap.josm.io.OsmReader;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Unit tests of {@link DefaultNameFormatter} class.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public class DefaultNameFormatterTest {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Setup tests
|
---|
34 | */
|
---|
35 | @BeforeClass
|
---|
36 | public static void setUpBeforeClass() {
|
---|
37 | JOSMFixture.createUnitTestFixture().init();
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/9632">#9632</a>.
|
---|
42 | * @throws IllegalDataException
|
---|
43 | * @throws IOException
|
---|
44 | * @throws SAXException
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testTicket9632() throws IllegalDataException, IOException, SAXException {
|
---|
48 | String source = "http://josm.openstreetmap.de/josmfile?page=Presets/BicycleJunction&preset";
|
---|
49 | TaggingPresetPreference.taggingPresets = TaggingPresetReader.readAll(source, true);
|
---|
50 |
|
---|
51 | Comparator<Relation> comparator = DefaultNameFormatter.getInstance().getRelationComparator();
|
---|
52 |
|
---|
53 | try (InputStream is = new FileInputStream(TestUtils.getTestDataRoot() + "regress/9632/data.osm.zip")) {
|
---|
54 | DataSet ds = OsmReader.parseDataSet(Compression.ZIP.getUncompressedInputStream(is), null);
|
---|
55 | Relation[] relations = new ArrayList<>(ds.getRelations()).toArray(new Relation[0]);
|
---|
56 | System.out.println(Arrays.toString(relations));
|
---|
57 | // Check each compare possibility
|
---|
58 | for (int i=0; i<relations.length; i++) {
|
---|
59 | Relation r1 = relations[i];
|
---|
60 | for (int j=i; j<relations.length; j++) {
|
---|
61 | Relation r2 = relations[j];
|
---|
62 | int a = comparator.compare(r1, r2);
|
---|
63 | int b = comparator.compare(r2, r1);
|
---|
64 | if (i==j || a==b) {
|
---|
65 | if (a != 0 || b != 0) {
|
---|
66 | fail(getFailMessage(r1, r2, a, b));
|
---|
67 | }
|
---|
68 | } else {
|
---|
69 | if (a != -b) {
|
---|
70 | fail(getFailMessage(r1, r2, a, b));
|
---|
71 | }
|
---|
72 | }
|
---|
73 | for (int k=j; k<relations.length; k++) {
|
---|
74 | Relation r3 = relations[k];
|
---|
75 | int c = comparator.compare(r1, r3);
|
---|
76 | int d = comparator.compare(r2, r3);
|
---|
77 | if (a > 0 && d > 0) {
|
---|
78 | if (c <= 0) {
|
---|
79 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
80 | }
|
---|
81 | } else if (a == 0 && d == 0) {
|
---|
82 | if (c != 0) {
|
---|
83 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
84 | }
|
---|
85 | } else if (a < 0 && d < 0) {
|
---|
86 | if (c >= 0) {
|
---|
87 | fail(getFailMessage(r1, r2, r3, a, b, c, d));
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | // Sort relation array
|
---|
94 | Arrays.sort(relations, comparator);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static String getFailMessage(Relation r1, Relation r2, int a, int b) {
|
---|
99 | return new StringBuilder("Compared\nr1: ").append(r1).append("\nr2: ")
|
---|
100 | .append(r2).append("\ngave: ").append(a).append("/").append(b)
|
---|
101 | .toString();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static String getFailMessage(Relation r1, Relation r2, Relation r3, int a, int b, int c, int d) {
|
---|
105 | return new StringBuilder(getFailMessage(r1, r2, a, b))
|
---|
106 | .append("\nCompared\nr1: ").append(r1).append("\nr3: ").append(r3).append("\ngave: ").append(c)
|
---|
107 | .append("\nCompared\nr2: ").append(r2).append("\nr3: ").append(r3).append("\ngave: ").append(d)
|
---|
108 | .toString();
|
---|
109 | }
|
---|
110 | }
|
---|