source: josm/trunk/test/unit/org/openstreetmap/josm/actions/mapmode/SelectActionTest.java@ 10937

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

sonar - squid:S3658 - Unit tests should throw exceptions

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.mapmode;
3
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertSame;
6import static org.junit.Assert.assertTrue;
7
8import java.awt.event.InputEvent;
9import java.awt.event.MouseEvent;
10import java.lang.reflect.Field;
11import java.util.Arrays;
12import java.util.Collection;
13
14import org.junit.BeforeClass;
15import org.junit.Test;
16import org.openstreetmap.josm.JOSMFixture;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.coor.EastNorth;
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.data.osm.Node;
21import org.openstreetmap.josm.data.osm.Way;
22import org.openstreetmap.josm.gui.MapFrame;
23import org.openstreetmap.josm.gui.layer.OsmDataLayer;
24import org.openstreetmap.josm.tools.Utils;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests for class {@link SelectAction}.
30 */
31public class SelectActionTest {
32
33 boolean nodesMerged;
34
35 class SelectActionMock extends SelectAction {
36 SelectActionMock(MapFrame mapFrame, DataSet dataSet, OsmDataLayer layer) throws ReflectiveOperationException {
37 super(mapFrame);
38 Field mv = SelectAction.class.getDeclaredField("mv");
39 Utils.setObjectsAccessible(mv);
40 mv.set(this, new MapViewMock());
41 }
42
43 @Override
44 public void mergeNodes(OsmDataLayer layer, Collection<Node> nodes,
45 Node targetLocationNode) {
46 assertSame(String.format("Should merge two nodes, %d found", nodes.size()),
47 nodes.size(), 2);
48 nodesMerged = true;
49 }
50 }
51
52 /**
53 * Setup test.
54 */
55 @BeforeClass
56 public static void setUpBeforeClass() {
57 JOSMFixture.createUnitTestFixture().init(true);
58 }
59
60 /**
61 * Test case: Move a two nodes way near a third node.
62 * Resulting way should be attach to the third node.
63 * see #10748
64 * @throws ReflectiveOperationException if an error occurs
65 */
66 @Test
67 @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
68 public void testTicket10748() throws ReflectiveOperationException {
69 DataSet dataSet = new DataSet();
70 OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
71
72 Node n1 = new Node(new EastNorth(0, 0));
73 Node n2 = new Node(new EastNorth(100, 0));
74 Node n3 = new Node(new EastNorth(200, 0));
75 dataSet.addPrimitive(n1);
76 dataSet.addPrimitive(n2);
77 dataSet.addPrimitive(n3);
78
79 Way w = new Way();
80 w.setNodes(Arrays.asList(new Node[] {n2, n3}));
81 dataSet.addPrimitive(w);
82
83 dataSet.addSelected(n2);
84 dataSet.addSelected(w);
85
86 Main.pref.put("edit.initial-move-delay", "0");
87 Main.getLayerManager().addLayer(layer);
88 try {
89 SelectAction action = new SelectActionMock(Main.map, dataSet, layer);
90 nodesMerged = false;
91
92 action.setEnabled(true);
93 action.putValue("active", true);
94
95 MouseEvent event;
96 event = new MouseEvent(Main.map,
97 MouseEvent.MOUSE_PRESSED,
98 0,
99 InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK,
100 100, 0,
101 1,
102 false);
103 action.mousePressed(event);
104 event = new MouseEvent(Main.map,
105 MouseEvent.MOUSE_DRAGGED,
106 1000,
107 InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK,
108 50, 0,
109 1,
110 false);
111 action.mouseDragged(event);
112 event = new MouseEvent(Main.map,
113 MouseEvent.MOUSE_RELEASED,
114 2000,
115 InputEvent.BUTTON1_MASK | InputEvent.CTRL_MASK,
116 5, 0,
117 1,
118 false);
119 action.mouseReleased(event);
120
121 // As result of test, we must find a 2 nodes way, from EN(0, 0) to EN(100, 0)
122 assertTrue("Nodes are not merged", nodesMerged);
123 assertSame(String.format("Expect exactly one way, found %d%n", dataSet.getWays().size()),
124 dataSet.getWays().size(), 1);
125 Way rw = dataSet.getWays().iterator().next();
126 assertFalse("Way shouldn't be deleted\n", rw.isDeleted());
127 assertSame(String.format("Way shouldn't have 2 nodes, %d found%n", w.getNodesCount()),
128 rw.getNodesCount(), 2);
129 Node r1 = rw.firstNode();
130 Node r2 = rw.lastNode();
131 if (r1.getEastNorth().east() > r2.getEastNorth().east()) {
132 Node tmp = r1;
133 r1 = r2;
134 r2 = tmp;
135 }
136 assertSame(String.format("East should be 0, found %f%n", r1.getEastNorth().east()),
137 Double.compare(r1.getEastNorth().east(), 0), 0);
138 assertSame(String.format("East should be 100, found %f%n", r2.getEastNorth().east()),
139 Double.compare(r2.getEastNorth().east(), 100), 0);
140 } finally {
141 // Ensure we clean the place before leaving, even if test fails.
142 Main.getLayerManager().removeLayer(layer);
143 }
144 }
145}
Note: See TracBrowser for help on using the repository browser.