source: josm/trunk/test/unit/org/openstreetmap/josm/actions/OrthogonalizeActionTest.java@ 14219

Last change on this file since 14219 was 12656, checked in by Don-vip, 7 years ago

see #15182 - move SearchCompiler from actions.search to data.osm.search

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