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.assertTrue;
|
---|
6 | import static org.junit.Assert.fail;
|
---|
7 |
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.InputStream;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Set;
|
---|
14 | import java.util.TreeSet;
|
---|
15 | import java.util.function.Function;
|
---|
16 | import java.util.function.Predicate;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.validation.Test;
|
---|
21 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
22 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
23 | import org.openstreetmap.josm.io.OsmReader;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Utilities for validator unit tests.
|
---|
27 | */
|
---|
28 | public final class ValidatorTestUtils {
|
---|
29 |
|
---|
30 | private ValidatorTestUtils() {
|
---|
31 | // Hide default constructor for utilities classes
|
---|
32 | }
|
---|
33 |
|
---|
34 | static <T extends OsmPrimitive> void testSampleFile(String sampleFile,
|
---|
35 | Function<DataSet, Iterable<T>> provider, Predicate<String> namePredicate,
|
---|
36 | Test... tests) throws Exception {
|
---|
37 | try (InputStream is = new FileInputStream(sampleFile)) {
|
---|
38 | for (T t: provider.apply(OsmReader.parseDataSet(is, null))) {
|
---|
39 | String name = DefaultNameFormatter.getInstance().format(t);
|
---|
40 | String codes = t.get("josm_error_codes");
|
---|
41 | if (codes != null) {
|
---|
42 | List<TestError> errors = new ArrayList<>();
|
---|
43 | for (Test test : tests) {
|
---|
44 | test.initialize();
|
---|
45 | test.startTest(null);
|
---|
46 | test.visit(Collections.singleton(t));
|
---|
47 | test.endTest();
|
---|
48 | errors.addAll(test.getErrors());
|
---|
49 | }
|
---|
50 | Set<Integer> expectedCodes = new TreeSet<>();
|
---|
51 | if (!"none".equals(codes)) {
|
---|
52 | for (String code : codes.split(",")) {
|
---|
53 | expectedCodes.add(Integer.parseInt(code));
|
---|
54 | }
|
---|
55 | }
|
---|
56 | Set<Integer> actualCodes = new TreeSet<>();
|
---|
57 | for (TestError error : errors) {
|
---|
58 | Integer code = error.getCode();
|
---|
59 | assertTrue(name + " does not expect JOSM error code " + code + ": " + error.getDescription(),
|
---|
60 | expectedCodes.contains(code));
|
---|
61 | actualCodes.add(code);
|
---|
62 | }
|
---|
63 | assertEquals(name + " " + expectedCodes + " => " + actualCodes,
|
---|
64 | expectedCodes.size(), actualCodes.size());
|
---|
65 | } else if (t.hasKey("name") && namePredicate != null && namePredicate.test(t.getName())) {
|
---|
66 | fail(name + " lacks josm_error_codes tag");
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|