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