source: josm/trunk/test/unit/org/openstreetmap/josm/actions/SimplifyWayActionTest.java@ 11241

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

checkstyle

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