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

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

fix warning in unit test

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