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