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.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.junit.Assert.fail;
|
---|
8 |
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.InputStream;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Set;
|
---|
15 | import java.util.TreeSet;
|
---|
16 |
|
---|
17 | import org.junit.Rule;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.osm.Node;
|
---|
21 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
22 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
23 | import org.openstreetmap.josm.data.osm.Way;
|
---|
24 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
25 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
26 | import org.openstreetmap.josm.gui.mappaint.ElemStyles;
|
---|
27 | import org.openstreetmap.josm.io.OsmReader;
|
---|
28 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
29 |
|
---|
30 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * JUnit Test of Multipolygon validation test.
|
---|
34 | */
|
---|
35 | public class MultipolygonTestTest {
|
---|
36 |
|
---|
37 | private static final MultipolygonTest MULTIPOLYGON_TEST = new MultipolygonTest();
|
---|
38 | private static final RelationChecker RELATION_TEST = new RelationChecker();
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Setup test.
|
---|
42 | */
|
---|
43 | @Rule
|
---|
44 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
45 | public JOSMTestRules test = new JOSMTestRules().commands();
|
---|
46 |
|
---|
47 | private static Way createUnclosedWay(String tags) {
|
---|
48 | List<Node> nodes = new ArrayList<>();
|
---|
49 | nodes.add(new Node(new LatLon(0, 1)));
|
---|
50 | nodes.add(new Node(new LatLon(0, 2)));
|
---|
51 |
|
---|
52 | Way w = (Way) OsmUtils.createPrimitive("way "+tags);
|
---|
53 | w.setNodes(nodes);
|
---|
54 | return w;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Non-regression test for bug #10469.
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | public void testTicket10469() {
|
---|
62 | MULTIPOLYGON_TEST.initialize();
|
---|
63 | MULTIPOLYGON_TEST.startTest(null);
|
---|
64 |
|
---|
65 | // Erroneous tag
|
---|
66 | Way w = createUnclosedWay("amenity=parking");
|
---|
67 | MULTIPOLYGON_TEST.visit(w);
|
---|
68 | assertTrue(ElemStyles.hasAreaElemStyle(w, false));
|
---|
69 | assertEquals(1, MULTIPOLYGON_TEST.getErrors().size());
|
---|
70 |
|
---|
71 | // Erroneous tag, but managed by another test
|
---|
72 | w = createUnclosedWay("building=yes");
|
---|
73 | MULTIPOLYGON_TEST.visit(w);
|
---|
74 | assertTrue(ElemStyles.hasAreaElemStyle(w, false));
|
---|
75 | assertEquals(1, MULTIPOLYGON_TEST.getErrors().size());
|
---|
76 |
|
---|
77 | // Correct tag, without area style since #10601 (r7603)
|
---|
78 | w = createUnclosedWay("aeroway=taxiway");
|
---|
79 | MULTIPOLYGON_TEST.visit(w);
|
---|
80 | assertFalse(ElemStyles.hasAreaElemStyle(w, false));
|
---|
81 | assertEquals(1, MULTIPOLYGON_TEST.getErrors().size());
|
---|
82 |
|
---|
83 | MULTIPOLYGON_TEST.endTest();
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Test all error cases manually created in multipolygon.osm.
|
---|
88 | * @throws Exception in case of error
|
---|
89 | */
|
---|
90 | @Test
|
---|
91 | public void testMultipolygonFile() throws Exception {
|
---|
92 | try (InputStream is = new FileInputStream("data_nodist/multipolygon.osm")) {
|
---|
93 | for (Relation r : OsmReader.parseDataSet(is, null).getRelations()) {
|
---|
94 | if (r.isMultipolygon()) {
|
---|
95 | String name = DefaultNameFormatter.getInstance().format(r);
|
---|
96 | String codes = r.get("josm_error_codes");
|
---|
97 | if (codes != null) {
|
---|
98 | List<TestError> errors = new ArrayList<>();
|
---|
99 | for (org.openstreetmap.josm.data.validation.Test test : Arrays.asList(MULTIPOLYGON_TEST, RELATION_TEST)) {
|
---|
100 | test.initialize();
|
---|
101 | test.startTest(null);
|
---|
102 | test.visit(r);
|
---|
103 | test.endTest();
|
---|
104 | errors.addAll(test.getErrors());
|
---|
105 | }
|
---|
106 | Set<Integer> expectedCodes = new TreeSet<>();
|
---|
107 | for (String code : codes.split(",")) {
|
---|
108 | expectedCodes.add(Integer.parseInt(code));
|
---|
109 | }
|
---|
110 | Set<Integer> actualCodes = new TreeSet<>();
|
---|
111 | for (TestError error : errors) {
|
---|
112 | Integer code = error.getCode();
|
---|
113 | assertTrue(name + " does not expect JOSM error code " + code + ": " + error.getDescription(),
|
---|
114 | expectedCodes.contains(code));
|
---|
115 | actualCodes.add(code);
|
---|
116 | }
|
---|
117 | assertEquals(name + " " + expectedCodes + " => " + actualCodes,
|
---|
118 | expectedCodes.size(), actualCodes.size());
|
---|
119 | } else if (r.hasKey("name") && (r.getName().startsWith("06") || r.getName().startsWith("07"))) {
|
---|
120 | fail(name + " lacks josm_error_codes tag");
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|