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

Last change on this file was 18870, checked in by taylor.smock, 6 months ago

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

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