source: josm/trunk/test/unit/org/openstreetmap/josm/actions/CreateCircleActionTest.java@ 8513

Last change on this file since 8513 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

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