1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertSame;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 |
|
---|
7 | import java.lang.reflect.Field;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 |
|
---|
11 | import org.junit.jupiter.api.Test;
|
---|
12 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
13 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
20 | import org.openstreetmap.josm.tools.GeoProperty;
|
---|
21 | import org.openstreetmap.josm.tools.GeoPropertyIndex;
|
---|
22 | import org.openstreetmap.josm.tools.Geometry;
|
---|
23 | import org.openstreetmap.josm.tools.ReflectionUtils;
|
---|
24 | import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
|
---|
25 |
|
---|
26 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Unit tests for class {@link CreateCircleAction}.
|
---|
30 | */
|
---|
31 | final class CreateCircleActionTest {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Setup test.
|
---|
35 | */
|
---|
36 | @RegisterExtension
|
---|
37 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
38 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Test case: When Create Circle action is performed with a single way selected,
|
---|
42 | * circle direction must equals way direction.
|
---|
43 | * see #7421
|
---|
44 | * @throws ReflectiveOperationException if an error occurs
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | void testTicket7421case0() throws ReflectiveOperationException {
|
---|
48 | DataSet dataSet = new DataSet();
|
---|
49 |
|
---|
50 | Node n1 = new Node(new EastNorth(0, 0));
|
---|
51 | Node n2 = new Node(new EastNorth(-1, 1));
|
---|
52 | Node n3 = new Node(new EastNorth(1, 1));
|
---|
53 | dataSet.addPrimitive(n1);
|
---|
54 | dataSet.addPrimitive(n2);
|
---|
55 | dataSet.addPrimitive(n3);
|
---|
56 |
|
---|
57 | Way w = new Way(); // Way is Clockwize
|
---|
58 | w.setNodes(Arrays.asList(new Node[] {n1, n2, n3}));
|
---|
59 | dataSet.addPrimitive(w);
|
---|
60 |
|
---|
61 | dataSet.addSelected(w);
|
---|
62 |
|
---|
63 | CreateCircleAction.runOn(dataSet);
|
---|
64 |
|
---|
65 | // Expected result: Dataset contain one closed way, clockwise
|
---|
66 | Collection<Way> resultingWays = dataSet.getWays();
|
---|
67 | assertSame(1, resultingWays.size(), String.format("Expect one way after perform action. %d found", resultingWays.size()));
|
---|
68 | Way resultingWay = resultingWays.iterator().next();
|
---|
69 | assertTrue(resultingWay.isClosed(), "Resulting way is not closed");
|
---|
70 | assertTrue(Geometry.isClockwise(resultingWay), "Found anti-clockwise circle while way was clockwise");
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Mock left/right hand traffic database with constant traffic hand
|
---|
75 | */
|
---|
76 | private static class ConstantTrafficHand implements GeoProperty<Boolean> {
|
---|
77 | boolean isLeft;
|
---|
78 |
|
---|
79 | ConstantTrafficHand(boolean isLeft) {
|
---|
80 | this.isLeft = isLeft;
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public Boolean get(LatLon ll) {
|
---|
85 | return isLeft;
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public Boolean get(BBox box) {
|
---|
90 | return isLeft;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Test case: When Create Circle action is performed with nodes, resulting
|
---|
96 | * circle direction depend on traffic hand. Simulate a left hand traffic.
|
---|
97 | * see #7421
|
---|
98 | * @throws ReflectiveOperationException if an error occurs
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | void testTicket7421case1() throws ReflectiveOperationException {
|
---|
102 | DataSet dataSet = new DataSet();
|
---|
103 |
|
---|
104 | Node n1 = new Node(new EastNorth(0, 0));
|
---|
105 | Node n2 = new Node(new EastNorth(-1, 1));
|
---|
106 | Node n3 = new Node(new EastNorth(1, 1));
|
---|
107 | dataSet.addPrimitive(n1);
|
---|
108 | dataSet.addPrimitive(n2);
|
---|
109 | dataSet.addPrimitive(n3);
|
---|
110 |
|
---|
111 | dataSet.addSelected(n1);
|
---|
112 | dataSet.addSelected(n2);
|
---|
113 | dataSet.addSelected(n3);
|
---|
114 |
|
---|
115 | // Mock left/right hand traffic database
|
---|
116 | Field rlCache = RightAndLefthandTraffic.class.getDeclaredField("rlCache");
|
---|
117 | ReflectionUtils.setObjectsAccessible(rlCache);
|
---|
118 | Object origRlCache = rlCache.get(null);
|
---|
119 | rlCache.set(null, new GeoPropertyIndex<>(new ConstantTrafficHand(true), 24));
|
---|
120 |
|
---|
121 | try {
|
---|
122 | CreateCircleAction.runOn(dataSet);
|
---|
123 |
|
---|
124 | // Expected result: Dataset contain one closed way, clockwise
|
---|
125 | Collection<Way> resultingWays = dataSet.getWays();
|
---|
126 | assertSame(1, resultingWays.size(), String.format("Expect one way after perform action. %d found", resultingWays.size()));
|
---|
127 | Way resultingWay = resultingWays.iterator().next();
|
---|
128 | assertTrue(resultingWay.isClosed(), "Resulting way is not closed");
|
---|
129 | assertTrue(Geometry.isClockwise(resultingWay), "Found anti-clockwise way while traffic is left hand.");
|
---|
130 | } finally {
|
---|
131 | // Restore left/right hand traffic database
|
---|
132 | rlCache.set(null, origRlCache);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|