1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import org.junit.Assert;
|
---|
5 | import org.junit.Rule;
|
---|
6 | import org.junit.Test;
|
---|
7 | import org.openstreetmap.josm.Main;
|
---|
8 | import org.openstreetmap.josm.data.Bounds;
|
---|
9 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
10 |
|
---|
11 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Unit tests of {@link OsmUrlToBounds} class.
|
---|
15 | */
|
---|
16 | public class OsmUrlToBoundsTest {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Setup test.
|
---|
20 | */
|
---|
21 | @Rule
|
---|
22 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
23 | public JOSMTestRules test = new JOSMTestRules();
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Test for {@link OsmUrlToBounds#positionToBounds}.
|
---|
27 | */
|
---|
28 | @Test
|
---|
29 | public void testPositionToBounds() {
|
---|
30 | Assert.assertEquals(new Bounds(51.7167359,8.7573485,51.720724,8.7659315),
|
---|
31 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17));
|
---|
32 | Assert.assertEquals(new Bounds(40.8609329,-75.7523458,40.8633671,-75.7480542),
|
---|
33 | OsmUrlToBounds.positionToBounds(40.86215, -75.75020, 18));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * data for {@link #testParse}
|
---|
38 | */
|
---|
39 | private static final ParseTestItem[] parseTestData = {
|
---|
40 | new ParseTestItem("https://www.openstreetmap.org", null),
|
---|
41 | new ParseTestItem("geo:12.34,56.78?z=9",
|
---|
42 | OsmUrlToBounds.positionToBounds(12.34, 56.78, 9)),
|
---|
43 | new ParseTestItem("https://www.openstreetmap.org/?bbox=-0.489,51.28,0.236,51.686",
|
---|
44 | new Bounds(51.28, -0.489, 51.686, 0.236)),
|
---|
45 | new ParseTestItem("https://www.openstreetmap.org/?minlon=-0.489&minlat=51.28&maxlon=0.236&maxlat=51.686",
|
---|
46 | new Bounds(51.28, -0.489, 51.686, 0.236)),
|
---|
47 | new ParseTestItem("https://www.openstreetmap.org/?maxlat=51.686&maxlon=0.236&minlat=51.28&minlon=-0.489",
|
---|
48 | new Bounds(51.28, -0.489, 51.686, 0.236)),
|
---|
49 | new ParseTestItem("https://www.openstreetmap.org/?zoom=17&lat=51.71873&lon=8.76164",
|
---|
50 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
|
---|
51 | new ParseTestItem("https://www.openstreetmap.org/?lon=8.76164&lat=51.71873&zoom=17&foo",
|
---|
52 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
|
---|
53 | new ParseTestItem("https://www.openstreetmap.org/?mlon=8.76164&mlat=51.71873",
|
---|
54 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 18)),
|
---|
55 | new ParseTestItem("http://osm.org/go/euulwp",
|
---|
56 | OsmUrlToBounds.positionToBounds(51.48262023925781, -0.29937744140625, 8)),
|
---|
57 | new ParseTestItem("https://www.openstreetmap.org/#map=17/51.71873/8.76164",
|
---|
58 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
|
---|
59 | new ParseTestItem("https://www.openstreetmap.org/#map=17/51.71873/8.76164&layers=CN",
|
---|
60 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 17)),
|
---|
61 | new ParseTestItem("https%3A%2F%2Fwww.openstreetmap.org%2F%23map%3D16%2F51.71873%2F8.76164",
|
---|
62 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 16)),
|
---|
63 | new ParseTestItem("https%3A%2F%2Fwww.openstreetmap.org%2F%23map%3D16%2F51.71873%2F8.76164%26layers%3DCN",
|
---|
64 | OsmUrlToBounds.positionToBounds(51.71873, 8.76164, 16)),
|
---|
65 | new ParseTestItem("https://www.openstreetmap.org/?note=26325#map=18/40.86215/-75.75020",
|
---|
66 | OsmUrlToBounds.positionToBounds(40.86215, -75.75020, 18)),
|
---|
67 | new ParseTestItem("https://www.openstreetmap.org/?note=26325#map=18/40.86215/-75.75020&layers=N",
|
---|
68 | OsmUrlToBounds.positionToBounds(40.86215, -75.75020, 18)),
|
---|
69 | new ParseTestItem("https://www.openstreetmap.org/?mlat=51.5&mlon=-0.01#map=10/51.4831/-0.1270",
|
---|
70 | OsmUrlToBounds.positionToBounds(51.4831, -0.1270, 10)),
|
---|
71 | new ParseTestItem("https://www.openstreetmap.org/?mlat=51.5&mlon=-0.01#map=10/51.4831/-0.3509&layers=T",
|
---|
72 | OsmUrlToBounds.positionToBounds(51.4831, -0.3509, 10)),
|
---|
73 | new ParseTestItem("https://www.openstreetmap.org/#map", null),
|
---|
74 | new ParseTestItem("https://www.openstreetmap.org/#map=foo", null),
|
---|
75 | new ParseTestItem("https://www.openstreetmap.org/#map=fooz/foolat/foolon", null)
|
---|
76 | };
|
---|
77 |
|
---|
78 | private static class ParseTestItem {
|
---|
79 | public String url;
|
---|
80 | public Bounds bounds;
|
---|
81 |
|
---|
82 | ParseTestItem(String url, Bounds bounds) {
|
---|
83 | this.url = url;
|
---|
84 | this.bounds = bounds;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Test URL parsing
|
---|
90 | */
|
---|
91 | @Test
|
---|
92 | public void testParse() {
|
---|
93 | for (ParseTestItem item : parseTestData) {
|
---|
94 | Bounds bounds = null;
|
---|
95 | try {
|
---|
96 | bounds = OsmUrlToBounds.parse(item.url);
|
---|
97 | } catch (IllegalArgumentException e) {
|
---|
98 | // Ignore. check if bounds is null after
|
---|
99 | Main.trace(e);
|
---|
100 | }
|
---|
101 | Assert.assertEquals(item.url, item.bounds, bounds);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Test for {@link OsmUrlToBounds#getZoom}.
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | public void testGetZoom() {
|
---|
110 | Assert.assertEquals(4, OsmUrlToBounds.getZoom(OsmUrlToBounds.positionToBounds(0, 0, 4)));
|
---|
111 | Assert.assertEquals(10, OsmUrlToBounds.getZoom(OsmUrlToBounds.positionToBounds(5, 5, 10)));
|
---|
112 | Assert.assertEquals(18, OsmUrlToBounds.getZoom(OsmUrlToBounds.positionToBounds(40, 20, 18)));
|
---|
113 | }
|
---|
114 | }
|
---|