1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.io.FileInputStream;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Iterator;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import org.fest.reflect.core.Reflection;
|
---|
13 | import org.fest.reflect.reference.TypeRef;
|
---|
14 | import org.junit.Assert;
|
---|
15 | import org.junit.BeforeClass;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.JOSMFixture;
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
20 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
21 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.io.OsmReader;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Unit tests of {@link QuadBuckets}.
|
---|
26 | */
|
---|
27 | public class QuadBucketsTest {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Setup test.
|
---|
31 | */
|
---|
32 | @BeforeClass
|
---|
33 | public static void init() {
|
---|
34 | JOSMFixture.createUnitTestFixture().init();
|
---|
35 | }
|
---|
36 |
|
---|
37 | private void removeAllTest(DataSet ds) {
|
---|
38 | List<Node> allNodes = new ArrayList<>(ds.getNodes());
|
---|
39 | List<Way> allWays = new ArrayList<>(ds.getWays());
|
---|
40 | List<Relation> allRelations = new ArrayList<>(ds.getRelations());
|
---|
41 |
|
---|
42 | QuadBuckets<Node> nodes = Reflection.field("nodes").ofType(new TypeRef<QuadBuckets<Node>>() {}).in(ds).get();
|
---|
43 | QuadBuckets<Way> ways = Reflection.field("ways").ofType(new TypeRef<QuadBuckets<Way>>() {}).in(ds).get();
|
---|
44 | Collection<Relation> relations = Reflection.field("relations").ofType(new TypeRef<Collection<Relation>>() {}).in(ds).get();
|
---|
45 |
|
---|
46 | int expectedCount = allNodes.size();
|
---|
47 | for (OsmPrimitive o: allNodes) {
|
---|
48 | ds.removePrimitive(o);
|
---|
49 | checkIterator(nodes, --expectedCount);
|
---|
50 | }
|
---|
51 | expectedCount = allWays.size();
|
---|
52 | for (OsmPrimitive o: allWays) {
|
---|
53 | ds.removePrimitive(o);
|
---|
54 | checkIterator(ways, --expectedCount);
|
---|
55 | }
|
---|
56 | for (OsmPrimitive o: allRelations) {
|
---|
57 | ds.removePrimitive(o);
|
---|
58 | }
|
---|
59 | Assert.assertTrue(nodes.isEmpty());
|
---|
60 | Assert.assertTrue(ways.isEmpty());
|
---|
61 | Assert.assertTrue(relations.isEmpty());
|
---|
62 | }
|
---|
63 |
|
---|
64 | private void checkIterator(Collection<? extends OsmPrimitive> col, int expectedCount) {
|
---|
65 | int count = 0;
|
---|
66 | Iterator<? extends OsmPrimitive> it = col.iterator();
|
---|
67 | while (it.hasNext()) {
|
---|
68 | count++;
|
---|
69 | it.next();
|
---|
70 | }
|
---|
71 | Assert.assertEquals(expectedCount, count);
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Test
|
---|
75 | public void testRemove() throws Exception {
|
---|
76 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
77 | try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
|
---|
78 | DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
79 | removeAllTest(ds);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Test
|
---|
84 | public void testMove() throws Exception {
|
---|
85 | Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
|
---|
86 | try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
|
---|
87 | DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
|
---|
88 |
|
---|
89 | for (Node n: ds.getNodes()) {
|
---|
90 | n.setCoor(new LatLon(10, 10));
|
---|
91 | }
|
---|
92 |
|
---|
93 | removeAllTest(ds);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Test handling of objects with invalid bbox
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | public void testSpecialBBox() {
|
---|
102 | QuadBuckets<Node> qbNodes = new QuadBuckets<>();
|
---|
103 | QuadBuckets<Way> qbWays = new QuadBuckets<>();
|
---|
104 | Way w1 = new Way(1);
|
---|
105 | Way w2 = new Way(2);
|
---|
106 | Way w3 = new Way(3);
|
---|
107 | Node n1 = new Node(1);
|
---|
108 | Node n2 = new Node(2); n2.setCoor(new LatLon(10, 20));
|
---|
109 | Node n3 = new Node(3); n2.setCoor(new LatLon(20, 30));
|
---|
110 | w2.setNodes(Arrays.asList(n1));
|
---|
111 | w3.setNodes(Arrays.asList(n1, n2, n3));
|
---|
112 |
|
---|
113 | qbNodes.add(n1);
|
---|
114 | qbNodes.add(n2);
|
---|
115 | Assert.assertEquals(2, qbNodes.size());
|
---|
116 | Assert.assertTrue(qbNodes.contains(n1));
|
---|
117 | Assert.assertTrue(qbNodes.contains(n2));
|
---|
118 | Assert.assertFalse(qbNodes.contains(n3));
|
---|
119 | qbNodes.remove(n1);
|
---|
120 | Assert.assertEquals(1, qbNodes.size());
|
---|
121 | Assert.assertFalse(qbNodes.contains(n1));
|
---|
122 | Assert.assertTrue(qbNodes.contains(n2));
|
---|
123 | qbNodes.remove(n2);
|
---|
124 | Assert.assertEquals(0, qbNodes.size());
|
---|
125 | Assert.assertFalse(qbNodes.contains(n1));
|
---|
126 | Assert.assertFalse(qbNodes.contains(n2));
|
---|
127 |
|
---|
128 | qbNodes.addAll(Arrays.asList(n1, n2, n3));
|
---|
129 | qbNodes.removeAll(Arrays.asList(n1, n3));
|
---|
130 | Assert.assertEquals(1, qbNodes.size());
|
---|
131 | Assert.assertTrue(qbNodes.contains(n2));
|
---|
132 |
|
---|
133 | qbWays.add(w1);
|
---|
134 | qbWays.add(w2);
|
---|
135 | qbWays.add(w3);
|
---|
136 | Assert.assertEquals(3, qbWays.size());
|
---|
137 | Assert.assertTrue(qbWays.contains(w1));
|
---|
138 | Assert.assertTrue(qbWays.contains(w2));
|
---|
139 | Assert.assertTrue(qbWays.contains(w3));
|
---|
140 | qbWays.remove(w1);
|
---|
141 | Assert.assertEquals(2, qbWays.size());
|
---|
142 | Assert.assertFalse(qbWays.contains(w1));
|
---|
143 | Assert.assertTrue(qbWays.contains(w2));
|
---|
144 | Assert.assertTrue(qbWays.contains(w3));
|
---|
145 | qbWays.remove(w2);
|
---|
146 | Assert.assertEquals(1, qbWays.size());
|
---|
147 | Assert.assertFalse(qbWays.contains(w1));
|
---|
148 | Assert.assertFalse(qbWays.contains(w2));
|
---|
149 | Assert.assertTrue(qbWays.contains(w3));
|
---|
150 | qbWays.remove(w3);
|
---|
151 | Assert.assertEquals(0, qbWays.size());
|
---|
152 | Assert.assertFalse(qbWays.contains(w1));
|
---|
153 | Assert.assertFalse(qbWays.contains(w2));
|
---|
154 | Assert.assertFalse(qbWays.contains(w3));
|
---|
155 |
|
---|
156 | qbWays.clear();
|
---|
157 | Assert.assertEquals(0, qbWays.size());
|
---|
158 | List<Way> allWays = new ArrayList<>(Arrays.asList(w1, w2, w3));
|
---|
159 | qbWays.addAll(allWays);
|
---|
160 | Assert.assertEquals(3, qbWays.size());
|
---|
161 | int count = 0;
|
---|
162 | for (Way w : qbWays) {
|
---|
163 | Assert.assertTrue(allWays.contains(w));
|
---|
164 | count++;
|
---|
165 | }
|
---|
166 | Assert.assertEquals(3, count);
|
---|
167 | // test remove with iterator
|
---|
168 | Iterator<Way> iter = qbWays.iterator();
|
---|
169 | while (iter.hasNext()) {
|
---|
170 | iter.next();
|
---|
171 | iter.remove();
|
---|
172 | count--;
|
---|
173 | Assert.assertEquals(count, qbWays.size());
|
---|
174 | }
|
---|
175 | Assert.assertEquals(0, qbWays.size());
|
---|
176 | }
|
---|
177 | }
|
---|