source: josm/trunk/test/unit/org/openstreetmap/josm/actions/SimplifyWayActionTest.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.3 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.assertTrue;
7
8import java.util.Collection;
9import java.util.Collections;
10import java.util.stream.Stream;
11
12import org.junit.Before;
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.command.DeleteCommand;
17import org.openstreetmap.josm.command.SequenceCommand;
18import org.openstreetmap.josm.data.coor.LatLon;
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.layer.OsmDataLayer;
23import org.openstreetmap.josm.testutils.JOSMTestRules;
24import org.openstreetmap.josm.tools.Utils;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests for class {@link SimplifyWayAction}.
30 */
31public final class SimplifyWayActionTest {
32
33 /** Class under test. */
34 private static SimplifyWayAction action;
35
36 /**
37 * Setup test.
38 */
39 @Rule
40 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
41 public JOSMTestRules test = new JOSMTestRules().main();
42
43 /**
44 * Setup test.
45 */
46 @Before
47 public void setUp() {
48 if (action == null) {
49 action = Main.main.menu.simplifyWay;
50 action.setEnabled(true);
51 }
52 }
53
54 private static Way createWaySelected(DataSet ds, double latStart) {
55 Node n1 = new Node(new LatLon(latStart, 1.0));
56 ds.addPrimitive(n1);
57 Node n2 = new Node(new LatLon(latStart+1.0, 1.0));
58 ds.addPrimitive(n2);
59 Way w = new Way();
60 w.addNode(n1);
61 w.addNode(n2);
62 ds.addPrimitive(w);
63 ds.addSelected(w);
64 return w;
65 }
66
67 /**
68 * Test without any selection.
69 */
70 @Test
71 public void testSelectionEmpty() {
72 DataSet ds = new DataSet();
73 OsmDataLayer layer = new OsmDataLayer(ds, "", null);
74 try {
75 Main.getLayerManager().addLayer(layer);
76 assertTrue(ds.getSelected().isEmpty());
77 action.actionPerformed(null);
78 } finally {
79 Main.getLayerManager().removeLayer(layer);
80 }
81 }
82
83 /**
84 * Test with a single way.
85 */
86 @Test
87 public void testSingleWay() {
88 DataSet ds = new DataSet();
89 createWaySelected(ds, 0.0);
90 OsmDataLayer layer = new OsmDataLayer(ds, "", null);
91 try {
92 Main.getLayerManager().addLayer(layer);
93 assertEquals(1, ds.getSelected().size());
94 action.actionPerformed(null);
95 } finally {
96 Main.getLayerManager().removeLayer(layer);
97 }
98 }
99
100 /**
101 * Test with more than 10 ways.
102 */
103 @Test
104 public void testMoreThanTenWays() {
105 DataSet ds = new DataSet();
106 for (int i = 0; i < 11; i++) {
107 createWaySelected(ds, i);
108 }
109 OsmDataLayer layer = new OsmDataLayer(ds, "", null);
110 try {
111 Main.getLayerManager().addLayer(layer);
112 assertEquals(11, ds.getSelected().size());
113 action.actionPerformed(null);
114 } finally {
115 Main.getLayerManager().removeLayer(layer);
116 }
117 }
118
119 /**
120 * Tests that also the first node may be simplified, see #13094.
121 */
122 @Test
123 public void testSimplifyFirstNode() {
124 final DataSet ds = new DataSet();
125 final Node n1 = new Node(new LatLon(47.26269614984, 11.34044231149));
126 final Node n2 = new Node(new LatLon(47.26274590831, 11.34053120859));
127 final Node n3 = new Node(new LatLon(47.26276562382, 11.34034715039));
128 final Node n4 = new Node(new LatLon(47.26264639132, 11.34035341438));
129 final Way w = new Way();
130 Stream.of(n1, n2, n3, n4, w).forEach(ds::addPrimitive);
131 Stream.of(n1, n2, n3, n4, n1).forEach(w::addNode);
132 final SequenceCommand command = action.simplifyWay(w);
133 assertNotNull(command);
134 assertEquals(2, command.getChildren().size());
135 final Collection<DeleteCommand> deleteCommands = Utils.filteredCollection(command.getChildren(), DeleteCommand.class);
136 assertEquals(1, deleteCommands.size());
137 assertEquals(Collections.singleton(n1), deleteCommands.iterator().next().getParticipatingPrimitives());
138 }
139}
Note: See TracBrowser for help on using the repository browser.