1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Map;
|
---|
12 | import java.util.TreeMap;
|
---|
13 |
|
---|
14 | import org.junit.Rule;
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.openstreetmap.josm.TestUtils;
|
---|
17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
20 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
21 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
22 | import org.openstreetmap.josm.data.osm.Way;
|
---|
23 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
---|
24 | import org.openstreetmap.josm.data.osm.search.SearchParseError;
|
---|
25 | import org.openstreetmap.josm.data.osm.search.SearchSetting;
|
---|
26 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
27 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
28 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
29 | import org.openstreetmap.josm.io.OsmReader;
|
---|
30 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
31 | import org.openstreetmap.josm.tools.Pair;
|
---|
32 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
33 |
|
---|
34 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Unit test of {@link CreateMultipolygonAction}
|
---|
38 | */
|
---|
39 | public class CreateMultipolygonActionTest {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Setup test.
|
---|
43 | */
|
---|
44 | @Rule
|
---|
45 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
46 | public JOSMTestRules test = new JOSMTestRules().projection().main().preferences();
|
---|
47 |
|
---|
48 | private static Map<String, String> getRefToRoleMap(Relation relation) {
|
---|
49 | Map<String, String> refToRole = new TreeMap<>();
|
---|
50 | String ref = relation.get("ref");
|
---|
51 | if (ref != null) {
|
---|
52 | refToRole.put(ref, "outer");
|
---|
53 | }
|
---|
54 | for (RelationMember i : relation.getMembers()) {
|
---|
55 | ref = i.getMember().get("ref");
|
---|
56 | if (ref != null) {
|
---|
57 | refToRole.put(ref, i.getRole());
|
---|
58 | }
|
---|
59 | }
|
---|
60 | return refToRole;
|
---|
61 | }
|
---|
62 |
|
---|
63 | private static SearchSetting regexpSearch(String search) {
|
---|
64 | SearchSetting setting = new SearchSetting();
|
---|
65 | setting.text = search;
|
---|
66 | setting.regexSearch = true;
|
---|
67 | return setting;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @SuppressWarnings("unchecked")
|
---|
71 | private static Relation createMultipolygon(Collection<Way> ways, String pattern, Relation r, boolean runCmd)
|
---|
72 | throws SearchParseError {
|
---|
73 | Pair<SequenceCommand, Relation> cmd = CreateMultipolygonAction.createMultipolygonCommand(
|
---|
74 | (Collection<Way>) (Collection<?>) SubclassFilteredCollection.filter(ways, SearchCompiler.compile(regexpSearch(pattern))), r);
|
---|
75 | if (runCmd) {
|
---|
76 | cmd.a.executeCommand();
|
---|
77 | }
|
---|
78 | return cmd.b;
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Test
|
---|
82 | public void testCreate1() throws Exception {
|
---|
83 | DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "create_multipolygon.osm")), null);
|
---|
84 | Pair<SequenceCommand, Relation> mp = CreateMultipolygonAction.createMultipolygonCommand(ds.getWays(), null);
|
---|
85 | assertEquals("Sequence: Create multipolygon", mp.a.getDescriptionText());
|
---|
86 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.1.2=outer, 1.2=inner}", getRefToRoleMap(mp.b).toString());
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void testCreate2() throws Exception {
|
---|
91 | DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "create_multipolygon.osm")), null);
|
---|
92 | Relation mp = createMultipolygon(ds.getWays(), "ref=1 OR ref:1.1.", null, true);
|
---|
93 | assertEquals("{1=outer, 1.1.1=inner, 1.1.2=inner}", getRefToRoleMap(mp).toString());
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Test
|
---|
97 | public void testUpdate1() throws Exception {
|
---|
98 | DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "create_multipolygon.osm")), null);
|
---|
99 | Relation mp = createMultipolygon(ds.getWays(), "ref=\".*1$\"", null, true);
|
---|
100 | assertEquals(3, mp.getMembersCount());
|
---|
101 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer}", getRefToRoleMap(mp).toString());
|
---|
102 | Relation mp2 = createMultipolygon(ds.getWays(), "ref=1.2", mp, true);
|
---|
103 | assertEquals(4, mp2.getMembersCount());
|
---|
104 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.2=inner}", getRefToRoleMap(mp2).toString());
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Test
|
---|
108 | public void testUpdate2() throws Exception {
|
---|
109 | DataSet ds = OsmReader.parseDataSet(Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "create_multipolygon.osm")), null);
|
---|
110 | Relation mp = createMultipolygon(ds.getWays(), "ref=1 OR ref:1.1.1", null, true);
|
---|
111 | assertEquals("{1=outer, 1.1.1=inner}", getRefToRoleMap(mp).toString());
|
---|
112 | Relation mp2 = createMultipolygon(ds.getWays(), "ref=1.1 OR ref=1.2 OR ref=1.1.2", mp, false);
|
---|
113 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.1.2=outer, 1.2=inner}", getRefToRoleMap(mp2).toString());
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17767">Bug #17767</a>.
|
---|
118 | * @throws Exception if an error occurs
|
---|
119 | */
|
---|
120 | @Test
|
---|
121 | public void testTicket17767() throws Exception {
|
---|
122 | DataSet ds = OsmReader.parseDataSet(TestUtils.getRegressionDataStream(17767, "upd-mp.osm"), null);
|
---|
123 | Layer layer = new OsmDataLayer(ds, null, null);
|
---|
124 | MainApplication.getLayerManager().addLayer(layer);
|
---|
125 | try {
|
---|
126 | CreateMultipolygonAction updateAction = new CreateMultipolygonAction(true);
|
---|
127 | CreateMultipolygonAction createAction = new CreateMultipolygonAction(false);
|
---|
128 | assertFalse(updateAction.isEnabled());
|
---|
129 | assertFalse(createAction.isEnabled());
|
---|
130 | ds.setSelected(ds.getPrimitiveById(189944949L, OsmPrimitiveType.WAY));
|
---|
131 | assertFalse(updateAction.isEnabled());
|
---|
132 | assertTrue(createAction.isEnabled());
|
---|
133 | } finally {
|
---|
134 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/17768">Bug #17768</a>.
|
---|
140 | * @throws Exception if an error occurs
|
---|
141 | */
|
---|
142 | @Test
|
---|
143 | public void testTicket17768() throws Exception {
|
---|
144 | DataSet ds = OsmReader.parseDataSet(TestUtils.getRegressionDataStream(17768, "dupmem.osm"), null);
|
---|
145 | Layer layer = new OsmDataLayer(ds, null, null);
|
---|
146 | MainApplication.getLayerManager().addLayer(layer);
|
---|
147 | try {
|
---|
148 | Relation old = (Relation) ds.getPrimitiveById(580092, OsmPrimitiveType.RELATION);
|
---|
149 | assertEquals(3, old.getMembersCount());
|
---|
150 | Relation mp = createMultipolygon(ds.getWays(), "type:way", old, true);
|
---|
151 | assertEquals(mp.getPrimitiveId(), old.getPrimitiveId());
|
---|
152 | assertEquals(2, mp.getMembersCount());
|
---|
153 | } finally {
|
---|
154 | MainApplication.getLayerManager().removeLayer(layer);
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 | }
|
---|