1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.junit.Before;
|
---|
11 | import org.junit.Test;
|
---|
12 | import org.openstreetmap.josm.JOSMFixture;
|
---|
13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Unit test of {@link SimilarNamedWays}
|
---|
21 | */
|
---|
22 | public class SimilarNamedWaysTest {
|
---|
23 |
|
---|
24 | private final SimilarNamedWays test = new SimilarNamedWays();
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Setup test
|
---|
28 | */
|
---|
29 | @Before
|
---|
30 | public void setUp() {
|
---|
31 | JOSMFixture.createUnitTestFixture().init();
|
---|
32 | }
|
---|
33 |
|
---|
34 | private static List<TestError> testWays(String namea, String nameb) {
|
---|
35 | DataSet ds = new DataSet();
|
---|
36 |
|
---|
37 | Node n00 = new Node(LatLon.ZERO);
|
---|
38 | Node n10 = new Node(new LatLon(1, 0));
|
---|
39 | Node n20 = new Node(new LatLon(2, 0));
|
---|
40 | Node n30 = new Node(new LatLon(3, 0));
|
---|
41 | Node n40 = new Node(new LatLon(4, 0));
|
---|
42 |
|
---|
43 | ds.addPrimitive(n00);
|
---|
44 | ds.addPrimitive(n10);
|
---|
45 | ds.addPrimitive(n20);
|
---|
46 | ds.addPrimitive(n30);
|
---|
47 | ds.addPrimitive(n40);
|
---|
48 |
|
---|
49 | Way waya = new Way();
|
---|
50 | waya.addNode(n00);
|
---|
51 | waya.addNode(n10);
|
---|
52 | waya.addNode(n20);
|
---|
53 | waya.put("name", namea);
|
---|
54 | Way wayb = new Way();
|
---|
55 | wayb.addNode(n20);
|
---|
56 | wayb.addNode(n30);
|
---|
57 | wayb.addNode(n40);
|
---|
58 | wayb.put("name", nameb);
|
---|
59 |
|
---|
60 | ds.addPrimitive(waya);
|
---|
61 | ds.addPrimitive(wayb);
|
---|
62 |
|
---|
63 | assertTrue(waya.isUsable());
|
---|
64 | assertTrue(wayb.isUsable());
|
---|
65 |
|
---|
66 | SimilarNamedWays t = new SimilarNamedWays();
|
---|
67 | t.startTest(null);
|
---|
68 | t.visit(waya);
|
---|
69 | t.visit(wayb);
|
---|
70 | return t.getErrors();
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Test
|
---|
74 | public void testCombinations() {
|
---|
75 | assertTrue(testWays("Church Street", "Water Street").isEmpty());
|
---|
76 | assertFalse(testWays("Main Street", "Maim Street").isEmpty());
|
---|
77 | assertFalse(testWays("First Street", "Frist Street").isEmpty());
|
---|
78 |
|
---|
79 | assertTrue(testWays("1st Street", "2nd Street").isEmpty());
|
---|
80 | assertTrue(testWays("First Avenue", "Second Avenue").isEmpty());
|
---|
81 | assertTrue(testWays("West Main Street", "East Main Street").isEmpty());
|
---|
82 | assertTrue(testWays("A Street", "B Street").isEmpty());
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void checkSimilarity(String message, String name1, String name2, boolean expected) {
|
---|
86 | boolean actual = test.similaryName(name1, name2);
|
---|
87 | assertEquals(message, expected, actual);
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Test similar names.
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | public void testSimilarNames() {
|
---|
95 | checkSimilarity("same string", "Testname", "Testname", false);
|
---|
96 | checkSimilarity("different case", "Testname", "TestName", true);
|
---|
97 | checkSimilarity("typo", "Testname", "Testxame", true);
|
---|
98 | checkSimilarity("missing char", "Testname", "Testame", true);
|
---|
99 | checkSimilarity("additional char", "Testname", "Testxname", true);
|
---|
100 | checkSimilarity("2 changes", "Testname", "Tostxname", true);
|
---|
101 | checkSimilarity("3 changes", "Testname", "Tostxnam", false);
|
---|
102 |
|
---|
103 | // regular expression rule
|
---|
104 | checkSimilarity("same number", "track 1", "track 1", false);
|
---|
105 | checkSimilarity("different number", "track 1", "track 2", false);
|
---|
106 | checkSimilarity("different number length", "track 9", "track 10", false);
|
---|
107 | checkSimilarity("multiple numbers", "track 8 - 9", "track 10 - 11", false);
|
---|
108 | // persian numbers, see #15869
|
---|
109 | checkSimilarity("persian numbers", "بنبست نیلوفر ۵", "بنبست نیلوفر ۶", false);
|
---|
110 |
|
---|
111 | checkSimilarity("1st and 2nd", "1st Street", "2nd Street", false);
|
---|
112 | checkSimilarity("1st case", "1St Street", "1st Street", true);
|
---|
113 | checkSimilarity("1st and 2nd case", "1St Street", "2nd Street", true);
|
---|
114 | checkSimilarity("3rd and 4th", "2rd Street", "4th Street", false);
|
---|
115 |
|
---|
116 | // synonyms
|
---|
117 | checkSimilarity("east and west", "East Foothill Drive", "West Foothill Drive", false);
|
---|
118 | checkSimilarity("east and west case", "east Foothill Drive", "West Foothill Drive", true);
|
---|
119 | checkSimilarity("first and second", "First Street", "Second Street", false);
|
---|
120 | checkSimilarity("first and second case", "First Street", "second Street", true);
|
---|
121 | checkSimilarity("first and second typo", "Forst Street", "Second Street", true);
|
---|
122 | checkSimilarity("first and second typo2", "First Street", "Socond Street", true);
|
---|
123 | checkSimilarity("first and second 2 changes", "First Street", "Soconds Street", true);
|
---|
124 | checkSimilarity("first and second 3 changes", "First Street", "Soconds Stret", false);
|
---|
125 |
|
---|
126 | // case only, see #14858
|
---|
127 | checkSimilarity("case only", "Rua São João", "Rua Sao Joao", true);
|
---|
128 | checkSimilarity("case only", "Rua São João", "Rua SAO JOAO", true);
|
---|
129 | checkSimilarity("case only", "Rua Sao Joao", "Rua SAO JOAO", true);
|
---|
130 | checkSimilarity("case only", "Rue éèçàïù", "Rue EeCAIU", true);
|
---|
131 | }
|
---|
132 | }
|
---|