source: josm/trunk/test/unit/org/openstreetmap/josm/data/osm/QuadBucketsTest.java@ 9130

Last change on this file since 9130 was 8857, checked in by Don-vip, 9 years ago

improve/cleanup unit tests

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import java.io.FileInputStream;
5import java.io.InputStream;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Iterator;
9import java.util.List;
10
11import org.fest.reflect.core.Reflection;
12import org.fest.reflect.reference.TypeRef;
13import org.junit.Assert;
14import org.junit.BeforeClass;
15import org.junit.Test;
16import org.openstreetmap.josm.JOSMFixture;
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.projection.Projections;
20import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
21import org.openstreetmap.josm.io.OsmReader;
22
23public class QuadBucketsTest {
24
25 /**
26 * Setup test.
27 */
28 @BeforeClass
29 public static void init() {
30 JOSMFixture.createUnitTestFixture().init();
31 }
32
33 private void removeAllTest(DataSet ds) {
34 List<Node> allNodes = new ArrayList<>(ds.getNodes());
35 List<Way> allWays = new ArrayList<>(ds.getWays());
36 List<Relation> allRelations = new ArrayList<>(ds.getRelations());
37
38 QuadBuckets<Node> nodes = Reflection.field("nodes").ofType(new TypeRef<QuadBuckets<Node>>() {}).in(ds).get();
39 QuadBuckets<Way> ways = Reflection.field("ways").ofType(new TypeRef<QuadBuckets<Way>>() {}).in(ds).get();
40 Collection<Relation> relations = Reflection.field("relations").ofType(new TypeRef<Collection<Relation>>() {}).in(ds).get();
41
42 int expectedCount = allNodes.size();
43 for (OsmPrimitive o: allNodes) {
44 ds.removePrimitive(o);
45 checkIterator(nodes, --expectedCount);
46 }
47 expectedCount = allWays.size();
48 for (OsmPrimitive o: allWays) {
49 ds.removePrimitive(o);
50 checkIterator(ways, --expectedCount);
51 }
52 for (OsmPrimitive o: allRelations) {
53 ds.removePrimitive(o);
54 }
55 Assert.assertTrue(nodes.isEmpty());
56 Assert.assertTrue(ways.isEmpty());
57 Assert.assertTrue(relations.isEmpty());
58 }
59
60 private void checkIterator(Collection<? extends OsmPrimitive> col, int expectedCount) {
61 int count = 0;
62 Iterator<? extends OsmPrimitive> it = col.iterator();
63 while (it.hasNext()) {
64 count++;
65 it.next();
66 }
67 Assert.assertEquals(expectedCount, count);
68 }
69
70 @Test
71 public void testRemove() throws Exception {
72 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
73 try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
74 DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
75 removeAllTest(ds);
76 }
77 }
78
79 @Test
80 public void testMove() throws Exception {
81 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); // Mercator
82 try (InputStream fis = new FileInputStream("data_nodist/restriction.osm")) {
83 DataSet ds = OsmReader.parseDataSet(fis, NullProgressMonitor.INSTANCE);
84
85 for (Node n: ds.getNodes()) {
86 n.setCoor(new LatLon(10, 10));
87 }
88
89 removeAllTest(ds);
90 }
91 }
92}
Note: See TracBrowser for help on using the repository browser.