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