source: josm/trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java@ 17360

Last change on this file since 17360 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 20.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertNull;
7import static org.junit.jupiter.api.Assertions.assertSame;
8import static org.junit.jupiter.api.Assertions.assertThrows;
9import static org.junit.jupiter.api.Assertions.assertTrue;
10
11import java.io.File;
12import java.io.IOException;
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.Collections;
16import java.util.HashMap;
17import java.util.LinkedList;
18import java.util.List;
19import java.util.Locale;
20import java.util.Map;
21import java.util.TreeMap;
22import java.util.regex.Pattern;
23
24import org.junit.jupiter.api.Test;
25import org.junit.jupiter.api.extension.RegisterExtension;
26import org.openstreetmap.josm.testutils.JOSMTestRules;
27
28import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29import net.trajano.commons.testing.UtilityClassTestUtil;
30
31/**
32 * Unit tests of {@link Utils} class.
33 */
34class UtilsTest {
35 /**
36 * Use default, basic test rules.
37 */
38 @RegisterExtension
39 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
40 public JOSMTestRules rules = new JOSMTestRules();
41
42 /**
43 * Tests that {@code Utils} satisfies utility class criteria.
44 * @throws ReflectiveOperationException if an error occurs
45 */
46 @Test
47 void testUtilityClass() throws ReflectiveOperationException {
48 UtilityClassTestUtil.assertUtilityClassWellDefined(Utils.class);
49 }
50
51 /**
52 * Test of {@link Utils#strip} method.
53 */
54 @Test
55 void testStrip() {
56 // CHECKSTYLE.OFF: SingleSpaceSeparator
57 final String someWhite =
58 "\u00A0"+ // SPACE_SEPARATOR
59 "\u2007"+ // LINE_SEPARATOR
60 "\u202F"+ // NARROW NO-BREAK SPACE
61 "\u0009"+ // HORIZONTAL TABULATION
62 "\n" + // LINE FEED (U+000A, cannot be put as it in Java)
63 "\u000B"+ // VERTICAL TABULATION
64 "\u000C"+ // FORM FEED
65 "\r" + // CARRIAGE RETURN (U+000D, cannot be put as it in Java)
66 "\u001C"+ // FILE SEPARATOR
67 "\u001D"+ // GROUP SEPARATOR
68 "\u001E"+ // RECORD SEPARATOR
69 "\u001F"+ // UNIT SEPARATOR
70 "\u2003"+ // EM SPACE
71 "\u2007"+ // FIGURE SPACE
72 "\u200B"+ // ZERO WIDTH SPACE
73 "\uFEFF"+ // ZERO WIDTH NO-BREAK SPACE
74 "\u3000"; // IDEOGRAPHIC SPACE
75 // CHECKSTYLE.ON: SingleSpaceSeparator
76 assertNull(Utils.strip(null));
77 assertEquals("", Utils.strip(""));
78 assertEquals("", Utils.strip(" "));
79 assertEquals("", Utils.strip(" "));
80 assertEquals("", Utils.strip(" "));
81 assertEquals("", Utils.strip(someWhite));
82 assertEquals("", Utils.strip("\u200B"));
83 assertEquals("", Utils.strip("\uFEFF"));
84 assertEquals("a", Utils.strip("a"));
85 assertEquals("ab", Utils.strip("ab"));
86 assertEquals("abc", Utils.strip("abc"));
87 assertEquals("a", Utils.strip(" a"));
88 assertEquals("ab", Utils.strip(" ab"));
89 assertEquals("abc", Utils.strip(" abc"));
90 assertEquals("a", Utils.strip("a "));
91 assertEquals("ab", Utils.strip("ab "));
92 assertEquals("abc", Utils.strip("abc "));
93 assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
94 assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
95 assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
96
97 // extended skip
98 assertNull(Utils.strip(null, "abc"));
99 assertEquals("", Utils.strip("", "b"));
100 assertEquals("", Utils.strip("abbbb", "ab"));
101 assertEquals("a", Utils.strip("a", "b"));
102 assertEquals("a", Utils.strip("abbbb", "b"));
103 assertEquals("b", Utils.strip("acbcac", "ac"));
104 }
105
106 /**
107 * Test of {@link Utils#isStripEmpty} method.
108 */
109 @Test
110 void testIsStripEmpty() {
111 assertTrue(Utils.isStripEmpty(null));
112 assertTrue(Utils.isStripEmpty(""));
113 assertTrue(Utils.isStripEmpty(" "));
114 assertTrue(Utils.isStripEmpty(" "));
115 assertFalse(Utils.isStripEmpty("a"));
116 assertFalse(Utils.isStripEmpty("foo"));
117 assertFalse(Utils.isStripEmpty(" foo"));
118 assertFalse(Utils.isStripEmpty("foo "));
119 assertFalse(Utils.isStripEmpty(" foo "));
120 }
121
122 /**
123 * Test of {@link Utils#toHexString} method.
124 */
125 @Test
126 void testToHexString() {
127 assertEquals("", Utils.toHexString(null));
128 assertEquals("", Utils.toHexString(new byte[0]));
129 assertEquals("01", Utils.toHexString(new byte[]{0x1}));
130 assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
131 assertEquals("12", Utils.toHexString(new byte[]{0x12}));
132 assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
133 assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
134 }
135
136 /**
137 * Test of {@link Utils#getPositionListString} method.
138 */
139 @Test
140 void testPositionListString() {
141 assertEquals("1", Utils.getPositionListString(Arrays.asList(1)));
142 assertEquals("1-2", Utils.getPositionListString(Arrays.asList(1, 2)));
143 assertEquals("1-3", Utils.getPositionListString(Arrays.asList(1, 2, 3)));
144 assertEquals("1-3", Utils.getPositionListString(Arrays.asList(3, 1, 2)));
145 assertEquals("1-3,6-8", Utils.getPositionListString(Arrays.asList(1, 2, 3, 6, 7, 8)));
146 assertEquals("1-2,5-7", Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)));
147 }
148
149 /**
150 * Test of {@link Utils#getDurationString} method.
151 */
152 @Test
153 void testDurationString() {
154 I18n.set("en");
155 assertEquals("0 ms", Utils.getDurationString(0));
156 assertEquals("123 ms", Utils.getDurationString(123));
157 assertEquals("1.0 s", Utils.getDurationString(1000));
158 assertEquals("1.2 s", Utils.getDurationString(1234));
159 assertEquals("57.0 s", Utils.getDurationString(57 * 1000));
160 assertEquals("1 min 0 s", Utils.getDurationString(60 * 1000));
161 assertEquals("8 min 27 s", Utils.getDurationString(507 * 1000));
162 assertEquals("1 h 0 min", Utils.getDurationString(60 * 60 * 1000));
163 assertEquals("8 h 24 min", Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)));
164 assertEquals("1 day 0 h", Utils.getDurationString(24 * 60 * 60 * 1000));
165 assertEquals("1 day 12 h", Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)));
166 assertEquals("8 days 12 h", Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)));
167 }
168
169 /**
170 * Test of {@link Utils#getDurationString} method.
171 */
172 @Test
173 void testDurationStringNegative() {
174 assertThrows(IllegalArgumentException.class, () -> Utils.getDurationString(-1));
175 }
176
177 /**
178 * Test of {@link Utils#escapeReservedCharactersHTML} method.
179 */
180 @Test
181 void testEscapeReservedCharactersHTML() {
182 assertEquals("foo -> bar -> '&'", Utils.escapeReservedCharactersHTML("foo -> bar -> '&'"));
183 }
184
185 /**
186 * Test of {@link Utils#shortenString} method.
187 */
188 @Test
189 void testShortenString() {
190 assertNull(Utils.shortenString(null, 3));
191 assertEquals("...", Utils.shortenString("123456789", 3));
192 assertEquals("1...", Utils.shortenString("123456789", 4));
193 assertEquals("12...", Utils.shortenString("123456789", 5));
194 assertEquals("123...", Utils.shortenString("123456789", 6));
195 assertEquals("1234...", Utils.shortenString("123456789", 7));
196 assertEquals("12345...", Utils.shortenString("123456789", 8));
197 assertEquals("123456789", Utils.shortenString("123456789", 9));
198 }
199
200 /**
201 * Test of {@link Utils#shortenString} method.
202 */
203 @Test
204 void testShortenStringTooShort() {
205 assertThrows(IllegalArgumentException.class, () -> Utils.shortenString("123456789", 2));
206 }
207
208 /**
209 * Test of {@link Utils#restrictStringLines} method.
210 */
211 @Test
212 void testRestrictStringLines() {
213 assertNull(Utils.restrictStringLines(null, 2));
214 assertEquals("1\n...", Utils.restrictStringLines("1\n2\n3", 2));
215 assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 3));
216 assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4));
217 }
218
219 /**
220 * Test of {@link Utils#limit} method.
221 */
222 @Test
223 void testLimit() {
224 assertNull(Utils.limit(null, 2, "..."));
225 assertEquals(Arrays.asList("1", "..."), Utils.limit(Arrays.asList("1", "2", "3"), 2, "..."));
226 assertEquals(Arrays.asList("1", "2", "3"), Utils.limit(Arrays.asList("1", "2", "3"), 3, "..."));
227 assertEquals(Arrays.asList("1", "2", "3"), Utils.limit(Arrays.asList("1", "2", "3"), 4, "..."));
228 }
229
230 /**
231 * Test of {@link Utils#getSizeString} method.
232 */
233 @Test
234 void testSizeString() {
235 assertEquals("0 B", Utils.getSizeString(0, Locale.ENGLISH));
236 assertEquals("123 B", Utils.getSizeString(123, Locale.ENGLISH));
237 assertEquals("1023 B", Utils.getSizeString(1023, Locale.ENGLISH));
238 assertEquals("1.00 kB", Utils.getSizeString(1024, Locale.ENGLISH));
239 assertEquals("10.00 kB", Utils.getSizeString(10 * 1024, Locale.ENGLISH));
240 assertEquals("10.0 kB", Utils.getSizeString(10 * 1024 + 1, Locale.ENGLISH));
241 assertEquals("100.0 kB", Utils.getSizeString(100 * 1024, Locale.ENGLISH));
242 assertEquals("100 kB", Utils.getSizeString(100 * 1024 + 1, Locale.ENGLISH));
243 assertEquals("11.7 kB", Utils.getSizeString(12024, Locale.ENGLISH));
244 assertEquals("1024 kB", Utils.getSizeString(1024 * 1024 - 1, Locale.ENGLISH));
245 assertEquals("1.00 MB", Utils.getSizeString(1024 * 1024, Locale.ENGLISH));
246 assertEquals("1024 MB", Utils.getSizeString(1024 * 1024 * 1024 - 1, Locale.ENGLISH));
247 assertEquals("1.00 GB", Utils.getSizeString(1024 * 1024 * 1024, Locale.ENGLISH));
248 assertEquals("8.00 EB", Utils.getSizeString(Long.MAX_VALUE, Locale.ENGLISH));
249 }
250
251 /**
252 * Test of {@link Utils#getSizeString} method.
253 */
254 @Test
255 void testSizeStringNegative() {
256 assertThrows(IllegalArgumentException.class, () -> Utils.getSizeString(-1, Locale.ENGLISH));
257 }
258
259 /**
260 * Test {@link Utils#joinAsHtmlUnorderedList(Iterable)}
261 */
262 @Test
263 void testJoinAsHtmlUnorderedList() {
264 List<? extends Object> items = Arrays.asList("1", Integer.valueOf(2));
265 assertEquals("<ul><li>1</li><li>2</li></ul>", Utils.joinAsHtmlUnorderedList(items));
266 assertEquals("<ul></ul>", Utils.joinAsHtmlUnorderedList(Collections.emptyList()));
267 }
268
269 /**
270 * Test {@link Utils#getJavaVersion}
271 */
272 @Test
273 void testGetJavaVersion() {
274 String javaVersion = System.getProperty("java.version");
275 try {
276 System.setProperty("java.version", "1.8.0_72-ea");
277 assertEquals(8, Utils.getJavaVersion());
278
279 System.setProperty("java.version", "9-ea");
280 assertEquals(9, Utils.getJavaVersion());
281
282 System.setProperty("java.version", "9");
283 assertEquals(9, Utils.getJavaVersion());
284
285 System.setProperty("java.version", "9.0.1");
286 assertEquals(9, Utils.getJavaVersion());
287
288 System.setProperty("java.version", "10");
289 assertEquals(10, Utils.getJavaVersion());
290
291 System.setProperty("java.version", "10-ea");
292 assertEquals(10, Utils.getJavaVersion());
293
294 System.setProperty("java.version", "10.0.1");
295 assertEquals(10, Utils.getJavaVersion());
296 } finally {
297 System.setProperty("java.version", javaVersion);
298 }
299 }
300
301 /**
302 * Test {@link Utils#getJavaUpdate}
303 */
304 @Test
305 void testGetJavaUpdate() {
306 String javaVersion = System.getProperty("java.version");
307 try {
308 System.setProperty("java.version", "1.8.0");
309 assertEquals(0, Utils.getJavaUpdate());
310
311 System.setProperty("java.version", "1.8.0_131");
312 assertEquals(131, Utils.getJavaUpdate());
313
314 System.setProperty("java.version", "1.8.0_152-ea");
315 assertEquals(152, Utils.getJavaUpdate());
316
317 System.setProperty("java.version", "9-ea");
318 assertEquals(0, Utils.getJavaUpdate());
319
320 System.setProperty("java.version", "9");
321 assertEquals(0, Utils.getJavaUpdate());
322
323 System.setProperty("java.version", "9.1.2");
324 assertEquals(1, Utils.getJavaUpdate());
325 } finally {
326 System.setProperty("java.version", javaVersion);
327 }
328 }
329
330 /**
331 * Test {@link Utils#getJavaBuild}
332 */
333 @Test
334 void testGetJavaBuild() {
335 String javaVersion = System.getProperty("java.runtime.version");
336 try {
337 System.setProperty("java.runtime.version", "1.8.0_131-b11");
338 assertEquals(11, Utils.getJavaBuild());
339
340 System.setProperty("java.runtime.version", "1.8.0_152-ea-b04");
341 assertEquals(4, Utils.getJavaBuild());
342
343 System.setProperty("java.runtime.version", "9-ea+170");
344 assertEquals(170, Utils.getJavaBuild());
345
346 System.setProperty("java.runtime.version", "9+200");
347 assertEquals(200, Utils.getJavaBuild());
348
349 System.setProperty("java.runtime.version", "9.1.2+62");
350 assertEquals(62, Utils.getJavaBuild());
351
352 // IBM version example
353 System.setProperty("java.runtime.version", "pwa6480sr4fp7-20170627_02 (SR4 FP7)");
354 assertEquals(0, Utils.getJavaBuild());
355
356 } finally {
357 System.setProperty("java.runtime.version", javaVersion);
358 }
359 }
360
361 /**
362 * Tests if readBytesFromStream handles null streams (might happen when there is no data on error stream)
363 * @throws IOException in case of I/O error
364 */
365 @Test
366 void testNullStreamForReadBytesFromStream() throws IOException {
367 assertEquals(0, Utils.readBytesFromStream(null).length, "Empty on null stream");
368 }
369
370 /**
371 * Test of {@link Utils#getLevenshteinDistance} method.
372 */
373 @Test
374 void testLevenshteinDistance() {
375 assertEquals(0, Utils.getLevenshteinDistance("foo", "foo"));
376 assertEquals(3, Utils.getLevenshteinDistance("foo", "bar"));
377 assertEquals(1, Utils.getLevenshteinDistance("bar", "baz"));
378 assertEquals(3, Utils.getLevenshteinDistance("foo", ""));
379 assertEquals(3, Utils.getLevenshteinDistance("", "baz"));
380 assertEquals(2, Utils.getLevenshteinDistance("ABjoYZ", "ABsmYZ"));
381 }
382
383 /**
384 * Test of {@link Utils#isSimilar} method.
385 */
386 @Test
387 void testIsSimilar() {
388 assertFalse(Utils.isSimilar("foo", "foo"));
389 assertFalse(Utils.isSimilar("foo", "bar"));
390 assertTrue(Utils.isSimilar("bar", "baz"));
391 assertTrue(Utils.isSimilar("bar", "baz"));
392 assertTrue(Utils.isSimilar("Rua São João", "Rua SAO Joao"));
393 }
394
395 /**
396 * Test of {@link Utils#stripHtml(String)}
397 */
398 @Test
399 void testStripHtml() {
400 assertEquals("Hoogte 55 m", Utils.stripHtml(
401 "<table width=\"100%\"><tr>" +
402 "<td align=\"left\" valign=\"center\"><small><b>Hoogte</b></small></td>" +
403 "<td align=\"center\" valign=\"center\">55 m</td></tr></table>"));
404 }
405
406 /**
407 * Test of {@link Utils#firstNonNull}
408 */
409 @Test
410 void testFirstNonNull() {
411 assertNull(Utils.firstNonNull());
412 assertNull(Utils.firstNonNull(null, null));
413 assertEquals("foo", Utils.firstNonNull(null, "foo", null));
414 }
415
416 /**
417 * Test of {@link Utils#getMatches}
418 */
419 @Test
420 void testGetMatches() {
421 final Pattern pattern = Pattern.compile("(foo)x(bar)y(baz)");
422 assertNull(Utils.getMatches(pattern.matcher("")));
423 assertEquals(Arrays.asList("fooxbarybaz", "foo", "bar", "baz"), Utils.getMatches(pattern.matcher("fooxbarybaz")));
424 }
425
426 /**
427 * Test of {@link Utils#encodeUrl}
428 */
429 @Test
430 void testEncodeUrl() {
431 assertEquals("%C3%A4%C3%B6%C3%BC%C3%9F", Utils.encodeUrl("äöüß"));
432 }
433
434 /**
435 * Test of {@link Utils#encodeUrl}
436 */
437 @Test
438 void testEncodeUrlNull() {
439 assertThrows(NullPointerException.class, () -> Utils.encodeUrl(null));
440 }
441
442 /**
443 * Test of {@link Utils#decodeUrl}
444 */
445 @Test
446 void testDecodeUrl() {
447 assertEquals("äöüß", Utils.decodeUrl("%C3%A4%C3%B6%C3%BC%C3%9F"));
448 }
449
450 /**
451 * Test of {@link Utils#decodeUrl}
452 */
453 @Test
454 void testDecodeUrlNull() {
455 assertThrows(NullPointerException.class, () -> Utils.decodeUrl(null));
456 }
457
458 /**
459 * Test of {@link Utils#clamp}
460 */
461 @Test
462 void testClamp() {
463 assertEquals(3, Utils.clamp(2, 3, 5));
464 assertEquals(3, Utils.clamp(3, 3, 5));
465 assertEquals(4, Utils.clamp(4, 3, 5));
466 assertEquals(5, Utils.clamp(5, 3, 5));
467 assertEquals(5, Utils.clamp(6, 3, 5));
468 assertEquals(3., Utils.clamp(2., 3., 5.), 1e-3);
469 assertEquals(3., Utils.clamp(3., 3., 5.), 1e-3);
470 assertEquals(4., Utils.clamp(4., 3., 5.), 1e-3);
471 assertEquals(5., Utils.clamp(5., 3., 5.), 1e-3);
472 assertEquals(5., Utils.clamp(6., 3., 5.), 1e-3);
473 }
474
475 /**
476 * Test of {@link Utils#clamp}
477 */
478 @Test
479 void testClampIAE1() {
480 assertThrows(IllegalArgumentException.class, () -> Utils.clamp(0, 5, 4));
481 }
482
483 /**
484 * Test of {@link Utils#clamp}
485 */
486 @Test
487 void testClampIAE2() {
488 assertThrows(IllegalArgumentException.class, () -> Utils.clamp(0., 5., 4.));
489 }
490
491 /**
492 * Test of {@link Utils#hasExtension}
493 */
494 @Test
495 void testHasExtension() {
496 assertFalse(Utils.hasExtension("JOSM.txt"));
497 assertFalse(Utils.hasExtension("JOSM.txt", "jpg"));
498 assertFalse(Utils.hasExtension("JOSM.txt", ".jpg", ".txt"));
499 assertTrue(Utils.hasExtension("JOSM.txt", "jpg", "txt"));
500 assertTrue(Utils.hasExtension(new File("JOSM.txt"), "jpg", "txt"));
501 }
502
503 /**
504 * Test of {@link Utils#toUnmodifiableList}
505 */
506 @Test
507 void testToUnmodifiableList() {
508 assertSame(Collections.emptyList(), Utils.toUnmodifiableList(null));
509 assertSame(Collections.emptyList(), Utils.toUnmodifiableList(Collections.emptyList()));
510 assertSame(Collections.emptyList(), Utils.toUnmodifiableList(new ArrayList<>()));
511 assertEquals(Collections.singletonList("foo"), Utils.toUnmodifiableList(new ArrayList<>(Collections.singletonList("foo"))));
512 assertEquals(Arrays.asList("foo", "bar", "baz"), Utils.toUnmodifiableList(Arrays.asList("foo", "bar", "baz")));
513 assertEquals(Arrays.asList("foo", "bar", "baz"), Utils.toUnmodifiableList(new ArrayList<>(Arrays.asList("foo", "bar", "baz"))));
514 assertEquals(Arrays.asList("foo", "bar", "baz"), Utils.toUnmodifiableList(new LinkedList<>(Arrays.asList("foo", "bar", "baz"))));
515 }
516
517 /**
518 * Test of {@link Utils#toUnmodifiableMap}
519 */
520 @Test
521 void testToUnmodifiableMap() {
522 assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(null));
523 assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(Collections.emptyMap()));
524 assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(new HashMap<>()));
525 assertSame(Collections.emptyMap(), Utils.toUnmodifiableMap(new TreeMap<>()));
526 assertEquals(Collections.singletonMap("foo", "bar"), Utils.toUnmodifiableMap(new HashMap<>(Collections.singletonMap("foo", "bar"))));
527 assertEquals(Collections.singletonMap("foo", "bar"), Utils.toUnmodifiableMap(new TreeMap<>(Collections.singletonMap("foo", "bar"))));
528 final Map<String, String> map4 = new HashMap<>();
529 map4.put("jjj", "foo");
530 map4.put("ooo", "bar");
531 map4.put("sss", "baz");
532 map4.put("mmm", ":-)");
533 assertEquals(map4, Utils.toUnmodifiableMap(map4));
534 }
535
536 /**
537 * Test of {@link Utils#execOutput}
538 * @throws Exception if an error occurs
539 */
540 @Test
541 void testExecOutput() throws Exception {
542 final String output = Utils.execOutput(Arrays.asList("echo", "Hello", "World"));
543 assertEquals("Hello World", output);
544 }
545}
Note: See TracBrowser for help on using the repository browser.