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.Arrays;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.junit.Assert;
|
---|
11 | import org.junit.BeforeClass;
|
---|
12 | import org.junit.Test;
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
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 | public class WayConnectionTypeCalculatorTest {
|
---|
22 |
|
---|
23 | private RelationSorter sorter = new RelationSorter();
|
---|
24 | private WayConnectionTypeCalculator wayConnectionTypeCalculator = new WayConnectionTypeCalculator();
|
---|
25 | private static DataSet testDataset;
|
---|
26 |
|
---|
27 | @BeforeClass
|
---|
28 | public static void loadData() throws IllegalDataException, IOException {
|
---|
29 | Main.initApplicationPreferences();
|
---|
30 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
31 | try (InputStream fis = new FileInputStream("data_nodist/relation_sort.osm")) {
|
---|
32 | testDataset = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | private Relation getRelation(String testType) {
|
---|
37 | for (Relation r: testDataset.getRelations()) {
|
---|
38 | if (testType.equals(r.get("test")))
|
---|
39 | return r;
|
---|
40 | }
|
---|
41 | return null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private String getConnections(List<WayConnectionType> connections) {
|
---|
45 | String[] result = new String[connections.size()];
|
---|
46 | for (int i = 0; i < result.length; i++) {
|
---|
47 | WayConnectionType wc = connections.get(i);
|
---|
48 |
|
---|
49 | if (wc.isValid()) {
|
---|
50 | StringBuilder sb = new StringBuilder();
|
---|
51 | if (wc.isLoop) {
|
---|
52 | sb.append("L");
|
---|
53 | }
|
---|
54 | if (wc.isOnewayLoopForwardPart) {
|
---|
55 | sb.append("FP");
|
---|
56 | }
|
---|
57 | if (wc.isOnewayLoopBackwardPart) {
|
---|
58 | sb.append("BP");
|
---|
59 | }
|
---|
60 | if (wc.isOnewayHead) {
|
---|
61 | sb.append("H");
|
---|
62 | }
|
---|
63 | if (wc.isOnewayTail) {
|
---|
64 | sb.append("T");
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (sb.length() > 0) {
|
---|
68 | sb.append(" ");
|
---|
69 | }
|
---|
70 | sb.append(wc.direction);
|
---|
71 | result[i] = sb.toString();
|
---|
72 |
|
---|
73 | } else {
|
---|
74 | result[i] = "I";
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|
78 | return Arrays.toString(result);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Test
|
---|
82 | public void testGeneric() {
|
---|
83 | Relation relation = getRelation("generic");
|
---|
84 | String actual = getConnections(wayConnectionTypeCalculator.updateLinks(relation.getMembers()));
|
---|
85 | Assert.assertEquals("[NONE, NONE, FORWARD, FORWARD, NONE, NONE, NONE, I, I]", actual);
|
---|
86 | actual = getConnections(wayConnectionTypeCalculator.updateLinks(sorter.sortMembers(relation.getMembers())));
|
---|
87 | Assert.assertEquals("[FORWARD, FORWARD, FORWARD, FORWARD, BACKWARD, BACKWARD, NONE, I, I]", actual);
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Test
|
---|
91 | public void testAssociatedStreet() {
|
---|
92 | Relation relation = getRelation("associatedStreet");
|
---|
93 | String actual = getConnections(wayConnectionTypeCalculator.updateLinks(relation.getMembers()));
|
---|
94 | Assert.assertEquals("[NONE, I, I, I, NONE, I]", actual);
|
---|
95 | actual = getConnections(wayConnectionTypeCalculator.updateLinks(sorter.sortMembers(relation.getMembers())));
|
---|
96 | Assert.assertEquals("[FORWARD, FORWARD, I, I, I, I]", actual);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void testLoop() {
|
---|
101 | Relation relation = getRelation("loop");
|
---|
102 | String actual = getConnections(wayConnectionTypeCalculator.updateLinks(relation.getMembers()));
|
---|
103 | Assert.assertEquals("[FPH FORWARD, FP FORWARD, NONE, FPH FORWARD, NONE, FPH FORWARD, NONE]", actual);
|
---|
104 | //TODO Sorting doesn't work well in this case
|
---|
105 | actual = getConnections(wayConnectionTypeCalculator.updateLinks(sorter.sortMembers(relation.getMembers())));
|
---|
106 | Assert.assertEquals("[BACKWARD, BACKWARD, BACKWARD, FPH FORWARD, FPH FORWARD, FPH FORWARD, FPH FORWARD]", actual);
|
---|
107 | }
|
---|
108 | }
|
---|