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.actions.search.SearchAction.SearchSetting;
|
---|
15 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
16 | import org.openstreetmap.josm.actions.search.SearchCompiler.ParseError;
|
---|
17 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
18 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
21 | import org.openstreetmap.josm.data.osm.Way;
|
---|
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 | for (RelationMember i : relation.getMembers()) {
|
---|
44 | refToRole.put(i.getMember().get("ref"), i.getRole());
|
---|
45 | }
|
---|
46 | return refToRole;
|
---|
47 | }
|
---|
48 |
|
---|
49 | private static SearchSetting regexpSearch(String search) {
|
---|
50 | SearchSetting setting = new SearchSetting();
|
---|
51 | setting.text = search;
|
---|
52 | setting.regexSearch = true;
|
---|
53 | return setting;
|
---|
54 | }
|
---|
55 |
|
---|
56 | @SuppressWarnings("unchecked")
|
---|
57 | private static Pair<SequenceCommand, Relation> createMultipolygonCommand(Collection<Way> ways, String pattern, Relation r)
|
---|
58 | throws ParseError {
|
---|
59 | return CreateMultipolygonAction.createMultipolygonCommand(
|
---|
60 | (Collection<Way>) (Collection<?>) SubclassFilteredCollection.filter(ways, SearchCompiler.compile(regexpSearch(pattern))), r);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Test
|
---|
64 | public void testCreate1() throws Exception {
|
---|
65 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
66 | Pair<SequenceCommand, Relation> mp = CreateMultipolygonAction.createMultipolygonCommand(ds.getWays(), null);
|
---|
67 | assertEquals("Sequence: Create multipolygon", mp.a.getDescriptionText());
|
---|
68 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.1.2=outer, 1.2=inner}", getRefToRoleMap(mp.b).toString());
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Test
|
---|
72 | public void testCreate2() throws Exception {
|
---|
73 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
74 | Pair<SequenceCommand, Relation> mp = createMultipolygonCommand(ds.getWays(), "ref=1 OR ref:1.1.", null);
|
---|
75 | assertEquals("{1=outer, 1.1.1=inner, 1.1.2=inner}", getRefToRoleMap(mp.b).toString());
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Test
|
---|
79 | public void testUpdate1() throws Exception {
|
---|
80 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
81 | Pair<SequenceCommand, Relation> mp = createMultipolygonCommand(ds.getWays(), "ref=\".*1$\"", null);
|
---|
82 | assertEquals(3, mp.b.getMembersCount());
|
---|
83 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer}", getRefToRoleMap(mp.b).toString());
|
---|
84 | Pair<SequenceCommand, Relation> mp2 = createMultipolygonCommand(ds.getWays(), "ref=1.2", mp.b);
|
---|
85 | assertEquals(4, mp2.b.getMembersCount());
|
---|
86 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.2=inner}", getRefToRoleMap(mp2.b).toString());
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Test
|
---|
90 | public void testUpdate2() throws Exception {
|
---|
91 | DataSet ds = OsmReader.parseDataSet(new FileInputStream(TestUtils.getTestDataRoot() + "create_multipolygon.osm"), null);
|
---|
92 | Pair<SequenceCommand, Relation> mp = createMultipolygonCommand(ds.getWays(), "ref=1 OR ref:1.1.1", null);
|
---|
93 | assertEquals("{1=outer, 1.1.1=inner}", getRefToRoleMap(mp.b).toString());
|
---|
94 | Pair<SequenceCommand, Relation> mp2 = createMultipolygonCommand(ds.getWays(), "ref=1.1 OR ref=1.2 OR ref=1.1.2", mp.b);
|
---|
95 | assertEquals("{1=outer, 1.1=inner, 1.1.1=outer, 1.1.2=outer, 1.2=inner}", getRefToRoleMap(mp2.b).toString());
|
---|
96 | }
|
---|
97 | }
|
---|