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 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link Utils} class.
|
---|
23 | */
|
---|
24 | public class UtilsTest {
|
---|
25 | /**
|
---|
26 | * Use default, basic test rules.
|
---|
27 | */
|
---|
28 | @Rule
|
---|
29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
30 | public JOSMTestRules rules = new JOSMTestRules();
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Test of {@link Utils#strip} method.
|
---|
34 | */
|
---|
35 | @Test
|
---|
36 | public void testStrip() {
|
---|
37 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
38 | final String someWhite =
|
---|
39 | "\u00A0"+ // SPACE_SEPARATOR
|
---|
40 | "\u2007"+ // LINE_SEPARATOR
|
---|
41 | "\u202F"+ // PARAGRAPH_SEPARATOR
|
---|
42 | "\u0009"+ // HORIZONTAL TABULATION
|
---|
43 | "\n" + // LINE FEED (U+000A, cannot be put as it in Java)
|
---|
44 | "\u000B"+ // VERTICAL TABULATION
|
---|
45 | "\u000C"+ // FORM FEED
|
---|
46 | "\r" + // CARRIAGE RETURN (U+000D, cannot be put as it in Java)
|
---|
47 | "\u001C"+ // FILE SEPARATOR
|
---|
48 | "\u001D"+ // GROUP SEPARATOR
|
---|
49 | "\u001E"+ // RECORD SEPARATOR
|
---|
50 | "\u001F"+ // UNIT SEPARATOR
|
---|
51 | "\u2003"+ // EM SPACE
|
---|
52 | "\u2007"+ // FIGURE SPACE
|
---|
53 | "\u200B"+ // ZERO WIDTH SPACE
|
---|
54 | "\uFEFF"+ // ZERO WIDTH NO-BREAK SPACE
|
---|
55 | "\u3000"; // IDEOGRAPHIC SPACE
|
---|
56 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
57 | assertNull(Utils.strip(null));
|
---|
58 | assertEquals("", Utils.strip(""));
|
---|
59 | assertEquals("", Utils.strip(" "));
|
---|
60 | assertEquals("", Utils.strip(" "));
|
---|
61 | assertEquals("", Utils.strip(" "));
|
---|
62 | assertEquals("", Utils.strip(someWhite));
|
---|
63 | assertEquals("a", Utils.strip("a"));
|
---|
64 | assertEquals("ab", Utils.strip("ab"));
|
---|
65 | assertEquals("abc", Utils.strip("abc"));
|
---|
66 | assertEquals("a", Utils.strip(" a"));
|
---|
67 | assertEquals("ab", Utils.strip(" ab"));
|
---|
68 | assertEquals("abc", Utils.strip(" abc"));
|
---|
69 | assertEquals("a", Utils.strip("a "));
|
---|
70 | assertEquals("ab", Utils.strip("ab "));
|
---|
71 | assertEquals("abc", Utils.strip("abc "));
|
---|
72 | assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
|
---|
73 | assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
|
---|
74 | assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
|
---|
75 |
|
---|
76 | // extended skip
|
---|
77 | assertEquals("a", Utils.strip("a", "b"));
|
---|
78 | assertEquals("b", Utils.strip("acbcac", "ac"));
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Test of {@link Utils#isStripEmpty} method.
|
---|
83 | */
|
---|
84 | @Test
|
---|
85 | public void testIsStripEmpty() {
|
---|
86 | assertTrue(Utils.isStripEmpty(null));
|
---|
87 | assertTrue(Utils.isStripEmpty(""));
|
---|
88 | assertTrue(Utils.isStripEmpty(" "));
|
---|
89 | assertTrue(Utils.isStripEmpty(" "));
|
---|
90 | assertFalse(Utils.isStripEmpty("a"));
|
---|
91 | assertFalse(Utils.isStripEmpty("foo"));
|
---|
92 | assertFalse(Utils.isStripEmpty(" foo"));
|
---|
93 | assertFalse(Utils.isStripEmpty("foo "));
|
---|
94 | assertFalse(Utils.isStripEmpty(" foo "));
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Test of {@link Utils#toHexString} method.
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | public void testToHexString() {
|
---|
102 | assertEquals("", Utils.toHexString(null));
|
---|
103 | assertEquals("", Utils.toHexString(new byte[0]));
|
---|
104 | assertEquals("01", Utils.toHexString(new byte[]{0x1}));
|
---|
105 | assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
|
---|
106 | assertEquals("12", Utils.toHexString(new byte[]{0x12}));
|
---|
107 | assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
|
---|
108 | assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Test of {@link Utils#getPositionListString} method.
|
---|
113 | */
|
---|
114 | @Test
|
---|
115 | public void testPositionListString() {
|
---|
116 | assertEquals("1", Utils.getPositionListString(Arrays.asList(1)));
|
---|
117 | assertEquals("1-3", Utils.getPositionListString(Arrays.asList(1, 2, 3)));
|
---|
118 | assertEquals("1-3", Utils.getPositionListString(Arrays.asList(3, 1, 2)));
|
---|
119 | assertEquals("1-3,6-8", Utils.getPositionListString(Arrays.asList(1, 2, 3, 6, 7, 8)));
|
---|
120 | assertEquals("1-2,5-7", Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)));
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Test of {@link Utils#getDurationString} method.
|
---|
125 | */
|
---|
126 | @Test
|
---|
127 | public void testDurationString() {
|
---|
128 | I18n.set("en");
|
---|
129 | assertEquals("123 ms", Utils.getDurationString(123));
|
---|
130 | assertEquals("1.2 s", Utils.getDurationString(1234));
|
---|
131 | assertEquals("57.0 s", Utils.getDurationString(57 * 1000));
|
---|
132 | assertEquals("8 min 27 s", Utils.getDurationString(507 * 1000));
|
---|
133 | assertEquals("8 h 24 min", Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)));
|
---|
134 | assertEquals("1 day 12 h", Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)));
|
---|
135 | assertEquals("8 days 12 h", Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)));
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Test of {@link Utils#escapeReservedCharactersHTML} method.
|
---|
140 | */
|
---|
141 | @Test
|
---|
142 | public void testEscapeReservedCharactersHTML() {
|
---|
143 | assertEquals("foo -> bar -> '&'", Utils.escapeReservedCharactersHTML("foo -> bar -> '&'"));
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Test of {@link Utils#restrictStringLines} method.
|
---|
148 | */
|
---|
149 | @Test
|
---|
150 | public void testRestrictStringLines() {
|
---|
151 | assertEquals("1\n...", Utils.restrictStringLines("1\n2\n3", 2));
|
---|
152 | assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 3));
|
---|
153 | assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4));
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Test of {@link Utils#getSizeString} method.
|
---|
158 | */
|
---|
159 | @Test
|
---|
160 | public void testSizeString() {
|
---|
161 | assertEquals("0 B", Utils.getSizeString(0, Locale.ENGLISH));
|
---|
162 | assertEquals("123 B", Utils.getSizeString(123, Locale.ENGLISH));
|
---|
163 | assertEquals("1023 B", Utils.getSizeString(1023, Locale.ENGLISH));
|
---|
164 | assertEquals("1.00 kB", Utils.getSizeString(1024, Locale.ENGLISH));
|
---|
165 | assertEquals("11.7 kB", Utils.getSizeString(12024, Locale.ENGLISH));
|
---|
166 | assertEquals("8.00 EB", Utils.getSizeString(Long.MAX_VALUE, Locale.ENGLISH));
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Test of {@link Utils#getSizeString} method.
|
---|
171 | */
|
---|
172 | @Test(expected = IllegalArgumentException.class)
|
---|
173 | public void testSizeStringNegative() {
|
---|
174 | Utils.getSizeString(-1, Locale.ENGLISH);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Test {@link Utils#joinAsHtmlUnorderedList(Iterable)}
|
---|
179 | */
|
---|
180 | @Test
|
---|
181 | public void testJoinAsHtmlUnorderedList() {
|
---|
182 | List<? extends Object> items = Arrays.asList("1", Integer.valueOf(2));
|
---|
183 | assertEquals("<ul><li>1</li><li>2</li></ul>", Utils.joinAsHtmlUnorderedList(items));
|
---|
184 | assertEquals("<ul></ul>", Utils.joinAsHtmlUnorderedList(Collections.emptyList()));
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Tests if readBytesFromStream handles null streams (might happen when there is no data on error stream)
|
---|
189 | * @throws IOException in case of I/O error
|
---|
190 | */
|
---|
191 | @Test
|
---|
192 | public void testNullStreamForReadBytesFromStream() throws IOException {
|
---|
193 | assertEquals("Empty on null stream", 0, Utils.readBytesFromStream(null).length);
|
---|
194 | }
|
---|
195 | }
|
---|