source: josm/trunk/test/unit/org/openstreetmap/josm/gui/conflict/tags/TagConflictResolutionUtilTest.java@ 17536

Last change on this file since 17536 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 21.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.tags;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNull;
7import static org.junit.jupiter.api.Assertions.assertTrue;
8import static org.junit.jupiter.api.Assertions.assertThrows;
9
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.HashSet;
13import java.util.List;
14import java.util.regex.PatternSyntaxException;
15import java.util.stream.Collectors;
16
17import org.junit.jupiter.api.Test;
18import org.junit.jupiter.api.extension.RegisterExtension;
19import org.openstreetmap.josm.data.osm.Tag;
20import org.openstreetmap.josm.data.osm.TagCollection;
21import org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.AutomaticChoice;
22import org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.AutomaticChoiceGroup;
23import org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.AutomaticCombine;
24import org.openstreetmap.josm.testutils.JOSMTestRules;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests of {@link TagConflictResolutionUtil} class.
30 */
31class TagConflictResolutionUtilTest {
32
33 /**
34 * Setup test.
35 */
36 @RegisterExtension
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public JOSMTestRules test = new JOSMTestRules();
39
40 private static HashSet<String> newHashSet(String... values) {
41 return Arrays.stream(values).collect(Collectors.toCollection(HashSet::new));
42 }
43
44 /**
45 * Unit test of {@link TagConflictResolutionUtil#applyAutomaticTagConflictResolution}.
46 * assume predefined rules for US TIGER and French Cadastre.
47 */
48 @Test
49 void testApplyAutomaticTagConflictResolution() {
50 // Check that general tag conflict are not resolved
51 TagCollection tc = new TagCollection();
52 tc.add(new Tag("building", "school"));
53 tc.add(new Tag("building", "garage"));
54 TagConflictResolutionUtil.applyAutomaticTagConflictResolution(tc);
55 assertEquals(newHashSet("school", "garage"), new HashSet<>(tc.getValues("building")));
56
57 // Check US Tiger tag conflict resolution
58 tc = new TagCollection();
59 tc.add(new Tag("tiger:test", "A:B"));
60 tc.add(new Tag("tiger:test", "A"));
61 TagConflictResolutionUtil.applyAutomaticTagConflictResolution(tc);
62 assertEquals(newHashSet("A:B"), new HashSet<>(tc.getValues("tiger:test")));
63
64 // Check FR:cadastre source tag conflict resolution (most common values from taginfo except last one without accentuated characters)
65 tc = new TagCollection();
66 // CHECKSTYLE.OFF: LineLength
67 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2007"));
68 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2008"));
69 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2009"));
70 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2010"));
71 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2008"));
72 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2009"));
73 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2010"));
74 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2011"));
75 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2012"));
76 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2013"));
77 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre. Mise à jour : 2014"));
78 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Impôts - Cadas. Mise à jour : 2010"));
79 tc.add(new Tag("source", "extraction vectorielle v1 cadastre-dgi-fr source : Direction Générale des Impôts - Cadas. Mise à jour : 2010"));
80 tc.add(new Tag("source", "Direction Générale des Finances Publiques - Cadastre ; mise à jour : 2010"));
81 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Finances Publiques - Cadastre. Mise à jour : 2013"));
82 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Générale des Finances Publiques - Cadastre. Mise à jour : 2014"));
83 tc.add(new Tag("source", "cadastre-dgi-fr source : Direction Generale des Finances Publiques - Cadastre. Mise a jour : 2015"));
84 // CHECKSTYLE.ON: LineLength
85 Tag otherSource = new Tag("source", "other");
86 tc.add(otherSource); // other source should prevent resolution
87 TagConflictResolutionUtil.applyAutomaticTagConflictResolution(tc);
88 assertEquals(18, tc.getValues("source").size());
89 tc.remove(otherSource);
90 TagConflictResolutionUtil.applyAutomaticTagConflictResolution(tc);
91 assertEquals(newHashSet("cadastre-dgi-fr source : Direction Generale des Finances Publiques - Cadastre. Mise a jour : 2015"),
92 new HashSet<>(tc.getValues("source")));
93
94 // Check CA:canvec source tag conflict resolution
95 tc = new TagCollection();
96 tc.add(new Tag("source", "CanVec_Import_2009"));
97 tc.add(new Tag("source", "CanVec 4.0 - NRCan"));
98 tc.add(new Tag("source", "CanVec 6.0 - NRCan"));
99 tc.add(new Tag("source", "NRCan-CanVec-7.0"));
100 tc.add(new Tag("source", "NRCan-CanVec-8.0"));
101 tc.add(new Tag("source", "NRCan-CanVec-10.0"));
102 tc.add(new Tag("source", "NRCan-CanVec-12.0"));
103 TagConflictResolutionUtil.applyAutomaticTagConflictResolution(tc);
104 assertEquals(newHashSet("NRCan-CanVec-12.0"), new HashSet<>(tc.getValues("source")));
105 }
106
107 /**
108 * Unit tests of {@link AutomaticCombine} class.
109 */
110 static class AutomaticCombineTest {
111
112 /**
113 * Return AutomaticCombine instantiated with the two possible constructors.
114 * @param ac a model for the constructed object.
115 * @return AutomaticCombine object constructed with the two different constructors.
116 */
117 private static List<AutomaticCombine> differentlyConstructed(AutomaticCombine ac) {
118 AutomaticCombine fullyConstructed = new AutomaticCombine(ac.key, ac.description, ac.isRegex, ac.separator, ac.sort);
119 AutomaticCombine defaultConstructed = new AutomaticCombine();
120 defaultConstructed.key = ac.key;
121 defaultConstructed.description = ac.description;
122 defaultConstructed.isRegex = ac.isRegex;
123 defaultConstructed.separator = ac.separator;
124 defaultConstructed.sort = ac.sort;
125 return Arrays.asList(defaultConstructed, fullyConstructed);
126 }
127
128 /**
129 * Setup test.
130 */
131 @RegisterExtension
132 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
133 public JOSMTestRules test = new JOSMTestRules();
134
135 /**
136 * Unit test of {@link AutomaticCombine#matchesKey} with empty key.
137 */
138 @Test
139 void testMatchesKeyEmptyKey() {
140 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine("", "random description", true, ";", null))) {
141 assertFalse(resolver.matchesKey("a"));
142 assertTrue(resolver.matchesKey(""));
143 }
144 }
145
146 /**
147 * Unit test of {@link AutomaticCombine#matchesKey} when regex not used.
148 */
149 @Test
150 void testMatchesKeyNotRegex() {
151 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine(
152 "keyname", "random description", false, "|", null))) {
153 assertFalse(resolver.matchesKey("key"));
154 assertFalse(resolver.matchesKey("keyname2"));
155 assertFalse(resolver.matchesKey("name"));
156 assertFalse(resolver.matchesKey("key.*("));
157 assertTrue(resolver.matchesKey("keyname"));
158 }
159 }
160
161 /**
162 * Unit test of {@link AutomaticCombine#matchesKey} when regex used.
163 */
164 @Test
165 void testMatchesKeyRegex() {
166 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine("test[45].*", "", true, ";", "Integer"))) {
167 assertFalse(resolver.matchesKey("key"));
168 assertFalse(resolver.matchesKey("test[45].*"));
169 assertTrue(resolver.matchesKey("test400 !"));
170 }
171 }
172
173 /**
174 * Unit test of {@link AutomaticCombine} with invalid regex.
175 */
176 @Test
177 void testInvalidRegex() {
178 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine("invalidregex.(]", "", false, ";", null))) {
179 // Should not raise exception if the resolver.isRexEx == false:
180 assertTrue(resolver.matchesKey("invalidregex.(]"));
181 }
182
183 // Should not raise exception if isRexEx, invalid regex but only constructed:
184 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine("invalidregex.(]", "", true, ";", null))) {
185 assertTrue(resolver.isRegex);
186 }
187 }
188
189 /**
190 * Unit test of {@link AutomaticCombine} with invalid regex.
191 */
192 @Test
193 void testInvalidRegexExceptionDefaultConstructed() {
194 AutomaticCombine resolver = new AutomaticCombine("AB.(]", "", true, ";", null);
195 assertThrows(PatternSyntaxException.class, () -> resolver.matchesKey("AB"));
196 }
197
198 /**
199 * Unit test of {@link AutomaticCombine} with invalid regex.
200 */
201 @Test
202 void testInvalidRegexExceptionFullyConstructed() {
203 AutomaticCombine resolver = new AutomaticCombine();
204 resolver.key = "AB.(]";
205 resolver.isRegex = true;
206 assertThrows(PatternSyntaxException.class, () -> resolver.matchesKey("AB"));
207 }
208
209 /**
210 * Unit test of {@link AutomaticCombine#resolve}.
211 */
212 @Test
213 void testResolve() {
214 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine("random", "", true, "|", "String"))) {
215 assertEquals(resolver.resolve(newHashSet("value1", "value2")), "value1|value2");
216 assertEquals(resolver.resolve(newHashSet("3|1", "4|2|1", "6|05", "3;1")), "05|1|2|3|3;1|4|6");
217 }
218
219 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine("test[45].*", "", true, ";", "Integer"))) {
220 assertEquals(resolver.resolve(newHashSet("1254545;95;24", "25;24;3")), "3;24;25;95;1254545");
221 }
222
223 for (AutomaticCombine resolver: differentlyConstructed(new AutomaticCombine("AB", "", true, ";", null))) {
224 String resolution = resolver.resolve(newHashSet("3;x;1", "4;x"));
225 assertTrue(resolution.equals("3;x;1;4") || resolution.equals("4;x;3;1"));
226 }
227 }
228 }
229
230 /**
231 * Unit tests of {@link AutomaticChoice} class.
232 */
233 static class AutomaticChoiceTest {
234 /**
235 * Setup test.
236 */
237 @RegisterExtension
238 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
239 public JOSMTestRules test = new JOSMTestRules();
240
241 /**
242
243 * Return AutomaticCombine instantiated with the two possible constructors.
244 * @param ac a model for the constructed object.
245 * @return AutomaticCombine object constructed with the two different constructors.
246 */
247 private static List<AutomaticChoice> differentlyConstructed(AutomaticChoice ac) {
248 AutomaticChoice fullyConstructed = new AutomaticChoice(ac.key, ac.group, ac.description, ac.isRegex, ac.value, ac.score);
249 AutomaticChoice defaultConstructed = new AutomaticChoice();
250 defaultConstructed.key = ac.key;
251 defaultConstructed.group = ac.group;
252 defaultConstructed.description = ac.description;
253 defaultConstructed.isRegex = ac.isRegex;
254 defaultConstructed.value = ac.value;
255 defaultConstructed.score = ac.score;
256 return Arrays.asList(defaultConstructed, fullyConstructed);
257 }
258
259 /**
260 * Unit test of {@link AutomaticChoice#matchesValue}.
261 */
262 @Test
263 void testMatchesValue() {
264 for (AutomaticChoice resolver: differentlyConstructed(new AutomaticChoice(
265 "random key", "random group", "random description", false, ".*valueToMatch", "Score$0\\1"))) {
266 assertTrue(resolver.matchesValue(".*valueToMatch"));
267 assertFalse(resolver.matchesValue(".*valueToMatch.*"));
268 assertFalse(resolver.matchesValue("test"));
269 assertFalse(resolver.matchesValue(""));
270 }
271 for (AutomaticChoice resolver: differentlyConstructed(new AutomaticChoice(
272 "", "", "", true, "test([ab].*)", "ok $1"))) {
273 assertTrue(resolver.matchesValue("testa"));
274 assertTrue(resolver.matchesValue("testb129"));
275 assertFalse(resolver.matchesValue("test[ab].*"));
276 assertFalse(resolver.matchesValue("test"));
277 assertFalse(resolver.matchesValue(""));
278 }
279 }
280
281 /**
282 * Unit test of {@link AutomaticChoice#computeScoreFromValue}.
283 */
284 @Test
285 void testComputeScoreFromValue() {
286 for (AutomaticChoice resolver: differentlyConstructed(new AutomaticChoice(
287 "random key", "random group", "random description", false, ".*valueToMatch", "Score$0\\1"))) {
288 assertEquals(resolver.computeScoreFromValue(".*valueToMatch"), "Score$0\\1");
289 }
290 for (AutomaticChoice resolver: differentlyConstructed(new AutomaticChoice(
291 "", "", "", true, "test([ab].*)", "ok $1"))) {
292 assertEquals(resolver.computeScoreFromValue("testa"), "ok a");
293 assertEquals(resolver.computeScoreFromValue("testb129"), "ok b129");
294 }
295 }
296
297 /**
298 * Unit test of {@link AutomaticChoice} when invalid regex is used.
299 */
300 @Test
301 void testInvalidRegex() {
302 for (AutomaticChoice resolver: differentlyConstructed(new AutomaticChoice(
303 "k", "g", "", false, "invalidregex.(]", "InvalidScore$0\\1$-4"))) {
304 // Should not raise exception if the resolver.isRexEx == false:
305 assertTrue(resolver.matchesValue("invalidregex.(]"));
306 assertFalse(resolver.matchesValue("test"));
307 assertEquals(resolver.computeScoreFromValue("invalidregex.(]"), "InvalidScore$0\\1$-4");
308 }
309 // Should not raise exception if isRexEx, invalid regex but only constructed:
310 for (AutomaticChoice resolver: differentlyConstructed(new AutomaticChoice(
311 "k", "g", "", true, "invalidregex.(]", "InvalidScore$0\\1$-4"))) {
312 assertTrue(resolver.isRegex);
313 }
314 }
315
316 /**
317 * Unit test of {@link AutomaticChoice} when invalid regex is used.
318 */
319 @Test
320 void testMatchesValueInvalidRegex() {
321 AutomaticChoice resolver = new AutomaticChoice("k", "g", "", true, "invalidregex.(]", "InvalidScore$0\\1$-4");
322 assertThrows(PatternSyntaxException.class, () -> resolver.matchesValue("test"));
323 }
324
325 /**
326 * Unit test of {@link AutomaticChoice} when invalid regex is used.
327 */
328 @Test
329 void testComputeScoreFromValueInvalidRegex() {
330 AutomaticChoice resolver = new AutomaticChoice("k", "g", "", true, "invalidregex.(]", "valid");
331 assertThrows(PatternSyntaxException.class, () -> resolver.computeScoreFromValue("valid"));
332 }
333
334 /**
335 * Unit test of {@link AutomaticChoice} when invalid score replacement is used.
336 */
337 @Test
338 void testComputeScoreFromValueInvalidReplacement() {
339 AutomaticChoice resolver = new AutomaticChoice("k", "g", "", true, "valid", "InvalidScore$0\\1$-4");
340 boolean exceptionThrown = false;
341 try {
342 resolver.computeScoreFromValue("valid");
343 } catch (Exception e) {
344 exceptionThrown = true;
345 }
346 assertTrue(exceptionThrown);
347 }
348 }
349
350 /**
351 * Unit tests of {@link AutomaticChoiceGroup} class.
352 */
353 static class AutomaticChoiceGroupTest {
354 /**
355 * Setup test.
356 */
357 @RegisterExtension
358 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
359 public JOSMTestRules test = new JOSMTestRules();
360
361 AutomaticChoice choiceKey1Group1 = new AutomaticChoice("Key1", "Group1", "", false, "value1", "score1");
362 AutomaticChoice choiceKey1Group1bis = new AutomaticChoice("Key1", "Group1", "", false, "value2", "score2");
363 AutomaticChoice choiceKey1Group2 = new AutomaticChoice("Key1", "Group2", "", false, "value1", "score1");
364 AutomaticChoice choiceKey1Group2bis = new AutomaticChoice("Key1", "Group2", "", false, "value2", "score2");
365 AutomaticChoice choiceKey2Group1 = new AutomaticChoice("test[45].*", "Group1", "", true, "value1", "score1");
366 AutomaticChoice choiceKey2Group1bis = new AutomaticChoice("test[45].*", "Group1", "", true, "value2", "score2");
367 AutomaticChoice choiceKey2Group2 = new AutomaticChoice("test[45].*", "Group2", "", true, "value1(.*)", "$1");
368 AutomaticChoice choiceKey2Group2bis = new AutomaticChoice("test[45].*", "Group2", "", true, "value2(.*)", "$1");
369 AutomaticChoice choiceEmpty = new AutomaticChoice();
370
371 /**
372 * Unit test of {@link AutomaticChoiceGroup#groupChoices}.
373 */
374 @Test
375 void testGroupChoices() {
376 Collection<AutomaticChoiceGroup> groups = AutomaticChoiceGroup.groupChoices(Arrays.asList(choiceKey1Group1, choiceKey1Group2));
377 assertEquals(2, groups.size());
378
379 groups = AutomaticChoiceGroup.groupChoices(Arrays.asList(
380 choiceKey1Group1, choiceKey1Group2, choiceKey2Group1, choiceKey2Group2, choiceEmpty));
381 assertEquals(5, groups.size());
382
383 groups = AutomaticChoiceGroup.groupChoices(Arrays.asList(choiceKey1Group1, choiceKey1Group1bis));
384 assertEquals(1, groups.size());
385 AutomaticChoiceGroup group1 = groups.iterator().next();
386 assertEquals(group1.key, choiceKey1Group1.key);
387 assertEquals(group1.group, choiceKey1Group1.group);
388 assertEquals(new HashSet<>(group1.choices), new HashSet<>(Arrays.asList(choiceKey1Group1, choiceKey1Group1bis)));
389
390 groups = AutomaticChoiceGroup.groupChoices(Arrays.asList(
391 choiceKey1Group1, choiceKey1Group1bis, choiceKey1Group2, choiceKey1Group2bis,
392 choiceKey2Group1, choiceKey2Group1bis, choiceKey2Group2, choiceKey2Group2bis));
393 assertEquals(4, groups.size());
394 for (AutomaticChoiceGroup group: groups) {
395 for (AutomaticChoice choice: group.choices) {
396 assertEquals(choice.key, group.key);
397 assertEquals(choice.group, group.group);
398 assertEquals(choice.isRegex, group.isRegex);
399 }
400 }
401 }
402
403 /**
404 * Unit test of {@link AutomaticChoiceGroup#matchesKey}.
405 */
406 @Test
407 void testMatchesKey() {
408 AutomaticChoiceGroup group = new AutomaticChoiceGroup(
409 choiceKey1Group1.key, choiceKey1Group1.group, choiceKey1Group1.isRegex,
410 Arrays.asList(choiceKey1Group1, choiceKey1Group1bis));
411 assertFalse(group.matchesKey("key"));
412 assertFalse(group.matchesKey("keyname2"));
413 assertFalse(group.matchesKey("name"));
414 assertFalse(group.matchesKey("key.*("));
415 assertTrue(group.matchesKey(choiceKey1Group1.key));
416
417 group = new AutomaticChoiceGroup(
418 choiceKey2Group1.key, choiceKey2Group2.group, choiceKey2Group2.isRegex,
419 Arrays.asList(choiceKey2Group2, choiceKey2Group2bis));
420 assertFalse(group.matchesKey("key"));
421 assertFalse(group.matchesKey("test[45].*"));
422 assertTrue(group.matchesKey("test400 !"));
423 }
424
425 /**
426 * Unit test of {@link AutomaticChoiceGroup#resolve}.
427 */
428 @Test
429 void testResolve() {
430 AutomaticChoiceGroup group = new AutomaticChoiceGroup(
431 choiceKey1Group1.key, choiceKey1Group1.group, choiceKey1Group1.isRegex,
432 Arrays.asList(choiceKey1Group1, choiceKey1Group1bis));
433 assertEquals(group.resolve(newHashSet(choiceKey1Group1.value)), choiceKey1Group1.value);
434 assertEquals(group.resolve(newHashSet(choiceKey1Group1.value, choiceKey1Group1bis.value)), choiceKey1Group1bis.value);
435 assertNull(group.resolve(newHashSet("random", choiceKey1Group1.value, choiceKey1Group1bis.value)));
436
437 group = new AutomaticChoiceGroup(
438 choiceKey2Group1.key, choiceKey2Group2.group, choiceKey2Group2.isRegex,
439 Arrays.asList(choiceKey2Group2, choiceKey2Group2bis));
440 assertEquals(group.resolve(newHashSet("value1")), "value1");
441 assertEquals(group.resolve(newHashSet("value1Z", "value2A")), "value1Z");
442 assertEquals(group.resolve(newHashSet("value1A", "value2Z")), "value2Z");
443 assertNull(group.resolve(newHashSet("value1Z", "value2A", "other not matched value")));
444 }
445 }
446}
Note: See TracBrowser for help on using the repository browser.