1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.binary;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
27 import java.io.UnsupportedEncodingException;
28 import java.nio.ByteBuffer;
29 import java.nio.charset.StandardCharsets;
30
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 public class StringUtilsTest {
37
38 private static final byte[] BYTES_FIXTURE = { 'a', 'b', 'c' };
39
40
41 private static final byte[] BYTES_FIXTURE_16BE = { 0, 'a', 0, 'b', 0, 'c' };
42
43
44 private static final byte[] BYTES_FIXTURE_16LE = { 'a', 0, 'b', 0, 'c', 0 };
45
46 private static final String STRING_FIXTURE = "ABC";
47
48 @Test
49 public void testByteBufferUtf8() {
50 assertNull(StringUtils.getByteBufferUtf8(null), "Should be null safe");
51 final String text = "asdhjfhsadiogasdjhagsdygfjasfgsdaksjdhfk";
52 final ByteBuffer bb = StringUtils.getByteBufferUtf8(text);
53 assertArrayEquals(text.getBytes(StandardCharsets.UTF_8), bb.array());
54 }
55
56
57
58
59
60 @Test
61 public void testConstructor() {
62 new StringUtils();
63 }
64
65 @Test
66 public void testEqualsCS1() {
67 assertFalse(StringUtils.equals(new StringBuilder("abc"), null));
68 assertFalse(StringUtils.equals(null, new StringBuilder("abc")));
69 assertTrue(StringUtils.equals(new StringBuilder("abc"), new StringBuilder("abc")));
70 assertFalse(StringUtils.equals(new StringBuilder("abc"), new StringBuilder("abcd")));
71 assertFalse(StringUtils.equals(new StringBuilder("abcd"), new StringBuilder("abc")));
72 assertFalse(StringUtils.equals(new StringBuilder("abc"), new StringBuilder("ABC")));
73 }
74
75 @Test
76 public void testEqualsCS2() {
77 assertTrue(StringUtils.equals("abc", new StringBuilder("abc")));
78 assertFalse(StringUtils.equals(new StringBuilder("abc"), "abcd"));
79 assertFalse(StringUtils.equals("abcd", new StringBuilder("abc")));
80 assertFalse(StringUtils.equals(new StringBuilder("abc"), "ABC"));
81 }
82
83 @Test
84 public void testEqualsString() {
85 assertTrue(StringUtils.equals(null, null));
86 assertFalse(StringUtils.equals("abc", null));
87 assertFalse(StringUtils.equals(null, "abc"));
88 assertTrue(StringUtils.equals("abc", "abc"));
89 assertFalse(StringUtils.equals("abc", "abcd"));
90 assertFalse(StringUtils.equals("abcd", "abc"));
91 assertFalse(StringUtils.equals("abc", "ABC"));
92 }
93
94 @Test
95 public void testGetBytesIso8859_1() throws UnsupportedEncodingException {
96 final String charsetName = StandardCharsets.ISO_8859_1.name();
97 testGetBytesUnchecked(charsetName);
98 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
99 final byte[] actual = StringUtils.getBytesIso8859_1(STRING_FIXTURE);
100 assertArrayEquals(expected, actual);
101 }
102
103 private void testGetBytesUnchecked(final String charsetName) throws UnsupportedEncodingException {
104 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
105 final byte[] actual = StringUtils.getBytesUnchecked(STRING_FIXTURE, charsetName);
106 assertArrayEquals(expected, actual);
107 }
108
109 @Test
110 public void testGetBytesUncheckedBadName() {
111 assertThrows(IllegalStateException.class, () -> StringUtils.getBytesUnchecked(STRING_FIXTURE, "UNKNOWN"));
112 }
113
114 @Test
115 public void testGetBytesUncheckedNullInput() {
116 assertNull(StringUtils.getBytesUnchecked(null, "UNKNOWN"));
117 }
118
119 @Test
120 public void testGetBytesUsAscii() throws UnsupportedEncodingException {
121 final String charsetName = StandardCharsets.US_ASCII.name();
122 testGetBytesUnchecked(charsetName);
123 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
124 final byte[] actual = StringUtils.getBytesUsAscii(STRING_FIXTURE);
125 assertArrayEquals(expected, actual);
126 }
127
128 @Test
129 public void testGetBytesUtf16() throws UnsupportedEncodingException {
130 final String charsetName = StandardCharsets.UTF_16.name();
131 testGetBytesUnchecked(charsetName);
132 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
133 final byte[] actual = StringUtils.getBytesUtf16(STRING_FIXTURE);
134 assertArrayEquals(expected, actual);
135 }
136
137 @Test
138 public void testGetBytesUtf16Be() throws UnsupportedEncodingException {
139 final String charsetName = StandardCharsets.UTF_16BE.name();
140 testGetBytesUnchecked(charsetName);
141 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
142 final byte[] actual = StringUtils.getBytesUtf16Be(STRING_FIXTURE);
143 assertArrayEquals(expected, actual);
144 }
145
146 @Test
147 public void testGetBytesUtf16Le() throws UnsupportedEncodingException {
148 final String charsetName = StandardCharsets.UTF_16LE.name();
149 testGetBytesUnchecked(charsetName);
150 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
151 final byte[] actual = StringUtils.getBytesUtf16Le(STRING_FIXTURE);
152 assertArrayEquals(expected, actual);
153 }
154
155 @Test
156 public void testGetBytesUtf8() throws UnsupportedEncodingException {
157 final String charsetName = StandardCharsets.UTF_8.name();
158 testGetBytesUnchecked(charsetName);
159 final byte[] expected = STRING_FIXTURE.getBytes(charsetName);
160 final byte[] actual = StringUtils.getBytesUtf8(STRING_FIXTURE);
161 assertArrayEquals(expected, actual);
162 }
163
164 private void testNewString(final String charsetName) throws UnsupportedEncodingException {
165 final String expected = new String(BYTES_FIXTURE, charsetName);
166 final String actual = StringUtils.newString(BYTES_FIXTURE, charsetName);
167 assertEquals(expected, actual);
168 }
169
170 @Test
171 public void testNewStringBadEnc() {
172 assertThrows(IllegalStateException.class, () -> StringUtils.newString(BYTES_FIXTURE, "UNKNOWN"));
173 }
174
175 @Test
176 public void testNewStringIso8859_1() throws UnsupportedEncodingException {
177 final String charsetName = StandardCharsets.ISO_8859_1.name();
178 testNewString(charsetName);
179 final String expected = new String(BYTES_FIXTURE, charsetName);
180 final String actual = StringUtils.newStringIso8859_1(BYTES_FIXTURE);
181 assertEquals(expected, actual);
182 }
183
184 @Test
185 public void testNewStringNullInput() {
186 assertNull(StringUtils.newString(null, "UNKNOWN"));
187 }
188
189 @Test
190 public void testNewStringNullInput_CODEC229() {
191 assertNull(StringUtils.newStringUtf8(null));
192 assertNull(StringUtils.newStringIso8859_1(null));
193 assertNull(StringUtils.newStringUsAscii(null));
194 assertNull(StringUtils.newStringUtf16(null));
195 assertNull(StringUtils.newStringUtf16Be(null));
196 assertNull(StringUtils.newStringUtf16Le(null));
197 }
198
199 @Test
200 public void testNewStringUsAscii() throws UnsupportedEncodingException {
201 final String charsetName = StandardCharsets.US_ASCII.name();
202 testNewString(charsetName);
203 final String expected = new String(BYTES_FIXTURE, charsetName);
204 final String actual = StringUtils.newStringUsAscii(BYTES_FIXTURE);
205 assertEquals(expected, actual);
206 }
207
208 @Test
209 public void testNewStringUtf16() throws UnsupportedEncodingException {
210 final String charsetName = StandardCharsets.UTF_16.name();
211 testNewString(charsetName);
212 final String expected = new String(BYTES_FIXTURE, charsetName);
213 final String actual = StringUtils.newStringUtf16(BYTES_FIXTURE);
214 assertEquals(expected, actual);
215 }
216
217 @Test
218 public void testNewStringUtf16Be() throws UnsupportedEncodingException {
219 final String charsetName = StandardCharsets.UTF_16BE.name();
220 testNewString(charsetName);
221 final String expected = new String(BYTES_FIXTURE_16BE, charsetName);
222 final String actual = StringUtils.newStringUtf16Be(BYTES_FIXTURE_16BE);
223 assertEquals(expected, actual);
224 }
225
226 @Test
227 public void testNewStringUtf16Le() throws UnsupportedEncodingException {
228 final String charsetName = StandardCharsets.UTF_16LE.name();
229 testNewString(charsetName);
230 final String expected = new String(BYTES_FIXTURE_16LE, charsetName);
231 final String actual = StringUtils.newStringUtf16Le(BYTES_FIXTURE_16LE);
232 assertEquals(expected, actual);
233 }
234
235 @Test
236 public void testNewStringUtf8() throws UnsupportedEncodingException {
237 final String charsetName = StandardCharsets.UTF_8.name();
238 testNewString(charsetName);
239 final String expected = new String(BYTES_FIXTURE, charsetName);
240 final String actual = StringUtils.newStringUtf8(BYTES_FIXTURE);
241 assertEquals(expected, actual);
242 }
243 }