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

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

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

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