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

Last change on this file since 10956 was 10758, checked in by Don-vip, 8 years ago

sonar - squid:S3578 - Test methods should comply with a naming convention

File size: 5.4 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 RotateCommand rotate = new RotateCommand(Arrays.asList(n1, n2, n3), new EastNorth(0, 0));
58 rotate.setRotationAngle(Math.PI / 4);
59 rotate.executeCommand();
60
61 assertEquals(Math.sqrt(2) * 10, n1.getEastNorth().east(), 0.0001);
62 assertEquals(0, n1.getEastNorth().north(), 0.0001);
63 assertEquals(-1 / Math.sqrt(2), n2.getEastNorth().east(), 0.0001);
64 assertEquals(1 / Math.sqrt(2), n2.getEastNorth().north(), 0.0001);
65 }
66
67 /**
68 * Test {@link RotateCommand#undoCommand()}
69 */
70 @Test
71 public void testUndo() {
72 Node n1 = new Node(new EastNorth(10, 10));
73 Node n2 = new Node(new EastNorth(-1, 0));
74 Node n3 = new Node(new EastNorth(-9, -10));
75 RotateCommand rotate = new RotateCommand(Arrays.asList(n1, n2, n3), new EastNorth(0, 0));
76 rotate.setRotationAngle(Math.PI / 4);
77 rotate.executeCommand();
78 rotate.undoCommand();
79
80 assertEquals(10, n1.getEastNorth().east(), 0.0001);
81 assertEquals(10, n1.getEastNorth().north(), 0.0001);
82 assertEquals(-1, n2.getEastNorth().east(), 0.0001);
83 assertEquals(0, n2.getEastNorth().north(), 0.0001);
84
85 rotate.executeCommand();
86
87 assertEquals(-1 / Math.sqrt(2), n2.getEastNorth().east(), 0.0001);
88 assertEquals(1 / Math.sqrt(2), n2.getEastNorth().north(), 0.0001);
89 }
90
91 /**
92 * Tests {@link RotateCommand#fillModifiedData(java.util.Collection, java.util.Collection, java.util.Collection)}
93 */
94 @Test
95 public void testFillModifiedData() {
96 ArrayList<OsmPrimitive> modified = new ArrayList<>();
97 ArrayList<OsmPrimitive> deleted = new ArrayList<>();
98 ArrayList<OsmPrimitive> added = new ArrayList<>();
99 RotateCommand command = new RotateCommand(Arrays.asList(testData.existingNode),
100 new EastNorth(0, 0));
101 // intentionally empty
102 command.fillModifiedData(modified, deleted, added);
103 assertArrayEquals(new Object[] {}, modified.toArray());
104 assertArrayEquals(new Object[] {}, deleted.toArray());
105 assertArrayEquals(new Object[] {}, added.toArray());
106 }
107
108 /**
109 * Tests {@link RotateCommand#getParticipatingPrimitives()}
110 */
111 @Test
112 public void testGetParticipatingPrimitives() {
113 RotateCommand command = new RotateCommand(Arrays.asList(testData.existingNode), new EastNorth(0, 0));
114 command.executeCommand();
115 assertArrayEquals(new Object[] {testData.existingNode}, command.getParticipatingPrimitives().toArray());
116 }
117
118 /**
119 * Test {@link RotateCommand#getDescriptionText()}
120 */
121 @Test
122 public void testDescription() {
123 assertEquals("Rotate 1 node",
124 new RotateCommand(Arrays.asList(testData.existingNode), new EastNorth(0, 0))
125 .getDescriptionText());
126 assertEquals("Rotate 2 nodes",
127 new RotateCommand(Arrays.asList(testData.existingNode, testData.existingNode2), new EastNorth(0, 0))
128 .getDescriptionText());
129 }
130
131 /**
132 * Unit test of methods {@link RotateCommand#equals} and {@link RotateCommand#hashCode}.
133 */
134 @Test
135 public void testEqualsContract() {
136 EqualsVerifier.forClass(RotateCommand.class).usingGetClass()
137 .withPrefabValues(LatLon.class, LatLon.ZERO, new LatLon(45, 45))
138 .withPrefabValues(DataSet.class, new DataSet(), new DataSet())
139 .withPrefabValues(User.class, User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
140 .withPrefabValues(OsmDataLayer.class, new OsmDataLayer(new DataSet(), "1", null),
141 new OsmDataLayer(new DataSet(), "2", null))
142 .suppress(Warning.NONFINAL_FIELDS).verify();
143 }
144}
Note: See TracBrowser for help on using the repository browser.