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