1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.hamcrest.CoreMatchers.is;
|
---|
5 | import static org.junit.Assert.assertThat;
|
---|
6 |
|
---|
7 | import java.io.BufferedReader;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Locale;
|
---|
11 |
|
---|
12 | import org.junit.Assert;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Unit tests of {@link Utils} class.
|
---|
18 | */
|
---|
19 | public class UtilsTest {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Test of {@link Utils#strip} method.
|
---|
23 | */
|
---|
24 | @Test
|
---|
25 | public void testStrip() {
|
---|
26 | final String someWhite =
|
---|
27 | "\u00A0"+ // SPACE_SEPARATOR
|
---|
28 | "\u2007"+ // LINE_SEPARATOR
|
---|
29 | "\u202F"+ // PARAGRAPH_SEPARATOR
|
---|
30 | "\u0009"+ // HORIZONTAL TABULATION
|
---|
31 | "\n" + // LINE FEED (U+000A, cannot be put as it in Java)
|
---|
32 | "\u000B"+ // VERTICAL TABULATION
|
---|
33 | "\u000C"+ // FORM FEED
|
---|
34 | "\r" + // CARRIAGE RETURN (U+000D, cannot be put as it in Java)
|
---|
35 | "\u001C"+ // FILE SEPARATOR
|
---|
36 | "\u001D"+ // GROUP SEPARATOR
|
---|
37 | "\u001E"+ // RECORD SEPARATOR
|
---|
38 | "\u001F"+ // UNIT SEPARATOR
|
---|
39 | "\u2003"+ // EM SPACE
|
---|
40 | "\u2007"+ // FIGURE SPACE
|
---|
41 | "\u200B"+ // ZERO WIDTH SPACE
|
---|
42 | "\uFEFF"+ // ZERO WIDTH NO-BREAK SPACE
|
---|
43 | "\u3000"; // IDEOGRAPHIC SPACE
|
---|
44 | Assert.assertNull(Utils.strip(null));
|
---|
45 | Assert.assertEquals("", Utils.strip(""));
|
---|
46 | Assert.assertEquals("", Utils.strip(" "));
|
---|
47 | Assert.assertEquals("", Utils.strip(" "));
|
---|
48 | Assert.assertEquals("", Utils.strip(" "));
|
---|
49 | Assert.assertEquals("", Utils.strip(someWhite));
|
---|
50 | Assert.assertEquals("a", Utils.strip("a"));
|
---|
51 | Assert.assertEquals("ab", Utils.strip("ab"));
|
---|
52 | Assert.assertEquals("abc", Utils.strip("abc"));
|
---|
53 | Assert.assertEquals("a", Utils.strip(" a"));
|
---|
54 | Assert.assertEquals("ab", Utils.strip(" ab"));
|
---|
55 | Assert.assertEquals("abc", Utils.strip(" abc"));
|
---|
56 | Assert.assertEquals("a", Utils.strip("a "));
|
---|
57 | Assert.assertEquals("ab", Utils.strip("ab "));
|
---|
58 | Assert.assertEquals("abc", Utils.strip("abc "));
|
---|
59 | Assert.assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
|
---|
60 | Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
|
---|
61 | Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Test of {@link Utils#toHexString} method.
|
---|
66 | */
|
---|
67 | @Test
|
---|
68 | public void testToHexString(){
|
---|
69 | Assert.assertEquals("", Utils.toHexString(null));
|
---|
70 | Assert.assertEquals("", Utils.toHexString(new byte[0]));
|
---|
71 | Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
|
---|
72 | Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1,0x2}));
|
---|
73 | Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
|
---|
74 | Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
|
---|
75 | Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Test
|
---|
79 | public void testOpenUrlGzip() throws Exception {
|
---|
80 | Main.initApplicationPreferences();
|
---|
81 | try (BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/1613906/data"), true)) {
|
---|
82 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Test
|
---|
87 | public void testOpenUrlBzip() throws Exception {
|
---|
88 | Main.initApplicationPreferences();
|
---|
89 | try (BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/785544/data"), true)) {
|
---|
90 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Test
|
---|
95 | public void testPositionListString() throws Exception {
|
---|
96 | assertThat(Utils.getPositionListString(Arrays.asList(1)), is("1"));
|
---|
97 | assertThat(Utils.getPositionListString(Arrays.asList(1, 2, 3)), is("1-3"));
|
---|
98 | assertThat(Utils.getPositionListString(Arrays.asList(3, 1, 2)), is("1-3"));
|
---|
99 | assertThat(Utils.getPositionListString(Arrays.asList(1, 2, 3, 6, 7, 8)), is("1-3,6-8"));
|
---|
100 | assertThat(Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)), is("1-2,5-7"));
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Test
|
---|
104 | public void testDurationString() throws Exception {
|
---|
105 | Locale.setDefault(Locale.ENGLISH);
|
---|
106 | assertThat(Utils.getDurationString(123), is("123 ms"));
|
---|
107 | assertThat(Utils.getDurationString(1234), is("1.2 s"));
|
---|
108 | assertThat(Utils.getDurationString(57 * 1000), is("57.0 s"));
|
---|
109 | assertThat(Utils.getDurationString(507 * 1000), is("8 min 27 s"));
|
---|
110 | assertThat(Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)), is("8 h 24 min"));
|
---|
111 | assertThat(Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)), is("1 day 12 h"));
|
---|
112 | assertThat(Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)), is("8 days 12 h"));
|
---|
113 | }
|
---|
114 | }
|
---|