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.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 |
|
---|
11 | import org.junit.Rule;
|
---|
12 | import org.junit.Test;
|
---|
13 | import org.openstreetmap.josm.TestUtils;
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
19 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.io.OsmReader;
|
---|
21 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
22 |
|
---|
23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * JUnit Test of "Duplicate way" validation test.
|
---|
27 | */
|
---|
28 | public class DuplicateWayTest {
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Setup test by initializing JOSM preferences and projection.
|
---|
32 | */
|
---|
33 | @Rule
|
---|
34 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
35 | public JOSMTestRules test = new JOSMTestRules();
|
---|
36 |
|
---|
37 | private static final DuplicateWay TEST = new DuplicateWay();
|
---|
38 |
|
---|
39 | private static void doTest(int code) {
|
---|
40 | doTest(code, "");
|
---|
41 | }
|
---|
42 |
|
---|
43 | private static void doTest(int code, String tags) {
|
---|
44 | doTest(code, tags, tags, true);
|
---|
45 | }
|
---|
46 |
|
---|
47 | private static void doTest(int code, String tags1, String tags2, boolean fixable) {
|
---|
48 | performTest(code, buildDataSet(tags1, tags2), fixable);
|
---|
49 | }
|
---|
50 |
|
---|
51 | private static void performTest(int code, DataSet ds, boolean fixable) {
|
---|
52 | TEST.startTest(NullProgressMonitor.INSTANCE);
|
---|
53 | TEST.visit(ds.allPrimitives());
|
---|
54 | TEST.endTest();
|
---|
55 |
|
---|
56 | assertEquals(1, TEST.getErrors().size());
|
---|
57 | TestError error = TEST.getErrors().iterator().next();
|
---|
58 | assertEquals(code, error.getCode());
|
---|
59 | assertEquals(fixable, error.isFixable());
|
---|
60 | }
|
---|
61 |
|
---|
62 | private static DataSet buildDataSet(String tags1, String tags2) {
|
---|
63 | DataSet ds = new DataSet();
|
---|
64 |
|
---|
65 | Node a = new Node(new LatLon(10.0, 5.0));
|
---|
66 | Node b = new Node(new LatLon(10.0, 6.0));
|
---|
67 | ds.addPrimitive(a);
|
---|
68 | ds.addPrimitive(b);
|
---|
69 | ds.addPrimitive(TestUtils.newWay(tags1, a, b));
|
---|
70 | ds.addPrimitive(TestUtils.newWay(tags2, a, b));
|
---|
71 | return ds;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Test of "Duplicate way" validation test - no tags.
|
---|
76 | */
|
---|
77 | @Test
|
---|
78 | public void testDuplicateWayNoTags() {
|
---|
79 | doTest(DuplicateWay.DUPLICATE_WAY);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Test of "Duplicate way" validation test - same tags.
|
---|
84 | */
|
---|
85 | @Test
|
---|
86 | public void testDuplicateWaySameTags() {
|
---|
87 | doTest(DuplicateWay.DUPLICATE_WAY, "highway=motorway");
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Test of "Duplicate way" validation test - different tags.
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | public void testDuplicateWayDifferentTags() {
|
---|
95 | doTest(DuplicateWay.SAME_WAY, "highway=motorway", "highway=trunk", false);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14891">Bug #14891</a>.
|
---|
100 | * @throws Exception if an error occurs
|
---|
101 | */
|
---|
102 | @Test
|
---|
103 | public void testFixError() throws Exception {
|
---|
104 | DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "duplicate-ways.osm")), null);
|
---|
105 | TEST.startTest(NullProgressMonitor.INSTANCE);
|
---|
106 | TEST.visit(ds.allPrimitives());
|
---|
107 | TEST.endTest();
|
---|
108 |
|
---|
109 | assertEquals(2, TEST.getErrors().size());
|
---|
110 | for (TestError error: TEST.getErrors()) {
|
---|
111 | error = TEST.getErrors().iterator().next();
|
---|
112 | assertTrue(error.isFixable());
|
---|
113 | Command fix = error.getFix();
|
---|
114 | assertNotNull(fix);
|
---|
115 | }
|
---|
116 | for (TestError error: TEST.getErrors()) {
|
---|
117 | error.getFix().executeCommand();
|
---|
118 | }
|
---|
119 | TEST.startTest(NullProgressMonitor.INSTANCE);
|
---|
120 | TEST.visit(ds.allPrimitives());
|
---|
121 | TEST.endTest();
|
---|
122 | assertTrue(TEST.getErrors().isEmpty());
|
---|
123 |
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|