1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation.sort;
|
---|
3 |
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.junit.Assert;
|
---|
10 | import org.junit.BeforeClass;
|
---|
11 | import org.junit.Test;
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
16 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
19 | import org.openstreetmap.josm.io.OsmReader;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link RelationSorter} class.
|
---|
23 | */
|
---|
24 | public class RelationSorterTest {
|
---|
25 |
|
---|
26 | private final RelationSorter sorter = new RelationSorter();
|
---|
27 | private static DataSet testDataset;
|
---|
28 |
|
---|
29 | @BeforeClass
|
---|
30 | public static void loadData() throws IllegalDataException, IOException {
|
---|
31 | Main.initApplicationPreferences();
|
---|
32 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
33 | try (InputStream fis = new FileInputStream("data_nodist/relation_sort.osm")) {
|
---|
34 | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | private Relation getRelation(String testType) {
|
---|
39 | for (Relation r: testDataset.getRelations()) {
|
---|
40 | if (testType.equals(r.get("test")))
|
---|
41 | return r;
|
---|
42 | }
|
---|
43 | return null;
|
---|
44 | }
|
---|
45 |
|
---|
46 | private String[] getNames(List<RelationMember> members) {
|
---|
47 | String[] result = new String[members.size()];
|
---|
48 | for (int i = 0; i < result.length; i++) {
|
---|
49 | result[i] = members.get(i).getMember().get("name");
|
---|
50 | }
|
---|
51 | return result;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Test
|
---|
55 | public void testGeneric() {
|
---|
56 | String[] actual = getNames(sorter.sortMembers(getRelation("generic").getMembers()));
|
---|
57 | final String[] expected = {"t1w4", "t1w3", "t1w2", "t1w1", "t1w7", "t1w6", "t1w5", "t1n1", "t1n2"};
|
---|
58 | // expect nodes to be sorted correctly
|
---|
59 | Assert.assertEquals(expected[7], actual[7]);
|
---|
60 | Assert.assertEquals(expected[8], actual[8]);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Test
|
---|
64 | public void testAssociatedStreet() {
|
---|
65 | String[] actual = getNames(sorter.sortMembers(getRelation("associatedStreet").getMembers()));
|
---|
66 | Assert.assertArrayEquals(new String[] {"t2w1", "t2w2", "t2n1", "t2n2", "t2n3", "t2n4"}, actual);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Test
|
---|
70 | public void testStreet() {
|
---|
71 | String[] actual = getNames(sorter.sortMembers(getRelation("street").getMembers()));
|
---|
72 | Assert.assertArrayEquals(new String[]{"t2w1", "t2w2", "t2n1", "t2n2", "t2n3", "t2n4", "playground", "tree"}, actual);
|
---|
73 | }
|
---|
74 | }
|
---|