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

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

improve unit test coverage of utilities classes thanks to https://trajano.github.io/commons-testing

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