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

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

see #12231 - Update unit tests, fix regression

  • Property svn:eol-style set to native
File size: 6.5 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;
10
11import org.junit.Assert;
12import org.junit.Test;
13import org.openstreetmap.josm.Main;
14
15/**
16 * Unit tests of {@link Utils} class.
17 */
18public class UtilsTest {
19
20 /**
21 * Test of {@link Utils#strip} method.
22 */
23 @Test
24 public void testStrip() {
25 final String someWhite =
26 "\u00A0"+ // SPACE_SEPARATOR
27 "\u2007"+ // LINE_SEPARATOR
28 "\u202F"+ // PARAGRAPH_SEPARATOR
29 "\u0009"+ // HORIZONTAL TABULATION
30 "\n" + // LINE FEED (U+000A, cannot be put as it in Java)
31 "\u000B"+ // VERTICAL TABULATION
32 "\u000C"+ // FORM FEED
33 "\r" + // CARRIAGE RETURN (U+000D, cannot be put as it in Java)
34 "\u001C"+ // FILE SEPARATOR
35 "\u001D"+ // GROUP SEPARATOR
36 "\u001E"+ // RECORD SEPARATOR
37 "\u001F"+ // UNIT SEPARATOR
38 "\u2003"+ // EM SPACE
39 "\u2007"+ // FIGURE SPACE
40 "\u200B"+ // ZERO WIDTH SPACE
41 "\uFEFF"+ // ZERO WIDTH NO-BREAK SPACE
42 "\u3000"; // IDEOGRAPHIC SPACE
43 Assert.assertNull(Utils.strip(null));
44 Assert.assertEquals("", Utils.strip(""));
45 Assert.assertEquals("", Utils.strip(" "));
46 Assert.assertEquals("", Utils.strip(" "));
47 Assert.assertEquals("", Utils.strip(" "));
48 Assert.assertEquals("", Utils.strip(someWhite));
49 Assert.assertEquals("a", Utils.strip("a"));
50 Assert.assertEquals("ab", Utils.strip("ab"));
51 Assert.assertEquals("abc", Utils.strip("abc"));
52 Assert.assertEquals("a", Utils.strip(" a"));
53 Assert.assertEquals("ab", Utils.strip(" ab"));
54 Assert.assertEquals("abc", Utils.strip(" abc"));
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(someWhite+"a"+someWhite));
59 Assert.assertEquals("ab", Utils.strip(someWhite+"ab"+someWhite));
60 Assert.assertEquals("abc", Utils.strip(someWhite+"abc"+someWhite));
61
62 // extended skip
63 Assert.assertEquals("a", Utils.strip("a", "b"));
64 Assert.assertEquals("b", Utils.strip("acbcac", "ac"));
65 }
66
67 /**
68 * Test of {@link Utils#toHexString} method.
69 */
70 @Test
71 public void testToHexString() {
72 Assert.assertEquals("", Utils.toHexString(null));
73 Assert.assertEquals("", Utils.toHexString(new byte[0]));
74 Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
75 Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1, 0x2}));
76 Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
77 Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
78 Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
79 }
80
81 /**
82 * Test of {@link Utils#openURLReaderAndDecompress} method with Gzip compression.
83 * @throws IOException if any I/O error occurs
84 */
85 @Test
86 public void testOpenUrlGzip() throws IOException {
87 Main.initApplicationPreferences();
88 final URL url = new URL("https://www.openstreetmap.org/trace/1613906/data");
89 try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
90 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
91 }
92 }
93
94 /**
95 * Test of {@link Utils#openURLReaderAndDecompress} method with Bzip compression.
96 * @throws IOException if any I/O error occurs
97 */
98 @Test
99 public void testOpenUrlBzip() throws IOException {
100 Main.initApplicationPreferences();
101 final URL url = new URL("https://www.openstreetmap.org/trace/785544/data");
102 try (BufferedReader x = HttpClient.create(url).connect().uncompress(true).getContentReader()) {
103 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
104 }
105 }
106
107 /**
108 * Test of {@link Utils#openURLReaderAndDecompress} method with Bzip compression.
109 * @throws IOException if any I/O error occurs
110 */
111 @Test
112 public void testTicket9660() throws IOException {
113 Main.initApplicationPreferences();
114 final URL url = new URL("http://www.openstreetmap.org/trace/1350010/data");
115 try (BufferedReader x = HttpClient.create(url).connect()
116 .uncompress(true).uncompressAccordingToContentDisposition(true).getContentReader()) {
117 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
118 }
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 -&gt; bar -&gt; '&amp;'", 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}
Note: See TracBrowser for help on using the repository browser.