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

Last change on this file since 11614 was 11614, checked in by Don-vip, 7 years ago

see #14374 - sonar - pmd-unit-tests:UseAssertSameInsteadOfAssertTrue - Use assertSame instead of assertTrue

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