source: josm/trunk/test/unit/org/openstreetmap/josm/actions/AlignInCircleActionTest.java@ 17394

Last change on this file since 17394 was 17394, checked in by GerdP, 3 years ago

fix checkstyle issue

File size: 7.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNotNull;
7import static org.junit.jupiter.api.Assertions.assertNull;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9
10import java.nio.file.Files;
11import java.nio.file.Paths;
12import java.util.Set;
13import java.util.stream.Collectors;
14
15import org.junit.jupiter.api.Test;
16import org.junit.jupiter.api.extension.RegisterExtension;
17import org.openstreetmap.josm.TestUtils;
18import org.openstreetmap.josm.actions.AlignInCircleAction.InvalidSelection;
19import org.openstreetmap.josm.command.Command;
20import org.openstreetmap.josm.data.osm.DataSet;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.data.osm.Way;
24import org.openstreetmap.josm.io.OsmReader;
25import org.openstreetmap.josm.testutils.JOSMTestRules;
26import org.opentest4j.AssertionFailedError;
27
28import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29
30/**
31 * Unit tests for class {@link AlignInLineAction}.
32 */
33final class AlignInCircleActionTest {
34
35 /**
36 * Setup test.
37 */
38 @RegisterExtension
39 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
40 public JOSMTestRules test = new JOSMTestRules().projection();
41
42
43 /**
44 * Test case: way with several nodes selected
45 * @throws Exception if an error occurs
46 */
47 @Test
48 void testWaySelected() throws Exception {
49 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleBefore.osm")), null);
50 DataSet ds2 = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleAfter1.osm")), null);
51
52 Way roundabout = null;
53 for (Way w : ds.getWays()) {
54 if ("roundabout".equals(w.get("junction"))) {
55 roundabout = w;
56 break;
57 }
58 }
59 assertNotNull(roundabout);
60 if (roundabout != null) {
61 ds.setSelected(roundabout);
62 Command c = AlignInCircleAction.buildCommand(ds);
63 c.executeCommand();
64 Way expected = (Way) ds2.getPrimitiveById(roundabout);
65 assertNotNull(expected);
66 assertEquals(expected, roundabout);
67 assertEquals(expected.getNodesCount(), roundabout.getNodesCount());
68 for (Node n1 : roundabout.getNodes()) {
69 Node n2 = (Node) ds2.getPrimitiveById(n1);
70 assertEquals(n1.lat(), n2.lat(), 1e-5);
71 assertEquals(n1.lon(), n2.lon(), 1e-5);
72 }
73 }
74 }
75
76 /**
77 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/20041">Bug #20041</a>.
78 * Don't create move commands when no node is visibly moved.
79 * @throws Exception if an error occurs
80 */
81 @Test
82 void testTicket20041() throws Exception {
83 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleAfter1.osm")), null);
84
85 Way roundabout = null;
86 for (Way w : ds.getWays()) {
87 if ("roundabout".equals(w.get("junction"))) {
88 roundabout = w;
89 break;
90 }
91 }
92 assertNotNull(roundabout);
93 if (roundabout != null) {
94 ds.setSelected(roundabout);
95 assertNull(AlignInCircleAction.buildCommand(ds));
96 }
97 }
98
99 /**
100 * Test case: way with several nodes selected
101 * @throws Exception if an error occurs
102 */
103 @Test
104 void testNodesSelected() throws Exception {
105 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleBefore.osm")), null);
106 DataSet ds2 = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleAfter2.osm")), null);
107
108 Way circularWay = null;
109 for (Way w : ds.getWays()) {
110 if ("roundabout".equals(w.get("junction"))) {
111 circularWay = w;
112 break;
113 }
114 }
115 assertNotNull(circularWay);
116 if (circularWay != null) {
117 ds.setSelected(circularWay.getNodes());
118 Command c = AlignInCircleAction.buildCommand(ds);
119 assertNotNull(c);
120 c.executeCommand();
121 Way expected = (Way) ds2.getPrimitiveById(circularWay);
122 assertNotNull(expected);
123 assertEquals(expected, circularWay);
124 assertEquals(expected.getNodesCount(), circularWay.getNodesCount());
125 for (Node n1 : circularWay.getNodes()) {
126 Node n2 = (Node) ds2.getPrimitiveById(n1);
127 assertEquals(n1.lat(), n2.lat(), 1e-5);
128 assertEquals(n1.lon(), n2.lon(), 1e-5);
129 }
130 }
131 }
132
133 /**
134 * Test case: original roundabout was split, two ways selected, they build a closed ring
135 * @throws Exception if an error occurs
136 */
137 @Test
138 void testOpenWaysSelected() throws Exception {
139 DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleTwoWaysBefore.osm")), null);
140 DataSet ds2 = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleTwoWaysAfter.osm")), null);
141
142 Set<Way> junctions = ds.getWays().stream().filter(w -> "roundabout".equals(w.get("junction"))).collect(Collectors.toSet());
143 assertEquals(2, junctions.size());
144 ds.setSelected(junctions);
145 Command c = AlignInCircleAction.buildCommand(ds);
146 assertNotNull(c);
147 c.executeCommand();
148 for (Way way : junctions) {
149 for (Node n1 : way.getNodes()) {
150 Node n2 = (Node) ds2.getPrimitiveById(n1);
151 assertEquals(n1.lat(), n2.lat(), 1e-5);
152 assertEquals(n1.lon(), n2.lon(), 1e-5);
153 }
154 }
155 }
156
157 /**
158 * Various cases of selections in file
159 * @throws Exception if an error occurs
160 */
161 @Test
162 void testSelectionEvaluation() throws Exception {
163 DataSet ds = OsmReader.parseDataSet(
164 Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "alignCircleCases.osm")), null);
165
166 for (int i = 0; i < 80; i++) {
167 final String selVal = Integer.toString(i);
168 Set<OsmPrimitive> sel = ds.allPrimitives().stream().filter(p -> p.hasTag("sel", selVal))
169 .collect(Collectors.toSet());
170 if (sel.isEmpty())
171 continue;
172 ds.setSelected(sel);
173 boolean selValid = sel.stream().noneMatch(p -> p.hasKey("invalid-selection"));
174 try {
175 AlignInCircleAction.buildCommand(ds);
176 assertTrue(selValid, "sel=" + selVal + " is not valid?");
177 } catch (InvalidSelection e) {
178 assertFalse(selValid, "sel=" + selVal + " is not invalid?");
179 } catch (Exception e) {
180 throw new AssertionFailedError("test failed: sel=" + selVal, e);
181 }
182 }
183 }
184}
Note: See TracBrowser for help on using the repository browser.