1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertNull;
|
---|
7 | import static org.junit.Assert.assertTrue;
|
---|
8 |
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Locale;
|
---|
14 |
|
---|
15 | import org.junit.Rule;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
18 |
|
---|
19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
20 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Unit tests of {@link Utils} class.
|
---|
24 | */
|
---|
25 | public class UtilsTest {
|
---|
26 | /**
|
---|
27 | * Use default, basic test rules.
|
---|
28 | */
|
---|
29 | @Rule
|
---|
30 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
31 | public JOSMTestRules rules = new JOSMTestRules();
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Tests that {@code Utils} satisfies utility class criterias.
|
---|
35 | * @throws ReflectiveOperationException if an error occurs
|
---|
36 | */
|
---|
37 | @Test
|
---|
38 | public void testUtilityClass() throws ReflectiveOperationException {
|
---|
39 | UtilityClassTestUtil.assertUtilityClassWellDefined(Utils.class);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Test of {@link Utils#strip} method.
|
---|
44 | */
|
---|
45 | @Test
|
---|
46 | public void testStrip() {
|
---|
47 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
48 | final String someWhite =
|
---|
49 | "\u00A0"+ // SPACE_SEPARATOR
|
---|
50 | "\u2007"+ // LINE_SEPARATOR
|
---|
51 | "\u202F"+ // PARAGRAPH_SEPARATOR
|
---|
52 | "\u0009"+ // HORIZONTAL TABULATION
|
---|
53 | "\n" + // LINE FEED (U+000A, cannot be put as it in Java)
|
---|
54 | "\u000B"+ // VERTICAL TABULATION
|
---|
55 | "\u000C"+ // FORM FEED
|
---|
56 | "\r" + // CARRIAGE RETURN (U+000D, cannot be put as it in Java)
|
---|
57 | "\u001C"+ // FILE SEPARATOR
|
---|
58 | "\u001D"+ // GROUP SEPARATOR
|
---|
59 | "\u001E"+ // RECORD SEPARATOR
|
---|
60 | "\u001F"+ // UNIT SEPARATOR
|
---|
61 | "\u2003"+ // EM SPACE
|
---|
62 | "\u2007"+ // FIGURE SPACE
|
---|
63 | "\u200B"+ // ZERO WIDTH SPACE
|
---|
64 | "\uFEFF"+ // ZERO WIDTH NO-BREAK SPACE
|
---|
65 | "\u3000"; // IDEOGRAPHIC SPACE
|
---|
66 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
67 | assertNull(Utils.strip(null));
|
---|
68 | assertEquals("", Utils.strip(""));
|
---|
69 | assertEquals("", Utils.strip(" "));
|
---|
70 | assertEquals("", Utils.strip(" "));
|
---|
71 | assertEquals("", Utils.strip(" "));
|
---|
72 | assertEquals("", Utils.strip(someWhite));
|
---|
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 | assertEquals("a", Utils.strip("a", "b"));
|
---|
88 | assertEquals("b", Utils.strip("acbcac", "ac"));
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Test of {@link Utils#isStripEmpty} method.
|
---|
93 | */
|
---|
94 | @Test
|
---|
95 | public void testIsStripEmpty() {
|
---|
96 | assertTrue(Utils.isStripEmpty(null));
|
---|
97 | assertTrue(Utils.isStripEmpty(""));
|
---|
98 | assertTrue(Utils.isStripEmpty(" "));
|
---|
99 | assertTrue(Utils.isStripEmpty(" "));
|
---|
100 | assertFalse(Utils.isStripEmpty("a"));
|
---|
101 | assertFalse(Utils.isStripEmpty("foo"));
|
---|
102 | assertFalse(Utils.isStripEmpty(" foo"));
|
---|
103 | assertFalse(Utils.isStripEmpty("foo "));
|
---|
104 | assertFalse(Utils.isStripEmpty(" foo "));
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Test of {@link Utils#toHexString} method.
|
---|
109 | */
|
---|
110 | @Test
|
---|
111 | public void testToHexString() {
|
---|
112 | assertEquals("", Utils.toHexString(null));
|
---|
113 | assertEquals("", Utils.toHexString(new byte[0]));
|
---|
114 | assertEquals("01", Utils.toHexString(new byte[]{0x1}));
|
---|
115 | assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
|
---|
116 | assertEquals("12", Utils.toHexString(new byte[]{0x12}));
|
---|
117 | assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
|
---|
118 | assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Test of {@link Utils#getPositionListString} method.
|
---|
123 | */
|
---|
124 | @Test
|
---|
125 | public void testPositionListString() {
|
---|
126 | assertEquals("1", Utils.getPositionListString(Arrays.asList(1)));
|
---|
127 | assertEquals("1-3", Utils.getPositionListString(Arrays.asList(1, 2, 3)));
|
---|
128 | assertEquals("1-3", Utils.getPositionListString(Arrays.asList(3, 1, 2)));
|
---|
129 | assertEquals("1-3,6-8", Utils.getPositionListString(Arrays.asList(1, 2, 3, 6, 7, 8)));
|
---|
130 | assertEquals("1-2,5-7", Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)));
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Test of {@link Utils#getDurationString} method.
|
---|
135 | */
|
---|
136 | @Test
|
---|
137 | public void testDurationString() {
|
---|
138 | I18n.set("en");
|
---|
139 | assertEquals("123 ms", Utils.getDurationString(123));
|
---|
140 | assertEquals("1.2 s", Utils.getDurationString(1234));
|
---|
141 | assertEquals("57.0 s", Utils.getDurationString(57 * 1000));
|
---|
142 | assertEquals("8 min 27 s", Utils.getDurationString(507 * 1000));
|
---|
143 | assertEquals("8 h 24 min", Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)));
|
---|
144 | assertEquals("1 day 12 h", Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)));
|
---|
145 | assertEquals("8 days 12 h", Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)));
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Test of {@link Utils#escapeReservedCharactersHTML} method.
|
---|
150 | */
|
---|
151 | @Test
|
---|
152 | public void testEscapeReservedCharactersHTML() {
|
---|
153 | assertEquals("foo -> bar -> '&'", Utils.escapeReservedCharactersHTML("foo -> bar -> '&'"));
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Test of {@link Utils#restrictStringLines} method.
|
---|
158 | */
|
---|
159 | @Test
|
---|
160 | public void testRestrictStringLines() {
|
---|
161 | assertEquals("1\n...", Utils.restrictStringLines("1\n2\n3", 2));
|
---|
162 | assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 3));
|
---|
163 | assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4));
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Test of {@link Utils#getSizeString} method.
|
---|
168 | */
|
---|
169 | @Test
|
---|
170 | public void testSizeString() {
|
---|
171 | assertEquals("0 B", Utils.getSizeString(0, Locale.ENGLISH));
|
---|
172 | assertEquals("123 B", Utils.getSizeString(123, Locale.ENGLISH));
|
---|
173 | assertEquals("1023 B", Utils.getSizeString(1023, Locale.ENGLISH));
|
---|
174 | assertEquals("1.00 kB", Utils.getSizeString(1024, Locale.ENGLISH));
|
---|
175 | assertEquals("11.7 kB", Utils.getSizeString(12024, Locale.ENGLISH));
|
---|
176 | assertEquals("8.00 EB", Utils.getSizeString(Long.MAX_VALUE, Locale.ENGLISH));
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Test of {@link Utils#getSizeString} method.
|
---|
181 | */
|
---|
182 | @Test(expected = IllegalArgumentException.class)
|
---|
183 | public void testSizeStringNegative() {
|
---|
184 | Utils.getSizeString(-1, Locale.ENGLISH);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Test {@link Utils#joinAsHtmlUnorderedList(Iterable)}
|
---|
189 | */
|
---|
190 | @Test
|
---|
191 | public void testJoinAsHtmlUnorderedList() {
|
---|
192 | List<? extends Object> items = Arrays.asList("1", Integer.valueOf(2));
|
---|
193 | assertEquals("<ul><li>1</li><li>2</li></ul>", Utils.joinAsHtmlUnorderedList(items));
|
---|
194 | assertEquals("<ul></ul>", Utils.joinAsHtmlUnorderedList(Collections.emptyList()));
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Test {@link Utils#getJavaVersion}
|
---|
199 | */
|
---|
200 | @Test
|
---|
201 | public void testGetJavaVersion() {
|
---|
202 | String javaVersion = System.getProperty("java.version");
|
---|
203 | try {
|
---|
204 | System.setProperty("java.version", "1.8.0_72-ea");
|
---|
205 | assertEquals(8, Utils.getJavaVersion());
|
---|
206 |
|
---|
207 | System.setProperty("java.version", "9-ea");
|
---|
208 | assertEquals(9, Utils.getJavaVersion());
|
---|
209 |
|
---|
210 | System.setProperty("java.version", "9");
|
---|
211 | assertEquals(9, Utils.getJavaVersion());
|
---|
212 |
|
---|
213 | System.setProperty("java.version", "9.0.1");
|
---|
214 | assertEquals(9, Utils.getJavaVersion());
|
---|
215 |
|
---|
216 | System.setProperty("java.version", "10");
|
---|
217 | assertEquals(10, Utils.getJavaVersion());
|
---|
218 |
|
---|
219 | System.setProperty("java.version", "10-ea");
|
---|
220 | assertEquals(10, Utils.getJavaVersion());
|
---|
221 |
|
---|
222 | System.setProperty("java.version", "10.0.1");
|
---|
223 | assertEquals(10, Utils.getJavaVersion());
|
---|
224 | } finally {
|
---|
225 | System.setProperty("java.version", javaVersion);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /**
|
---|
230 | * Test {@link Utils#getJavaUpdate}
|
---|
231 | */
|
---|
232 | @Test
|
---|
233 | public void testGetJavaUpdate() {
|
---|
234 | String javaVersion = System.getProperty("java.version");
|
---|
235 | try {
|
---|
236 | System.setProperty("java.version", "1.8.0");
|
---|
237 | assertEquals(0, Utils.getJavaUpdate());
|
---|
238 |
|
---|
239 | System.setProperty("java.version", "1.8.0_131");
|
---|
240 | assertEquals(131, Utils.getJavaUpdate());
|
---|
241 |
|
---|
242 | System.setProperty("java.version", "1.8.0_152-ea");
|
---|
243 | assertEquals(152, Utils.getJavaUpdate());
|
---|
244 |
|
---|
245 | System.setProperty("java.version", "9-ea");
|
---|
246 | assertEquals(0, Utils.getJavaUpdate());
|
---|
247 |
|
---|
248 | System.setProperty("java.version", "9");
|
---|
249 | assertEquals(0, Utils.getJavaUpdate());
|
---|
250 |
|
---|
251 | System.setProperty("java.version", "9.1.2");
|
---|
252 | assertEquals(1, Utils.getJavaUpdate());
|
---|
253 | } finally {
|
---|
254 | System.setProperty("java.version", javaVersion);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Test {@link Utils#getJavaBuild}
|
---|
260 | */
|
---|
261 | @Test
|
---|
262 | public void testGetJavaBuild() {
|
---|
263 | String javaVersion = System.getProperty("java.runtime.version");
|
---|
264 | try {
|
---|
265 | System.setProperty("java.runtime.version", "1.8.0_131-b11");
|
---|
266 | assertEquals(11, Utils.getJavaBuild());
|
---|
267 |
|
---|
268 | System.setProperty("java.runtime.version", "1.8.0_152-ea-b04");
|
---|
269 | assertEquals(4, Utils.getJavaBuild());
|
---|
270 |
|
---|
271 | System.setProperty("java.runtime.version", "9-ea+170");
|
---|
272 | assertEquals(170, Utils.getJavaBuild());
|
---|
273 |
|
---|
274 | System.setProperty("java.runtime.version", "9+200");
|
---|
275 | assertEquals(200, Utils.getJavaBuild());
|
---|
276 |
|
---|
277 | System.setProperty("java.runtime.version", "9.1.2+62");
|
---|
278 | assertEquals(62, Utils.getJavaBuild());
|
---|
279 |
|
---|
280 | // IBM version example
|
---|
281 | System.setProperty("java.runtime.version", "pwa6480sr4fp7-20170627_02 (SR4 FP7)");
|
---|
282 | assertEquals(0, Utils.getJavaBuild());
|
---|
283 |
|
---|
284 | } finally {
|
---|
285 | System.setProperty("java.runtime.version", javaVersion);
|
---|
286 | }
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Tests if readBytesFromStream handles null streams (might happen when there is no data on error stream)
|
---|
291 | * @throws IOException in case of I/O error
|
---|
292 | */
|
---|
293 | @Test
|
---|
294 | public void testNullStreamForReadBytesFromStream() throws IOException {
|
---|
295 | assertEquals("Empty on null stream", 0, Utils.readBytesFromStream(null).length);
|
---|
296 | }
|
---|
297 | }
|
---|