source: josm/trunk/test/unit/org/openstreetmap/josm/command/ScaleCommandTest.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.3 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 ScaleCommand} class.
29 */
30public class ScaleCommandTest {
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 2.5 scale. Tests {@link ScaleCommand#executeCommand()}
50 */
51 @Test
52 public void testScale() {
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 ScaleCommand scale = new ScaleCommand(Arrays.asList(n1, n2, n3), new EastNorth(0, 0));
58 scale.setScalingFactor(2.5);
59 scale.executeCommand();
60
61 assertEquals(25, n1.getEastNorth().east(), 0.0001);
62 assertEquals(25, n1.getEastNorth().north(), 0.0001);
63 assertEquals(-2.5, n2.getEastNorth().east(), 0.0001);
64 assertEquals(0, n2.getEastNorth().north(), 0.0001);
65 }
66
67 /**
68 * Test {@link ScaleCommand#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 ScaleCommand scale = new ScaleCommand(Arrays.asList(n1, n2, n3), new EastNorth(0, 0));
76 scale.setScalingFactor(2.5);
77 scale.executeCommand();
78 scale.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 scale.executeCommand();
86
87 assertEquals(-2.5, n2.getEastNorth().east(), 0.0001);
88 assertEquals(0, n2.getEastNorth().north(), 0.0001);
89 }
90
91 /**
92 * Tests {@link ScaleCommand#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 ScaleCommand command = new ScaleCommand(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 ScaleCommand#getParticipatingPrimitives()}
110 */
111 @Test
112 public void testGetParticipatingPrimitives() {
113 ScaleCommand command = new ScaleCommand(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 ScaleCommand#getDescriptionText()}
120 */
121 @Test
122 public void testDescription() {
123 assertEquals("Scale 1 node",
124 new ScaleCommand(Arrays.asList(testData.existingNode), new EastNorth(0, 0))
125 .getDescriptionText());
126 assertEquals("Scale 2 nodes",
127 new ScaleCommand(Arrays.asList(testData.existingNode, testData.existingNode2), new EastNorth(0, 0))
128 .getDescriptionText());
129 }
130
131 /**
132 * Unit test of methods {@link ScaleCommand#equals} and {@link ScaleCommand#hashCode}.
133 */
134 @Test
135 public void testEqualsContract() {
136 EqualsVerifier.forClass(ScaleCommand.class).usingGetClass()
137 .withPrefabValues(LatLon.class,
138 LatLon.ZERO, new LatLon(45, 45))
139 .withPrefabValues(DataSet.class,
140 new DataSet(), new DataSet())
141 .withPrefabValues(User.class,
142 User.createOsmUser(1, "foo"), User.createOsmUser(2, "bar"))
143 .withPrefabValues(OsmDataLayer.class,
144 new OsmDataLayer(new DataSet(), "1", null), new OsmDataLayer(new DataSet(), "2", null))
145 .suppress(Warning.NONFINAL_FIELDS)
146 .verify();
147 }
148}
Note: See TracBrowser for help on using the repository browser.