source: josm/trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java@ 11974

Last change on this file since 11974 was 11921, checked in by Don-vip, 7 years ago

improve unit test coverage of utilities classes thanks to https://trajano.github.io/commons-testing

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertNull;
7import static org.junit.Assert.assertTrue;
8
9import java.io.IOException;
10import java.util.Arrays;
11import java.util.Collections;
12import java.util.List;
13import java.util.Locale;
14
15import org.junit.Rule;
16import org.junit.Test;
17import org.openstreetmap.josm.testutils.JOSMTestRules;
18
19import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20import net.trajano.commons.testing.UtilityClassTestUtil;
21
22/**
23 * Unit tests of {@link Utils} class.
24 */
25public 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 * Tests if readBytesFromStream handles null streams (might happen when there is no data on error stream)
199 * @throws IOException in case of I/O error
200 */
201 @Test
202 public void testNullStreamForReadBytesFromStream() throws IOException {
203 assertEquals("Empty on null stream", 0, Utils.readBytesFromStream(null).length);
204 }
205}
Note: See TracBrowser for help on using the repository browser.