Debian Codegraph @

src/test/java/ezvcard/util/GeoUriTest.java

raw symbols SCIP
1 package ezvcard.util;
2
3 import static ezvcard.util.TestUtils.assertEqualsAndHash;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNull;
6
7 import java.net.URI;
8 import java.util.HashMap;
9 import java.util.LinkedHashMap;
10 import java.util.Locale;
11 import java.util.Map;
12
13 import nl.jqno.equalsverifier.EqualsVerifier;
14
15 import org.junit.ClassRule;
16 import org.junit.Test;
17
18 /*
19 Copyright (c) 2012-2023, Michael Angstadt
20 All rights reserved.
21
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24
25 1. Redistributions of source code must retain the above copyright notice, this
26 list of conditions and the following disclaimer.
27 2. Redistributions in binary form must reproduce the above copyright notice,
28 this list of conditions and the following disclaimer in the documentation
29 and/or other materials provided with the distribution.
30
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
35 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42 The views and conclusions contained in the software and documentation are those
43 of the authors and should not be interpreted as representing official policies,
44 either expressed or implied, of the FreeBSD Project.
45 */
46
47 /**
48 * @author Michael Angstadt
49 */
50 public class GeoUriTest {
51 //Germany uses "," as the decimal separator, but "." should still be used in a geo URI
52 @ClassRule
53 public static final DefaultLocaleRule localeRule = new DefaultLocaleRule(Locale.GERMANY);
54
55 @Test
56 public void parse_all() {
57 GeoUri uri = GeoUri.parse("geo:12.34,56.78,-21.43;crs=wgs84;u=12;param=value");
58 assertEquals(12.34, uri.getCoordA(), 0.01);
59 assertEquals(56.78, uri.getCoordB(), 0.01);
60 assertEquals(-21.43, uri.getCoordC().doubleValue(), 0.01);
61 assertEquals("wgs84", uri.getCrs());
62 assertEquals(12, uri.getUncertainty().doubleValue(), 0.01);
63 assertEquals("value", uri.getParameter("param"));
64 Map<String, String> params = new LinkedHashMap<>();
65 params.put("param", "value");
66 assertEquals(params, uri.getParameters());
67 }
68
69 @Test
70 public void parse_no_params() {
71 GeoUri uri = GeoUri.parse("geo:12.34,56.78,-21.43;crs=wgs84;u=12");
72 assertEquals(12.34, uri.getCoordA(), 0.01);
73 assertEquals(56.78, uri.getCoordB(), 0.01);
74 assertEquals(-21.43, uri.getCoordC().doubleValue(), 0.01);
75 assertEquals("wgs84", uri.getCrs());
76 assertEquals(12, uri.getUncertainty().doubleValue(), 0.01);
77 assertNull(uri.getParameter("param"));
78 Map<String, String> params = new LinkedHashMap<>();
79 assertEquals(params, uri.getParameters());
80 }
81
82 @Test
83 public void parse_no_params_or_u() {
84 GeoUri uri = GeoUri.parse("geo:12.34,56.78,-21.43;crs=wgs84");
85 assertEquals(12.34, uri.getCoordA(), 0.01);
86 assertEquals(56.78, uri.getCoordB(), 0.01);
87 assertEquals(-21.43, uri.getCoordC().doubleValue(), 0.01);
88 assertEquals("wgs84", uri.getCrs());
89 assertNull(uri.getUncertainty());
90 assertNull(uri.getParameter("param"));
91 Map<String, String> params = new LinkedHashMap<>();
92 assertEquals(params, uri.getParameters());
93 }
94
95 @Test
96 public void parse_no_params_or_crs() {
97 GeoUri uri = GeoUri.parse("geo:12.34,56.78,-21.43;u=12");
98 assertEquals(12.34, uri.getCoordA(), 0.01);
99 assertEquals(56.78, uri.getCoordB(), 0.01);
100 assertEquals(-21.43, uri.getCoordC().doubleValue(), 0.01);
101 assertNull(uri.getCrs());
102 assertEquals(12, uri.getUncertainty().doubleValue(), 0.01);
103 assertNull(uri.getParameter("param"));
104 Map<String, String> params = new LinkedHashMap<>();
105 assertEquals(params, uri.getParameters());
106 }
107
108 @Test
109 public void parse_no_params_or_u_or_crs() {
110 GeoUri uri = GeoUri.parse("geo:12.34,56.78,-21.43");
111 assertEquals(12.34, uri.getCoordA(), 0.01);
112 assertEquals(56.78, uri.getCoordB(), 0.01);
113 assertEquals(-21.43, uri.getCoordC().doubleValue(), 0.01);
114 assertNull(uri.getCrs());
115 assertNull(uri.getUncertainty());
116 assertNull(uri.getParameter("param"));
117 Map<String, String> params = new LinkedHashMap<>();
118 assertEquals(params, uri.getParameters());
119 }
120
121 @Test
122 public void parse_no_params_or_u_or_crs_or_coordC() {
123 GeoUri uri = GeoUri.parse("geo:12.34,56.78");
124 assertEquals(12.34, uri.getCoordA(), 0.01);
125 assertEquals(56.78, uri.getCoordB(), 0.01);
126 assertNull(uri.getCoordC());
127 assertNull(uri.getCrs());
128 assertNull(uri.getUncertainty());
129 assertNull(uri.getParameter("param"));
130 Map<String, String> params = new LinkedHashMap<>();
131 assertEquals(params, uri.getParameters());
132 }
133
134 @Test
135 public void parse_invalid_uncertainty() {
136 GeoUri uri = GeoUri.parse("geo:12.34,56.78;u=invalid");
137 assertEquals(12.34, uri.getCoordA(), 0.01);
138 assertEquals(56.78, uri.getCoordB(), 0.01);
139 assertNull(uri.getCoordC());
140 assertNull(uri.getCrs());
141 assertNull(uri.getUncertainty());
142
143 Map<String, String> expectedParams = new HashMap<>();
144 expectedParams.put("u", "invalid");
145 assertEquals(expectedParams, uri.getParameters());
146 }
147
148 @Test(expected = IllegalArgumentException.class)
149 public void parse_no_params_or_u_or_crs_or_coordsC_or_coordB() {
150 GeoUri.parse("geo:12.34");
151 }
152
153 @Test(expected = IllegalArgumentException.class)
154 public void parse_no_params_or_u_or_crs_or_coordsC_or_coordB_or_coordA() {
155 GeoUri.parse("geo:");
156 }
157
158 @Test(expected = IllegalArgumentException.class)
159 public void parse_not_geo_uri() {
160 GeoUri.parse("http://www.ietf.org");
161 }
162
163 @Test
164 public void parse_decode_special_chars_in_param_value() {
165 GeoUri uri = GeoUri.parse("geo:12.34,56.78;param=with%20%3d%20special%20&%20chars");
166 assertEquals(12.34, uri.getCoordA(), 0.01);
167 assertEquals(56.78, uri.getCoordB(), 0.01);
168 assertEquals("with = special & chars", uri.getParameter("param"));
169 }
170
171 @Test
172 public void builder_crs() {
173 GeoUri uri = new GeoUri.Builder(12.34, 56.78).crs("123-valid").build();
174 assertEquals("123-valid", uri.getCrs());
175 }
176
177 @Test(expected = IllegalArgumentException.class)
178 public void builder_crs_validate_chars() {
179 new GeoUri.Builder(12.34, 56.78).crs("!not-valid!");
180 }
181
182 @Test
183 public void builder_uncertainty() {
184 GeoUri uri = new GeoUri.Builder(12.34, 56.78).uncertainty(12.0).build();
185 assertEquals(12.0, uri.getUncertainty().doubleValue(), 0.01);
186 }
187
188 @Test
189 public void builder_parameter() {
190 GeoUri uri = new GeoUri.Builder(12.34, 56.78).parameter("one", "1").parameter("two", "2").parameter("one", null).build();
191 assertNull(uri.getParameter("one")); //parameter was removed
192 assertEquals("2", uri.getParameter("two"));
193 assertNull(uri.getParameter("three"));
194
195 Map<String, String> expected = new HashMap<>();
196 expected.put("two", "2");
197 assertEquals(expected, uri.getParameters());
198 }
199
200 @Test(expected = UnsupportedOperationException.class)
201 public void getParameters_unmodifiable() {
202 GeoUri uri = new GeoUri.Builder(12.34, 56.78).build();
203 uri.getParameters().put("one", "1");
204 }
205
206 @Test(expected = IllegalArgumentException.class)
207 public void builder_parameter_validate_param_name_chars() {
208 new GeoUri.Builder(12.34, 56.78).parameter("!not-valid!", "value");
209 }
210
211 @Test
212 public void builder_parameter_preserve_insertion_order() {
213 GeoUri.Builder builder = new GeoUri.Builder(12.34, 56.78);
214 for (int i = 9; i >= 0; i--) {
215 builder.parameter(i + "p", i + "v");
216 }
217
218 GeoUri uri = builder.build();
219 int i = 9;
220 for (Map.Entry<String, String> param : uri.getParameters().entrySet()) {
221 assertEquals(i + "p", param.getKey());
222 assertEquals(i + "v", param.getValue());
223 i--;
224 }
225
226 assertEquals("geo:12.34,56.78;9p=9v;8p=8v;7p=7v;6p=6v;5p=5v;4p=4v;3p=3v;2p=2v;1p=1v;0p=0v", uri.toString());
227 }
228
229 @Test
230 public void builder_copy_constructor() {
231 GeoUri orig = new GeoUri.Builder(12.34, 56.78).coordC(90.12).crs("crs").parameter("param", "value").uncertainty(0.01).build();
232 GeoUri copy = new GeoUri.Builder(orig).build();
233
234 assertEquals(orig.getCoordA(), copy.getCoordA());
235 assertEquals(orig.getCoordB(), copy.getCoordB());
236 assertEquals(orig.getCoordC(), copy.getCoordC());
237 assertEquals(orig.getCrs(), copy.getCrs());
238 assertEquals(orig.getParameters(), copy.getParameters());
239 assertEquals(orig.getUncertainty(), copy.getUncertainty());
240 }
241
242 @Test
243 public void toUri() {
244 GeoUri uri = new GeoUri.Builder(12.34, 45.67).build();
245
246 URI theUri = uri.toUri();
247 assertEquals("geo", theUri.getScheme());
248 assertEquals("12.34,45.67", theUri.getRawSchemeSpecificPart());
249 }
250
251 @Test
252 public void toString_() {
253 GeoUri uri = new GeoUri.Builder(12.34, 45.67).coordC(-21.43).crs("theCrs").uncertainty(12.0).parameter("param", "value").parameter("param2", "value2").build();
254 assertEquals("geo:12.34,45.67,-21.43;crs=theCrs;u=12.0;param=value;param2=value2", uri.toString());
255 }
256
257 @Test
258 public void toString_special_chars_in_param_value() {
259 GeoUri uri = new GeoUri.Builder(12.34, 45.67).parameter("param", "with = special & chars " + (char) 128).build();
260 assertEquals("geo:12.34,45.67;param=with%20%3d%20special%20&%20chars%20%80", uri.toString());
261 }
262
263 @Test
264 public void toString_missing_required_fields() {
265 GeoUri uri = new GeoUri.Builder(null, null).build();
266 assertEquals("geo:0.0,0.0", uri.toString());
267
268 uri = new GeoUri.Builder(12.34, null).build();
269 assertEquals("geo:12.34,0.0", uri.toString());
270
271 uri = new GeoUri.Builder(null, 45.67).build();
272 assertEquals("geo:0.0,45.67", uri.toString());
273 }
274
275 @Test
276 public void toString_do_not_display_wgs84() {
277 GeoUri uri = new GeoUri.Builder(12.34, 45.67).crs("wgs84").build();
278 assertEquals("geo:12.34,45.67", uri.toString());
279
280 //case-insensitive
281 uri = new GeoUri.Builder(12.34, 45.67).crs("WgS84").build();
282 assertEquals("geo:12.34,45.67", uri.toString());
283 }
284
285 @Test
286 public void toString_precision() {
287 GeoUri uri = new GeoUri.Builder(12.3488888888, 45.6711111111).build();
288 assertEquals("geo:12.348889,45.671111", uri.toString());
289 assertEquals("geo:12.3489,45.6711", uri.toString(4));
290 assertEquals("geo:12,46", uri.toString(0));
291 assertEquals("geo:12,46", uri.toString(-1));
292 }
293
294 @Test
295 public void equals_contract() {
296 EqualsVerifier.forClass(GeoUri.class).usingGetClass().verify();
297 }
298
299 @Test
300 public void equals_ignore_case() {
301 GeoUri one = new GeoUri.Builder(1.0, 2.0).crs("crs").parameter("name", "value").build();
302 GeoUri two = new GeoUri.Builder(1.0, 2.0).crs("CRS").parameter("NAME", "VALUE").build();
303 assertEqualsAndHash(one, two);
304 }
305 }