1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation.sort;
|
---|
3 |
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.nio.file.Files;
|
---|
7 | import java.nio.file.Paths;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.junit.Assert;
|
---|
11 | import org.junit.Before;
|
---|
12 | import org.junit.Rule;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
19 | import org.openstreetmap.josm.io.OsmReader;
|
---|
20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
21 |
|
---|
22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Unit tests of {@link RelationSorter} class.
|
---|
26 | */
|
---|
27 | public class RelationSorterTest {
|
---|
28 |
|
---|
29 | private final RelationSorter sorter = new RelationSorter();
|
---|
30 | private DataSet testDataset;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Use Mercator projection
|
---|
34 | */
|
---|
35 | @Rule
|
---|
36 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
37 | public JOSMTestRules test = new JOSMTestRules().preferences().projection();
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Load the test data set
|
---|
41 | * @throws IllegalDataException if an error was found while parsing the data
|
---|
42 | * @throws IOException in case of I/O error
|
---|
43 | */
|
---|
44 | @Before
|
---|
45 | public void loadData() throws IllegalDataException, IOException {
|
---|
46 | if (testDataset == null) {
|
---|
47 | try (InputStream fis = Files.newInputStream(Paths.get("data_nodist/relation_sort.osm"))) {
|
---|
48 | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private Relation getRelation(String testType) {
|
---|
54 | for (Relation r: testDataset.getRelations()) {
|
---|
55 | if (testType.equals(r.get("test")))
|
---|
56 | return r;
|
---|
57 | }
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private String[] getNames(List<RelationMember> members) {
|
---|
62 | String[] result = new String[members.size()];
|
---|
63 | for (int i = 0; i < result.length; i++) {
|
---|
64 | result[i] = members.get(i).getMember().get("name");
|
---|
65 | }
|
---|
66 | return result;
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Test
|
---|
70 | public void testGeneric() {
|
---|
71 | String[] actual = getNames(sorter.sortMembers(getRelation("generic").getMembers()));
|
---|
72 | final String[] expected = {"t1w4", "t1w3", "t1w2", "t1w1", "t1w7", "t1w6", "t1w5", "t1n1", "t1n2"};
|
---|
73 | // expect nodes to be sorted correctly
|
---|
74 | Assert.assertEquals(expected[7], actual[7]);
|
---|
75 | Assert.assertEquals(expected[8], actual[8]);
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Test
|
---|
79 | public void testAssociatedStreet() {
|
---|
80 | String[] actual = getNames(sorter.sortMembers(getRelation("associatedStreet").getMembers()));
|
---|
81 | Assert.assertArrayEquals(new String[] {"t2w1", "t2w2", "t2n1", "t2n2", "t2n3", "t2n4"}, actual);
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Test
|
---|
85 | public void testStreet() {
|
---|
86 | String[] actual = getNames(sorter.sortMembers(getRelation("street").getMembers()));
|
---|
87 | Assert.assertArrayEquals(new String[]{"t2w1", "t2w2", "t2n1", "t2n2", "t2n3", "t2n4", "playground", "tree"}, actual);
|
---|
88 | }
|
---|
89 | }
|
---|