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

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

sonar - fix recent issues

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