source: josm/trunk/test/unit/org/openstreetmap/josm/actions/SelectByInternalPointActionTest.java@ 12632

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

see #15182 - fix unit tests

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8
9import org.junit.Rule;
10import org.junit.Test;
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.coor.EastNorth;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.data.osm.Node;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.data.osm.RelationMember;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.gui.layer.Layer;
19import org.openstreetmap.josm.gui.layer.OsmDataLayer;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23import net.trajano.commons.testing.UtilityClassTestUtil;
24
25/**
26 * Unit tests for class {@link SelectByInternalPointAction}.
27 */
28public final class SelectByInternalPointActionTest {
29
30 /**
31 * Setup test.
32 */
33 @Rule
34 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
35 public JOSMTestRules rules = new JOSMTestRules().preferences().projection().main();
36
37 /**
38 * Tests that {@code SelectByInternalPointAction} satisfies utility class criterias.
39 * @throws ReflectiveOperationException if an error occurs
40 */
41 @Test
42 public void testUtilityClass() throws ReflectiveOperationException {
43 UtilityClassTestUtil.assertUtilityClassWellDefined(SelectByInternalPointAction.class);
44 }
45
46 /**
47 * Unit test - no dataset.
48 */
49 @Test
50 public void testNoDataSet() {
51 assertNull(Main.getLayerManager().getEditDataSet());
52 assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(null).size());
53 assertNull(SelectByInternalPointAction.getSmallestSurroundingObject(null));
54 SelectByInternalPointAction.performSelection(null, false, false);
55 }
56
57 private static Layer initDataSet() {
58 DataSet ds = new DataSet();
59 Node n1 = new Node(new EastNorth(1, 1));
60 Node n2 = new Node(new EastNorth(1, 2));
61 Node n3 = new Node(new EastNorth(2, 2));
62 Node n4 = new Node(new EastNorth(2, 1));
63 ds.addPrimitive(n1);
64 ds.addPrimitive(n2);
65 ds.addPrimitive(n3);
66 ds.addPrimitive(n4);
67 Way w = new Way();
68 w.addNode(n1);
69 w.addNode(n2);
70 w.addNode(n3);
71 w.addNode(n4);
72 w.addNode(n1);
73 assertTrue(w.isClosed());
74 ds.addPrimitive(w);
75 Relation r = new Relation();
76 r.addMember(new RelationMember("outer", w));
77 ds.addPrimitive(r);
78 OsmDataLayer layer = new OsmDataLayer(ds, "", null);
79 Main.getLayerManager().addLayer(layer);
80 return layer;
81 }
82
83 /**
84 * Unit test of {@link SelectByInternalPointAction#getSurroundingObjects} method.
85 */
86 @Test
87 public void testGetSurroundingObjects() {
88 initDataSet();
89 assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(null).size());
90 assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(new EastNorth(0, 0)).size());
91 assertEquals(1, SelectByInternalPointAction.getSurroundingObjects(new EastNorth(1.5, 1.5)).size());
92 assertEquals(0, SelectByInternalPointAction.getSurroundingObjects(new EastNorth(3, 3)).size());
93 }
94
95 /**
96 * Unit test of {@link SelectByInternalPointAction#getSmallestSurroundingObject} method.
97 */
98 @Test
99 public void testGetSmallestSurroundingObject() {
100 initDataSet();
101 assertNull(SelectByInternalPointAction.getSmallestSurroundingObject(null));
102 assertNotNull(SelectByInternalPointAction.getSmallestSurroundingObject(new EastNorth(1.5, 1.5)));
103 }
104
105 /**
106 * Unit test of {@link SelectByInternalPointAction#performSelection} method.
107 */
108 @Test
109 public void testPerformSelection() {
110 initDataSet();
111 DataSet ds = Main.getLayerManager().getEditDataSet();
112
113 assertEquals(0, ds.getSelected().size());
114 SelectByInternalPointAction.performSelection(null, false, false);
115 assertEquals(0, ds.getSelected().size());
116 SelectByInternalPointAction.performSelection(new EastNorth(0, 0), false, false);
117 assertEquals(0, ds.getSelected().size());
118 SelectByInternalPointAction.performSelection(new EastNorth(1.5, 1.5), false, false);
119 assertEquals(1, ds.getSelected().size());
120 ds.clearSelection();
121 ds.addSelected(ds.getNodes());
122 assertEquals(4, ds.getSelected().size());
123 SelectByInternalPointAction.performSelection(new EastNorth(1.5, 1.5), true, false);
124 assertEquals(5, ds.getSelected().size());
125 SelectByInternalPointAction.performSelection(new EastNorth(1.5, 1.5), false, true);
126 assertEquals(4, ds.getSelected().size());
127 }
128}
Note: See TracBrowser for help on using the repository browser.