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

Last change on this file since 7134 was 7076, checked in by Don-vip, 10 years ago

fix unit test on system with non-english locale + javadoc

File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.hamcrest.CoreMatchers.is;
5import static org.junit.Assert.assertThat;
6
7import java.io.BufferedReader;
8import java.io.IOException;
9import java.net.URL;
10import java.util.Arrays;
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
64 /**
65 * Test of {@link Utils#toHexString} method.
66 */
67 @Test
68 public void testToHexString(){
69 Assert.assertEquals("", Utils.toHexString(null));
70 Assert.assertEquals("", Utils.toHexString(new byte[0]));
71 Assert.assertEquals("01", Utils.toHexString(new byte[]{0x1}));
72 Assert.assertEquals("0102", Utils.toHexString(new byte[]{0x1,0x2}));
73 Assert.assertEquals("12", Utils.toHexString(new byte[]{0x12}));
74 Assert.assertEquals("127f", Utils.toHexString(new byte[]{0x12, 0x7f}));
75 Assert.assertEquals("fedc", Utils.toHexString(new byte[]{(byte) 0xfe, (byte) 0xdc}));
76 }
77
78 /**
79 * Test of {@link Utils#openURLReaderAndDecompress} method with Gzip compression.
80 * @throws IOException if any I/O error occurs
81 */
82 @Test
83 public void testOpenUrlGzip() throws IOException {
84 Main.initApplicationPreferences();
85 try (BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/1613906/data"), true)) {
86 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
87 }
88 }
89
90 /**
91 * Test of {@link Utils#openURLReaderAndDecompress} method with Bzip compression.
92 * @throws IOException if any I/O error occurs
93 */
94 @Test
95 public void testOpenUrlBzip() throws IOException {
96 Main.initApplicationPreferences();
97 try (BufferedReader x = Utils.openURLReaderAndDecompress(new URL("https://www.openstreetmap.org/trace/785544/data"), true)) {
98 Assert.assertTrue(x.readLine().startsWith("<?xml version="));
99 }
100 }
101
102 /**
103 * Test of {@link Utils#getPositionListString} method.
104 */
105 @Test
106 public void testPositionListString() {
107 assertThat(Utils.getPositionListString(Arrays.asList(1)), is("1"));
108 assertThat(Utils.getPositionListString(Arrays.asList(1, 2, 3)), is("1-3"));
109 assertThat(Utils.getPositionListString(Arrays.asList(3, 1, 2)), is("1-3"));
110 assertThat(Utils.getPositionListString(Arrays.asList(1, 2, 3, 6, 7, 8)), is("1-3,6-8"));
111 assertThat(Utils.getPositionListString(Arrays.asList(1, 5, 2, 6, 7)), is("1-2,5-7"));
112 }
113
114 /**
115 * Test of {@link Utils#getDurationString} method.
116 */
117 @Test
118 public void testDurationString() {
119 I18n.set("en");
120 assertThat(Utils.getDurationString(123), is("123 ms"));
121 assertThat(Utils.getDurationString(1234), is("1.2 s"));
122 assertThat(Utils.getDurationString(57 * 1000), is("57.0 s"));
123 assertThat(Utils.getDurationString(507 * 1000), is("8 min 27 s"));
124 assertThat(Utils.getDurationString((long) (8.4 * 60 * 60 * 1000)), is("8 h 24 min"));
125 assertThat(Utils.getDurationString((long) (1.5 * 24 * 60 * 60 * 1000)), is("1 day 12 h"));
126 assertThat(Utils.getDurationString((long) (8.5 * 24 * 60 * 60 * 1000)), is("8 days 12 h"));
127 }
128}
Note: See TracBrowser for help on using the repository browser.