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