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 | import java.util.Collections;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Locale;
|
---|
13 |
|
---|
14 | import org.junit.Assert;
|
---|
15 | import org.junit.Test;
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Unit tests of {@link Utils} class.
|
---|
21 | */
|
---|
22 | public class UtilsTest {
|
---|
23 | /**
|
---|
24 | * Use default, basic test rules.
|
---|
25 | */
|
---|
26 | public JOSMTestRules rules = new JOSMTestRules();
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Test of {@link Utils#strip} method.
|
---|
30 | */
|
---|
31 | @Test
|
---|
32 | public void testStrip() {
|
---|
33 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
34 | final String someWhite =
|
---|
35 | "\u00A0"+ // SPACE_SEPARATOR
|
---|
36 | "\u2007"+ // LINE_SEPARATOR
|
---|
37 | "\u202F"+ // PARAGRAPH_SEPARATOR
|
---|
38 | "\u0009"+ // HORIZONTAL TABULATION
|
---|
39 | "\n" + // LINE FEED (U+000A, cannot be put as it in Java)
|
---|
40 | "\u000B"+ // VERTICAL TABULATION
|
---|
41 | "\u000C"+ // FORM FEED
|
---|
42 | "\r" + // CARRIAGE RETURN (U+000D, cannot be put as it in Java)
|
---|
43 | "\u001C"+ // FILE SEPARATOR
|
---|
44 | "\u001D"+ // GROUP SEPARATOR
|
---|
45 | "\u001E"+ // RECORD SEPARATOR
|
---|
46 | "\u001F"+ // UNIT SEPARATOR
|
---|
47 | "\u2003"+ // EM SPACE
|
---|
48 | "\u2007"+ // FIGURE SPACE
|
---|
49 | "\u200B"+ // ZERO WIDTH SPACE
|
---|
50 | "\uFEFF"+ // ZERO WIDTH NO-BREAK SPACE
|
---|
51 | "\u3000"; // IDEOGRAPHIC SPACE
|
---|
52 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
53 | Assert.assertNull(Utils.strip(null));
|
---|
54 | Assert.assertEquals("", Utils.strip(""));
|
---|
55 | Assert.assertEquals("", Utils.strip(" "));
|
---|
56 | Assert.assertEquals("", Utils.strip(" "));
|
---|
57 | Assert.assertEquals("", Utils.strip(" "));
|
---|
58 | Assert.assertEquals("", Utils.strip(someWhite));
|
---|
59 | Assert.assertEquals("a", Utils.strip("a"));
|
---|
60 | Assert.assertEquals("ab", Utils.strip("ab"));
|
---|
61 | Assert.assertEquals("abc", Utils.strip("abc"));
|
---|
62 | Assert.assertEquals("a", Utils.strip(" a"));
|
---|
63 | Assert.assertEquals("ab", Utils.strip(" ab"));
|
---|
64 | Assert.assertEquals("abc", Utils.strip(" abc"));
|
---|
65 | Assert.assertEquals("a", Utils.strip("a "));
|
---|
66 | Assert.assertEquals("ab", Utils.strip("ab "));
|
---|
67 | Assert.assertEquals("abc", Utils.strip("abc "));
|
---|
68 | Assert.assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
|
---|
69 | Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
|
---|
70 | Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
|
---|
71 |
|
---|
72 | // extended skip
|
---|
73 | Assert.assertEquals("a", Utils.strip("a", "b"));
|
---|
74 | Assert.assertEquals("b", Utils.strip("acbcac", "ac"));
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Test of {@link Utils#toHexString} method.
|
---|
79 | */
|
---|
80 | @Test
|
---|
81 | public void testToHexString() {
|
---|
82 | Assert.assertEquals("", Utils.toHexString(null));
|
---|
83 | Assert.assertEquals("", Utils.toHexString(new byte[0]));
|
---|
84 | Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
|
---|
85 | Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
|
---|
86 | Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
|
---|
87 | Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
|
---|
88 | Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Test of {@link Utils#openURLReaderAndDecompress} method with Gzip compression.
|
---|
93 | * @throws IOException if any I/O error occurs
|
---|
94 | */
|
---|
95 | @Test
|
---|
96 | public void testOpenUrlGzip() throws IOException {
|
---|
97 | Main.initApplicationPreferences();
|
---|
98 | final URL url = new URL("https://www.openstreetmap.org/trace/1613906/data");
|
---|
99 | try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
|
---|
100 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Test of {@link Utils#openURLReaderAndDecompress} method with Bzip compression.
|
---|
106 | * @throws IOException if any I/O error occurs
|
---|
107 | */
|
---|
108 | @Test
|
---|
109 | public void testOpenUrlBzip() throws IOException {
|
---|
110 | Main.initApplicationPreferences();
|
---|
111 | final URL url = new URL("https://www.openstreetmap.org/trace/785544/data");
|
---|
112 | try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
|
---|
113 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Test of {@link Utils#openURLReaderAndDecompress} method with Bzip compression.
|
---|
119 | * @throws IOException if any I/O error occurs
|
---|
120 | */
|
---|
121 | @Test
|
---|
122 | public void testTicket9660() throws IOException {
|
---|
123 | Main.initApplicationPreferences();
|
---|
124 | final URL url = new URL("http://www.openstreetmap.org/trace/1350010/data");
|
---|
125 | try (BufferedReader x = HttpClient.create(url).connect()
|
---|
126 | .uncompress(true).uncompressAccordingToContentDisposition(true).getContentReader()) {
|
---|
127 | Assert.assertTrue(x.readLine().startsWith("<?xml version="));
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Test of {@link Utils#getPositionListString} method.
|
---|
133 | */
|
---|
134 | @Test
|
---|
135 | public void testPositionListString() {
|
---|
136 | assertEquals("1", Utils.getPositionListString(Arrays.asList(1)));
|
---|
137 | assertEquals("1-3", Utils.getPositionListString(Arrays.asList(1, 2, 3)));
|
---|
138 | assertEquals("1-3", Utils.getPositionListString(Arrays.asList(3, 1, 2)));
|
---|
139 | assertEquals("1-3,6-8", Utils.getPositionListString(Arrays.asList(1, 2, 3, 6, 7, 8)));
|
---|
140 | assertEquals("1-2,5-7", Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)));
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Test of {@link Utils#getDurationString} method.
|
---|
145 | */
|
---|
146 | @Test
|
---|
147 | public void testDurationString() {
|
---|
148 | I18n.set("en");
|
---|
149 | assertEquals("123 ms", Utils.getDurationString(123));
|
---|
150 | assertEquals("1.2 s", Utils.getDurationString(1234));
|
---|
151 | assertEquals("57.0 s", Utils.getDurationString(57 * 1000));
|
---|
152 | assertEquals("8 min 27 s", Utils.getDurationString(507 * 1000));
|
---|
153 | assertEquals("8 h 24 min", Utils.getDurationString((long) (8.4 * 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#escapeReservedCharactersHTML} method.
|
---|
160 | */
|
---|
161 | @Test
|
---|
162 | public void testEscapeReservedCharactersHTML() {
|
---|
163 | assertEquals("foo -> bar -> '&'", Utils.escapeReservedCharactersHTML("foo -> bar -> '&'"));
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Test of {@link Utils#restrictStringLines} method.
|
---|
168 | */
|
---|
169 | @Test
|
---|
170 | public void testRestrictStringLines() {
|
---|
171 | assertEquals("1\n...", Utils.restrictStringLines("1\n2\n3", 2));
|
---|
172 | assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 3));
|
---|
173 | assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4));
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Test of {@link Utils#getSizeString} method.
|
---|
178 | */
|
---|
179 | @Test
|
---|
180 | public void testSizeString() {
|
---|
181 | assertEquals("0 B", Utils.getSizeString(0, Locale.ENGLISH));
|
---|
182 | assertEquals("123 B", Utils.getSizeString(123, Locale.ENGLISH));
|
---|
183 | assertEquals("1023 B", Utils.getSizeString(1023, Locale.ENGLISH));
|
---|
184 | assertEquals("1.00 kB", Utils.getSizeString(1024, Locale.ENGLISH));
|
---|
185 | assertEquals("11.7 kB", Utils.getSizeString(12024, Locale.ENGLISH));
|
---|
186 | assertEquals("8.00 EB", Utils.getSizeString(Long.MAX_VALUE, Locale.ENGLISH));
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Test of {@link Utils#getSizeString} method.
|
---|
191 | */
|
---|
192 | @Test(expected = IllegalArgumentException.class)
|
---|
193 | public void testSizeStringNegative() {
|
---|
194 | Utils.getSizeString(-1, Locale.ENGLISH);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Test {@link Utils#joinAsHtmlUnorderedList(Iterable)}
|
---|
199 | */
|
---|
200 | @Test
|
---|
201 | public void joinAsHtmlUnorderedList() {
|
---|
202 | List<? extends Object> items = Arrays.asList("1", new Integer(2));
|
---|
203 | assertEquals("<ul><li>1</li><li>2</li></ul>", Utils.joinAsHtmlUnorderedList(items));
|
---|
204 | assertEquals("<ul></ul>", Utils.joinAsHtmlUnorderedList(Collections.emptyList()));
|
---|
205 | }
|
---|
206 | }
|
---|