source: josm/trunk/test/unit/org/openstreetmap/josm/command/RotateCommandTest.java@ 17275

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

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.command;
3
4import static org.junit.jupiter.api.Assertions.assertArrayEquals;
5import static org.junit.jupiter.api.Assertions.assertEquals;
6
7import java.util.ArrayList;
8import java.util.Arrays;
9
10import org.junit.jupiter.api.BeforeEach;
11import org.junit.jupiter.api.Test;
12import org.junit.jupiter.api.extension.RegisterExtension;
13import org.openstreetmap.josm.TestUtils;
14import org.openstreetmap.josm.command.CommandTest.CommandTestData;
15import org.openstreetmap.josm.data.coor.EastNorth;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
20import org.openstreetmap.josm.data.osm.User;
21import org.openstreetmap.josm.gui.layer.OsmDataLayer;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25import nl.jqno.equalsverifier.EqualsVerifier;
26import nl.jqno.equalsverifier.Warning;
27
28/**
29 * Unit tests of {@link RotateCommand} class.
30 */
31class RotateCommandTest {
32
33 /**
34 * We need prefs for nodes.
35 */
36 @RegisterExtension
37 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
38 public JOSMTestRules test = new JOSMTestRules().preferences().projection();
39 private CommandTestData testData;
40
41 /**
42 * Set up the test data.
43 */
44 @BeforeEach
45 public void createTestData() {
46 testData = new CommandTestData();
47 }
48
49 /**
50 * Test a simple 45° rotation. Tests {@link RotateCommand#executeCommand()}
51 */
52 @Test
53 void testRotate() {
54 // pivot needs to be at 0,0
55 Node n1 = new Node(new EastNorth(10, 10));
56 Node n2 = new Node(new EastNorth(-1, 0));
57 Node n3 = new Node(new EastNorth(-9, -10));
58 new DataSet(n1, n2, n3);
59 RotateCommand rotate = new RotateCommand(Arrays.asList(n1, n2, n3), new EastNorth(0, 0));
60 rotate.setRotationAngle(Math.PI / 4);
61 rotate.executeCommand();
62
63 assertEquals(Math.sqrt(2) * 10, n1.getEastNorth().east(), 0.0001);
64 assertEquals(0, n1.getEastNorth().north(), 0.0001);
65 assertEquals(-1 / Math.sqrt(2), n2.getEastNorth().east(), 0.0001);
66 assertEquals(1 / Math.sqrt(2), n2.getEastNorth().north(), 0.0001);
67 }
68
69 /**
70 * Test {@link RotateCommand#undoCommand()}
71 */
72 @Test
73 void testUndo() {
74 Node n1 = new Node(new EastNorth(10, 10));
75 Node n2 = new Node(new EastNorth(-1, 0));
76 Node n3 = new Node(new EastNorth(-9, -10));
77 new DataSet(n1, n2, n3);
78 RotateCommand rotate = new RotateCommand(Arrays.asList(n1, n2, n3), new EastNorth(0, 0));
79 rotate.setRotationAngle(Math.PI / 4);
80 rotate.executeCommand();
81 rotate.undoCommand();
82
83 assertEquals(10, n1.getEastNorth().east(), 0.0001);
84 assertEquals(10, n1.getEastNorth().north(), 0.0001);
85 assertEquals(-1, n2.getEastNorth().east(), 0.0001);
86 assertEquals(0, n2.getEastNorth().north(), 0.0001);
87
88 rotate.executeCommand();
89
90 assertEquals(-1 / Math.sqrt(2), n2.getEastNorth().east(), 0.0001);
91 assertEquals(1 / Math.sqrt(2), n2.getEastNorth().north(), 0.0001);
92 }
93
94 /**
95 * Tests {@link RotateCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
96 */
97 @Test
98 void testFillModifiedData() {
99 ArrayList<OsmPrimitive> modified = new ArrayList<>();
100 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
101 ArrayList<OsmPrimitive> added = new ArrayList<>();
102 RotateCommand command = new RotateCommand(Arrays.asList(testData.existingNode),
103 new EastNorth(0, 0));
104 // intentionally empty
105 command.fillModifiedData(modified, deleted, added);
106 assertArrayEquals(new Object[] {}, modified.toArray());
107 assertArrayEquals(new Object[] {}, deleted.toArray());
108 assertArrayEquals(new Object[] {}, added.toArray());
109 }
110
111 /**
112 * Tests {@link RotateCommand#getParticipatingPrimitives()}
113 */
114 @Test
115 void testGetParticipatingPrimitives() {
116 RotateCommand command = new RotateCommand(Arrays.asList(testData.existingNode), new EastNorth(0, 0));
117 command.executeCommand();
118 assertArrayEquals(new Object[] {testData.existingNode}, command.getParticipatingPrimitives().toArray());
119 }
120
121 /**
122 * Test {@link RotateCommand#getDescriptionText()}
123 */
124 @Test
125 void testDescription() {
126 assertEquals("Rotate 1 node",
127 new RotateCommand(Arrays.asList(testData.existingNode), new EastNorth(0, 0))
128 .getDescriptionText());
129 assertEquals("Rotate 2 nodes",
130 new RotateCommand(Arrays.asList(testData.existingNode, testData.existingNode2), new EastNorth(0, 0))
131 .getDescriptionText());
132 }
133
134 /**
135 * Unit test of methods {@link RotateCommand#equals} and {@link RotateCommand#hashCode}.
136 */
137 @Test
138 void testEqualsContract() {
139 TestUtils.assumeWorkingEqualsVerifier();
140 EqualsVerifier.forClass(RotateCommand.class).usingGetClass()
141 .withPrefabValues(LatLon.class, LatLon.ZERO, new LatLon(45, 45))
142 .withPrefabValues(DataSet.class, new DataSet(), new DataSet())
143 .withPrefabValues(User.class, User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
144 .withPrefabValues(OsmDataLayer.class, new OsmDataLayer(new DataSet(), "1", null),
145 new OsmDataLayer(new DataSet(), "2", null))
146 .suppress(Warning.NONFINAL_FIELDS).verify();
147 }
148}
Note: See TracBrowser for help on using the repository browser.