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