[5630] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs.relation.sort;
|
---|
| 3 |
|
---|
[7033] | 4 | import java.io.IOException;
|
---|
| 5 | import java.io.InputStream;
|
---|
[15034] | 6 | import java.nio.file.Files;
|
---|
| 7 | import java.nio.file.Paths;
|
---|
[5630] | 8 | import java.util.List;
|
---|
| 9 |
|
---|
| 10 | import org.junit.Assert;
|
---|
[10876] | 11 | import org.junit.Before;
|
---|
| 12 | import org.junit.Rule;
|
---|
[5630] | 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;
|
---|
[10876] | 20 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
[5630] | 21 |
|
---|
[10876] | 22 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
| 23 |
|
---|
[8857] | 24 | /**
|
---|
| 25 | * Unit tests of {@link RelationSorter} class.
|
---|
| 26 | */
|
---|
[5630] | 27 | public class RelationSorterTest {
|
---|
| 28 |
|
---|
[8857] | 29 | private final RelationSorter sorter = new RelationSorter();
|
---|
[10876] | 30 | private DataSet testDataset;
|
---|
[5630] | 31 |
|
---|
[10876] | 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) {
|
---|
[16006] | 47 | try (InputStream fis = Files.newInputStream(Paths.get("nodist/data/relation_sort.osm"))) {
|
---|
[10876] | 48 | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
| 49 | }
|
---|
[7033] | 50 | }
|
---|
[5630] | 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()];
|
---|
[8510] | 63 | for (int i = 0; i < result.length; i++) {
|
---|
[5630] | 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()));
|
---|
[6461] | 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]);
|
---|
[5630] | 76 | }
|
---|
| 77 |
|
---|
| 78 | @Test
|
---|
| 79 | public void testAssociatedStreet() {
|
---|
| 80 | String[] actual = getNames(sorter.sortMembers(getRelation("associatedStreet").getMembers()));
|
---|
[6461] | 81 | Assert.assertArrayEquals(new String[] {"t2w1", "t2w2", "t2n1", "t2n2", "t2n3", "t2n4"}, actual);
|
---|
[5630] | 82 | }
|
---|
| 83 |
|
---|
[6461] | 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 | }
|
---|
[5630] | 89 | }
|
---|