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

Last change on this file since 9274 was 9274, checked in by simon04, 8 years ago

Uniform display of sizes (B, kB, MB, ...)

  • Property svn:eol-style set to native
File size: 7.4 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.BufferedReader;
7import java.io.IOException;
8import java.net.URL;
9import java.util.Arrays;
10import java.util.Locale;
11
12import org.junit.Assert;
13import org.junit.Test;
14import org.openstreetmap.josm.Main;
15
16/**
17 * Unit tests of {@link Utils} class.
18 */
19public class UtilsTest {
20
21 /**
22 * Test of {@link Utils#strip} method.
23 */
24 @Test
25 public void testStrip() {
26 final String someWhite =
27 "\u00A0"+ // SPACE_SEPARATOR
28 "\u2007"+ // LINE_SEPARATOR
29 "\u202F"+ // PARAGRAPH_SEPARATOR
30 "\u0009"+ // HORIZONTAL TABULATION
31 "\n" + // LINE FEED (U+000A, cannot be put as it in Java)
32 "\u000B"+ // VERTICAL TABULATION
33 "\u000C"+ // FORM FEED
34 "\r" + // CARRIAGE RETURN (U+000D, cannot be put as it in Java)
35 "\u001C"+ // FILE SEPARATOR
36 "\u001D"+ // GROUP SEPARATOR
37 "\u001E"+ // RECORD SEPARATOR
38 "\u001F"+ // UNIT SEPARATOR
39 "\u2003"+ // EM SPACE
40 "\u2007"+ // FIGURE SPACE
41 "\u200B"+ // ZERO WIDTH SPACE
42 "\uFEFF"+ // ZERO WIDTH NO-BREAK SPACE
43 "\u3000"; // IDEOGRAPHIC SPACE
44 Assert.assertNull(Utils.strip(null));
45 Assert.assertEquals("", Utils.strip(""));
46 Assert.assertEquals("", Utils.strip(" "));
47 Assert.assertEquals("", Utils.strip(" "));
48 Assert.assertEquals("", Utils.strip(" "));
49 Assert.assertEquals("", Utils.strip(someWhite));
50 Assert.assertEquals("a", Utils.strip("a"));
51 Assert.assertEquals("ab", Utils.strip("ab"));
52 Assert.assertEquals("abc", Utils.strip("abc"));
53 Assert.assertEquals("a", Utils.strip(" a"));
54 Assert.assertEquals("ab", Utils.strip(" ab"));
55 Assert.assertEquals("abc", Utils.strip(" abc"));
56 Assert.assertEquals("a", Utils.strip("a "));
57 Assert.assertEquals("ab", Utils.strip("ab "));
58 Assert.assertEquals("abc", Utils.strip("abc "));
59 Assert.assertEquals("a", Utils.strip(someWhite+"a"+someWhite));
60 Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
61 Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
62
63 // extended skip
64 Assert.assertEquals("a", Utils.strip("a", "b"));
65 Assert.assertEquals("b", Utils.strip("acbcac", "ac"));
66 }
67
68 /**
69 * Test of {@link Utils#toHexString} method.
70 */
71 @Test
72 public void testToHexString() {
73 Assert.assertEquals("", Utils.toHexString(null));
74 Assert.assertEquals("", Utils.toHexString(new byte[0]));
75 Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
76 Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
77 Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
78 Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
79 Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
80 }
81
82 /**
83 * Test of {@link Utils#openURLReaderAndDecompress} method with Gzip compression.
84 * @throws IOException if any I/O error occurs
85 */
86 @Test
87 public void testOpenUrlGzip() throws IOException {
88 Main.initApplicationPreferences();
89 final URL url = new URL("https://www.openstreetmap.org/trace/1613906/data");
90 try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
91 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
92 }
93 }
94
95 /**
96 * Test of {@link Utils#openURLReaderAndDecompress} method with Bzip compression.
97 * @throws IOException if any I/O error occurs
98 */
99 @Test
100 public void testOpenUrlBzip() throws IOException {
101 Main.initApplicationPreferences();
102 final URL url = new URL("https://www.openstreetmap.org/trace/785544/data");
103 try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
104 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
105 }
106 }
107
108 /**
109 * Test of {@link Utils#openURLReaderAndDecompress} method with Bzip compression.
110 * @throws IOException if any I/O error occurs
111 */
112 @Test
113 public void testTicket9660() throws IOException {
114 Main.initApplicationPreferences();
115 final URL url = new URL("http://www.openstreetmap.org/trace/1350010/data");
116 try (BufferedReader x = HttpClient.create(url).connect()
117 .uncompress(true).uncompressAccordingToContentDisposition(true).getContentReader()) {
118 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
119 }
120 }
121
122 /**
123 * Test of {@link Utils#getPositionListString} method.
124 */
125 @Test
126 public void testPositionListString() {
127 assertEquals("1", Utils.getPositionListString(Arrays.asList(1)));
128 assertEquals("1-3", Utils.getPositionListString(Arrays.asList(1, 2, 3)));
129 assertEquals("1-3", Utils.getPositionListString(Arrays.asList(3, 1, 2)));
130 assertEquals("1-3,6-8", Utils.getPositionListString(Arrays.asList(1, 2, 3, 6, 7, 8)));
131 assertEquals("1-2,5-7", Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)));
132 }
133
134 /**
135 * Test of {@link Utils#getDurationString} method.
136 */
137 @Test
138 public void testDurationString() {
139 I18n.set("en");
140 assertEquals("123 ms", Utils.getDurationString(123));
141 assertEquals("1.2 s", Utils.getDurationString(1234));
142 assertEquals("57.0 s", Utils.getDurationString(57 * 1000));
143 assertEquals("8 min 27 s", Utils.getDurationString(507 * 1000));
144 assertEquals("8 h 24 min", Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)));
145 assertEquals("1 day 12 h", Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)));
146 assertEquals("8 days 12 h", Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)));
147 }
148
149 /**
150 * Test of {@link Utils#escapeReservedCharactersHTML} method.
151 */
152 @Test
153 public void testEscapeReservedCharactersHTML() {
154 assertEquals("foo -&gt; bar -&gt; '&amp;'", Utils.escapeReservedCharactersHTML("foo -> bar -> '&'"));
155 }
156
157 /**
158 * Test of {@link Utils#restrictStringLines} method.
159 */
160 @Test
161 public void testRestrictStringLines() {
162 assertEquals("1\n...", Utils.restrictStringLines("1\n2\n3", 2));
163 assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 3));
164 assertEquals("1\n2\n3", Utils.restrictStringLines("1\n2\n3", 4));
165 }
166
167 /**
168 * Test of {@link Utils#getSizeString} method.
169 */
170 @Test
171 public void testSizeString() throws Exception {
172 assertEquals("0 B", Utils.getSizeString(0, Locale.ENGLISH));
173 assertEquals("123 B", Utils.getSizeString(123, Locale.ENGLISH));
174 assertEquals("1023 B", Utils.getSizeString(1023, Locale.ENGLISH));
175 assertEquals("1.00 kB", Utils.getSizeString(1024, Locale.ENGLISH));
176 assertEquals("11.7 kB", Utils.getSizeString(12024, Locale.ENGLISH));
177 assertEquals("8.00 EB", Utils.getSizeString(Long.MAX_VALUE, Locale.ENGLISH));
178 }
179
180 /**
181 * Test of {@link Utils#getSizeString} method.
182 */
183 @Test(expected = IllegalArgumentException.class)
184 public void testSizeStringNegative() throws Exception {
185 Utils.getSizeString(-1, Locale.ENGLISH);
186 }
187
188}
Note: See TracBrowser for help on using the repository browser.