1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
---|
6 |
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.Test;
|
---|
14 | import org.junit.jupiter.api.extension.RegisterExtension;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.actions.OrthogonalizeAction.Direction;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Node;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
24 | import org.openstreetmap.josm.io.OsmReader;
|
---|
25 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
26 | import org.openstreetmap.josm.tools.Geometry;
|
---|
27 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
28 |
|
---|
29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
30 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Unit tests for class {@link OrthogonalizeAction}.
|
---|
34 | */
|
---|
35 | class OrthogonalizeActionTest {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Setup test.
|
---|
39 | */
|
---|
40 | @RegisterExtension
|
---|
41 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
42 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
43 |
|
---|
44 | @Test
|
---|
45 | void testNoSelection() throws Exception {
|
---|
46 | assertThrows(OrthogonalizeAction.InvalidUserInputException.class, () -> performTest("nothing selected"));
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Test
|
---|
50 | void testClosedWay() throws Exception {
|
---|
51 | final DataSet ds = performTest("name=ClosedWay");
|
---|
52 | final Way way = ds.getSelectedWays().iterator().next();
|
---|
53 | assertEquals(new LatLon(8.5388082, 55.7297890), way.getNode(0).getCoor().getRoundedToOsmPrecision());
|
---|
54 | assertEquals(new LatLon(8.5396182, 55.7303980), way.getNode(1).getCoor().getRoundedToOsmPrecision());
|
---|
55 | assertEquals(new LatLon(8.5389933, 55.7312479), way.getNode(2).getCoor().getRoundedToOsmPrecision());
|
---|
56 | assertEquals(new LatLon(8.5381833, 55.7306389), way.getNode(3).getCoor().getRoundedToOsmPrecision());
|
---|
57 | verifyRectangleClockwise(way);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Test
|
---|
61 | void testTwoWaysFormingClosedWay() throws Exception {
|
---|
62 | performTest("name=TwoWaysFormingClosedWay");
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Test
|
---|
66 | void testTwoRingsAtOnce() throws Exception {
|
---|
67 | performTest("name=ClosedWay OR name=TwoWaysFormingClosedWay");
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Test
|
---|
71 | void testClosedWayWithReferenceNodes() throws Exception {
|
---|
72 | final DataSet ds = performTest("name=ClosedWayWithReferenceNodes");
|
---|
73 | final Way way = ds.getSelectedWays().iterator().next();
|
---|
74 | assertEquals(new LatLon(8.5347114, 55.7300067), way.getNode(0).getCoor().getRoundedToOsmPrecision());
|
---|
75 | assertEquals(new LatLon(8.5354772, 55.7306714), way.getNode(1).getCoor().getRoundedToOsmPrecision());
|
---|
76 | assertEquals(new LatLon(8.5348355, 55.7314274), way.getNode(2).getCoor().getRoundedToOsmPrecision());
|
---|
77 | assertEquals(new LatLon(8.5340697, 55.7307626), way.getNode(3).getCoor().getRoundedToOsmPrecision());
|
---|
78 | verifyRectangleClockwise(way);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Test
|
---|
82 | void testFourNodes() throws Exception {
|
---|
83 | final DataSet ds = performTest(
|
---|
84 | "name=NodeToRectify-01", "name=NodeToRectify-02", "name=NodeToRectify-03", "name=NodeToRectify-04");
|
---|
85 | final List<Node> nodes = new ArrayList<>(ds.getSelectedNodes());
|
---|
86 | assertEquals(new LatLon(8.5327354, 55.7298695), nodes.get(0).getCoor().getRoundedToOsmPrecision());
|
---|
87 | assertEquals(new LatLon(8.5335208, 55.7304333), nodes.get(1).getCoor().getRoundedToOsmPrecision());
|
---|
88 | assertEquals(new LatLon(8.5329143, 55.7312973), nodes.get(2).getCoor().getRoundedToOsmPrecision());
|
---|
89 | assertEquals(new LatLon(8.5320550, 55.7306805), nodes.get(3).getCoor().getRoundedToOsmPrecision());
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Tests that {@code OrthogonalizeAction.EN} satisfies utility class criteria.
|
---|
94 | * @throws ReflectiveOperationException if an error occurs
|
---|
95 | */
|
---|
96 | @Test
|
---|
97 | void testUtilityClass() throws ReflectiveOperationException {
|
---|
98 | UtilityClassTestUtil.assertUtilityClassWellDefined(OrthogonalizeAction.EN.class);
|
---|
99 | }
|
---|
100 |
|
---|
101 | DataSet performTest(String... search) throws Exception {
|
---|
102 | try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "orthogonalize.osm"))) {
|
---|
103 | final DataSet ds = OsmReader.parseDataSet(in, null);
|
---|
104 | // TODO: Executing commands depends on active edit layer
|
---|
105 | MainApplication.getLayerManager().addLayer(new OsmDataLayer(ds, "ds", null));
|
---|
106 | for (String s : search) {
|
---|
107 | ds.addSelected(SubclassFilteredCollection.filter(ds.allPrimitives(), SearchCompiler.compile(s)));
|
---|
108 | }
|
---|
109 | OrthogonalizeAction.orthogonalize(ds.getSelected()).executeCommand();
|
---|
110 | return ds;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | void verifyRectangleClockwise(final Way way) {
|
---|
115 | for (int i = 1; i < way.getNodesCount() - 1; i++) {
|
---|
116 | assertEquals(-Math.PI / 2, Geometry.getCornerAngle(
|
---|
117 | way.getNode(i - 1).getEastNorth(), way.getNode(i).getEastNorth(), way.getNode(i + 1).getEastNorth()), 1e-6);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Unit test of {@link Direction} enum.
|
---|
123 | */
|
---|
124 | @Test
|
---|
125 | void testEnumDirection() {
|
---|
126 | TestUtils.superficialEnumCodeCoverage(Direction.class);
|
---|
127 | }
|
---|
128 | }
|
---|