1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation.sort;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.InputStream;
|
---|
9 | import java.nio.file.Files;
|
---|
10 | import java.nio.file.Paths;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import org.junit.jupiter.api.BeforeEach;
|
---|
14 | import org.junit.jupiter.api.Test;
|
---|
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.gui.progress.NullProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
20 | import org.openstreetmap.josm.io.OsmReader;
|
---|
21 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
22 | import org.openstreetmap.josm.testutils.annotations.Projection;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Unit tests of {@link RelationSorter} class.
|
---|
26 | */
|
---|
27 | @BasicPreferences
|
---|
28 | @Projection
|
---|
29 | class RelationSorterTest {
|
---|
30 |
|
---|
31 | private final RelationSorter sorter = new RelationSorter();
|
---|
32 | private DataSet testDataset;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Load the test data set
|
---|
36 | * @throws IllegalDataException if an error was found while parsing the data
|
---|
37 | * @throws IOException in case of I/O error
|
---|
38 | */
|
---|
39 | @BeforeEach
|
---|
40 | public void loadData() throws IllegalDataException, IOException {
|
---|
41 | if (testDataset == null) {
|
---|
42 | try (InputStream fis = Files.newInputStream(Paths.get("nodist/data/relation_sort.osm"))) {
|
---|
43 | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private Relation getRelation(String testType) {
|
---|
49 | return testDataset.getRelations().stream().filter(r -> testType.equals(r.get("test"))).findFirst().orElse(null);
|
---|
50 | }
|
---|
51 |
|
---|
52 | private String[] getNames(List<RelationMember> members) {
|
---|
53 | return members.stream().map(member -> member.getMember().get("name")).toArray(String[]::new);
|
---|
54 | }
|
---|
55 |
|
---|
56 | // This cluster of tests checks whether these relations are sorted
|
---|
57 | // as expected, so these relations are of course not sorted in the
|
---|
58 | // data file.
|
---|
59 |
|
---|
60 | @Test
|
---|
61 | void testGeneric() {
|
---|
62 | String[] actual = getNames(sorter.sortMembers(getRelation("generic").getMembers()));
|
---|
63 | final String[] expected = {"t1w4", "t1w3", "t1w2", "t1w1", "t1w7", "t1w6", "t1w5", "t1n1", "t1n2"};
|
---|
64 | // expect nodes to be sorted correctly
|
---|
65 | assertEquals(expected[7], actual[7]);
|
---|
66 | assertEquals(expected[8], actual[8]);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Test
|
---|
70 | void testAssociatedStreet() {
|
---|
71 | String[] actual = getNames(sorter.sortMembers(getRelation("associatedStreet").getMembers()));
|
---|
72 | assertArrayEquals(new String[] {"t2w1", "t2w2", "t2n1", "t2n2", "t2n3", "t2n4"}, actual);
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Test
|
---|
76 | void testStreet() {
|
---|
77 | String[] actual = getNames(sorter.sortMembers(getRelation("street").getMembers()));
|
---|
78 | assertArrayEquals(new String[]{"t2w1", "t2w2", "t2n1", "t2n2", "t2n3", "t2n4", "playground", "tree"}, actual);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // The following cluster of tests does the same, but with various
|
---|
82 | // configurations involving split / dual carriageway routes (i.e.
|
---|
83 | // containing members with role=forward or role=backward). Again,
|
---|
84 | // these are intentionally not already sorted.
|
---|
85 |
|
---|
86 | @Test
|
---|
87 | void testThreeLoopsEndsLoop() {
|
---|
88 | Relation relation = getRelation("three-loops-ends-loop");
|
---|
89 | // Check the first way before sorting, otherwise the sorter
|
---|
90 | // might pick a different loop starting point than expected below
|
---|
91 | assertEquals("t5w1", relation.getMembers().get(0).getMember().get("name"));
|
---|
92 |
|
---|
93 | String[] actual = getNames(sorter.sortMembers(relation.getMembers()));
|
---|
94 | assertArrayEquals(new String[]{
|
---|
95 | "t5w1", "t5w2a", "t5w3a", "t5w4a", "t5w2b", "t5w3b", "t5w4b",
|
---|
96 | "t5w5", "t5w6a", "t5w7a", "t5w8a", "t5w6b", "t5w7b", "t5w8b",
|
---|
97 | "t5w9a", "t5w10a", "t5w11a", "t5w9b", "t5w10b", "t5w11b",
|
---|
98 | "t5w12", "t5w13",
|
---|
99 | }, actual);
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Test
|
---|
103 | void testThreeLoopsEndsWay() {
|
---|
104 | Relation relation = getRelation("three-loops-ends-way");
|
---|
105 | // Check the first way before sorting, otherwise the sorter
|
---|
106 | // might sort in reverse compared to what is expected below
|
---|
107 | assertEquals("t5w1", relation.getMembers().get(0).getMember().get("name"));
|
---|
108 |
|
---|
109 | String[] actual = getNames(sorter.sortMembers(relation.getMembers()));
|
---|
110 | assertArrayEquals(new String[]{
|
---|
111 | "t5w1", "t5w2a", "t5w3a", "t5w4a", "t5w2b", "t5w3b", "t5w4b",
|
---|
112 | "t5w5", "t5w6a", "t5w7a", "t5w8a", "t5w6b", "t5w7b", "t5w8b",
|
---|
113 | "t5w9a", "t5w10a", "t5w11a", "t5w9b", "t5w10b", "t5w11b",
|
---|
114 | "t5w12",
|
---|
115 | }, actual);
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Test
|
---|
119 | void testThreeLoopsEndsNode() {
|
---|
120 | Relation relation = getRelation("three-loops-ends-node");
|
---|
121 | String[] actual = getNames(sorter.sortMembers(relation.getMembers()));
|
---|
122 | assertArrayEquals(new String[]{
|
---|
123 | "t5w4a", "t5w3a", "t5w2a", "t5w2b", "t5w3b", "t5w4b",
|
---|
124 | "t5w5", "t5w6a", "t5w7a", "t5w8a", "t5w6b", "t5w7b", "t5w8b",
|
---|
125 | "t5w9a", "t5w10a", "t5w11a", "t5w11b", "t5w10b", "t5w9b",
|
---|
126 | }, actual);
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Test
|
---|
130 | void testOneLoopEndsSplit() {
|
---|
131 | Relation relation = getRelation("one-loop-ends-split");
|
---|
132 | String[] actual = getNames(sorter.sortMembers(relation.getMembers()));
|
---|
133 | assertArrayEquals(new String[]{
|
---|
134 | "t5w3a", "t5w4a", "t5w3b", "t5w4b",
|
---|
135 | "t5w5", "t5w6a", "t5w7a", "t5w8a", "t5w6b", "t5w7b", "t5w8b",
|
---|
136 | "t5w9a", "t5w10a", "t5w9b", "t5w10b",
|
---|
137 | }, actual);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Test
|
---|
141 | void testNoLoopEndsSplit() {
|
---|
142 | Relation relation = getRelation("no-loop-ends-split");
|
---|
143 | // TODO: This is not yet sorted properly, so this route is
|
---|
144 | // presorted in the data file, making this a bit of a dummy test
|
---|
145 | // for now.
|
---|
146 | String[] actual = getNames(relation.getMembers());
|
---|
147 | assertArrayEquals(new String[]{
|
---|
148 | "t5w7a", "t5w8a", "t5w7b", "t5w8b",
|
---|
149 | "t5w9a", "t5w10a", "t5w9b", "t5w10b",
|
---|
150 | }, actual);
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Test
|
---|
154 | void testIncompleteLoops() {
|
---|
155 | Relation relation = getRelation("incomplete-loops");
|
---|
156 | // TODO: This is not yet sorted perfectly (might not be possible)
|
---|
157 | String[] actual = getNames(sorter.sortMembers(relation.getMembers()));
|
---|
158 | assertArrayEquals(new String[]{
|
---|
159 | "t5w1", "t5w2a", "t5w3a", "t5w4a", "t5w2b", "t5w3b",
|
---|
160 | "t5w5", "t5w6a", "t5w7a", "t5w8a", "t5w9a", "t5w10a", "t5w11a", "t5w6b", "t5w7b",
|
---|
161 | "t5w12", "t5w11b", "t5w10b", "t5w9b",
|
---|
162 | }, actual);
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Test
|
---|
166 | void testParallelOneWay() {
|
---|
167 | Relation relation = getRelation("parallel-oneway");
|
---|
168 | // TODO: This is not always sorted properly, only when the right
|
---|
169 | // way is already at the top, so check that
|
---|
170 | assertEquals("t6w1a", relation.getMembers().get(0).getMember().get("name"));
|
---|
171 |
|
---|
172 | String[] actual = getNames(sorter.sortMembers(relation.getMembers()));
|
---|
173 | assertArrayEquals(new String[]{
|
---|
174 | "t6w1a", "t6w2a", "t6w3a",
|
---|
175 | "t6w1b", "t6w2b", "t6w3b",
|
---|
176 | }, actual);
|
---|
177 | }
|
---|
178 | }
|
---|