Ticket #14374: TagConflictResolutionUtilTest.java

File TagConflictResolutionUtilTest.java, 18.9 KB (added by Tyndare, 9 years ago)

Unit test

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