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 | import static org.junit.Assert.fail;
|
---|
7 |
|
---|
8 | import java.awt.geom.Area;
|
---|
9 | import java.lang.reflect.Field;
|
---|
10 | import java.lang.reflect.Method;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Collection;
|
---|
14 | import java.util.Collections;
|
---|
15 |
|
---|
16 | import org.junit.BeforeClass;
|
---|
17 | import org.junit.Test;
|
---|
18 | import org.openstreetmap.josm.JOSMFixture;
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
22 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
23 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
24 | import org.openstreetmap.josm.data.osm.Node;
|
---|
25 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
28 | import org.openstreetmap.josm.tools.GeoPropertyIndex;
|
---|
29 | import org.openstreetmap.josm.tools.GeoPropertyIndex.GeoProperty;
|
---|
30 | import org.openstreetmap.josm.tools.Geometry;
|
---|
31 | import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
|
---|
32 | import org.openstreetmap.josm.tools.Utils;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unit tests for class {@link CreateCircleAction}.
|
---|
36 | */
|
---|
37 | public final class CreateCircleActionTest {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Setup test.
|
---|
41 | */
|
---|
42 | @BeforeClass
|
---|
43 | public static void setUp() {
|
---|
44 | JOSMFixture.createUnitTestFixture().init(true);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * FIXME: Conveniance method to prevent Selection Change events.
|
---|
49 | * A more proper way should be to define a TestingDataSet class with limited
|
---|
50 | * functionalities, but DataSet is declare as final (due to Cloneable interface).
|
---|
51 | *
|
---|
52 | * I don't know why, but in other tests there are no problem to add selected primitives
|
---|
53 | * but in this case there is a problem with an even listener of selection change.
|
---|
54 | * @param p primitive
|
---|
55 | * @param ds data set
|
---|
56 | */
|
---|
57 | public void addSelected(OsmPrimitive p, DataSet ds) {
|
---|
58 | try {
|
---|
59 | Method method = ds.getClass()
|
---|
60 | .getDeclaredMethod("addSelected",
|
---|
61 | new Class<?>[] {Collection.class, boolean.class});
|
---|
62 | Utils.setObjectsAccessible(method);
|
---|
63 | method.invoke(ds, Collections.singleton(p), false);
|
---|
64 | } catch (ReflectiveOperationException e) {
|
---|
65 | e.printStackTrace();
|
---|
66 | fail("Can't add OsmPrimitive to dataset: " + e.getMessage());
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Test case: When Create Circle action is performed with a single way selected,
|
---|
72 | * circle direction must equals way direction.
|
---|
73 | * see #7421
|
---|
74 | */
|
---|
75 | @Test
|
---|
76 | public void test7421_0() {
|
---|
77 | DataSet dataSet = new DataSet();
|
---|
78 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
79 |
|
---|
80 | Node n1 = new Node(new EastNorth(0, 0));
|
---|
81 | Node n2 = new Node(new EastNorth(-1, 1));
|
---|
82 | Node n3 = new Node(new EastNorth(1, 1));
|
---|
83 | dataSet.addPrimitive(n1);
|
---|
84 | dataSet.addPrimitive(n2);
|
---|
85 | dataSet.addPrimitive(n3);
|
---|
86 |
|
---|
87 | Way w = new Way(); // Way is Clockwize
|
---|
88 | w.setNodes(Arrays.asList(new Node[] {n1, n2, n3}));
|
---|
89 | dataSet.addPrimitive(w);
|
---|
90 |
|
---|
91 | addSelected(w, dataSet);
|
---|
92 |
|
---|
93 | CreateCircleAction action = new CreateCircleAction();
|
---|
94 | action.setEnabled(true);
|
---|
95 | try {
|
---|
96 | Main.main.addLayer(layer);
|
---|
97 | action.actionPerformed(null);
|
---|
98 | } finally {
|
---|
99 | // Ensure we clean the place before leaving, even if test fails.
|
---|
100 | Main.main.removeLayer(layer);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // Expected result: Dataset contain one closed way, clockwise
|
---|
104 | Collection<Way> resultingWays = dataSet.getWays();
|
---|
105 | assertSame(String.format("Expect one way after perform action. %d found", resultingWays.size()),
|
---|
106 | resultingWays.size(), 1);
|
---|
107 | Way resultingWay = resultingWays.iterator().next();
|
---|
108 | assertTrue("Resulting way is not closed",
|
---|
109 | resultingWay.isClosed());
|
---|
110 | assertTrue("Found anti-clockwize circle while way was clockwize",
|
---|
111 | Geometry.isClockwise(resultingWay));
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Mock left/right hand traffic database with constant traffic hand
|
---|
116 | */
|
---|
117 | private static class ConstantTrafficHand implements GeoProperty<Boolean> {
|
---|
118 | boolean isLeft;
|
---|
119 |
|
---|
120 | ConstantTrafficHand(boolean isLeft) {
|
---|
121 | this.isLeft = isLeft;
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public Boolean get(LatLon ll) {
|
---|
126 | return isLeft;
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public Boolean get(BBox box) {
|
---|
131 | return isLeft;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Test case: When Create Circle action is performed with nodes, resulting
|
---|
137 | * circle direction depend on traffic hand. Simulate a left hand traffic.
|
---|
138 | * see #7421
|
---|
139 | */
|
---|
140 | @Test
|
---|
141 | public void test7421_1() {
|
---|
142 | DataSet dataSet = new DataSet();
|
---|
143 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
144 |
|
---|
145 | Node n1 = new Node(new EastNorth(0, 0));
|
---|
146 | Node n2 = new Node(new EastNorth(-1, 1));
|
---|
147 | Node n3 = new Node(new EastNorth(1, 1));
|
---|
148 | dataSet.addPrimitive(n1);
|
---|
149 | dataSet.addPrimitive(n2);
|
---|
150 | dataSet.addPrimitive(n3);
|
---|
151 |
|
---|
152 | addSelected(n1, dataSet);
|
---|
153 | addSelected(n2, dataSet);
|
---|
154 | addSelected(n3, dataSet);
|
---|
155 |
|
---|
156 | // Mock left/right hand traffic database
|
---|
157 | try {
|
---|
158 | Field leftHandTrafficPolygons = RightAndLefthandTraffic.class
|
---|
159 | .getDeclaredField("leftHandTrafficPolygons");
|
---|
160 | Field rlCache = RightAndLefthandTraffic.class.getDeclaredField("rlCache");
|
---|
161 | Utils.setObjectsAccessible(leftHandTrafficPolygons, rlCache);
|
---|
162 | leftHandTrafficPolygons.set(null, new ArrayList<Area>());
|
---|
163 | rlCache.set(null, new GeoPropertyIndex<>(new ConstantTrafficHand(true), 24));
|
---|
164 | } catch (ReflectiveOperationException e) {
|
---|
165 | e.printStackTrace();
|
---|
166 | fail("Impossible to mock left/right hand database: " + e.getMessage());
|
---|
167 | }
|
---|
168 |
|
---|
169 | CreateCircleAction action = new CreateCircleAction();
|
---|
170 | action.setEnabled(true);
|
---|
171 | try {
|
---|
172 | Main.main.addLayer(layer);
|
---|
173 | action.actionPerformed(null);
|
---|
174 | } finally {
|
---|
175 | // Ensure we clean the place before leaving, even if test fails.
|
---|
176 | Main.main.removeLayer(layer);
|
---|
177 | }
|
---|
178 |
|
---|
179 | // Expected result: Dataset contain one closed way, clockwise
|
---|
180 | Collection<Way> resultingWays = dataSet.getWays();
|
---|
181 | assertSame(String.format("Expect one way after perform action. %d found", resultingWays.size()),
|
---|
182 | resultingWays.size(), 1);
|
---|
183 | Way resultingWay = resultingWays.iterator().next();
|
---|
184 | assertTrue("Resulting way is not closed",
|
---|
185 | resultingWay.isClosed());
|
---|
186 | assertTrue("Found anti-clockwise way while traffic is left hand.",
|
---|
187 | Geometry.isClockwise(resultingWay));
|
---|
188 | }
|
---|
189 | }
|
---|