1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.io.FileInputStream;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.TreeMap;
|
---|
10 |
|
---|
11 | import org.junit.Rule;
|
---|
12 | import org.junit.Test;
|
---|
13 | import org.openstreetmap.josm.TestUtils;
|
---|
14 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
17 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
---|
20 | import org.openstreetmap.josm.data.osm.search.SearchParseError;
|
---|
21 | import org.openstreetmap.josm.data.osm.search.SearchSetting;
|
---|
22 | import org.openstreetmap.josm.io.OsmReader;
|
---|
23 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
24 | import org.openstreetmap.josm.tools.Pair;
|
---|
25 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
26 |
|
---|
27 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Unit test of {@link CreateMultipolygonAction}
|
---|
31 | */
|
---|
32 | public class CreateMultipolygonActionTest {
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Setup test.
|
---|
36 | */
|
---|
37 | @Rule
|
---|
38 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
39 | public JOSMTestRules test = new JOSMTestRules().projection();
|
---|
40 |
|
---|
41 | private static Map<String, String> getRefToRoleMap(Relation relation) {
|
---|
42 | Map<String, String> refToRole = new TreeMap<>();
|
---|
43 | String ref = relation.get("ref");
|
---|
44 | if (ref != null) {
|
---|
45 | refToRole.put(ref, "outer");
|
---|
46 | }
|
---|
47 | for (RelationMember i : relation.getMembers()) {
|
---|
48 | ref = i.getMember().get("ref");
|
---|
49 | if (ref != null) {
|
---|
50 | refToRole.put(ref, i.getRole());
|
---|
51 | }
|
---|
52 | }
|
---|
53 | return refToRole;
|
---|
54 | }
|
---|
55 |
|
---|
56 | private static SearchSetting regexpSearch(String search) {
|
---|
57 | SearchSetting setting = new SearchSetting();
|
---|
58 | setting.text = search;
|
---|
59 | setting.regexSearch = true;
|
---|
60 | return setting;
|
---|
61 | }
|
---|
62 |
|
---|
63 | @SuppressWarnings("unchecked")
|
---|
64 | private static Relation createMultipolygon(Collection<Way> ways, String pattern, Relation r, boolean runCmd)
|
---|
65 | throws SearchParseError {
|
---|
66 | Pair<SequenceCommand, Relation> cmd = CreateMultipolygonAction.createMultipolygonCommand(
|
---|
67 | (Collection<Way>) (Collection<?>) SubclassFilteredCollection.filter(ways, SearchCompiler.compile(regexpSearch(pattern))), r);
|
---|
68 | if (runCmd) {
|
---|
69 | cmd.a.executeCommand();
|
---|
70 | }
|
---|
71 | return cmd.b;
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Test
|
---|
75 | public void testCreate1() throws Exception {
|
---|
76 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
77 | Pair<SequenceCommand, Relation> mp = CreateMultipolygonAction.createMultipolygonCommand(ds.getWays(), null);
|
---|
78 | assertEquals("Sequence: Create multipolygon", mp.a.getDescriptionText());
|
---|
79 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.1.2=outer, 1.2=inner}", getRefToRoleMap(mp.b).toString());
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Test
|
---|
83 | public void testCreate2() throws Exception {
|
---|
84 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
85 | Relation mp = createMultipolygon(ds.getWays(), "ref=1 OR ref:1.1.", null, true);
|
---|
86 | assertEquals("{1=outer, 1.1.1=inner, 1.1.2=inner}", getRefToRoleMap(mp).toString());
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void testUpdate1() throws Exception {
|
---|
91 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
92 | Relation mp = createMultipolygon(ds.getWays(), "ref=\".*1$\"", null, true);
|
---|
93 | assertEquals(3, mp.getMembersCount());
|
---|
94 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer}", getRefToRoleMap(mp).toString());
|
---|
95 | Relation mp2 = createMultipolygon(ds.getWays(), "ref=1.2", mp, true);
|
---|
96 | assertEquals(4, mp2.getMembersCount());
|
---|
97 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.2=inner}", getRefToRoleMap(mp2).toString());
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Test
|
---|
101 | public void testUpdate2() throws Exception {
|
---|
102 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
103 | Relation mp = createMultipolygon(ds.getWays(), "ref=1 OR ref:1.1.1", null, true);
|
---|
104 | assertEquals("{1=outer, 1.1.1=inner}", getRefToRoleMap(mp).toString());
|
---|
105 | Relation mp2 = createMultipolygon(ds.getWays(), "ref=1.1 OR ref=1.2 OR ref=1.1.2", mp, false);
|
---|
106 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.1.2=outer, 1.2=inner}", getRefToRoleMap(mp2).toString());
|
---|
107 | }
|
---|
108 | }
|
---|